-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecuBinDisasm.h
237 lines (196 loc) · 5.64 KB
/
ecuBinDisasm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#define ROM_START 0x8000
#define MNEMONIC_LEN 6
#define OP_TABLE_SZ 0x100
#define FORMATS_NUM 11
#define SUB_OP_NUM 46
#define SYM_BUF_MAX 128
#define OP_SYMBOLS_MAX 3
#define ORG_MAX 4
#define RAS_MAX 50
#define RAW_BYTES_PAD " "
#define LABEL_PAD -20
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int uint;
//
// Argp stuff
//
const char *argp_program_version = "7675disassem 0.1";
const char *argp_program_bug_address = "<janehacker1@gmail.com>";
static char doc[] = "Disassembler for the MH6111/TMP76C75T(7675)";
static char args_doc[] = "<BINARY FILE> [SYMBOL FILE]";
static struct argp_option options[] = {
{ "linenumbers", 'l', 0, OPTION_ARG_OPTIONAL, "Print line numbers."},
{ "rawbytes", 'r', 0, OPTION_ARG_OPTIONAL, "Print raw bytes."},
{ 0 }
};
struct arguments {
bool lineNumbers;
bool rawBytes;
char *args[2];
char *option1;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch (key) {
case 'l': arguments->lineNumbers = 1; break;
case 'r': arguments->rawBytes = 1; break;
case ARGP_KEY_ARG:
// Too many arguments, if your program expects only one argument.
if(state->arg_num > 2)
argp_usage(state);
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
// Not enough arguments. if your program expects exactly one argument.
if(state->arg_num < 1)
argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
break;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
typedef enum {
ORG,
CODE,
DATA,
VECTOR
} ROMArea;
typedef enum {
PRINT,
SCAN
} decodeType;
// Primarily used to format the output properly (see: formats)
typedef enum {
IMPLIED,
IMMEDIATE, IMMEDIATE16,
DIRECT, DIRECT2, DIRECT3, DIRECT4,
INDEXED, INDEXEDY,
EXTENDED,
RELATIVE
} operatonType;
// Formats correspond with operatonTypes
char* formats[FORMATS_NUM] = {
"",
"#$%02x", "#$%02x%02x", // IMMEDIATE
"$%02x", "$%02x, #$%02x", "$%02x, #$%02x, $%02x", "$%04x, x, #$%02x, %s",// DIRECT
"$%02x,x", "$00,y", // INDEXED
"$%02x%02x",
"$%02x"
};
// Multi-byte operations
typedef struct {
char mnemonic[MNEMONIC_LEN];
uint numBytes; // 1-4
operatonType type;
bool isSubOp;
byte byte;
} subOp;
typedef struct {
byte parentByte;
subOp so;
} subOpDef;
typedef struct {
char mnemonic[MNEMONIC_LEN];
uint numBytes; // 1-4
operatonType type;
bool isSubOp;
uint numSubOps;
subOp* so;
} operation;
// Symbols
typedef struct {
word addr;
ROMArea area;
char label[32];
} symbolStruct;
typedef union {
operation op;
subOp subOp;
} opUnion;
typedef struct {
word address;
ROMArea area;
} romAreaStruct;
#include "operationTables.h"
symbolStruct* symbolTable;
uint numSymbols = 0;
// Buffer we read the entire binary file into
byte* binBuffer;
byte symBuffer[SYM_BUF_MAX];
// Used for generated labels (RELATIVE for now)
uint generatedLabel = 4003;
romAreaStruct ras[RAS_MAX] = {0};
uint rasIndex = 0;
ROMArea ra = DATA;
word orgTable[ORG_MAX];
word skipArray[ORG_MAX];
uint skipArrayLen = 0;
// Function definitions
uint loadEcuBinFile(char *binFilename);
// symbols.c
uint loadSymbolFile(char *symFilename);
bool addSymbol(word address, ROMArea ra, char* symbol);
void generateRelativeSymbols(word binCurrPos, opUnion ou, byte* buffPtr, bool isSubOp);
char* getSymbol(word address);
word getOpSymbol(word binCurrPos, opUnion op, byte* buffPtr, bool* chkFlg);
void sortSymbols();
void printSymbols();
void addOrg(word address);
void doOrg(word binCurrPos, char** symbol, bool lineNumbers, bool rawBytes);
bool inSkipArray(word opWord);
// decode.c
word decodeOpAndJump(word binCurrPos, byte * buffPtr);
byte* decodeOp(word binCurrPos, byte * buffPtr, decodeType dt, bool lineNumbers, bool rawBytes);
// output.c
void printRaw(byte* buffPtr, uint numBytes);
void printRamVariables(bool lineNumber, bool rawBytes);
void printOp(word binCurrPos, opUnion oper, byte* buffPtr, bool lineNumbers, bool rawBytes);
void subOpPrint(char* symbol, opUnion oper, byte* buffPtr, bool isSymbol);
void opPrint(char* symbol, opUnion oper, byte* buffPtr, bool isSymbol, word binCurrPos);
byte* printData(word binCurrPos, byte* buffPtr, bool LineNumbers, bool rawBytes);
// subops.c
uint addSubOps();
bool getSubOp(operation currOp, byte* buffPtr, subOp* currSubOp);
uint freeSubOps();
// rom.c
void buildRomAreaStruct();
ROMArea getRomArea(word binCurrPos, ROMArea ra, bool reset);
void updateRomArea(byte* buffPtr, ROMArea* ra, bool reset);
//helpers.c
uint bytesToNextLabel(byte* buffPtr);
uint bytesToNextSection(byte* buffPtr);
word getBinPos(byte* buffPtr);
uint loadEcuBinFile(char *binFilename) {
FILE* ecuBin;
long binSize;
size_t bytesRead;
ecuBin = fopen(binFilename, "rb");
if(ecuBin == NULL) {
fputs("\nFile error\n", stderr);
exit(1);
}
// obtain file size:
fseek(ecuBin, 0, SEEK_END);
binSize = ftell(ecuBin);
rewind(ecuBin);
// allocate memory to contain the whole file:
binBuffer = (byte*)malloc(sizeof(byte) * binSize);
if(binBuffer == NULL) {
fputs("\nMemory error\n", stderr);
fclose(ecuBin);
exit(2);
}
// copy the file into the buffer:
bytesRead = fread(binBuffer, 1, binSize, ecuBin);
if(bytesRead != binSize) {
fputs("\nReading error\n", stderr);
fclose(ecuBin);
exit(3);
}
fclose(ecuBin);
return bytesRead;
}