-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.c
264 lines (221 loc) · 6.59 KB
/
symbols.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
// Symbols
//
bool addSymbol(word address, ROMArea ra, char* symbol) {
symbolStruct* tempSymbolTable = (symbolStruct*)malloc(sizeof(symbolStruct) * (numSymbols + 1)); // End of main
if(0 != numSymbols) {
memcpy(tempSymbolTable, symbolTable, sizeof(symbolStruct) * numSymbols);
free(symbolTable);
}
//printf("\nAdding symbol: %-10s, at line: %04x\n", symbol, address);
tempSymbolTable[numSymbols].addr = address;
tempSymbolTable[numSymbols].area = ra;
strcpy(tempSymbolTable[numSymbols].label, symbol);
symbolTable = tempSymbolTable;
numSymbols++;
return 1;
}
char* getSymbol(word address) {
char* ret = "\0";
for(int i=0; i < numSymbols; i++) {
symbolStruct* ss = &symbolTable[i];
if(address == ss->addr) {
ret = ss->label;
break;
}
}
return ret;
}
uint loadSymbolFile(char *symFilename) {
FILE* symFile;
long symSize;
size_t bytesRead;
symFile = fopen(symFilename, "r");
if(symFile == NULL) {
fputs("\nFile error\n", stderr);
exit(1);
}
// obtain file size:
fseek(symFile, 0, SEEK_END);
symSize = ftell(symFile);
rewind(symFile);
// read a line into the buffer
while(fgets((char*)symBuffer, SYM_BUF_MAX, symFile)) {
word symAddress;
char symTypeStr[8] = {0};
char symLabelStr[32] = {0};
// If this line isn't a comment
if(';' != symBuffer[0]) {
// sSCANf it
sscanf((const char*)symBuffer, "%s\t%04x\t\t%s", symTypeStr,
(uint*)&symAddress, symLabelStr);
if(0 == strcmp("code", symTypeStr) || 0 == strcmp("reg", symTypeStr)) {
addSymbol(symAddress, CODE, symLabelStr);
} else if(0 == strcmp("data", symTypeStr)) {
addSymbol(symAddress, DATA, symLabelStr);
} else if(0 == strcmp("org", symTypeStr)) {
addOrg(symAddress);
} else if(0 == strcmp("vector", symTypeStr)) {
addSymbol(symAddress, VECTOR, symLabelStr);
} else if(0 == strcmp("skip", symTypeStr)) {
skipArray[skipArrayLen++] = symAddress;
}
}
}
fclose(symFile);
return bytesRead;
}
void generateRelativeSymbols(word binCurrPos, opUnion ou, byte* buffPtr, bool isSubOp) {
operation currOp;
subOp currSubOp;
if(isSubOp) {
currSubOp = ou.subOp;
} else {
currOp = ou.op;
}
uint btnl = bytesToNextLabel(buffPtr);
if(isSubOp) {
if(IMMEDIATE16 == currSubOp.type) {
word opWord = (buffPtr[2] << 8) + buffPtr[3];
if(0x8000 < opWord && !inSkipArray(opWord)) {
char* symbol = getSymbol(opWord);
// Make up a label
if('\0' == symbol[0]) {
symbol = (char*)malloc(sizeof(char) * 6);
// Make up a label for the symbol
sprintf(symbol, "T%i", generatedLabel++);
// Add the made up symbol to the symbol list
addSymbol(opWord, DATA, symbol);
}
}
}
} else {
if(RELATIVE == currOp.type) { // Calculate RELATIVE address
// Signed char for two's compliment
word opWord = binCurrPos + (char)buffPtr[1] + 0x0002;
char* symbol = getSymbol(opWord);
// Make up a label
if('\0' == symbol[0] && !inSkipArray(opWord)) {
symbol = (char*)malloc(sizeof(char) * 6);
// Make up a label for the symbol
sprintf(symbol, "L%i", generatedLabel++);
// Add the made up symbol to the symbol list
addSymbol(opWord, CODE, symbol);
}
} else if(DIRECT3 == currOp.type) {
// Signed char for two's compliment
word opWord = binCurrPos + (char)buffPtr[3] + 0x0004;
char* symbol = getSymbol(opWord);
// Make up a label
if('\0' == symbol[0] && !inSkipArray(opWord)) {
symbol = (char*)malloc(sizeof(char) * 6);
// Make up a label for the symbol
sprintf(symbol, "L%i", generatedLabel++);
// Add the made up symbol to the symbol list
addSymbol(opWord, CODE, symbol);
}
} else if(IMMEDIATE16 == currOp.type) {
word opWord = (buffPtr[1] << 8) + buffPtr[2];
if(
!inSkipArray(opWord) &&
// 0 != strcmp(currOp.mnemonic, "ldd") &&
//0 != strcmp(currOp.mnemonic, "ldx") &&
0xD000 < opWord &&
0xFFFF > opWord
) {
char* symbol = getSymbol(opWord);
// Make up a label
if('\0' == symbol[0]) {
symbol = (char*)malloc(sizeof(char) * 6);
// Make up a label for the symbol
sprintf(symbol, "T%i", generatedLabel++);
// Add the made up symbol to the symbol list
addSymbol(opWord, DATA, symbol);
}
}
}
}
}
void printSymbols() {
for(int i=0; i < numSymbols; i++){
printf("%04x:\t%i\t%-8s\n", symbolTable[i].addr, (int)symbolTable[i].area, symbolTable[i].label);
}
}
word getOpSymbol(word binCurrPos, opUnion op, byte* buffPtr, bool* chkFlg) {
word opWord = 0;
operation currOp = op.op;
// Direct is 1-byte address, EXTENDED is 2-byte address
switch(currOp.type) {
case RELATIVE:
// Calculate RELATIVE address
opWord = binCurrPos + (char)buffPtr[1] + 0x0002; // Signed char for two's compliment
*chkFlg = 1;
break;
case DIRECT:
case DIRECT2:
opWord = buffPtr[currOp.isSubOp ? 2 : 1];
*chkFlg = 1;
break;
case EXTENDED:
opWord = (buffPtr[currOp.isSubOp ? 2 : 1] << 8) + buffPtr[currOp.isSubOp ? 3 : 2];
*chkFlg = 1;
break;
case IMMEDIATE16:
opWord = (buffPtr[currOp.isSubOp ? 2 : 1] << 8) + buffPtr[currOp.isSubOp ? 3 : 2];
*chkFlg = 1;
break;
default:
;
break;
};
return opWord;
}
//
// org
//
void addOrg(word address) {
static int i = 0;
orgTable[i++] = address;
}
void doOrg(word binCurrPos, char** symbol, bool lineNumbers, bool rawBytes) {
// Origin (.org)
for(int i = 0; i < ORG_MAX; i++) {
if(orgTable[i] == binCurrPos) {
if(rawBytes) {
printf(" ");
}
char frmt[16] = "\0";
sprintf(frmt, "%%%is%%-8s$%%04x\n", LABEL_PAD);
// `.org`s should come first with a label
//"%-17s%-8s$%04x\n"
printf(frmt, *symbol, ".org", binCurrPos);
if(lineNumbers) {
printf("%04X ", binCurrPos);
}
// Clear the label for the rest of this binCurrPos
*symbol = "\0";
}
}
}
//
// Sorting
//
int cmpSymbols(const void * a, const void * b) {
return ( ((symbolStruct*)a)->addr - ((symbolStruct*)b)->addr );
}
void sortSymbols() {
qsort(symbolTable, numSymbols, sizeof(symbolStruct), cmpSymbols);
}
//
// Helper
//
bool inSkipArray(word opWord) {
bool ret = 0;
for(int i=0; i < skipArrayLen; i++) {
if(skipArray[i] == opWord) {
ret = 1;
break;
}
}
return ret;
}