fix: code quality improvements across C codebase
- Replace sprintf with snprintf (1024-byte errbuf) in hmmmm.c, base_device.c, compose_device.c, config.c - Fix WRITE_MEM macro: use smartAddrWriteHandlers for write ident (was incorrectly reading from smartAddrReadHandlers) - Replace alloca with malloc+free in dispatchMemAccessNotifications - Guard closeBaseDevice against NULL lib/partial initialization - Simplify intercept context storage to single contiguous allocation - Add NULL checks after calloc in config handler with proper cleanup - Guard find_device_by_id against zero-length path Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
105
devices/avr_generic/inc/instr.h
Normal file
105
devices/avr_generic/inc/instr.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#ifndef __INSTR_H__
|
||||
#define __INSTR_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "mem.h"
|
||||
|
||||
|
||||
#ifndef OPCODE_WORDSIZE
|
||||
#error OPCODE_WORDSIZE must be provided
|
||||
// #define OPCODE_WORDSIZE 1
|
||||
#endif
|
||||
#if OPCODE_WORDSIZE == 1
|
||||
typedef uint8_t opcode_t;
|
||||
#elif OPCODE_WORDSIZE == 2
|
||||
typedef uint16_t opcode_t;
|
||||
#elif OPCODE_WORDSIZE == 3
|
||||
typedef uint32_t opcode_t;
|
||||
#elif OPCODE_WORDSIZE == 4
|
||||
typedef uint32_t opcode_t;
|
||||
#else
|
||||
#error OPCODE_WORDSIZE must be one of 1,2,3,
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef PC_WORDSIZE
|
||||
#error PC_WORDSIZE must be provided
|
||||
// #define PC_WORDSIZE 1
|
||||
#endif
|
||||
#if PC_WORDSIZE == 1
|
||||
typedef uint8_t prog_counter_t;
|
||||
#elif PC_WORDSIZE == 2
|
||||
typedef uint16_t prog_counter_t;
|
||||
#elif PC_WORDSIZE == 3
|
||||
typedef uint32_t prog_counter_t;
|
||||
#elif PC_WORDSIZE == 4
|
||||
typedef uint32_t prog_counter_t;
|
||||
#else
|
||||
#error PC_WORDSIZE must be one of 1,2,3,4
|
||||
#endif
|
||||
|
||||
typedef uint8_t (*instr_h_func)(prog_counter_t*, device_mem_t*);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
instr_h_func h;
|
||||
opcode_t base;
|
||||
opcode_t argsMask;
|
||||
uint8_t opcodeLen;
|
||||
|
||||
} instruction_metadata_t;
|
||||
|
||||
instr_h_func* genInstrArray(char* errbuf);
|
||||
void setOpcodeSizes(uint8_t* opcodeSizes);
|
||||
opcode_t extractOpcode(device_mem_t* devMem, prog_counter_t programCounter);
|
||||
|
||||
|
||||
|
||||
|
||||
#define AVRe 1
|
||||
#define AVRxm 2
|
||||
#define AVRxt 3
|
||||
#define AVRrc 4
|
||||
|
||||
#ifndef ARCH
|
||||
#define ARCH AVRe
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define READ_MEM(__tgt, __mem, __segno, __addr, __cell_t) \
|
||||
{\
|
||||
uint64_t __globalAddr = __mem->memsegShifts[__segno] + __addr; \
|
||||
if (__mem->smartAddrReadHandlers[__globalAddr].func != NULL) \
|
||||
{ \
|
||||
__tgt = *(__cell_t*)__mem->smartAddrReadHandlers[__globalAddr].func(__mem->smartAddrReadHandlers[__globalAddr].ident, __addr, __mem->rawCells + __mem->memsegShifts[__segno]); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
__tgt = ((__cell_t*)__mem->cells[__segno])[__addr]; \
|
||||
} \
|
||||
__mem->memreadCellAddrs[__mem->memreadLen] = __globalAddr; \
|
||||
__mem->memreadLen += 1; \
|
||||
}
|
||||
|
||||
#define WRITE_MEM(__mem, __segno, __addr, __cell_t, __val) \
|
||||
{ \
|
||||
uint64_t __globalAddr = __mem->memsegShifts[__segno] + __addr; \
|
||||
if (__mem->smartAddrWriteHandlers[__globalAddr].func != NULL) \
|
||||
{ \
|
||||
__cell_t __dat = (__cell_t)(__val); \
|
||||
__mem->smartAddrWriteHandlers[__globalAddr].func(__mem->smartAddrWriteHandlers[__globalAddr].ident, __addr, __mem->rawCells + __mem->memsegShifts[__segno], (void*)&__dat); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
((__cell_t*)__mem->cells[__segno])[__addr] = (__cell_t)(__val); \
|
||||
} \
|
||||
__mem->memwriteCellAddrs[__mem->memwriteLen] = __globalAddr; \
|
||||
__mem->memwriteLen += 1; \
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // ifndef __INSTR_H__
|
||||
Reference in New Issue
Block a user