-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisas.c
546 lines (465 loc) · 16 KB
/
disas.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include "disas.h"
#include <stdarg.h>
#include <string.h>
typedef struct disas {
disas_entry_t *entries;
const mapper_t *shared_mapper;
} disas_t;
static void disas_recursive_virt(disas_ref self, uint16_t virt, bool subroutine,
bool allow_unofficial);
static int disas_absolute_jump_phys(disas_ref self, uint32_t phys,
bool ignore_boundary);
static int disas_absolute_jump_virt(disas_ref self, uint16_t vector,
bool ignore_boundary);
static void disas_recursive_phys(disas_ref self, uint16_t virt_base,
uint32_t phys, bool subroutine,
bool allow_unofficial);
static int disas_mark_data_virt(disas_ref self, uint16_t virt, uint32_t size);
static int disas_mark_data_phys(disas_ref self, uint32_t phys, uint32_t size);
static int disas_absolute_jump_phys(disas_ref self, uint32_t phys,
bool ignore_boundary);
static inline bool _is_potential_jump(disas_t *self, uint8_t *prg);
void disas_destroy(disas_ref self)
{
if (self) {
FREE(self->entries);
FREE(self);
}
}
disas_ref disas_create(const mapper_t *mapper)
{
disas_ref self = NULL;
TALLOC(self);
TALLOCS(self->entries, mapper->prg_size);
GUARD(self->shared_mapper = mapper);
return self;
error:
disas_destroy(self);
return NULL;
}
const disas_entry_t *disas_get_entries(disas_ref self)
{
return self->entries;
}
static int disas_absolute_jump_virt(disas_ref self, uint16_t vector,
bool ignore_boundary)
{
uint32_t phys =
self->shared_mapper->on_prg_phys(self->shared_mapper, vector);
return disas_absolute_jump_phys(self, phys, ignore_boundary);
}
int disas_mark_from_vector(disas_ref self, uint16_t vector)
{
return disas_absolute_jump_virt(self, vector, true);
}
int disas_mark_from_vectors(disas_ref self)
{
GUARD(disas_mark_data_runtime(self, 0xFFFA, 6));
GUARD(disas_mark_from_vector(self, 0xFFFA));
GUARD(disas_mark_from_vector(self, 0xFFFC));
GUARD(disas_mark_from_vector(self, 0xFFFE));
return SUCCESS;
error:
return NG;
}
static int disas_mark_data_virt(disas_ref self, uint16_t virt, uint32_t size)
{
uint32_t phys = self->shared_mapper->on_prg_phys(self->shared_mapper, virt);
return disas_mark_data_phys(self, phys, size);
}
int disas_mark_data_runtime(disas_ref self, uint16_t addr, uint32_t size)
{
return disas_mark_data_virt(self, addr, size);
}
static int disas_mark_data_phys(disas_ref self, uint32_t phys, uint32_t size)
{
for (int i = 0; i < size; ++i) {
GUARD(phys < self->shared_mapper->prg_size);
if (self->entries[phys].marked) {
if (!self->entries[phys].data) {
WARN("overlapping data %02x:%02x@%05x\n",
self->entries[phys].marked,
self->shared_mapper->prg_base[phys], phys);
self->entries[phys].data = 1;
}
} else {
self->entries[phys].data = 1;
}
++phys;
}
return SUCCESS;
error:
return NG;
}
static int _snprintf(char **buf_ref, int *inout_size, const char *fmt, ...)
{
if (*inout_size <= 0) {
return NG;
}
va_list args;
va_start(args, fmt);
int delta = vsnprintf(*buf_ref, *inout_size, fmt, args);
*buf_ref += delta;
*inout_size -= delta;
va_end(args);
return (delta > 0) ? SUCCESS : NG;
}
static int _print_op(disas_t *self, uint32_t phys, char *buf, int org_buf_size)
{
int buf_size = org_buf_size;
uint8_t *prg = self->shared_mapper->prg_base;
op_t op = prg[phys];
op_profile_t *profile = &op_profiles[op];
_snprintf(&buf, &buf_size, "%05x: ", phys);
for (int i = 0; i < 3; ++i) {
if (i < profile->length) {
if (i > 0) {
_snprintf(&buf, &buf_size, " ");
}
_snprintf(&buf, &buf_size, "%02x", prg[phys + i]);
} else {
_snprintf(&buf, &buf_size, " ");
}
}
_snprintf(&buf, &buf_size, ": ");
switch (profile->mode) {
case OP_MODE_IMPLIED: // Fallthrough
case OP_MODE_ACCUMULATOR:
_snprintf(&buf, &buf_size, "%s", profile->name);
break;
case OP_MODE_ZERO_PAGE:
_snprintf(&buf, &buf_size, "%s $%02x", profile->name, prg[phys + 1]);
break;
case OP_MODE_ZERO_PAGE_X:
_snprintf(&buf, &buf_size, "%s $%02x,x", profile->name, prg[phys + 1]);
break;
case OP_MODE_ZERO_PAGE_Y:
_snprintf(&buf, &buf_size, "%s $%02x,y", profile->name, prg[phys + 1]);
break;
case OP_MODE_ABSOLUTE:
_snprintf(&buf, &buf_size, "%s $%04x", profile->name,
(uint16_t)prg[phys + 1] | (prg[phys + 2] << 8));
break;
case OP_MODE_ABSOLUTE_X:
_snprintf(&buf, &buf_size, "%s $%04x,x", profile->name,
(uint16_t)prg[phys + 1] | (prg[phys + 2] << 8));
break;
case OP_MODE_ABSOLUTE_Y:
_snprintf(&buf, &buf_size, "%s $%04x,y", profile->name,
(uint16_t)prg[phys + 1] | (prg[phys + 2] << 8));
break;
case OP_MODE_POST_INDIRECT_Y:
_snprintf(&buf, &buf_size, "%s ($%02x),y", profile->name,
prg[phys + 1]);
break;
case OP_MODE_PRE_INDIRECT_X:
_snprintf(&buf, &buf_size, "%s ($%02x,x)", profile->name,
prg[phys + 1]);
break;
case OP_MODE_INDIRECT:
_snprintf(&buf, &buf_size, "%s ($%04x)", profile->name,
(uint16_t)prg[phys + 1] | (prg[phys + 2] << 8));
break;
case OP_MODE_IMMEDIATE:
_snprintf(&buf, &buf_size, "%s #$%02x", profile->name, prg[phys + 1]);
break;
case OP_MODE_RELATIVE:
_snprintf(&buf, &buf_size, "%s #%d", profile->name,
(int8_t)prg[phys + 1]);
break;
}
return org_buf_size - buf_size;
}
static bool _print_entry(disas_t *self, uint32_t phys, char *buf, int buf_size,
bool show_comments)
{
disas_entry_t *entries = self->entries;
uint8_t *prg = self->shared_mapper->prg_base;
if (self->entries[phys].op) {
int offset = _print_op(self, phys, buf, buf_size);
buf += offset;
buf_size -= offset;
} else if (!entries[phys].arg) {
_snprintf(&buf, &buf_size, "%05x: %02x", phys, prg[phys]);
} else {
return false;
}
if (show_comments) {
if (self->entries[phys].subroutine) {
_snprintf(&buf, &buf_size, " ;subroutine");
}
if (self->entries[phys].unofficial) {
_snprintf(&buf, &buf_size, " ;unofficial");
}
if (self->entries[phys].data) {
_snprintf(&buf, &buf_size, " ;data");
}
if (!self->entries[phys].marked) {
_snprintf(&buf, &buf_size, " ;unknown");
} else if (self->entries[phys].op &&
_is_potential_jump(self, &prg[phys])) {
_snprintf(&buf, &buf_size, " ;;");
}
if ((phys % self->shared_mapper->prg_split) == 0) {
_snprintf(&buf, &buf_size, " ;bank start %d/%d",
phys / self->shared_mapper->prg_split,
self->shared_mapper->num_prg_pages);
}
if ((phys % self->shared_mapper->prg_split) ==
self->shared_mapper->prg_split - 1) {
_snprintf(&buf, &buf_size, " ;bank end %d/%d",
phys / self->shared_mapper->prg_split + 1,
self->shared_mapper->num_prg_pages);
}
}
return true;
}
void disas_print_entry(disas_ref self, uint32_t phys, char *buf, int buf_size,
bool show_comments)
{
_print_entry(self, phys, buf, buf_size, show_comments);
}
static void disas_recursive_phys(disas_t *self, uint16_t virt_base,
uint32_t phys, bool subroutine,
bool allow_unofficial)
{
disas_entry_t *entries = self->entries;
uint8_t *prg = self->shared_mapper->prg_base;
entries[phys].subroutine = subroutine;
bool terminate = false;
do {
op_t op = prg[phys];
if (entries[phys].marked) {
if (entries[phys].op) {
break;
}
if (entries[phys].data || entries[phys].arg) {
int partial_advance = op_profiles[op].length;
if (entries[phys + partial_advance].op) {
WARN("HACK:instruction overlap %05x\n", phys);
entries[phys].op = 1;
break;
} else {
WARN("HACK:instruction is used as data %02x@%05x\n",
entries[phys].marked, phys);
}
}
}
if (!op_is_official(op)) {
if (allow_unofficial) {
WARN("accepted unofficial op %02x at %05x\n", op, phys);
entries[phys].unofficial = 1;
entries[phys].op = 1;
} else {
WARN("stop marking at unofficial op %02x at %05x\n", op, phys);
entries[phys].data = 1;
break;
}
}
allow_unofficial = false;
self->entries[phys].op = 1;
int advance = op_length(op);
if (phys + advance >= self->shared_mapper->prg_size) {
WARN("overrun %05x\n", phys);
break;
}
for (int i = 0; i < advance - 1; ++i) {
uint32_t pos = phys + 1 + i;
if (self->entries[pos].op) {
WARN("HACK:instruction overlap %05x\n", pos);
}
self->entries[pos].arg = 1;
}
switch (op) {
case OP_20_JSR:
if (self->shared_mapper->on_boundary_check(self->shared_mapper,
phys, phys + 2)) {
disas_absolute_jump_phys(self, phys + 1, false);
} else {
WARN("JSR arg crossed bank boundary: %05x\n", phys);
terminate = true;
}
break;
case OP_4C_JMP: {
disas_absolute_jump_phys(self, phys + 1, false);
terminate = true;
break;
}
case OP_40_RTI:
case OP_60_RTS:
case OP_6C_JMP:
terminate = true;
break;
default:
if (op_profiles[op].mode == OP_MODE_RELATIVE) {
uint32_t branch_phys =
phys + (int8_t)(prg[phys + 1]) + op_profiles[op].length;
if (self->shared_mapper->on_boundary_check(self->shared_mapper,
phys, branch_phys)) {
disas_recursive_phys(self, virt_base, branch_phys, false,
false);
} else {
terminate = true;
}
}
break;
}
if (self->shared_mapper->on_boundary_check(self->shared_mapper, phys,
phys + advance)) {
phys += advance;
} else {
terminate = true;
}
} while (!terminate);
}
static void disas_recursive_virt(disas_t *self, uint16_t virt, bool subroutine,
bool allow_unofficial)
{
uint32_t phys = self->shared_mapper->on_prg_phys(self->shared_mapper, virt);
disas_recursive_phys(self, virt, phys, subroutine, allow_unofficial);
}
void disas_mark_op_runtime(disas_ref self, uint16_t addr, bool subroutine)
{
return disas_recursive_virt(self, addr, subroutine, true);
}
static inline bool _is_potential_jump(disas_t *self, uint8_t *prg)
{
switch (*prg) {
case OP_60_RTS:
case OP_40_RTI:
return true;
case OP_00_BRK:
if (op_is_official(prg[1])) {
return true;
}
break;
case OP_4C_JMP:
case OP_6C_JMP:
case OP_20_JSR:
if (prg[2] < 0x8 || prg[2] >= 0x80) {
// potential valid instruction
return true;
}
break;
default:
if (op_profiles[*prg].mode == OP_MODE_RELATIVE) {
uint8_t jump_target = prg[(int8_t)prg[1]];
if (op_is_official(prg[2]) && op_is_official(jump_target)) {
return true;
}
}
break;
}
return false;
}
static void _analyze(disas_t *self)
{
disas_entry_t *entries = self->entries;
uint8_t *prg = self->shared_mapper->prg_base;
uint32_t unknown_start = 0;
bool unknown_zone = false;
for (uint32_t i = 0; i < self->shared_mapper->prg_size; ++i) {
if (unknown_zone) {
if (entries[i].marked) {
bool all_op = true;
uint32_t j = unknown_start;
uint32_t last_op = j;
while (j < i) {
if (!op_is_official(prg[j])) {
all_op = false;
break;
}
last_op = j;
j += op_length(prg[j]);
}
if (all_op && j == i &&
(entries[j].op ||
_is_potential_jump(self, &prg[last_op]))) {
j = unknown_start;
while (j < i) {
entries[j].op = 1;
int len = op_length(prg[j]);
for (int k = 1; k < len; ++k) {
entries[j + k].arg = 1;
}
j += len;
}
TRACE("heuristic op %05x-%05x\n", unknown_start, i);
} else {
bool all_data = true;
for (uint32_t j = unknown_start; j < i; ++j) {
if (_is_potential_jump(self, &prg[j])) {
all_data = false;
break;
}
}
if (all_data) {
for (uint32_t j = unknown_start; j < i; ++j) {
entries[j].data = 1;
}
TRACE("heuristic data %05x-%05x\n", unknown_start, i);
}
}
unknown_zone = false;
}
} else if (!entries[i].marked) {
unknown_zone = true;
unknown_start = i;
}
}
}
uint32_t disas_scroll_address(disas_t *self, uint32_t base, int32_t scroll)
{
disas_entry_t *entries = self->entries;
uint32_t prg_size = self->shared_mapper->prg_size;
int advance = 0;
while (scroll != 0) {
advance += sign(scroll);
uint32_t pos = (base + advance) % prg_size;
if (entries[pos].op || !entries[pos].arg) {
scroll -= sign(scroll);
}
}
return (base + advance) % prg_size;
}
uint32_t _count_marks(disas_t *self)
{
uint32_t num_marks = 0;
for (int i = 0; i < self->shared_mapper->prg_size; ++i) {
if (self->entries[i].marked) {
++num_marks;
}
}
return num_marks;
}
static int disas_absolute_jump_phys(disas_ref self, uint32_t phys,
bool ignore_boundary)
{
GUARD(phys < self->shared_mapper->prg_size - 1);
uint8_t *prg = self->shared_mapper->prg_base;
uint32_t to_phys = prg[phys] | (prg[phys + 1] << 8);
if (ignore_boundary || self->shared_mapper->on_boundary_check(
self->shared_mapper, phys, to_phys)) {
disas_recursive_virt(self, to_phys, true, false);
}
return SUCCESS;
error:
return NG;
}
int disas_dump(disas_t *self, FILE *fp)
{
GUARD(fp);
_analyze(self);
char buf[256] = {};
for (uint32_t i = 0; i < self->shared_mapper->prg_size; ++i) {
if (_print_entry(self, i, buf, 256, true)) {
fprintf(fp, "%s\n", buf);
}
}
fprintf(fp, "; marked %.3f%%\n",
100.0 * (float)_count_marks(self) /
(float)self->shared_mapper->prg_size);
return SUCCESS;
error:
return NG;
}