-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngineDVG.cc
More file actions
351 lines (281 loc) · 7.21 KB
/
Copy pathEngineDVG.cc
File metadata and controls
351 lines (281 loc) · 7.21 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dx.h"
#define BITCH(n, val) (((1<<n)&val)?'1':'0')
char *EngineDVG::scales[] = {
"???", "x2", "x4", "x8",
"x16", "x32", "x64", "x128",
"???", "/128", "/64", "/32",
"/16", "/8", "/4", "/2"
};
void EngineDVG::usage() {
printf("CPU == 'dvg'\n");
printf(" --game=<game name> (mandatory)\n");
printf(" --label-file=<label file>\n");
printf("\n");
}
void EngineDVG::initialise() {
int numArgs = args->getArgC(Args::argument);
const int req = Args::requires_argument;
const int reqNum = Args::requires_argument | Args::numeric_argument;
char labFile[MAXSTR];
char gameName[MAXSTR];
char cpuStr[MAXSTR];
if (numArgs != 1) {
Utils::abortf("Just one rom file expected\n");
}
parseopt_t mandatoryArgs[] = {
{"cpu", req, cpuStr, 'c'},
{"rom-start", reqNum, NULL, 'b'},
{"rom-size", reqNum, NULL, 's'},
{"game", req, gameName, 'g'},
{NULL, 0, NULL, 0}
};
parseopt_t optionalArgs[] = {
{"label-file", req, labFile, 'l'},
{"help", 0, NULL, '?'},
{"verbose", 0, &verbose, 'v'},
{NULL, 0, NULL, 0}
};
try {
args->parseArgs(mandatoryArgs, optionalArgs, true);
if (args->hasArg("label-file")) {
labels->readFile(labFile);
}
printf("Game: %s\n", gameName);
}
catch (CommandLineException e) {
Utils::abortf("Bad command line.\n");
}
for (int i=0; i < NUM_DVG_OPS; i++) {
opMap[i] = DVG_OP_NONE;
}
if (strcasecmp(gameName, "asteroids") == 0) {
opMap[0x8] = DVG_OP_VLDRAW;
opMap[0x9] = DVG_OP_VSCALED_DRAW;
opMap[0xa] = DVG_OP_VMOVETO;
opMap[0xb] = DVG_OP_HALT;
opMap[0xc] = DVG_OP_VJSR;
opMap[0xd] = DVG_OP_VRTS;
opMap[0xe] = DVG_OP_VJMP;
opMap[0xf] = DVG_OP_VSDRAW;
}
else {
Utils::abortf("Unknown game '%s'", gameName);
}
mem->setLittleEndian();
stackAddress(0);
}
int EngineDVG::decode4(int word) {
int res = word & 0x000f;
if (res & 0x0008) {
res = -(res & 0x0003);
}
return res;
}
int EngineDVG::decode12(int word) {
int res = word & 0x0fff;
if (res & 0x0800) {
res = -(res & 0x07ff);
}
return res;
}
int EngineDVG::findRTSOp() {
for (int i=0; i<NUM_DVG_OPS; i++) {
if (opMap[i] == DVG_OP_VRTS) {
return i<<12;
}
}
return -1;
}
void EngineDVG::backup(long addr) {
long nBytes;
if (addr < romStart) {
return;
}
nBytes = disassemble(addr);
if (nBytes > 0) {
// Ok so try and find instructions immediately before this one
if (validCode(addr-2)) {
backup(addr-2);
}
else if (validCode(addr-4)) {
backup(addr-4);
}
else {
return;
}
}
}
bool EngineDVG::canBranch(long addr) {
int op = mem->getWord(addr);
op = op>>12;
if ((opMap[op] == DVG_OP_VJSR) || (opMap[op] == DVG_OP_VJMP)) {
return true;
}
return false;
}
long EngineDVG::branchAddress(long addr) {
int inst = mem->getWord(addr);
return (inst & 0x0fff) * 2;
}
bool EngineDVG::validCode(long addr) {
int op = mem->getWord(addr);
op = op>>12;
if (opMap[op] == DVG_OP_NONE) {
return false;
}
return true;
}
int EngineDVG::codeSize(long addr) {
int op = mem->getWord(addr);
op = op>>12;
switch (opMap[op]) {
case DVG_OP_VCOLOR:
case DVG_OP_VSCALE:
case DVG_OP_VCENTER:
case DVG_OP_VJSR:
case DVG_OP_VRTS:
case DVG_OP_VJMP:
case DVG_OP_HALT:
case DVG_OP_VSDRAW:
return 2;
case DVG_OP_VSCALED_DRAW:
case DVG_OP_VMOVETO:
case DVG_OP_VLDRAW:
return 4;
case DVG_OP_NONE:
return -2;
}
}
void EngineDVG::pass1() {
long addr;
int rts = findRTSOp();
// Pass 1 - disassemble everything you can
for (addr = romStart; addr < romEnd; addr += 2) {
int op = mem->getWord(addr);
if (op == rts) {
backup(addr);
}
}
}
int EngineDVG::disassemble(long addr) {
int instruction;
int *opcode;
int immVal;
int inst;
int op;
char label[MAXSTR];
long target;
char regName;
OutputItem out(labels);
pc = addr = mem->maskAddress(addr);
out.clear();
out.setAddress(addr);
inst = fetch16();
op = (inst>>12) & 0x0f;
op = opMap[op];
switch (op) {
case DVG_OP_VLDRAW:
{
int inst2 = fetch16();
int y = decode12(inst);
int x = decode12(inst2);
int intensity = (inst2>>12) & 0x0f;
out.addComment("%04X: %02X %02X %02X %02X", addr, inst>>8, inst & 0xff, inst2>>8, inst2 & 0xff);
out.setInstruction("VEC");
out.setOperand("x=%d,y=%d,i=%d", x, y, intensity);
}
break;
case DVG_OP_VSCALED_DRAW:
{
int inst2 = fetch16();
int y = decode12(inst);
int x = decode12(inst2);
int scale = (inst2>>12) & 0x0f;
out.addComment("%04X: %02X %02X %02X %02X %s", addr, inst>>8, inst & 0xff, inst2>>8, inst2 & 0xff, scales[scale]);
out.setInstruction("SSV");
out.setOperand("x=%d,y=%d,s=%d", x, y, scale);
}
break;
case DVG_OP_HALT:
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("HALT");
break;
case DVG_OP_VSDRAW:
{
int y = decode4(inst>>8);
int x = decode4(inst);
int intensity = (inst >> 4) & 0x0f;
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("SVEC");
out.setOperand("x=%d,y=%d,i=%d", x, y, intensity);
}
break;
case DVG_OP_VSCALE:
{
int scale = (inst & 0x0fff);
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("SSC");
out.setOperand("%d", scale);
}
break;
case DVG_OP_VCOLOR:
{
int intensity = (inst & 0x00ff);
int rgb = (inst>>8) & 0x03;
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("SCOL");
out.setOperand("%d,%d", rgb, intensity);
}
break;
case DVG_OP_VCENTER:
out.addComment("%02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("CNTR");
break;
case DVG_OP_VJSR:
{
int dest = inst & 0x0fff;
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("JSR");
out.setOperand("$%04x", dest);
stackAddress(dest*2);
}
break;
case DVG_OP_VRTS:
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("RTS");
break;
case DVG_OP_VJMP:
{
int dest = inst & 0x0fff;
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("JMP");
out.setOperand("$%04x", dest);
stackAddress(dest*2);
}
break;
case DVG_OP_VMOVETO:
{
int inst2 = fetch16();
int y = (inst & 0x0fff);
int x = (inst2 & 0x0fff);
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("MOVE");
out.setOperand("%d,%d", x, y);
}
break;
case DVG_OP_NONE:
default:
out.addComment("%04X: %02X %02X", addr, inst>>8, inst & 0xff);
out.setInstruction("???");
// Unlike many disassemblers, we assume that all of the
// ROM image contains code
stackAddress(pc);
return -mem->setType(addr, Memory::BYTE, 2);
break;
}
return mem->setType(addr, Memory::CODE, pc-addr);
}