-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcdis.c
1354 lines (1174 loc) · 46.7 KB
/
lcdis.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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* LCDIS - LC86104C/108C disassembler
* Copyright 1999-2000 John Maushammer john@maushammer.com
* Portions by Alexander Villagran
* Helpful suggestions and code fixes from Marcus Comstedt
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* Release notes:
* Beta 0.1 - initial beta release
* Beta 0.2 - fixed DBNZ bug - not handled by mapmem. Added ascii output.
* Beta 0.3 - fixed R16 rollover problem, which was throwing off mapmem and causing it to mess up memory
* when it accessed an out-of-bound index. Surprisingly, this only happened in RELEASE, not
* DEBUG. Now an out-of-bound PC causes a fatal error.
* - a certain branch instruction didn't use print_data_label
* - Added use of MEM_INVALID to check for jumps into the middle of existing code (ie. into the
* second or third byte of a multi-byte instruction). Usually this means an error, but not
* necessarily.
* Beta 0.4 - Added user-defined entry points to trace.
* - Added call stack trace buffer for errors found during mapmem
* - Now labels mapmem entry points (i.e. L0000)
* Beta 0.5 - Added graphics disassembly options
* Beta 0.6 - Added predefined labels and some auto-commenting of certain code
* - Added better display of icon data
* Beta 0.61- Added autocommenting of MUL and DIV, some better ANSI compatibility
* Beta 0.7 - mapmem thought that "BR" was a conditional branch; it's really always taken.
* - mapmem deals with jump to other banks more realistically (FIRMWARECALL)
* - mapmem doesn't continue to follow code once it hits an illegal instruction
* - removed legacy mem_use[branchaddr]=MEM_CODE_LABELED instructions
* - changed "*** WARNING: used as both code and data:" error to more accurate
* "*** WARNING: this is the target of a possibly misaligned jump:"
* The general behavior here is a bit better.
* - Added bad vein checking ("STRICT") so disassembler doesn't run amok and errors
* can be found easier.
* Beta 0.8 - Added assembly out option, compatible with Marcus's assembler. It doesn't
* define variables yet; instead it just uses the address. Tested with boom, football,
* and chao - reassembly is an exact match of disassembly.
* Beta 0.81- Added new auto-commenting for PSW.1 and Flash read/write call entry (12/12/99)
*
* Rel 1.0 - Added BIOS flag (alters program flow assumptions)
* - Fixed error where illegal opcode was list as 2 decimal (not hex) digits.
* - added unknown opcodes $50 and $51. These seem to be one byte instructions.
* - correct clock speed auto-comments: LCD=fast/600kHZ, normal=slow/32kHz
* - cleaned up spacing of auto-comments, but still uneven
* - supressed ascii output if all 0xff's
* Rel 1.01 - supressed ascii output if none of characters are printable
* - corrected readFlash address, added verifyFlash
* - added more autocomments
* - added MEM - labeling of memory registers. Has some problems
* (search on "rambank problems"), but does a pretty good job of
* distinguishing between user and OS banks. Most notably, doesn't
* know what the result of "POP PSW" will be; doesn't return the
* state of PSW after a call; and doesn't detect code that is used
* both ways (example: memory clearing function of BIOS).
* This results in about 30 undefined conditions in the BIOS code;
* LCDIS displays its two guesses.
* Rel 1.02 - Added support for "BPC d9,b3,r8" instruction.
* - Changed LABELS to better describe interrupts (according to Marcus)
* - Added "FONT8," directive for printing 8-bit-wide font tables
* - fixed so that fonts and graphics can be labeled
* ERRATA- this release mistakenly labelled 1.01 in executable
* Rel 1.03 - fixed wrong commenting on MUL and DIV instructions:
* was "; B:ACC:C <- ACC:B * C" now "; B:ACC:C <- ACC:C * B"
* was "; ACC:C remainder B <- ACC:B / C"
* now "; ACC:C remainder B <- ACC:C / B"
* - blank line after unconditional branch (like after JMP or RET), for readability
* - split out and comment the VM description lines (at $200 and $210):
* 0200- | BYTE "TRICKSTYLE JR. " ; File comment on VM (16 bytes)
* 0210- | BYTE "TRICKSTYLE JR. VMU GAME. " ; File comment on Dreamcast (32 bytes)
* - Automatic null-terminated string decoding (uses BYTE "text")
* - add more MEM mem register definitions & auto-commenting.
*
*
* Desired features (future)---------------------------------------------------------
* - look up indirect variable names (ie. for "MOV #$xx,MEM000")
* - auto decoding of this construct (either order) to indicate effective addr
* 2381- 23 04 4e | MOV #$4e,TRL
* 2384- 23 05 27 | MOV #$27,TRH
* - Some sort of comment integration from a file, so that object code
* can be integrated with a comments-only file to generated annotated
* source. This respects the object code writer's copyright while
* allowing people to distribute their annotations.
* Rel 1.04 - A few more autocomments relating to the serial port.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <ctype.h>
#include "lcdis.h"
// This define needed for SUN environments:
#if (defined (sparc) || defined (__sparc__) || defined (__sparc))
#define strncmp(x,y,z) strncasecmp(x,y,z)
#endif
// big ugly global variables:
unsigned char mem[0x10000]; // 64K of memory max.
unsigned char mem_use[0x10000]; // memory usage:
unsigned char mem_bnk[0x10000]; // memory usage:
int strictmode=0; // strict mode that stops at first sign of memmap going amok.
int asmout=0; // for compiler-compatible output.
int biosmode=0; // for disassembling bios
int main (int argc, char * argv[])
{
FILE * fin;
int memsize;
int pin, p1; // address counters
int count;
int i;
printf ("; LC86104C/108C disassembler. (C) 1999-2000 John Maushammer.\n"
"; Version 1.04 john@maushammer.com\n"
"; GNU public liscense - see www.gnu.org\n;\n;\n");
if (argc < 2)
{ printf ("lcdis inputfile.vms {[entrypoint] ...}> outputfile\n\n"
" STRICT - kills bad 'veins'; helps prevent disassembly of bad code and\n"
" finds errors, but stops before all good code is disassembled\n"
" ASMOUT - outputs cleaner code for use with an assembler\n"
" BIOS - interpret file as a BIOS (use before ENTRY)\n"
" ENTRYn - define code starting at address n\n"
" GRAPHBYTESn,b - define b bytes of graphics at address n\n"
" FONT8,n,b - define b bytes of 8-bit wide fonts at address n\n"
" GRAPHPAGESn,p - define p pages (=0xC0 bytes) of graphics at address n\n\n"
"In addition to the standard entry points, other points can be disassembled.\n"
"Enter these as parameters after the input file. Use decimal or the 0x notation.\n"
"Example:\n"
" lcdis football.vms ENTRY0x139 > football.lst\n\n");
return (1);
}
printf ("; Source file=%s, ", argv[1]);
if ( (fin = fopen(argv[1],"rb")) == NULL)
{ printf ("can not open!\n");
return (1);
}
memset(mem, 0x00, 0x10000); // clear memory
memset(mem_use, MEM_UNUSED, 0x10000); // clear memory
memset(mem_bnk, BNK_UNKNOWN,0x10000); // clear memory
memsize=fread((void *) mem, sizeof (char), 0x10000, fin);
fclose(fin);
printf ("%d (0x%04x) bytes.\n", memsize, memsize);
memset(mem_use, MEM_UNKNOWN, memsize); // clear memory
if (argc >= 3) // if extra command-line arguments, then parse here
{
for (i=2; i<argc; i++)
{
if (strcmp(argv[i], "STRICT")==0)
{
printf ("; Strict mode enabled.\n");
strictmode=1;
}
else
if (strcmp(argv[i], "ASMOUT")==0)
{
printf ("; Assembler output mode output enabled.\n");
asmout=1;
}
else
if (strcmp(argv[i], "BIOS")==0)
{
printf ("; BIOS disassembly mode enabled.\n");
biosmode=1;
}
else
if (strncmp(argv[i], "ENTRY", 5)==0)
{
if (1==sscanf(& (argv[i][5]), "%i", &pin))
{ printf ("; Mapping memory... user-defined point $%04x\n", pin);
mapmem (pin, BNK_BANK1);
}
else
printf ("WARNING: cannot parse value in '%s'. Must use decimal or 0x notation.\n", argv[i]);
}
else
if (strncmp(argv[i], "GRAPHBYTES", 10)==0)
{
if (2==sscanf(& (argv[i][10]), "%i,%i", &pin, &count))
{ printf ("; Mapping graphics... user-defined point $%04x-$%04x ($%04x bytes)\n", pin, pin+count-1, count);
while (count-- && (pin>=0) && (pin <= 0xFFFF))
mem_use[pin++]=MEM_GRAPHICS;
}
else
printf ("WARNING: cannot parse value in '%s'. Must use decimal or 0x notation.\n", argv[i]);
}
else
if (strncmp(argv[i], "FONT8,", 6)==0)
{
if (2==sscanf(& (argv[i][6]), "%i,%i", &pin, &count))
{ printf ("; Mapping 8-bit-wide font... user-defined point $%04x-$%04x ($%04x bytes)\n", pin, pin+count-1, count);
while (count-- && (pin>=0) && (pin <= 0xFFFF))
mem_use[pin++]=MEM_FONT8;
}
else
printf ("WARNING: cannot parse value in '%s'. Must use decimal or 0x notation.\n", argv[i]);
}
else
if (strncmp(argv[i], "GRAPHPAGES", 10)==0)
{
if (2==sscanf(& (argv[i][10]), "%i,%i", &pin, &count))
{
printf ("; Mapping graphics... user-defined point $%04x-$%04x ($%04x pages) \n", pin, pin+(0xC0*count)-1, count);
count *= 0xC0; // packed format
while (count-- && (pin>=0) && (pin <= 0xFFFF))
mem_use[pin++]=MEM_GRAPHICS;
}
else
printf ("WARNING: cannot parse value in '%s'. Must use decimal or 0x notation.\n", argv[i]);
}
else
{ printf ("WARNING: unknown command line directive %s\n", argv[i]);
}
}
}
printf ("; Mapping memory... reset/start entry point\n");
// actually the bios probably starts with bank0, but it's code
// sets it, so it's irrelevant.
mapmem (0x00, BNK_BANK1); // reset/start
printf ("; Mapping memory... interrupt entry points\n");
// bank is unknown unless the programmer only uses one bank or takes
// special precautions
mapmem (0x03, BNK_UNKNOWN); // external interrupt 0?
mapmem (0x0b, BNK_UNKNOWN); // timer/counter 0 interrupt
mapmem (0x13, BNK_UNKNOWN); // external interrupt 1?
mapmem (0x1b, BNK_UNKNOWN); // timer/counter 1 interrupt
mapmem (0x23, BNK_UNKNOWN); // divider circuit/port 1/port 3 interrupt?
mapmem (0x2b, BNK_UNKNOWN); // interrupt
mapmem (0x33, BNK_UNKNOWN); // interrupt
mapmem (0x3b, BNK_UNKNOWN); // interrupt
mapmem (0x43, BNK_BANK0); // interrupt (only acted on by BIOS, not chao nor football, so I guess it's bank0)
mapmem (0x4b, BNK_UNKNOWN); // interrupt
if (biosmode)
{
printf ("; Mapping memory... BIOS entry points\n");
mapmem (0x100, BNK_BANK1); // writeFlash
mapmem (0x108, BNK_BANK1); // writeFlash2
mapmem (0x110, BNK_BANK1); // readFlash
mapmem (0x120, BNK_BANK1); // int120link
mapmem (0x130, BNK_BANK1); // intT1link (ROM's T1 interrupt code)
mapmem (0x140, BNK_BANK1); // unknown
mapmem (0x1f0, BNK_BANK1); // quit
}
search_text(memsize);
printf ("; Done mapping memory.\n");
printf ("\n\n;------------------------------------------------------------------\n\n");
if (asmout)
printf (" .include \"sfr.i\"\n\n"
" .org 0\n\n\n");
// simple straight-through disassembly: (all code)
for (pin=0; pin<memsize; )
{
dis(pin, &p1);
pin = p1;
}
return(0);
}
// FUNCTION dis
//
// inputs
// pin = address counter to disassemble
//
// outputs
// b1 = address of next instruction (or -1 if RETURN)
//
// calls
// dis_code or dis_data if needed
//
// Model line:
// "0593- 23 00 00 | MOV #$00,ACC"
void dis (int pin, int * b1)
{
int i;
unsigned char opcode;
int quoteopen;
opcode = mem[pin];
// Debug code to force all memory to be interpreted as code or data:
// mem_use[pin] = MEM_DATA;
// mem_use[pin] = MEM_CODE; // it's executable
if (pin <0 || pin>0xFFFF)
printf ("ERROR: attempted to dissassemble illegal address %04x!\n", pin);
switch (mem_use[pin])
{
case MEM_UNUSED: // not loaded from file
*b1=-1; // can't branch anywhere
break;
case MEM_UNKNOWN: // if unknown, then assume it's data
case MEM_DATA:
dis_data (pin, b1);
break;
case MEM_GRAPHICS:
if (!asmout)
printf ("%04x- | ", pin);
print_code_label(pin,2); // print label if possible
printf ("BYTE ");
printf ("$%02x", mem[pin]);
for (i=1; i<6; i++) /* 6 bytes per line */
printf (",$%02x", mem[pin+i]);
printf (" ;graphics \"");
for (i=0; i<6; i++) /* 6 bytes per line */
printf ("%c%c%c%c%c%c%c%c",
mem[pin+i] & 128 ? '#' : '.',
mem[pin+i] & 64 ? '#' : '.',
mem[pin+i] & 32 ? '#' : '.',
mem[pin+i] & 16? '#' : '.',
mem[pin+i] & 8 ? '#' : '.',
mem[pin+i] & 4 ? '#' : '.',
mem[pin+i] & 2 ? '#' : '.',
mem[pin+i] & 1 ? '#' : '.');
printf ("\"\n");
*b1=pin+6;
break;
case MEM_FONT8:
if (!asmout)
printf ("%04x- | ", pin);
print_code_label(pin,2); // print label if possible
printf ("BYTE $%02x ;font \"%c%c%c%c%c%c%c%c\"\n",
mem[pin],
mem[pin] & 128 ? '#' : '.',
mem[pin] & 64 ? '#' : '.',
mem[pin] & 32 ? '#' : '.',
mem[pin] & 16? '#' : '.',
mem[pin] & 8 ? '#' : '.',
mem[pin] & 4 ? '#' : '.',
mem[pin] & 2 ? '#' : '.',
mem[pin] & 1 ? '#' : '.');
*b1=pin+1;
break;
case MEM_TEXT:
if (!asmout)
printf ("%04x- | ", pin);
printf (" BYTE \"");
quoteopen=1;
i=0;
while (mem_use[pin] == MEM_TEXT) // or until broken by a $00
{
// This output must be compatible with fixup_string in lexar.c of Marcus's assembler.
// that function supports most c escape sequences, but not \\ nor \", so we just
// print the hex byte equivalents.
if ( !isprint (mem[pin]) // print as hex byte
|| (mem[pin]=='\\') // vmuasm has no escape sequence for this
|| (mem[pin]=='\"')) // vmuasm has no escape sequence for this
{
if (quoteopen)
{ printf ("\"");
quoteopen=0;
}
if (i!=0)
printf (","); // use a comma only if we've added something to the string.
printf ("$%02x", (unsigned int) ((unsigned char) mem[pin]));
}
else // print quoted text
{
if (!quoteopen)
{ printf (",\"");
quoteopen=1;
}
printf ("%c", mem[pin]);
}
if (mem[pin++] == 0) // end-of-line?
break; // then break out of it
i++; // count chars so we can use ',' only if needed
}
if (quoteopen)
{ printf ("\"");
quoteopen=0;
}
printf ("\n");
*b1=pin;
break;
case MEM_INVALID:
printf ("*** WARNING: this is the target of a possibly misaligned jump:\n");
// fall into code section
case MEM_CODE:
case MEM_CODE_LABELED:
dis_code (pin, b1);
break;
default:
printf ("FATAL INTERNAL ERROR: unknown type of memory\n");
exit(-1);
break;
}
}
// FUNCTION dis_data
//
// inputs
// pin = address counter to disassemble
//
// outputs
// b1 = address of next instruction (or -1 if RETURN)
//
// Model line:
// "0593- 23 00 00 | MOV #$00,ACC"
// 0200- | BYTE "TRICKSTYLE JR. " ;File comment on VM (16 bytes)
// 0210- | BYTE "TRICKSTYLE JR. VMU GAME. " ;File comment on Dreamcast (32 bytes)
void dis_data (int pin, int * b1)
{
int i,i2;
int valid;
int textsize;
int printdefault=0; // set if we don't have a special way to display the data.
// Sometimes set if the special data can't print the data correctly.
unsigned char opcode;
opcode = mem[pin];
// Handle game icon data:
if ((pin >= 0x280) && (pin < 0x280+mem[0x240]*0x200) && !biosmode)
{ // icon data for display on dreamcast
if (((pin-0x280) & 0x1FF) == 0x0) // in icon boundry?
{ if (!asmout)
printf ("%04x- | ;icon #%d\n", pin, (pin-0x280)/0x200);
else
printf (" ;icon #%d\n", (pin-0x280)/0x200);
}
if (!asmout)
printf ("%04x- | ", pin);
printf (" BYTE $%02x", opcode);
for (i=1; i<16; i++)
printf (",$%02x", mem[pin+i]);
printf (" "); // pad to make comment align
printf (" ;icon \"");
for (i=0; i<16; i++)
{
if (mem[pin+i] & 0xF0)
printf ("%x", mem[pin+i]>>4); // print hex digit 1-F
else
printf (" "); // print space instead of '0'
if (mem[pin+i] & 0x0F)
printf ("%x", mem[pin+i]&0xf); // print hex digit 1-F
else
printf (" "); // print space instead of '0'
}
printf ("\"");
*b1=pin+16; // icons are always lines of 16 bytes
}
else // handle game name fields
if ((pin == 0x200) || (pin == 0x210))
{
textsize = (pin==0x200) ? 16 : 32;
valid=1; // check validity
for (i=0; (i<textsize) && valid; i++)
if (!isprint (mem[pin+i]) || (mem[pin+i] & 0x80)) // not valid if not printable or has most significant bit set
valid=0;
if (valid)
{
if (!asmout)
printf ("%04x- | ", pin);
printf (" BYTE \"");
for (i=0; i<textsize; i++)
printf ("%c", mem[pin+i]);
printf ((pin==0x200) ? "\" ;File comment on VM (16 bytes)"
: "\" ;File comment on Dreamcast (32 bytes)");
*b1=pin+textsize; // icons are always lines of 16 bytes
}
else
printdefault=1; // can't print correctly, so default to binary.
}
else
printdefault=1;
if (printdefault) // general data
{
if (!asmout)
printf ("%04x- | ", pin);
printf (" BYTE $%02x", opcode);
i=pin+1;
i2=!isprint (opcode & 0x7F); // i2 is true as long as bytes are nonprintable
while ( (i & 0x7) &&
( (mem_use[i]==MEM_UNKNOWN) || (mem_use[i]==MEM_UNKNOWN)) )
{ i2 &= !isprint (mem[i] & 0x7F); // i2 is true as long as bytes are all 0xFF
printf (",$%02x", mem[i++]);
}
if (!i2) // if all data isn't 0xFF, then
{ // print ASCII representation
for (i2=8-(i-pin); i2; i2--)
printf (" "); // pad to make comment align
printf (" ;ascii \"");
for (i2=pin; i2<i; i2++)
printf ("%c", (isprint (mem[i2] & 0x7F)) ? (mem[i2] & 0x7F) : '.');
printf ("\"");
}
*b1=i;
}
printf ("\n"); // end of line
}
// FUNCTION dis_code
//
// inputs
// pin = address counter to disassemble
//
// outputs
// b1 = address of next instruction (or -1 if RETURN)
//
// Model line:
// "0593- 23 00 00 | MOV #$00,ACC"
void dis_code (int pin, int * b1)
{
char * model;
int found;
int i,i2;
unsigned char opcode;
opcode = mem[pin];
//Debug code: insert this to see what bank each line was calculated to be in
// (BNK0, BNK1, BNK2=unknown)
//printf ("BNK%d ", mem_bnk[pin]);
if (!asmout)
{
printf ("%04x- ", pin);
for (i=0; i<3; i++)
if (i<opcode_len(opcode))// print raw bytes:
printf ("%02x ", mem[pin+i]);
else
printf (" ");
printf ("| ");
}
// print label if wanted
if (mem_use[pin] == MEM_CODE_LABELED)
print_code_label(pin,1); // formatted
else
printf (" ");
model = get_opcode_model(opcode);
printf ("%5.5s ", model);
// calculate next word
*b1 = pin + opcode_len (opcode);
switch (model[5])
{
case ' ': // no parameters
break;
case '2': // a12 absolute
print_code_label (get_a12(pin),0);
// printf ("$%04x", get_a12(pin));
break;
case '6': // r16 relative
print_code_label (get_r16(pin),0);
// printf ("$%04x", get_r16(pin));
break;
case '7': // a16 absolute
print_code_label (get_a16(pin),0);
// printf ("$%04x", get_a16(pin));
break;
case '8': // r8 relative
print_code_label (get_r8(pin),0);
// printf ("$%04x", get_r8(pin));
break;
case '9': // d9 direct
print_data_label (get_d9(pin), mem_bnk[pin]);
// printf ("$%03x", get_d9(pin));
break;
case '@': // @Ri indirect
printf ("@R%d", get_reg(pin));
break;
case '#': // # immediate
printf ("#$%02x", mem[pin+1]);
break;
case '^': // ^=#i8,d9 immediate
printf ("#$%02x,", mem[pin+2]);
print_data_label (get_d9(pin), mem_bnk[pin]);
// printf ("#$%02x,$%03x", mem[pin+2], get_d9(pin));
break;
case '%': // %=#i8,@Ri
printf ("#$%02x, @R%d", mem[pin+1], get_reg(pin));
break;
case 'b': // b=d9,b3 bit manipulation
print_data_label(get_d9bit(pin), mem_bnk[pin]);
printf (", %d", mem[pin]&7);
// printf ("$%03x, %d", get_d9bit(pin), mem[pin]&7);
break;
case 'r': // r=d9,b3,r8 bit branch
print_data_label (get_d9bit(pin), mem_bnk[pin]);
printf (", %d, ", mem[pin]&7);
print_code_label (get_r8(pin+1),0);
// printf ("$%03x, %d, $%04x", get_d9bit(pin), mem[pin]&7, get_r8(pin+1));
break;
case 'z': // z=#i8,r8
printf ("#$%02x,", mem[pin+1]);
print_code_label (get_r8(pin+1),0);
// printf ("#$%02x,$%04x", mem[pin+1], get_r8(pin+1));
break;
case 'x': // x=d9,r8
print_data_label (get_d9(pin), mem_bnk[pin]);
printf (",");
print_code_label (get_r8(pin+1),0);
// printf ("$%03x,$%04x", get_d9(pin), get_r8(pin+1));
break;
case 'v': // v=@Ri,#i8,r8
printf ("@R%d,#$%02x,", get_reg(pin), mem[pin+1]);
print_code_label (get_r8(pin+1),0);
// printf ("@R%d,#$%02x,$%04x", get_reg(pin), mem[pin+1], get_r8(pin+1));
break;
case 'c': // c=@Ri,r8
printf ("@R%d,", get_reg(pin));
print_code_label (get_r8(pin),0);
// printf ("@R%d,$%04x", get_reg(pin), get_r8(pin));
break;
case '!': // illegal
printf ("$%02x ;illegal opcode", (int) opcode);
break;
default:
printf ("! ! !\nFATAL Error: unexpected model %c in op[] table!\n", model[5]);
exit (-1); // not graceful
}
// printf (" [model %c] ", model[5]); // helpful for debugging
// print pre-defined comments for particular instructions:
found=0;
for (i=0; (CODECMTS[i].code[0] != -1) && !found; i++) // check whole list
{
found = 1;
for (i2=0; (i2<3) && found; i2++)
if ((mem[pin+i2]!=CODECMTS[i].code[i2]) && CODECMTS[i].code[i2] != -1)
found=0; // didn't fit pattern
if (found)
printf (" %s", CODECMTS[i].text);
}
// add a comment for indirect variable names
// * - look up indirect variable names (ie. for "MOV #$xx,MEM000")
// (future) !!!
printf ("\n");
// add a blank line after some instructions:
if ( (strncmp(model, "JMP ", 5) == 0)
|| (strncmp(model, "JMPF ", 5) == 0)
|| (strncmp(model, "RET", 3) == 0) // RET and RETI
|| (strncmp(model, "BR ", 5) == 0) // unconditional branches
|| (strncmp(model, "BRF ", 5) == 0))
printf (asmout ? "\n" : " |\n");
}
// input: executable address
// rambank (in future versions, this may expand to a system state)
//
// warning: is fooled by branches that cover both cases
// i.e. BZ followed by a BNZ will always go to one of those
// cases, but this code will think that the BNZ case may not
// execute and continue mapping. Nice programmers would use
// a BR instruction in the second case, but those that aren't
// used to such new-fangled technology might not (like me).
//
// Also, computed gotos (if possible) are not honored
//
// rambank problems:
//
// rambank does not handle POP PSW (restoration). A complimentry
// bug is that calls don't affect rambank when they return, so if
// you assume that calls restore PSW after they are done with it,
// then missing POP PSW is ok. This does lead to some uncommented
// code at the end of routines (especially if push PSW is one of
// last things saved on the stack), but usually this matches up
// with the pushes quite nicely.
//
// Returns: 0=valid code
// 1=invalid code
int mapmem (int pin_in, int rambank)
{
int branchaddr;
char * model;
unsigned char opcode;
int i;
int pin; // pc during trace
int entry;
int badvein;
static int level=0; // static during recursion
static int calltrace[200];
// debug variables:
// int x; char junk[200];
pin=pin_in; // start trace at passed parameter
//-- record mapmem recursion
if (level<200)
calltrace[level]=pin;
level++;
//--
while (1)
{
if (pin <0 || pin>0xFFFF)
{ printf ("FATAL INTERNAL ERROR: attempted to map illegal address %04x!\n", pin);
exit (-1);
}
if ( (mem_use[pin] == MEM_DATA) // <- this is impossible so far because we don't mark any code data yet.
|| (mem_use[pin] == MEM_GRAPHICS)
|| (mem_use[pin] == MEM_UNUSED))
{
printf ("WARNING: branch exists to data/graphics/unused code at $%04x\n"
" trace stack: ", pin);
for (i=0; i<level; i++)
printf ("%04x ", calltrace[i]);
printf ("\n\n");
level--;
mem_use[pin_in] = MEM_INVALID; // we'll label it invalid.
return 1; // only explore the good stuff
}
if (mem_use[pin] == MEM_INVALID)
{
printf ("WARNING: branch exists to invalid code at $%04x\n"
" trace stack: ", pin);
for (i=0; i<level; i++)
printf ("%04x ", calltrace[i]);
printf ("\n\n");
level--;
return 1; // only explore the good stuff
}
if (mem_use[pin] != MEM_UNKNOWN)
{
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 0; // only explore the unknown
}
// figure out if mode change
if ((mem[pin] == 0xf9) && (mem[pin+1] == 0x01))
rambank = BNK_BANK1; // access game ram bank SET1 PSW, 1
if ((mem[pin] == 0xd9) && (mem[pin+1] == 0x01))
rambank = BNK_BANK0; // access OS ram bank CLR1 PSW, 1
if ((mem[pin] == 0x71) && (mem[pin+1] == 0x01))
rambank = BNK_UNKNOWN; // restore bank from stack POP PSW
mem_use[pin] = MEM_CODE; // it's executable
if (mem_bnk[pin] == BNK_UNKNOWN)
mem_bnk[pin] = rambank;
else
if (mem_bnk[pin] != rambank) // if found a conflicting instance
mem_bnk[pin] = BNK_VARIOUS;
///// use one or both of these for debugging:
/////dis(pin, &x);
/////gets (junk);
opcode = mem[pin];
model = get_opcode_model(opcode);
// Flag illegal code
if (model[5] == '!')
{
printf ("WARNING: illegal instruction found at $%04x\n"
" trace stack: ", pin);
for (i=0; i<level; i++)
printf ("%04x ", calltrace[i]);
printf ("\n\n");
level--; // don't continue to follow this vein
return 1; // and tell caller not to, either.
}
if (strncmp(model, "CALL ", 5) == 0)
{
branchaddr = get_a12(pin);
badvein=mapmem(branchaddr, rambank); // recurse
if (badvein && strictmode)
{ level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 1; // end of the line
}
}
else
if (strncmp(model, "CALLF", 5) == 0)
{
branchaddr = get_a16(pin);
badvein=mapmem(branchaddr, rambank); // recurse
if (badvein && strictmode)
{ level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 1; // end of the line
}
}
else
if (strncmp(model, "CALLR", 5) == 0)
{
branchaddr = get_r16(pin);
badvein=mapmem(branchaddr, rambank); // recurse
if (badvein && strictmode)
{ level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 1; // end of the line
}
}
else
if (strncmp(model, "JMP ", 5) == 0)
{
branchaddr = get_a12(pin);
badvein=mapmem(branchaddr, rambank); // recurse
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return badvein; // end of the line
}
else
if (strncmp(model, "JMPF ", 5) == 0)
{
branchaddr = get_a16(pin);
badvein=mapmem(branchaddr, rambank); // recurse
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return badvein;
}
else
if (strncmp(model, "RET", 3) == 0) // RET and RETI
{
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 0; // a dead-end
}
else
if (strncmp(model, "BRF ", 5) == 0)
{
branchaddr = get_r16(pin);
badvein=mapmem(branchaddr, rambank); // recurse
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return badvein; // a dead end
}
else
if (strncmp(model, "BR ", 5) == 0)
{
branchaddr = get_r8(pin);
badvein=mapmem(branchaddr, rambank); // recurse
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return badvein; // a dead end
}
else // These branch instructions are all assumed to be takeable or non-taken:
if ( (model[0]=='B') // if branch instruction
|| (strncmp(model, "DBNZ ", 5) == 0)) // funnily named branch instruction
{
// rambank = BNK_UNKNOWN; // we don't know if branch is taken or not
switch (model[5])
{
case '8': // r8 relative
case 'c': // c=@Ri,r8
branchaddr= get_r8(pin);
break;
case 'r': // r=d9,b3,r8 bit branch
branchaddr= get_r8(pin+1);
break;
case 'z': // z=#i8,r8
case 'x': // x=d9,r8
case 'v': // v=@Ri,#i8,r8
branchaddr= get_r8(pin+1);
break;
default:
printf ("FATAL Error: unexpected branch model %c in op[] table!\n", model[5]);
}
badvein=mapmem(branchaddr, rambank); // recurse
if (badvein && strictmode)
{ level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 1; // end of the line
}
}
else // Code could change execution location by switching banks:
if ( (mem[pin] == 0xB8) && (mem[pin+1]==0x0D) ) // {0xB8, 0x0D, -1} NOT1 EXT, 0
{
if (!biosmode)
{ entry= pin + opcode_len(opcode); // calc PC of code after this instruction
// (always 2 now, but might be 1 if some other way of modifing EXT is trapped)
for (i=0; FIRMWARECALL[i].entry != -1; i++)
{
if (FIRMWARECALL[i].entry == entry) // did we find exit?
{
// special case: after the NOT1 EXT,0 instruction, there is some code
// that doesn't look like it's executed, but looks like its inserted
// "just in case". Usually it's a jump to try the NOT1 EXT,0 portion
// again. It may be junk code. We'll disassemble just one opcode to
// make it look pretty.
if (mem_use[entry] == MEM_UNKNOWN) // if it would otherwise not be disassembled...
mem_use[entry] = MEM_CODE;
if (FIRMWARECALL[i].exit == -1)
{ // treat like a return (this is the exit vector)
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return 0; // a dead end
}
else
{ // treat like a jump
badvein=mapmem(FIRMWARECALL[i].exit, rambank); // recurse
level--;
mem_use[pin_in] = MEM_CODE_LABELED; // we'll label the main entry point
return badvein; // a dead end
}
}
}