-
Notifications
You must be signed in to change notification settings - Fork 8
/
disassemble.c
375 lines (351 loc) · 7.24 KB
/
disassemble.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <stdio.h>
#include "cpu6.h"
#include "disassemble.h"
/* Disassembler */
static const char *r8map[16] = {
"AH", "AL",
"BH", "BL",
"XH", "XL",
"YH", "YL",
"ZH", "ZL",
"SH", "SL",
"CH", "CL",
"PH", "PL"
};
static const char *r16map[16] = {
"A", "AHH",
"B", "BHH",
"X", "XHH",
"Y", "YHH",
"Z", "ZHH",
"S", "SHH",
"C", "CHH",
"P", "PHH"
};
static const char *r8name(unsigned n)
{
n &= 0x0F;
return r8map[n];
}
static const char *r16name(unsigned n)
{
n &= 0x0F;
return r16map[n];
}
static uint16_t get8d(unsigned rpc)
{
return mmu_mem_read8_debug(rpc);
}
static uint16_t get16d(unsigned rpc)
{
uint16_t n = mmu_mem_read8_debug(rpc) << 8;
n |= mmu_mem_read8_debug(rpc + 1);
return n;
}
static void dis16d(unsigned rpc)
{
uint16_t n = get16d(rpc);
fprintf(stderr, "%04X", n);
}
static void disindexed(unsigned rpc)
{
unsigned r = mmu_mem_read8_debug(rpc);
if (r & 4)
fputc('@', stderr);
if (r & 8)
fprintf(stderr, "%d", mmu_mem_read8_debug(rpc + 1));
switch (r & 3) {
case 0:
fprintf(stderr, "(%s)", r16name(r >> 4));
break;
case 1:
fprintf(stderr, "(%s+)", r16name(r >> 4));
break;
case 2:
fprintf(stderr, "(-%s)", r16name(r >> 4));
break;
case 3:
fprintf(stderr, "Bad indexing mode.");
break;
}
}
static void disaddr(unsigned rpc, unsigned size, unsigned op,
unsigned isjump)
{
switch (op) {
case 0:
if (size == 1)
fprintf(stderr, "%02X", mmu_mem_read8_debug(rpc));
else
dis16d(rpc);
break;
case 1:
if (!isjump)
fputc('(', stderr);
dis16d(rpc);
if (!isjump)
fputc(')', stderr);
break;
case 2:
if (!isjump)
fputc('@', stderr);
fputc('(', stderr);
dis16d(rpc);
fputc(')', stderr);
break;
case 3:
fprintf(stderr, "(PC+%d)", (int8_t) mmu_mem_read8_debug(rpc));
break;
case 4:
fprintf(stderr, "@(PC+%d)", (int8_t) mmu_mem_read8_debug(rpc));
break;
case 5:
disindexed(rpc);
break;
case 6:
case 7:
fprintf(stderr, "invalid address decode.");
break;
default:
fprintf(stderr, "(%s)", r16name((op & 0x07) << 1));
break;
}
fputc('\n', stderr);
}
static const char *dmaname[4] = { "STDMA", "LDDMA", "STDMAC", "LDDMAC" };
static void dis_dma(unsigned addr)
{
unsigned dmaop = mmu_mem_read8_debug(addr);
unsigned rp = dmaop >> 4;
dmaop &= 15;
if (dmaop == 5 || dmaop > 6) {
fprintf(stderr, "DMA unknown(%d), %s\n", dmaop, r16name(rp));
return;
}
if (dmaop < 4)
fprintf(stderr, "%s %s\n", dmaname[dmaop], r16name(rp));
else if (dmaop == 4)
fprintf(stderr, "dmamode %d\n", rp);
else
fprintf(stderr, "dmaen\n");
}
static void dis_mmu(unsigned addr)
{
unsigned op;
op = mmu_mem_read8_debug(addr);
switch(op) {
case 0x0C:
fprintf(stderr, "LDMMU %d (%04X)\n",
mmu_mem_read8_debug(addr + 1) & 7,
get16d(addr + 2));
break;
case 0x1C:
fprintf(stderr, "STMMU %d (%04X)\n",
mmu_mem_read8_debug(addr + 1) & 7,
get16d(addr + 2));
break;
default:
fprintf(stderr, "Unknown MMU op %02X\n", op);
break;
}
}
static void dis_block_op(unsigned addr)
{
unsigned op = mmu_mem_read8_debug(addr);
switch(op) {
case 0x40:
fprintf(stderr, "bcp ");
break;
case 0x80:
fprintf(stderr, "bcmp ");
break;
default:
fprintf(stderr, "Unknown 0x47 op %02X\n", op);
return;
}
fprintf(stderr, "%02X, (%04X), (%04X)\n",
mmu_mem_read8_debug(addr + 1) + 1,
get16d(addr + 2),
get16d(addr + 4));
}
static const char *op0name[] = {
"HLT", "NOP", "SF", "RF", "EI", "DI", "SL", "RL",
"CL", "RSR", "RI", "RIM", "ELO", "PCX", "DLY", "RSYS"
};
static const char *braname[] = {
"BL", "BNL", "BF", "BNF", "BZ", "BNZ", "BM", "BP",
"BGZ", "BLE", "BS1", "BS2", "BS3", "BS4", "BTM", "BEP"
};
static const char *alu1name[] = {
"INR", "DCR", "CLR", "IVR", "SRR", "SLR", "RRR", "RLR"
};
static const char *alu2name[] = {
"ADD", "SUB", "AND", "ORI", "ORE", "XFR"
};
static const char *ldst[] = {
"LDAB ", "LDA ",
"STAB ", "STA ",
"LDBB ", "LDB ",
"STBB ", "STB "
};
static void stack_op(const char *op, unsigned rpc)
{
uint8_t byte2 = mmu_mem_read8_debug(rpc);
uint8_t r = byte2 >> 4;
uint8_t end = r + (byte2 & 0x0F) + 1;
const char* s = "";
fprintf(stderr, "%s {", op);
if (r & 1) {
fputs(r8map[r], stderr);
s = ",";
}
while (r + 1 < end) {
fprintf(stderr, "%s%s", s, r16map[r]);
s = ",";
r += 2;
}
if (r < end) {
fprintf(stderr, "%s%s", s, r8map[r]);
}
fputs("}\n", stderr);
}
void disassemble(unsigned op)
{
unsigned rpc = cpu6_pc() + 1;
if (op < 0x10) {
fprintf(stderr, "%s\n", op0name[op]);
return;
}
if (op < 0x20) {
fprintf(stderr, "%s %d\n", braname[op & 0x0F],
mmu_mem_read8_debug(rpc));
return;
}
if (op < 0x28) {
uint8_t v = mmu_mem_read8_debug(rpc);
fprintf(stderr, "%sB %s", alu1name[op & 7],
r8name(v >> 4));
if (v & 0x0F)
fprintf(stderr, ", %d", v & 0x0F);
fprintf(stderr, "\n");
return;
}
if (op < 0x2E) {
fprintf(stderr, "%s AL\n", alu1name[op & 7]);
return;
}
if (op == 0x2E) {
dis_mmu(rpc);
return;
}
if (op == 0x2F) {
dis_dma(rpc);
return;
}
/* TODO DMA 2E MMU 2F */
if (op < 0x38) {
uint8_t v = mmu_mem_read8_debug(rpc);
fprintf(stderr, "%s %s", alu1name[op & 7],
r16name(v >> 4));
if (v & 0x0F)
fprintf(stderr, ", %d", v & 0x0F);
fprintf(stderr, "\n");
return;
}
if (op < 0x3E) {
fprintf(stderr, "%s A\n", alu1name[op & 7]);
return;
}
if (op == 0x3E) {
fputs("INX\n", stderr);
return;
}
if (op == 0x3F) {
fputs("DCX\n", stderr);
return;
}
if (op < 0x46) {
uint8_t v = mmu_mem_read8_debug(rpc);
fprintf(stderr, "%sB %s, %s\n", alu2name[op & 7],
r8name(v >> 4), r8name(v));
return;
}
if (op == 0x47) {
dis_block_op(rpc);
return;
}
/* TODO 46 47 */
if (op < 0x4E) {
fprintf(stderr, "%sB AL,BL\n", alu2name[op & 7]);
return;
}
/* 4E 4F mystery */
if (op < 0x56) {
uint8_t v = mmu_mem_read8_debug(rpc);
uint8_t f = v & 0x11;
v &= 0xEE;
switch(f) {
case 0x00:
fprintf(stderr, "%s %s, %s\n", alu2name[op & 7],
r16name(v >> 4), r16name(v));
break;
case 0x01:
fprintf(stderr, "%s %s, (%X)\n", alu2name[op & 7],
r16name(v >> 4), get16d(rpc + 1));
break;
case 0x10:
fprintf(stderr, "%s %s, %X\n", alu2name[op & 7],
r16name(v >> 4), get16d(rpc + 1));
break;
case 0x11:
fprintf(stderr, "%s (%X), %s\n", alu2name[op & 7],
get16d(rpc + 1), r16name(v));
break;
}
return;
}
/* TODO 46 47 */
if (op < 0x5B) {
fprintf(stderr, "%s A,B\n", alu2name[op & 7]);
return;
}
if (op < 0x60) {
fprintf(stderr, "XA%c\n", "XYBZS"[op - 0x5B]);
return;
}
if (op == 0x66) {
fprintf(stderr, "JSYS %02X\n", get8d(rpc));
return;
}
if (op < 0x70) {
/* X ops */
if (op & 0x08)
fputs("STX ", stderr);
else
fputs("LDX ", stderr);
disaddr(rpc, 2, op & 7, 1);
return;
}
if (op == 0x7e) {
stack_op("PUSH", rpc);
return;
}
if (op == 0x7f) {
stack_op("POP", rpc);
return;
}
if (op < 0x80) {
if (op == 0x76) {
fputs("SYSCALL?\n", stderr);
return;
}
if (op & 0x08)
fputs("JSR ", stderr);
else
fputs("JMP ", stderr);
disaddr(rpc, 2, op & 7, 0);
return;
}
fputs(ldst[(op & 0x7F) >> 4], stderr);
disaddr(rpc, (op & 0x10) ? 2 : 1, op & 15, 0);
}