-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.c
890 lines (785 loc) · 27.6 KB
/
decode.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/*
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
__/¯\____ ___/\__ _/\__ _/\_ _/\__ _/\___ ___/\__ __/\_ _/\__
\_ ____/_> ____ \_/ _ \_ \ < /_ \_/ _>> ____ \_ > \_/ _ \_
_> ___/ ¯>__> <<__// __ _/ |> ></ _/> </ ¯ \\__> <<__// /\ // __ _/
_> \7 <__/:. \__/:. \> \_/ L/ _____/. 7> .\_/:. \__/ <_/ </:. \> \_
|:::::::::::::::::::::::/:::::::::::::>::::::::/::::::::::::::::::::::::/:::::|
|¯¯\::::/\:/¯\::::/¯¯¯¯<::::/\::/¯¯\:/¯¯¯¯¯¯\::\::/¯¯\::::/¯¯\::::/¯¯¯¯<::::/¯|
|__ |¯¯| T _ |¯¯¯| ___ |¯¯| |¯| _ T ______ |¯¯¯¯| _ |¯¯¯| _ |¯¯¯| ___ |¯¯| _|
\|__|/\|/ \|___|/ \|__|/\|_|/ \|/ \| |/ \|___|/ \|___|/dNo\|__|/
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
This file is part of Etripator,
copyright (c) 2009--2023 Vincent Cruz.
Etripator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Etripator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Etripator. If not, see <http://www.gnu.org/licenses/>.
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
*/
#include "decode.h"
#include "message.h"
#include "opcodes.h"
#include <stdarg.h>
#define ETRIPATOR_LAST_COLUMN 80U
static const char *spacing = " ";
static int last_column_spacing(int current_char_count) {
return (current_char_count < ETRIPATOR_LAST_COLUMN) ? (ETRIPATOR_LAST_COLUMN - current_char_count) : 1;
}
static void print_comment(FILE *out, const char *str) {
if (!str) {
return;
}
while (*str) {
fputc(';', out);
fputc(' ', out);
for (; *str && (*str != '\n'); str++) {
fputc(*str, out);
}
fputc('\n', out);
if (*str) {
str++;
}
}
}
static void print_inline_comment(FILE *out, int n, const char *str) {
while (*str) {
for (; n < ETRIPATOR_LAST_COLUMN; n++) {
fputc(' ', out);
}
n = 0;
fputc(';', out);
fputc(' ', out);
for (; *str && (*str != '\n'); ++str) {
fputc(*str, out);
}
if (*str == '\n') {
++str;
if (*str) {
fputc('\n', out);
}
}
}
}
static void print_statement_address(FILE *out, int n, uint16_t logical, uint8_t page) {
fprintf(out, "%*c; bank: $%03x logical: $%04x", last_column_spacing(n), ' ', page, logical);
}
static void print_label(FILE *out, label_t *label) {
int n = last_column_spacing((int)strlen(label->name) + 1);
/* Print description */
print_comment(out, label->description);
/* Print label with bank and logical address as comments*/
fprintf(out, "%s:%*c; bank: $%03x logical: $%04x", label->name, n, ' ', label->page, label->logical);
}
/* Finds any jump address from the current section. */
int label_extract(section_t *section, memmap_t *map, label_repository_t *repository) {
int i;
uint8_t inst;
uint8_t data[6];
char buffer[32];
uint16_t logical;
uint8_t page;
const opcode_t *opcode;
if (section->type != Code) {
return 1;
}
/* Walk along section */
for (logical = section->logical; logical < (section->logical + section->size); logical += opcode->size) {
/* Read instruction */
inst = memmap_read(map, logical);
opcode = opcode_get(inst);
/* Read data (if any) */
for (i = 0; i < (opcode->size - 1); i++) {
data[i] = memmap_read(map, logical + i + 1);
}
if (opcode_is_local_jump(inst)) {
uint16_t jump;
int delta;
/* For BBR* and BBS* displacement is stored in the 2nd byte */
i = (((inst)&0x0F) == 0x0F) ? 1 : 0;
/* Detect negative number */
if (data[i] & 128) {
delta = -((data[i] - 1) ^ 0xff);
} else {
delta = data[i];
}
delta += opcode->size;
jump = logical + delta;
page = memmap_page(map, jump);
/* Create label name */
snprintf(buffer, 32, "l%04x_%02d", jump, page);
/* Insert offset to repository */
if (!label_repository_add(repository, buffer, jump, page, NULL)) {
return 0;
}
INFO_MSG("%04x short jump to %04x (%02x)", logical, jump, page);
} else if (opcode_is_far_jump(inst)) {
uint16_t jump = data[0] | (data[1] << 8);
page = memmap_page(map, jump);
/* Create label name */
snprintf(buffer, 32, "l%04x_%02d", jump, page);
/* Insert offset to repository */
if (!label_repository_add(repository, buffer, jump, page, NULL)) {
return 0;
}
INFO_MSG("%04x long jump to %04x (%02x)", logical, jump, page);
}
}
return 1;
}
static int data_extract_binary(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository) {
uint16_t logical;
int32_t i;
for (i = 0, logical = section->logical; i < section->size; i++, logical++) {
uint8_t data = memmap_read(map, logical);
fwrite(&data, 1, 1, out);
}
return 1;
}
static int data_extract_hex(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
comment_repository_t *comments, int extra_infos) {
const int32_t element_size = section->data.element_size;
const int32_t elements_per_line = section->data.elements_per_line;
int32_t i, j;
uint16_t logical;
label_t label;
comment_t comment;
uint8_t buffer[2] = {0};
int32_t top = 0;
size_t line_offset = ftell(out);
uint8_t line_page = section->page;
uint16_t line_logical = section->logical;
int has_comment = 0;
for (i = 0, j = 0, logical = section->logical; i < section->size; i++, logical++) {
uint8_t page = memmap_page(map, logical);
int has_label = label_repository_find(repository, logical, page, &label);
if (has_label) {
// flush any bytes left in the buffer.
if (top && (top < element_size)) {
fprintf(out, "\n%s.db $%02x", spacing, buffer[0]);
for (int32_t l = 1; l < top; l++) { // useless as top is always equal to 1
fprintf(out, ",$%02x", buffer[l]);
}
top = 0;
}
if (i) {
fputc('\n', out);
}
print_label(out, &label);
j = 0;
}
comment_t dummy;
if (comment_repository_find(comments, logical, page, &dummy)) {
if (has_comment) {
if (top && (top < element_size)) {
fprintf(out, "\n%s.db $%02x", spacing, buffer[0]);
for (int32_t l = 1; l < top; l++) { // useless as top is always equal to 1
fprintf(out, ",$%02x", buffer[l]);
}
top = 0;
}
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
}
memcpy(&comment, &dummy, sizeof(comment_t));
has_comment = 1;
j = 0;
}
buffer[top++] = memmap_read(map, logical);
if (top >= element_size) {
char sep;
if (j == 0) {
fputc('\n', out);
line_offset = ftell(out);
line_logical = logical - top + 1;
line_page = page;
const char *data_decl = (top > 1) ? ".dw" : ".db";
fprintf(out, "%s%s", spacing, data_decl);
sep = ' ';
} else {
sep = ',';
}
fputc(sep, out);
fputc('$', out);
if (top > 1) {
while (top--) {
fprintf(out, "%02x", buffer[top]);
}
} else {
fprintf(out, "%02x", buffer[0]);
}
top = 0;
j++;
if (j == elements_per_line) {
j = 0;
int n = (int)(ftell(out) - line_offset);
if (has_comment) {
print_inline_comment(out, n, comment.text);
has_comment = 0;
} else if (extra_infos) {
print_statement_address(out, n, line_logical, line_page);
}
}
}
}
// flush remaining bytes
if (top) {
int n = (int)(ftell(out) - line_offset);
if (has_comment) {
print_inline_comment(out, n, comment.text);
has_comment = 0;
} else if (extra_infos) {
print_statement_address(out, (int)(ftell(out) - line_offset), line_logical, line_page);
}
fprintf(out, "\n%s.db $%02x", spacing, buffer[0]);
for (int32_t j = 1; j < top; j++) { // useless as top is always equal to 1
fprintf(out, ",$%02x", buffer[j]);
}
}
fputc('\n', out);
return 1;
}
static int data_extract_string(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
comment_repository_t *comments, int extra_infos) {
const int32_t elements_per_line = section->data.elements_per_line;
int32_t i, j;
uint16_t logical;
int c = 0;
int has_label = 0;
int has_comment = 0;
size_t line_offset = 0;
uint16_t line_logical = 0;
uint8_t line_page;
comment_t comment = {0};
for (i = 0, j = 0, logical = section->logical; i < section->size; i++, logical++) {
uint8_t data = memmap_read(map, logical);
uint8_t page = memmap_page(map, logical);
label_t label = {0};
has_label = label_repository_find(repository, logical, page, &label);
if (has_label) {
if (c) { // close string if neededs
fputc('"', out);
c = 0;
}
if (i) {
fputc('\n', out);
}
j = 0;
print_label(out, &label);
}
comment_t dummy;
if (comment_repository_find(comments, logical, page, &dummy)) {
if (j) {
if (c) { // close string if neededs
fputc('"', out);
c = 0;
}
j = 0;
}
if (has_comment) {
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
}
memcpy(&comment, &dummy, sizeof(comment_t));
has_comment = 1;
}
// display directives
if (j == 0) {
fputc('\n', out);
line_offset = ftell(out); // record star of line
line_logical = logical;
line_page = page;
fprintf(out, "%s.db ", spacing);
}
// print char
if ((data >= 0x20) && (data < 0x7f)) {
// ascii/printable char
if (!c) {
if (j) {
fputc(',', out);
}
fputc('"', out);
c = 1;
}
if (data == '"') {
fputc('\\', out);
}
fputc(data, out);
} else {
// otherwise close string and display hex value
if (c) {
fputc('"', out);
c = 0;
}
if (j) {
fputc(',', out);
}
fprintf(out, "$%02x", data);
}
j++;
if (j == elements_per_line) {
j = 0;
if (c) {
fputc('"', out);
c = 0;
}
int n = (int)(ftell(out) - line_offset);
if (has_comment) {
print_inline_comment(out, n, comment.text);
has_comment = 0;
} else if (extra_infos) {
print_statement_address(out, n, line_logical, line_page);
}
}
}
if (j) {
if (c) {
fputc('"', out);
}
int n = (int)(ftell(out) - line_offset);
if (has_comment) {
print_inline_comment(out, n, comment.text);
has_comment = 0;
} else if (extra_infos) {
print_statement_address(out, n, line_logical, line_page);
}
}
fputc('\n', out);
return 1;
}
static int data_extract_jump_table(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
comment_repository_t *comments, int extra_infos) {
const int32_t elements_per_line = section->data.elements_per_line;
int32_t i, j;
uint8_t page;
uint16_t logical;
label_t label;
comment_t comment;
size_t line_offset = ftell(out);
uint8_t line_page = section->page;
uint16_t line_logical = section->logical;
int has_label = 0;
int has_comment = 0;
uint8_t data[2] = {0};
for (i = 0, j = 0, logical = section->logical; i < section->size; i += 2, logical += 2) {
page = memmap_page(map, logical);
data[0] = memmap_read(map, logical);
data[1] = memmap_read(map, logical + 1);
has_label = label_repository_find(repository, logical, page, &label);
if (has_label) {
if (i) {
fputc('\n', out);
}
print_label(out, &label);
j = 0;
}
comment_t dummy;
if (comment_repository_find(comments, logical, page, &dummy)) {
if (has_comment) {
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
}
memcpy(&comment, &dummy, sizeof(comment_t));
has_comment = 1;
j = 0;
}
if (j == 0) {
fputc('\n', out);
line_offset = ftell(out);
line_logical = logical;
line_page = page;
fprintf(out, "%s.dw ", spacing);
}
if (j) {
fputc(',', out);
}
uint16_t jump_logical = data[0] | (data[1] << 8);
uint8_t jump_page = memmap_page(map, jump_logical);
if (label_repository_find(repository, jump_logical, jump_page, &label)) {
fprintf(out, "%s", label.name);
} else {
fprintf(out, "$%04x", jump_logical);
}
j++;
if (j == elements_per_line) {
j = 0;
if (has_comment) {
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
} else if (extra_infos) {
print_statement_address(out, (int)(ftell(out) - line_offset), line_logical, line_page);
}
}
}
if (j) {
if (has_comment) {
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
} else if (extra_infos) {
print_statement_address(out, (int)(ftell(out) - line_offset), line_logical, line_page);
}
}
fputc('\n', out);
return 1;
}
/* Process data section. The result will be output has a binary file or an asm file containing hex values or strings. */
int data_extract(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
comment_repository_t *comments, int extra_infos) {
switch (section->data.type) {
case Binary:
return data_extract_binary(out, section, map, repository);
case Hex:
return data_extract_hex(out, section, map, repository, comments, extra_infos);
case String:
return data_extract_string(out, section, map, repository, comments, extra_infos);
case JumpTable:
return data_extract_jump_table(out, section, map, repository, comments, extra_infos);
default:
return 0;
}
}
/* Process code section. */
int decode(FILE *out, uint16_t *logical, section_t *section, memmap_t *map, label_repository_t *repository,
comment_repository_t *comments, int extra_infos) {
int i, delta;
uint8_t inst, data[6], is_jump;
char eor;
uint8_t page;
uint32_t offset;
uint8_t current_page;
uint16_t current_logical;
uint16_t next_logical;
label_t label;
const opcode_t *opcode;
eor = 0;
memset(data, 0, 6);
page = memmap_page(map, *logical);
/* Opcode */
inst = memmap_read(map, *logical);
opcode = opcode_get(inst);
current_page = page;
current_logical = *logical;
next_logical = *logical + opcode->size;
/* Is there a label ? */
if (label_repository_find(repository, current_logical, page, &label)) {
print_label(out, &label);
fputc('\n', out);
}
/* Offset of the line start */
size_t start = ftell(out);
/* Front spacing */
fwrite(spacing, 1, 10, out);
/* Print opcode string */
fwrite(opcode->name, 1, 4, out);
/* Add spacing */
fwrite(spacing, 1, 4, out);
/* End Of Routine (eor) is set to 1 if the instruction is RTI, RTS or BRK */
eor = ((inst == 0x40) || (inst == 0x60) || (inst == 0x00));
/* Data */
if (opcode->size > 1) {
for (i = 0; i < (opcode->size - 1); i++) {
data[i] = memmap_read(map, *logical + i + 1);
}
}
*logical = next_logical;
/* Swap LSB and MSB for words */
if (opcode->size > 2) {
uint8_t swap;
/* Special case : TST, BBR*, BBS*. The first byte is zp var */
i = ((opcode->type == 3) || (opcode->type == 4) || (opcode->type == 5) ||
(opcode->type == 6) || (opcode->type == 13) || (opcode->type == 20))
? 1
: 0;
for (; i < (opcode->size - 2); i += 2) {
swap = data[i];
data[i] = data[i + 1];
data[i + 1] = swap;
}
}
/* Handle special cases (jumps, tam/tma and unsupported opcodes ) */
is_jump = 0;
if (opcode_is_local_jump(inst)) {
is_jump = 1;
i = (((inst)&0x0F) == 0x0F) ? 1 : 0;
/* Detect negative number */
if (data[i] & 128)
delta = -((data[i] - 1) ^ 0xff);
else
delta = data[i];
offset = *logical + delta;
data[i] = offset >> 8;
data[i + 1] = offset & 0xff;
} else if (opcode_is_far_jump(inst)) {
is_jump = 1;
/* Build offset */
offset = (data[0] << 8) | data[1];
} else {
offset = 0;
/* Unknown instructions are output as raw data
* (ie) .db XX
*/
if (opcode->type == 22) {
data[0] = inst;
}
}
if (opcode->type == 1) {
fputc('A', out);
} else if (is_jump) {
/* BBR* and BBS* */
if ((inst & 0x0F) == 0x0F) {
uint16_t zp_offset = 0x2000 + data[0]; // [todo] RAM may not be in mpr1 ...
page = memmap_page(map, zp_offset);
if (label_repository_find(repository, zp_offset, page, &label)) {
fprintf(out, "<%s, ", label.name);
} else {
fprintf(out, "<$%02x, ", data[0]);
}
}
page = memmap_page(map, offset);
// Label name should have been set by the label extraction pass.
label_repository_find(repository, offset, page, &label);
fwrite(label.name, 1, strlen(label.name), out);
} else {
int has_label = 0;
if ((inst == 0x43) || (inst == 0x53)) {
/* tam and tma */
/* Compute log base 2 of data */
for (i = 0; (i < 8) && ((data[0] & 1) == 0); ++i, data[0] >>= 1) {
}
data[0] = i;
}
/* Print data */
if (opcode->type) {
/* [todo] this is ugly... */
const char *none = "";
const char *extra = none;
switch (opcode->type) {
case PCE_OP_nn_ZZ_X: /* #$aa, <$zp, X */
extra = ", X";
/* fall through */
case PCE_OP_nn_ZZ: /* #$aa, <$zp */
offset = 0x2000 + data[1];
page = memmap_page(map, offset);
has_label = label_repository_find(repository, offset, page, &label);
if (has_label) {
fprintf(out, "#$%02x, <%s%s", data[0], label.name, extra);
}
break;
case PCE_OP_nn_hhll_X: /* #$aa, $hhll, X */
extra = ", X";
/* fall through */
case PCE_OP_nn_hhll: /* #$aa, $hhll */
offset = (data[1] << 8) + data[2];
page = memmap_page(map, offset);
has_label = label_repository_find(repository, offset, page, &label);
if (has_label) {
fprintf(out, "#$%02x, %s%s", data[0], label.name, extra);
}
break;
case PCE_OP_ZZ_Y: /* <zp, Y */
extra = ", Y";
/* fall through */
case PCE_OP_ZZ_X: /* <zp, X */
if (none == extra) {
extra = ", X";
}
/* fall through */
case PCE_OP_ZZ: /* <zp */
offset = 0x2000 + data[0];
page = memmap_page(map, offset);
has_label = label_repository_find(repository, offset, page, &label);
if (has_label) {
fprintf(out, "<%s%s", label.name, extra);
}
break;
case PCE_OP__ZZ__Y_: /* [zp], Y */
extra = "], Y";
/* fall through */
case PCE_OP__ZZ_X__: /* [zp, X] */
if (none == extra) {
extra = ", X";
}
/* fall through */
case PCE_OP__ZZ__: /* [zp] */
if (none == extra) {
extra = "]";
}
offset = 0x2000 + data[0];
page = memmap_page(map, offset);
has_label = label_repository_find(repository, offset, page, &label);
if (has_label) {
fprintf(out, "[%s%s", label.name, extra);
}
break;
case PCE_OP__hhll_X__: /* [hhll, X] */
extra = ", X]";
/* fall through */
case PCE_OP__hhll__: /* [hhll] */
if (none == extra) {
extra = "]";
}
offset = (data[0] << 8) | data[1];
page = memmap_page(map, offset);
has_label = label_repository_find(repository, offset, page, &label);
if (has_label) {
fprintf(out, "[%s%s", label.name, extra);
}
break;
case PCE_OP_hhll_X: /* hhll, X */
extra = ", X";
/* fall through */
case PCE_OP_hhll_Y: /* hhll, Y */
if (none == extra) {
extra = ", Y";
}
/* fall through */
case PCE_OP_hhll: /* hhll */
offset = (data[0] << 8) | data[1];
page = memmap_page(map, offset);
has_label = label_repository_find(repository, offset, page, &label);
if (has_label) {
fprintf(out, "%s%s", label.name, extra);
}
break;
case PCE_OP_ZZ_hhll: /* <zp, $hhll */
offset = 0x2000 + data[0];
page = memmap_page(map, offset);
if (label_repository_find(repository, offset, page, &label)) {
fprintf(out, "<%s, ", label.name);
} else {
fprintf(out, "<%02x, ", data[0]);
}
offset = (data[1] << 8) | data[2];
page = memmap_page(map, offset);
if (label_repository_find(repository, offset, page, &label)) {
fprintf(out, "%s", label.name);
} else {
fprintf(out, "$%04x", offset);
}
has_label = 1;
break;
case PCE_OP_shsl_dhdl_hhll: /* shsl, dhdl, sz */
/* Source and destination */
for (i = 0; i < 4; i += 2) {
offset = (data[i] << 8) | data[i + 1];
page = memmap_page(map, offset);
if (label_repository_find(repository, offset, page, &label)) {
fprintf(out, "%s, ", label.name);
} else {
fprintf(out, "$%04x, ", offset);
}
}
/* Size */
fprintf(out, "$%02x%02x", data[4], data[5]);
has_label = 1;
break;
default:
has_label = 0;
break;
}
if (!has_label) {
const char *format;
i = 0;
while((format = opcode_format(opcode, i))) {
fprintf(out, format, data[i]);
i++;
}
}
}
}
/* display inline comments if any */
comment_t comment = {0};
int n = (int)(ftell(out) - start);
if(comment_repository_find(comments, current_logical, current_page, &comment)) {
print_inline_comment(out, n, comment.text);
} else if(extra_infos) {
print_statement_address(out, n, current_logical, current_page);
}
fputc('\n', out);
return eor;
}
/* Computes section size. */
int32_t compute_size(section_t *sections, int index, int count, memmap_t *map) {
uint8_t i;
uint8_t data[7];
section_t *current = §ions[index];
uint32_t start = current->logical;
uint32_t logical = start;
// Search for the closest section past the current one.
// This also ensures that we don't cross the current page.
uint32_t max_offset = 0xffffffff;
for (i = 0; i < count; i++) {
if (i != index) {
if (current->page == sections[i].page) {
uint32_t offset_current = current->offset & 0x1fff;
uint32_t offset_it = sections[i].offset & 0x1fff;
if ((offset_current < offset_it) && (max_offset > offset_it)) {
max_offset = offset_it;
}
}
}
}
for (int eor = 0; !eor;) {
if ((logical & 0x1fff) >= max_offset) {
break;
}
uint8_t page = memmap_page(map, logical);
data[0] = memmap_read(map, logical);
const opcode_t *opcode = opcode_get(data[0]);
for (i = 1; i < opcode->size; i++) {
data[i] = memmap_read(map, logical + i);
}
logical += opcode->size;
if (opcode_is_far_jump(data[0])) {
uint32_t jump = data[1] | (data[2] << 8);
if (data[0] == 0x4c) { // jmp hhll
uint8_t jump_page = memmap_page(map, jump);
if (page == jump_page) {
if (jump < logical) {
eor = 1;
} else {
logical = jump;
}
}
}
} else if (opcode_is_local_jump(data[0])) {
uint32_t jump = 0;
int32_t delta = 0;
/* For BBR* and BBS* displacement is stored in the 2nd byte */
i = ((data[0] & 0x0F) == 0x0F) ? 2 : 1;
if (data[i] & 128)
delta = -((data[i] - 1) ^ 0xff);
else
delta = data[i];
jump = logical + delta;
if (data[0] == 0x80) { // bra offset
if (jump < logical) {
eor = 1;
}
} else if (jump > logical) {
logical = jump;
}
} else if ((data[0] == 0x40) || (data[0] == 0x60) || (data[0] == 0x00)) { // rts, rti or brk
eor = 1;
}
}
return (logical - start);
}
/* Output hardware IO port and RAM labels. */
void label_dump(FILE *out, memmap_t *map, label_repository_t *repository) {
int count = label_repository_size(repository);
for (int i = 0; i < count; i++) {
label_t label;
if (label_repository_get(repository, i, &label)) {
// IO port and RAM
if ((label.page == 0xff) || (label.page == 0xf8)) {
print_comment(out, label.description);
fprintf(out, "%s .equ $%04x\n", label.name, label.logical);
}
}
}
}