source: vital-to8-sdk/dasm6809/6809dasm.c

Last change on this file was 1, checked in by svn, 5 years ago

Import initial

File size: 41.7 KB
Line 
1/* 6809dasm.c - a 6809 opcode disassembler */
2
3/* Version 1.4 1-MAR-95 */
4
5/* Copyright © 1995 Sean Riddle */
6
7
8
9/* thanks to Franklin Bowen for bug fixes, ideas */
10
11
12
13/* Freely distributable on any medium given all copyrights are retained */
14
15/* by the author and no charge greater than $7.00 is made for obtaining */
16
17/* this software */
18
19
20
21/* Please send all bug reports, update ideas and data files to: */
22
23/* sriddle@ionet.net */
24
25
26
27/* latest version at: */
28
29/* <a href="http://www.ionet.net/~sriddle">Please don't hurl on my URL!</a> */
30
31
32
33/* the data files must be kept compatible across systems! */
34
35
36
37/* usage: 6809disasm <file> [<data file>] */
38
39/* output to stdout, so use redirection */
40
41
42
43/* The optional data file contains 7 types of lines: */
44
45/* o Remarks - these are lines beginning with a semi-colon (;) */
46
47/*   they are completely ignored. */
48
49/* o 1 ORG line - gives the origin of the code; this is the starting */
50
51/*   address to be used for the disassembly. */
52
53/* o COMMENT lines - used to add comments to the end of lines of the */
54
55/*   disassembly. */
56
57/* o COMMENTLINE lines - provide full-line comments to be included before */
58
59/*   a given address in the disassembly. */
60
61/* o DATA lines - mark sections as data.  These sections will not be */
62
63/*   disassembled, but dumped as hex data instead. */
64
65/* o ASCII lines - mark sections as text.  These sections will not be */
66
67/*   disassembled, but printed as text instead. */
68
69/* o WTEXT lines - interprets section as text encoded as in Joust, */
70
71/*   Bubbles, Sinistar (0x0=0,...,0xa=space,0xb=A,...,0x24=Z,...,0x32=:*/
72
73/* See sample data files (*.dat) for examples. */
74
75
76
77/* current limitations: */
78
79/* o number of LABEL, DATA/ASCII, COMMENT and COMMENTLINE lines determined */
80
81/*   at compile-time - see MAXLABEL, MAXDATA, MAXCOMMENT and MAXCOMMLINE */
82
83/* o all DATA/ASCII lines in data file must be sorted in ascending */
84
85/*   address order */
86
87/* o ditto for COMMENT and COMMENTLINE lines */
88
89/* o if a DATA/ASCII area is preceded by what 6809dasm thinks is code */
90
91/*   that continues into the DATA/ASCII area, the data will not be marked */
92
93/*   as such, and an error will be printed.  If this is the case, mark the */
94
95/*   line before the data as data also. */
96
97
98
99/* to do: */
100
101/* o sort comment, ascii, data lines */
102
103/* o look at JMP and JSR addresses- set to code unless overridden */
104
105/*   in data file */
106
107/* o perhaps a 'scan' that creates a first-guess .dat file? */
108
109/*   generate dummy labels, mark code, find ASCII, etc. */
110
111
112
113/* compiled on Amiga SAS/C 6.51 and Sun 10 Unix cc */
114
115
116
117#include <stdio.h>
118
119#include <stdlib.h>
120
121#include <string.h>
122
123#include <ctype.h>
124
125
126
127/* a few of my datatypes (from Amiga) */
128
129#define TRUE (1==1)
130
131#define FALSE (!TRUE)
132
133typedef short BOOL;                                    /* boolean quantity */
134
135typedef unsigned char UBYTE;                    /* unsigned 8-bit quantity */
136
137
138
139/* array sizes for labels, DATA/ASCII definitions and COMMENT/COMMENTLINE */
140
141#define MAXLABEL 999
142
143#define MAXDATA 999                           /* both DATA and ASCII lines */
144
145#define MAXCOMMENT 999
146
147#define MAXCOMMLINE 999
148
149
150
151/* output listing spacing */
152
153#define TABOPNAME 19
154
155#define TABOPERAND 25
156
157#define TABCOMM 40
158
159
160
161#define OPNAMETAB (TABOPNAME-1)
162
163#define OPERANDTAB (TABOPERAND-1)
164
165#define COMMTAB (TABCOMM-1)
166
167
168
169typedef struct {                                       /* opcode structure */
170
171   UBYTE opcode;                                     /* 8-bit opcode value */
172
173   UBYTE numoperands;
174
175   char name[6];                                            /* opcode name */
176
177   UBYTE mode;                                          /* addressing mode */
178
179   UBYTE numcycles;                         /* number of cycles - not used */
180
181} opcodeinfo;
182
183   
184
185/* 6809 ADDRESSING MODES */
186
187#define INH 0       
188
189#define DIR 1
190
191#define IND 2
192
193#define REL 3
194
195#define EXT 4
196
197#define IMM 5
198
199#define LREL 6
200
201#define PG2 7                                    /* PAGE SWITCHES - Page 2 */
202
203#define PG3 8                                                    /* Page 3 */
204
205   
206
207/* number of opcodes in each page */
208
209#define NUMPG1OPS 223
210
211#define NUMPG2OPS 38
212
213#define NUMPG3OPS 9
214
215
216
217int numops[3]={
218
219   NUMPG1OPS,NUMPG2OPS,NUMPG3OPS,
220
221};
222
223
224
225char modenames[9][14]={
226
227   "inherent",
228
229   "direct",
230
231   "indexed",
232
233   "relative",
234
235   "extended",
236
237   "immediate",
238
239   "long relative",
240
241   "page 2",
242
243   "page 3",
244
245};
246
247
248
249opcodeinfo pg1opcodes[NUMPG1OPS]={                           /* page 1 ops */
250
251   0,1,"NEG",DIR,6,
252
253   3,1,"COM",DIR,6,
254
255   4,1,"LSR",DIR,6,
256
257   6,1,"ROR",DIR,6,
258
259   7,1,"ASR",DIR,6,
260
261   8,1,"ASL",DIR,6,
262
263   9,1,"ROL",DIR,6,
264
265   10,1,"DEC",DIR,6,   
266
267   12,1,"INC",DIR,6,
268
269   13,1,"TST",DIR,6,
270
271   14,1,"JMP",DIR,3,
272
273   15,1,"CLR",DIR,6,
274
275
276
277   16,1,"page2",PG2,0,
278
279   17,1,"page3",PG3,0,
280
281   18,0,"NOP",INH,2,
282
283   19,0,"SYNC",INH,4,
284
285   22,2,"LBRA",LREL,5,
286
287   23,2,"LBSR",LREL,9,
288
289   25,0,"DAA",INH,2,
290
291   26,1,"ORCC",IMM,3,
292
293   28,1,"ANDCC",IMM,3,
294
295   29,0,"SEX",INH,2,
296
297   30,1,"EXG",IMM,8,
298
299   31,1,"TFR",IMM,6,
300
301   
302
303   32,1,"BRA",REL,3,
304
305   33,1,"BRN",REL,3,
306
307   34,1,"BHI",REL,3,
308
309   35,1,"BLS",REL,3,
310
311   36,1,"BCC",REL,3,
312
313   37,1,"BCS",REL,3,
314
315   38,1,"BNE",REL,3,
316
317   39,1,"BEQ",REL,3,
318
319   40,1,"BVC",REL,3,
320
321   41,1,"BVS",REL,3,
322
323   42,1,"BPL",REL,3,
324
325   43,1,"BMI",REL,3,
326
327   44,1,"BGE",REL,3,
328
329   45,1,"BLT",REL,3,
330
331   46,1,"BGT",REL,3,
332
333   47,1,"BLE",REL,3,
334
335   
336
337   48,1,"LEAX",IND,2,
338
339   49,1,"LEAY",IND,2,
340
341   50,1,"LEAS",IND,2,
342
343   51,1,"LEAU",IND,2,
344
345   52,1,"PSHS",INH,5,
346
347   53,1,"PULS",INH,5,
348
349   54,1,"PSHU",INH,5,
350
351   55,1,"PULU",INH,5,
352
353   57,0,"RTS",INH,5,
354
355   58,0,"ABX",INH,3,
356
357   59,0,"RTI",INH,6,
358
359   60,1,"CWAI",IMM,20,
360
361   61,0,"MUL",INH,11,
362
363   63,0,"SWI",INH,19,
364
365   
366
367   64,0,"NEGA",INH,2,
368
369   67,0,"COMA",INH,2,
370
371   68,0,"LSRA",INH,2,
372
373   70,0,"RORA",INH,2,
374
375   71,0,"ASRA",INH,2,
376
377   72,0,"ASLA",INH,2,
378
379   73,0,"ROLA",INH,2,
380
381   74,0,"DECA",INH,2,
382
383   76,0,"INCA",INH,2,
384
385   77,0,"TSTA",INH,2,
386
387   79,0,"CLRA",INH,2,
388
389   
390
391   80,0,"NEGB",INH,2,
392
393   83,0,"COMB",INH,2,
394
395   84,0,"LSRB",INH,2,
396
397   86,0,"RORB",INH,2,
398
399   87,0,"ASRB",INH,2,
400
401   88,0,"ASLB",INH,2,
402
403   89,0,"ROLB",INH,2,
404
405   90,0,"DECB",INH,2,
406
407   92,0,"INCB",INH,2,
408
409   93,0,"TSTB",INH,2,
410
411   95,0,"CLRB",INH,2,
412
413   
414
415   96,1,"NEG",IND,6,
416
417   99,1,"COM",IND,6,
418
419   100,1,"LSR",IND,6,
420
421   102,1,"ROR",IND,6,
422
423   103,1,"ASR",IND,6,
424
425   104,1,"ASL",IND,6,
426
427   105,1,"ROL",IND,6,
428
429   106,1,"DEC",IND,6,
430
431   108,1,"INC",IND,6,
432
433   109,1,"TST",IND,6,
434
435   110,1,"JMP",IND,3,
436
437   111,1,"CLR",IND,6,
438
439   
440
441   112,2,"NEG",EXT,7,
442
443   115,2,"COM",EXT,7,
444
445   116,2,"LSR",EXT,7,
446
447   118,2,"ROR",EXT,7,
448
449   119,2,"ASR",EXT,7,
450
451   120,2,"ASL",EXT,7,
452
453   121,2,"ROL",EXT,7,
454
455   122,2,"DEC",EXT,7,
456
457   124,2,"INC",EXT,7,
458
459   125,2,"TST",EXT,7,
460
461   126,2,"JMP",EXT,4,
462
463   127,2,"CLR",EXT,7,
464
465   
466
467   128,1,"SUBA",IMM,2,
468
469   129,1,"CMPA",IMM,2,
470
471   130,1,"SBCA",IMM,2,
472
473   131,2,"SUBD",IMM,4,
474
475   132,1,"ANDA",IMM,2,
476
477   133,1,"BITA",IMM,2,
478
479   134,1,"LDA",IMM,2,
480
481   136,1,"EORA",IMM,2,
482
483   137,1,"ADCA",IMM,2,
484
485   138,1,"ORA",IMM,2,
486
487   139,1,"ADDA",IMM,2,
488
489   140,2,"CMPX",IMM,4,
490
491   141,1,"BSR",REL,7,
492
493   142,2,"LDX",IMM,3,
494
495   
496
497   144,1,"SUBA",DIR,4,
498
499   145,1,"CMPA",DIR,4,
500
501   146,1,"SBCA",DIR,4,
502
503   147,1,"SUBD",DIR,6,
504
505   148,1,"ANDA",DIR,4,
506
507   149,1,"BITA",DIR,4,
508
509   150,1,"LDA",DIR,4,
510
511   151,1,"STA",DIR,4,
512
513   152,1,"EORA",DIR,4,
514
515   153,1,"ADCA",DIR,4,
516
517   154,1,"ORA",DIR,4,
518
519   155,1,"ADDA",DIR,4,
520
521   156,1,"CPX",DIR,6,
522
523   157,1,"JSR",DIR,7,
524
525   158,1,"LDX",DIR,5,
526
527   159,1,"STX",DIR,5,
528
529   
530
531   160,1,"SUBA",IND,4,
532
533   161,1,"CMPA",IND,4,
534
535   162,1,"SBCA",IND,4,
536
537   163,1,"SUBD",IND,6,
538
539   164,1,"ANDA",IND,4,
540
541   165,1,"BITA",IND,4,
542
543   166,1,"LDA",IND,4,
544
545   167,1,"STA",IND,4,
546
547   168,1,"EORA",IND,4,
548
549   169,1,"ADCA",IND,4,
550
551   170,1,"ORA",IND,4,
552
553   171,1,"ADDA",IND,4,
554
555   172,1,"CPX",IND,6,
556
557   173,1,"JSR",IND,7,
558
559   174,1,"LDX",IND,5,
560
561   175,1,"STX",IND,5,
562
563   
564
565   176,2,"SUBA",EXT,5,
566
567   177,2,"CMPA",EXT,5,
568
569   178,2,"SBCA",EXT,5,
570
571   179,2,"SUBD",EXT,7,
572
573   180,2,"ANDA",EXT,5,
574
575   181,2,"BITA",EXT,5,
576
577   182,2,"LDA",EXT,5,
578
579   183,2,"STA",EXT,5,
580
581   184,2,"EORA",EXT,5,
582
583   185,2,"ADCA",EXT,5,
584
585   186,2,"ORA",EXT,5,
586
587   187,2,"ADDA",EXT,5,
588
589   188,2,"CPX",EXT,7,
590
591   189,2,"JSR",EXT,8,
592
593   190,2,"LDX",EXT,6,
594
595   191,2,"STX",EXT,6,
596
597   
598
599   192,1,"SUBB",IMM,2,
600
601   193,1,"CMPB",IMM,2,
602
603   194,1,"SBCB",IMM,2,
604
605   195,2,"ADDD",IMM,4,
606
607   196,1,"ANDB",IMM,2,
608
609   197,1,"BITB",IMM,2,
610
611   198,1,"LDB",IMM,2,
612
613   200,1,"EORB",IMM,2,
614
615   201,1,"ADCB",IMM,2,
616
617   202,1,"ORB",IMM,2,
618
619   203,1,"ADDB",IMM,2,
620
621   204,2,"LDD",IMM,3,
622
623   206,2,"LDU",IMM,3,
624
625   
626
627   208,1,"SUBB",DIR,4,
628
629   209,1,"CMPB",DIR,4,
630
631   210,1,"SBCB",DIR,4,
632
633   211,1,"ADDD",DIR,6,
634
635   212,1,"ANDB",DIR,4,
636
637   213,1,"BITB",DIR,4,
638
639   214,1,"LDB",DIR,4,
640
641   215,1,"STB",DIR,4,
642
643   216,1,"EORB",DIR,4,
644
645   217,1,"ADCB",DIR,4,
646
647   218,1,"ORB",DIR,4,
648
649   219,1,"ADDB",DIR,4,
650
651   220,1,"LDD",DIR,5,
652
653   221,1,"STD",DIR,5,
654
655   222,1,"LDU",DIR,5,
656
657   223,1,"STU",DIR,5,
658
659   
660
661   224,1,"SUBB",IND,4,
662
663   225,1,"CMPB",IND,4,
664
665   226,1,"SBCB",IND,4,
666
667   227,1,"ADDD",IND,6,
668
669   228,1,"ANDB",IND,4,
670
671   229,1,"BITB",IND,4,
672
673   230,1,"LDB",IND,4,
674
675   231,1,"STB",IND,4,
676
677   232,1,"EORB",IND,4,
678
679   233,1,"ADCB",IND,4,
680
681   234,1,"ORB",IND,4,
682
683   235,1,"ADDB",IND,4,
684
685   236,1,"LDD",IND,5,
686
687   237,1,"STD",IND,5,
688
689   238,1,"LDU",IND,5,
690
691   239,1,"STU",IND,5,
692
693   
694
695   240,2,"SUBB",EXT,5,
696
697   241,2,"CMPB",EXT,5,
698
699   242,2,"SBCB",EXT,5,
700
701   243,2,"ADDD",EXT,7,
702
703   244,2,"ANDB",EXT,5,
704
705   245,2,"BITB",EXT,5,
706
707   246,2,"LDB",EXT,5,
708
709   247,2,"STB",EXT,5,
710
711   248,2,"EORB",EXT,5,
712
713   249,2,"ADCB",EXT,5,
714
715   250,2,"ORB",EXT,5,
716
717   251,2,"ADDB",EXT,5,
718
719   252,2,"LDD",EXT,6,
720
721   253,2,"STD",EXT,6,
722
723   254,2,"LDU",EXT,6,
724
725   255,2,"STU",EXT,6,
726
727};
728
729
730
731opcodeinfo pg2opcodes[NUMPG2OPS]={                       /* page 2 ops 10xx*/
732
733   33,3,"LBRN",LREL,5,
734
735   34,3,"LBHI",LREL,5,
736
737   35,3,"LBLS",LREL,5,
738
739   36,3,"LBCC",LREL,5,
740
741   37,3,"LBCS",LREL,5,
742
743   38,3,"LBNE",LREL,5,
744
745   39,3,"LBEQ",LREL,5,
746
747   40,3,"LBVC",LREL,5,
748
749   41,3,"LBVS",LREL,5,
750
751   42,3,"LBPL",LREL,5,
752
753   43,3,"LBMI",LREL,5,
754
755   44,3,"LBGE",LREL,5,
756
757   45,3,"LBLT",LREL,5,
758
759   46,3,"LBGT",LREL,5,
760
761   47,3,"LBLE",LREL,5,
762
763   63,2,"SWI2",INH,20,
764
765   131,3,"CMPD",IMM,5,
766
767   140,3,"CMPY",IMM,5,
768
769   142,3,"LDY",IMM,4,
770
771   147,2,"CMPD",DIR,7,
772
773   156,2,"CMPY",DIR,7,
774
775   158,2,"LDY",DIR,6,
776
777   159,2,"STY",DIR,6,
778
779   163,2,"CMPD",IND,7,
780
781   172,2,"CMPY",IND,7,
782
783   174,2,"LDY",IND,6,
784
785   175,2,"STY",IND,6,
786
787   179,3,"CMPD",EXT,8,
788
789   188,3,"CMPY",EXT,8,
790
791   190,3,"LDY",EXT,7,
792
793   191,3,"STY",EXT,7,
794
795   206,3,"LDS",IMM,4,
796
797   222,2,"LDS",DIR,6,
798
799   223,2,"STS",DIR,6,
800
801   238,2,"LDS",IND,6,
802
803   239,2,"STS",IND,6,
804
805   254,3,"LDS",EXT,7,
806
807   255,3,"STS",EXT,7,
808
809};
810
811
812
813opcodeinfo pg3opcodes[NUMPG3OPS]={                      /* page 3 ops 11xx */
814
815   63,1,"SWI3",INH,20,
816
817   131,3,"CMPU",IMM,5,
818
819   140,3,"CMPS",IMM,5,
820
821   147,2,"CMPU",DIR,7,
822
823   156,2,"CMPS",DIR,7,
824
825   163,2,"CMPU",IND,7,
826
827   172,2,"CMPS",IND,7,
828
829   179,3,"CMPU",EXT,8,
830
831   188,3,"CMPS",EXT,8,
832
833};
834
835   
836
837opcodeinfo *pgpointers[3]={
838
839   pg1opcodes,pg2opcodes,pg3opcodes,
840
841};
842
843
844
845int count;                             /* current program counter for disasm */
846
847
848
849/* getbyte() - get a byte from a file, and increment the byte counter */
850
851int getbyte(FILE *fp) {
852
853   int c;
854
855
856
857   count++;
858
859   c=getc(fp);
860
861   return(c);
862
863}
864
865
866
867#ifdef NOCONST                                /* TFB had to undefine const */
868
869   #define const
870
871#endif
872
873const char *regs[5]={"X","Y","U","S","PC"};
874
875const char *teregs[16]={"D","X","Y","U","S","PC","inv","inv","A","B","CC",
876
877      "DP","inv","inv","inv","inv"};
878
879     
880
881BOOL PC=FALSE;                      /* to see if a PUL instr is pulling PC */
882
883
884
885#define LABELSIZE 40
886
887/* label structure */
888
889struct lastruct {
890
891   unsigned short lab;                                    /* label address */
892
893   char label[LABELSIZE];                                    /* label text */
894
895} *labarray=NULL;
896
897int numlab=0;                                  /* number of labels defined */
898
899
900
901#ifndef AMIGA
902
903/* hmmm, these aren't ANSI */
904
905/* stricmp() - compare two strings, case insensitive */
906
907int stricmp(const char *s1, const char *s2) {
908
909   for(;toupper(*s1)==toupper(*s2);++s1,++s2)
910
911      if(*s1=='\0')
912
913         return(0);
914
915   return((toupper(*(unsigned char *)s1)<toupper(*(unsigned char *)s2))?-1:1);
916
917}
918
919
920
921/* strnicmp() - compare two strings, case insensitive, length limited */
922
923int strnicmp(const char *s1, const char *s2, size_t n) {
924
925   for(;0<n;++s1,++s2,--n)
926
927      if(toupper(*s1)!=toupper(*s2))
928
929         return((toupper(*(unsigned char *)s1)<
930
931               toupper(*(unsigned char *)s2))?-1:1);
932
933      else if(*s1=='\0')
934
935         return(0);
936
937   return(0);
938
939}
940
941#endif
942
943
944
945char labtemp[30];                   /* global return for checklabs() - tsk */
946
947
948
949/* checklabs() - check the defined labels from data file */
950
951/* substitute label for address if found */
952
953char *checklabs(int address,BOOL lab2,BOOL printdollar) {
954
955   int i;
956
957   
958
959   address&=0xffff;
960
961   labtemp[0]='\0';
962
963   for(i=0;i<numlab;i++)
964
965      if(address==labarray[i].lab) {
966
967         sprintf(labtemp,"%s",labarray[i].label);
968
969         if(lab2)
970
971            strcat(labtemp,":\n");
972
973         i=numlab;
974
975      }
976
977   if(!strlen(labtemp)&&!lab2) {
978
979      if(printdollar)
980
981         sprintf(labtemp,"$%04hX",address);
982
983      else
984
985         sprintf(labtemp,"%04hX",address);
986
987   }
988
989   return(labtemp);
990
991}
992
993
994
995/* printoperands() - print operands for the given opcode */
996
997void printoperands(int opcode,UBYTE numoperands,UBYTE *operandarray,
998
999      UBYTE mode,FILE *fp,char *opname,char *str) {
1000
1001   int i,rel,pb,offset=0,reg,pb2;
1002
1003   BOOL comma;
1004
1005   char out2[80];
1006
1007   int sp;
1008
1009   BOOL printdollar;                  /* print a leading $? before address */
1010
1011
1012
1013   printdollar=FALSE;
1014
1015   PC=FALSE;
1016
1017   if((opcode!=0x1f)&&(opcode!=0x1e)) {
1018
1019      switch(mode) {                              /* print before operands */
1020
1021         case IMM:
1022
1023            strcat(str,"#");
1024
1025         case DIR:
1026
1027         case EXT:
1028
1029            printdollar=TRUE;
1030
1031            break;
1032
1033         default:
1034
1035            break;
1036
1037      }
1038
1039   }
1040
1041   switch(mode) {
1042
1043      case REL:                                          /* 8-bit relative */
1044
1045         rel=operandarray[0];
1046
1047         sprintf(out2,checklabs((short)(count+((rel<128) ? rel : rel-256)),
1048
1049               FALSE,TRUE));
1050
1051         strcat(str,out2);
1052
1053         break;
1054
1055      case LREL:                                   /* 16-bit long relative */
1056
1057         rel=(operandarray[0]<<8)+operandarray[1];
1058
1059         sprintf(out2,checklabs(count+((rel<32768) ? rel : rel-65536),
1060
1061               FALSE,TRUE));
1062
1063         strcat(str,out2);
1064
1065         break;
1066
1067      case IND:                                  /* indirect- many flavors */
1068
1069         pb=operandarray[0];
1070
1071         reg=(pb>>5)&0x3;
1072
1073         pb2=pb&0x8f;
1074
1075         if((pb2==0x88)||(pb2==0x8c)) {                    /* 8-bit offset */
1076
1077            offset=getbyte(fp);
1078
1079            sprintf(out2,"%02hX ",offset);
1080
1081            strcat(str,out2);
1082
1083            if(offset>127)                            /* convert to signed */
1084
1085               offset=offset-256;
1086
1087            if(pb==0x8c)
1088
1089               reg=4;
1090
1091            for(sp=strlen(str);sp<OPNAMETAB;sp++)
1092
1093               strcat(str," ");
1094
1095            sprintf(out2,"%s",opname);
1096
1097            strcat(str,out2);
1098
1099            for(sp=strlen(str);sp<OPERANDTAB;sp++)
1100
1101               strcat(str," ");
1102
1103            if((pb&0x90)==0x90)
1104
1105               strcat(str,"[");
1106
1107            if(pb==0x8c)
1108
1109               sprintf(out2,"%s,%s",checklabs(offset,FALSE,TRUE),regs[reg]);
1110
1111            else if(offset>=0)
1112
1113               sprintf(out2,"$%02X,%s",offset,regs[reg]);
1114
1115            else
1116
1117               sprintf(out2,"-$%02X,%s",-offset,regs[reg]);
1118
1119            strcat(str,out2);
1120
1121            if(pb==0x8c) {
1122
1123               sprintf(out2," ; ($%04X)",offset+count);
1124
1125               strcat(str,out2);
1126
1127            }
1128
1129         } else if((pb2==0x89)||(pb2==0x8d)||(pb2==0x8f)) { /* 16-bit */
1130
1131            offset=(getbyte(fp)<<8);
1132
1133            sprintf(out2,"%02X ",offset>>8);
1134
1135            strcat(str,out2);
1136
1137            offset+=getbyte(fp);
1138
1139            sprintf(out2,"%02X ",offset&0xff);
1140
1141            strcat(str,out2);
1142
1143            if((pb!=0x8f)&&(offset>32767))
1144
1145               offset=offset-65536;
1146
1147            offset&=0xffff;
1148
1149            if(pb==0x8d)
1150
1151               reg=4;
1152
1153            for(sp=strlen(str);sp<OPNAMETAB;sp++)
1154
1155               strcat(str," ");
1156
1157            sprintf(out2,"%s",opname);
1158
1159            strcat(str,out2);
1160
1161            for(sp=strlen(str);sp<OPERANDTAB;sp++)
1162
1163               strcat(str," ");
1164
1165            if((pb&0x90)==0x90)
1166
1167               strcat(str,"[");
1168
1169            if(pb==0x8d)
1170
1171               sprintf(out2,"%s,%s",checklabs(offset,FALSE,TRUE),regs[reg]);
1172
1173            else if(offset>=0)
1174
1175               sprintf(out2,"$%04X,%s",offset,regs[reg]);
1176
1177            else
1178
1179               sprintf(out2,"-$%04X,%s",offset,regs[reg]);
1180
1181            strcat(str,out2);
1182
1183            if(pb==0x8d) {
1184
1185               sprintf(out2," ; ($%04X)",offset+count);
1186
1187               strcat(str,out2);
1188
1189            }
1190
1191         } else if(pb&0x80) {
1192
1193            for(sp=strlen(str);sp<OPNAMETAB;sp++)
1194
1195               strcat(str," ");
1196
1197            sprintf(out2,"%s",opname);
1198
1199            strcat(str,out2);
1200
1201            for(sp=strlen(str);sp<OPERANDTAB;sp++)
1202
1203               strcat(str," ");
1204
1205            if((pb&0x90)==0x90)
1206
1207               strcat(str,"[");
1208
1209            if((pb&0x8f)==0x80) {
1210
1211               sprintf(out2,",%s+",regs[reg]);
1212
1213               strcat(str,out2);
1214
1215            } else if((pb&0x8f)==0x81) {
1216
1217               sprintf(out2,",%s++",regs[reg]);
1218
1219               strcat(str,out2);
1220
1221            } else if((pb&0x8f)==0x82) {
1222
1223               sprintf(out2,",-%s",regs[reg]);
1224
1225               strcat(str,out2);
1226
1227            } else if((pb&0x8f)==0x83) {
1228
1229               sprintf(out2,",--%s",regs[reg]);
1230
1231               strcat(str,out2);
1232
1233            } else if((pb&0x8f)==0x84) {
1234
1235               sprintf(out2,",%s",regs[reg]);
1236
1237               strcat(str,out2);
1238
1239            } else if((pb&0x8f)==0x85) {
1240
1241               sprintf(out2,"B,%s",regs[reg]);
1242
1243               strcat(str,out2);
1244
1245            } else if((pb&0x8f)==0x86) {
1246
1247               sprintf(out2,"A,%s",regs[reg]);
1248
1249               strcat(str,out2);
1250
1251            } else if((pb&0x8f)==0x8b) {
1252
1253               sprintf(out2,"D,%s",regs[reg]);
1254
1255               strcat(str,out2);
1256
1257            }
1258
1259         } else {                                          /* 5-bit offset */
1260
1261            offset=pb&0x1f;
1262
1263            if(offset>15)
1264
1265               offset=offset-32;
1266
1267            for(sp=strlen(str);sp<OPNAMETAB;sp++)
1268
1269               strcat(str," ");
1270
1271            sprintf(out2,"%s",opname);
1272
1273            strcat(str,out2);
1274
1275            for(sp=strlen(str);sp<OPERANDTAB;sp++)
1276
1277               strcat(str," ");
1278
1279            sprintf(out2,"%s,%s",checklabs(offset,FALSE,TRUE),regs[reg]);
1280
1281            strcat(str,out2);
1282
1283         }
1284
1285         if((pb&0x90)==0x90)
1286
1287            strcat(str,"]");
1288
1289         break;
1290
1291      default:
1292
1293         if((opcode==0x1f)||(opcode==0x1e)) {                   /* TFR/EXG */
1294
1295            sprintf(out2,"%s,%s",teregs[(operandarray[0]>>4)&0xf],
1296
1297                  teregs[operandarray[0]&0xf]);
1298
1299            strcat(str,out2);
1300
1301         } else if((opcode==0x34)||(opcode==0x36)) {              /* PUSH */
1302
1303            comma=FALSE;
1304
1305            if(operandarray[0]&0x80) {
1306
1307               strcat(str,"PC");
1308
1309               comma=TRUE;
1310
1311               PC=TRUE;
1312
1313            }
1314
1315            if(operandarray[0]&0x40) {
1316
1317               if(comma)
1318
1319                  strcat(str,",");
1320
1321               if((opcode==0x34)||(opcode==0x35))
1322
1323                  strcat(str,"U");
1324
1325               else
1326
1327                  strcat(str,"S");
1328
1329               comma=TRUE;
1330
1331            }
1332
1333            if(operandarray[0]&0x20) {
1334
1335               if(comma)
1336
1337                  strcat(str,",");
1338
1339               strcat(str,"Y");
1340
1341               comma=TRUE;
1342
1343            }
1344
1345            if(operandarray[0]&0x10) {
1346
1347               if(comma)
1348
1349                  strcat(str,",");
1350
1351               strcat(str,"X");
1352
1353               comma=TRUE;
1354
1355            }
1356
1357            if(operandarray[0]&0x8) {
1358
1359               if(comma)
1360
1361                  strcat(str,",");
1362
1363               strcat(str,"DP");
1364
1365               comma=TRUE;
1366
1367            }
1368
1369            if(operandarray[0]&0x4) {
1370
1371               if(comma)
1372
1373                  strcat(str,",");
1374
1375               strcat(str,"B");
1376
1377               comma=TRUE;
1378
1379            }
1380
1381            if(operandarray[0]&0x2) {
1382
1383               if(comma)
1384
1385                  strcat(str,",");
1386
1387               strcat(str,"A");
1388
1389               comma=TRUE;
1390
1391            }
1392
1393            if(operandarray[0]&0x1) {
1394
1395               if(comma)
1396
1397                  strcat(str,",");
1398
1399               strcat(str,"CC");
1400
1401            }
1402
1403         } else if((opcode==0x35)||(opcode==0x37)) {              /* PULL */
1404
1405            comma=FALSE;
1406
1407            if(operandarray[0]&0x1) {
1408
1409               strcat(str,"CC");
1410
1411               comma=TRUE;
1412
1413            }
1414
1415            if(operandarray[0]&0x2) {
1416
1417               if(comma)
1418
1419                  strcat(str,",");
1420
1421               strcat(str,"A");
1422
1423               comma=TRUE;
1424
1425            }
1426
1427            if(operandarray[0]&0x4) {
1428
1429               if(comma)
1430
1431                  strcat(str,",");
1432
1433               strcat(str,"B");
1434
1435               comma=TRUE;
1436
1437            }
1438
1439            if(operandarray[0]&0x8) {
1440
1441               if(comma)
1442
1443                  strcat(str,",");
1444
1445               strcat(str,"DP");
1446
1447               comma=TRUE;
1448
1449            }
1450
1451            if(operandarray[0]&0x10) {
1452
1453               if(comma)
1454
1455                  strcat(str,",");
1456
1457               strcat(str,"X");
1458
1459               comma=TRUE;
1460
1461            }
1462
1463            if(operandarray[0]&0x20) {
1464
1465               if(comma)
1466
1467                  strcat(str,",");
1468
1469               strcat(str,"Y");
1470
1471               comma=TRUE;
1472
1473            }
1474
1475            if(operandarray[0]&0x40) {
1476
1477               if(comma)
1478
1479                  strcat(str,",");
1480
1481               if((opcode==0x34)||(opcode==0x35))
1482
1483                  strcat(str,"U");
1484
1485               else
1486
1487                  strcat(str,"S");
1488
1489               comma=TRUE;
1490
1491            }
1492
1493            if(operandarray[0]&0x80) {
1494
1495               if(comma)
1496
1497                  strcat(str,",");
1498
1499               strcat(str,"PC");
1500
1501                                        strcat(str," ;(PUL? PC=RTS)");
1502
1503               PC=TRUE;
1504
1505            }
1506
1507         } else {
1508
1509            if(numoperands==2) {
1510
1511               strcat(str,checklabs((operandarray[0]<<8)+operandarray[1],
1512
1513                     FALSE,TRUE));
1514
1515            } else {
1516
1517               if(printdollar)
1518
1519                  strcat(str,"$");
1520
1521               for(i=0;i<numoperands;i++) {
1522
1523                  sprintf(out2,"%02X",operandarray[i]);
1524
1525                  strcat(str,out2);
1526
1527               }
1528
1529            }
1530
1531         }
1532
1533         break;
1534
1535   }
1536
1537}
1538
1539
1540
1541#define DATA 0                                  /* type of data to display */
1542
1543#define ASCII 1
1544
1545#define WTEXT 2
1546
1547
1548
1549/* DATA/ASCII structure definition */
1550
1551struct dastruct {
1552
1553   unsigned short start;                /* beginning address of DATA/ASCII */
1554
1555   unsigned short end;                                   /* ending address */
1556
1557   unsigned short per_line;                   /* values to print on a line */
1558
1559   short type;                                           /* DATA or ASCII ? */
1560
1561} *dataarray=NULL;
1562
1563
1564
1565#define COMMENTSIZE 80
1566
1567/* COMMENT/COMMENTLINE structure definition */
1568
1569struct castruct {
1570
1571   unsigned short comline;                         /* comment line address */
1572
1573   char comment[COMMENTSIZE];                              /* comment text */
1574
1575} *commarray=NULL,*commlinearray=NULL;
1576
1577
1578
1579char out[512],out2[80];                             /* temp string buffers */
1580
1581
1582
1583void readdatafile(char *filename,
1584
1585      int *org,int *numlab,int *numdata,int *numcomm,int *numcommline) {
1586
1587   char line2[256];
1588
1589   char *line;
1590
1591   FILE *fp;
1592
1593   int dataline,data,data2,per_line,i,k;
1594
1595   
1596
1597   if(fp=fopen(filename,"r")) {                          /* read data file */
1598
1599      while(fgets(line2,255,fp)) {
1600
1601         while(strlen(line2)&&!isprint(line2[strlen(line2)-1])) /* strip cr */
1602
1603            line2[strlen(line2)-1]='\0';
1604
1605         for(i=0;i<strlen(line2);i++)
1606
1607            if((line2[i]==';')||isalpha(line2[i])) {
1608
1609               line= &(line2[i]);
1610
1611               i=strlen(line2);
1612
1613            }
1614
1615         if(!strnicmp(line,"ORG ",4)) {
1616
1617            if(*org==-1)
1618
1619               sscanf(&line[4],"%X",org);
1620
1621            else
1622
1623               printf("More than one ORG line\n");
1624
1625         } else if(!strnicmp(line,"LABEL ",6)) {
1626
1627            if(*numlab<MAXLABEL) {
1628
1629               sscanf(&line[6],"%X",&dataline);
1630
1631               labarray[*numlab].lab=dataline;
1632
1633               k=6;
1634
1635               while(line[k]==' ')
1636
1637                  k++;
1638
1639               while(line[k]!=' ')
1640
1641                  k++;
1642
1643               while(line[k]==' ')
1644
1645                  k++;
1646
1647               strncpy(labarray[*numlab].label,&line[k],LABELSIZE);
1648
1649               labarray[*numlab].label[LABELSIZE-1]=0;  /* just in case */
1650
1651               for(i=0;i<*numlab;i++)
1652
1653                  if(!strcmp(labarray[i].label,labarray[*numlab].label)) {
1654
1655                     printf("duplicate label: %s\n",labarray[i].label);
1656
1657                     break;
1658
1659                  }
1660
1661               if(i>=*numlab)   
1662
1663                  (*numlab)++;
1664
1665            } else
1666
1667               printf("Too many labels\n");
1668
1669         } else if(!strnicmp(line,"DATA ",5)) {
1670
1671            if(*numdata<MAXDATA) {
1672
1673               data2=0;
1674
1675               sscanf(&line[5],"%X-%X",&data,&data2);
1676
1677               k=5;
1678
1679               while(line[k]==' ')
1680
1681                  k++;
1682
1683               while(line[k]!=' ')
1684
1685                  k++;
1686
1687               while(line[k]==' ')
1688
1689                  k++;
1690
1691               if (k < i)
1692
1693                  sscanf(&(line[k]),"%d",&per_line);
1694
1695               else
1696
1697                  per_line = 0;
1698
1699               if(data2<data)
1700
1701                  data2=data;
1702
1703               dataarray[*numdata].type=DATA;
1704
1705               dataarray[*numdata].start=data;
1706
1707               dataarray[*numdata].end=data2;
1708
1709               dataarray[*numdata].per_line=per_line;
1710
1711               if(((*numdata)&&(dataarray[*numdata-1].start<
1712
1713                     dataarray[*numdata].start))||!*numdata)
1714
1715                  (*numdata)++;
1716
1717               else
1718
1719                  printf("`%s'\nDATA out of order\n\n",line);
1720
1721            } else
1722
1723               printf("Too many DATA/ASCII lines\n");
1724
1725         } else if(!strnicmp(line,"ASCII ",6)) {
1726
1727            if(*numdata<MAXDATA) {
1728
1729               data2=0;
1730
1731               per_line=0;
1732
1733               sscanf(&line[6],"%X-%X %d",&data,&data2,&per_line);
1734
1735               if(data2<data)
1736
1737                  data2=data;
1738
1739               dataarray[*numdata].type=ASCII;
1740
1741               dataarray[*numdata].start=data;
1742
1743               dataarray[*numdata].end=data2;
1744
1745               dataarray[*numdata].per_line=per_line;
1746
1747               if(((*numdata)&&(dataarray[*numdata-1].start<
1748
1749                     dataarray[*numdata].start))||!*numdata)
1750
1751                  (*numdata)++;
1752
1753               else if(*numdata)
1754
1755                  printf("`%s'\nASCII out of order\n\n",line);
1756
1757            } else
1758
1759               printf("Too many DATA/ASCII lines\n");
1760
1761         } else if(!strnicmp(line,"WTEXT ",6)) {
1762
1763            if(*numdata<MAXDATA) {
1764
1765               data2=0;
1766
1767               per_line=0;
1768
1769               sscanf(&line[6],"%X-%X %d",&data,&data2,&per_line);
1770
1771               if(data2<data)
1772
1773                  data2=data;
1774
1775               dataarray[*numdata].type=WTEXT;
1776
1777               dataarray[*numdata].start=data;
1778
1779               dataarray[*numdata].end=data2;
1780
1781               dataarray[*numdata].per_line=per_line;
1782
1783               if(((*numdata)&&(dataarray[*numdata-1].start<
1784
1785                     dataarray[*numdata].start))||!*numdata)
1786
1787                  (*numdata)++;
1788
1789               else if(*numdata)
1790
1791                  printf("`%s'\nWTEXT out of order\n\n",line);
1792
1793            } else
1794
1795               printf("Too many DATA/ASCII lines\n");
1796
1797         } else if(!strnicmp(line,"COMMENT ",8)) {
1798
1799            if(*numcomm<MAXCOMMENT) {
1800
1801               sscanf(&line[8],"%X",&dataline);
1802
1803               commarray[*numcomm].comline=dataline;
1804
1805                             
1806
1807               k=8;
1808
1809               while(line[k]==' ')
1810
1811                  k++;
1812
1813               while(line[k]!=' ')
1814
1815                  k++;
1816
1817               while(line[k]==' ')
1818
1819                  k++;
1820
1821               strncpy(commarray[*numcomm].comment,&line[k],COMMENTSIZE);
1822
1823               commarray[*numcomm].comment[COMMENTSIZE-1]=0; /* in case */
1824
1825
1826
1827               if(((*numcomm)&&(commarray[(*numcomm)-1].comline<=
1828
1829                     commarray[*numcomm].comline))||!*numcomm)
1830
1831                  (*numcomm)++;
1832
1833               else
1834
1835                  printf("`%s'\nCOMMENT out of order\n\n",line);
1836
1837            } else
1838
1839               printf("Too many COMMENT lines\n");
1840
1841         } else if(!strnicmp(line,"COMMENTLINE ",12)) {
1842
1843            if(*numcommline<MAXCOMMLINE) {
1844
1845               sscanf(&line[12],"%X",&dataline);
1846
1847               commlinearray[*numcommline].comline=dataline;
1848
1849               k=12;
1850
1851               while(line[k]==' ')
1852
1853                  k++;
1854
1855               while(line[k]!=' ')
1856
1857                  k++;
1858
1859               while(line[k]==' ')
1860
1861                  k++;
1862
1863               strncpy(commlinearray[*numcommline].comment,
1864
1865                     &line[k],COMMENTSIZE);
1866
1867               commlinearray[*numcommline].comment[COMMENTSIZE-1]=0;
1868
1869               
1870
1871               if(((*numcommline)&&(commlinearray[(*numcommline)-1].comline<=
1872
1873                     commlinearray[*numcommline].comline))||!*numcommline)
1874
1875                  (*numcommline)++;
1876
1877               else
1878
1879                  printf("`%s'\nCOMMENTLINE out of order\n\n",line);
1880
1881            } else
1882
1883               printf("Too many COMMENTLINE lines\n");
1884
1885         } else if(line[0]==';')                    /* remark in data file */
1886
1887            ;
1888
1889         else if(strlen(line))
1890
1891            printf("`%s'\nError in file `%s'\n\n",line,filename);
1892
1893      }
1894
1895      fclose(fp);
1896
1897   } else
1898
1899      fprintf(stderr,"Can't open file `%s'\n",filename);
1900
1901}
1902
1903
1904
1905void docomment(int line,int *curcomm,int numcomm,int numoperands) {
1906
1907   int  sp;
1908
1909   BOOL first_comment = TRUE;
1910
1911
1912
1913   if(!numcomm)
1914
1915      return;
1916
1917   if(*curcomm<numcomm) {                 /* see if we've passed a comment */
1918
1919      while(((line-1)>commarray[*curcomm].comline+numoperands)&&
1920
1921            (*curcomm<numcomm)) {
1922
1923         printf("Error: missed a comment at %X, line=%X\n",
1924
1925               commarray[*curcomm].comline,line);
1926
1927         (*curcomm)++;
1928
1929      }
1930
1931
1932
1933      while((((line-1)+numoperands)>=commarray[*curcomm].comline)||
1934
1935            ((line==commarray[*curcomm].comline)&&(numoperands==1))||
1936
1937            (((line-1)==commarray[*curcomm].comline)&&(numoperands==0))) {
1938
1939         if(*curcomm>=numcomm)
1940
1941                                break;
1942
1943                        if(first_comment!=TRUE) {
1944
1945                                printf("%s\n",out);
1946
1947                                out[0]='\0';
1948
1949                        }
1950
1951         for(sp=strlen(out);sp<COMMTAB;sp++)
1952
1953            strcat(out," ");
1954
1955         strcat(out,";");
1956
1957         strcat(out,commarray[*curcomm].comment);
1958
1959         (*curcomm)++;
1960
1961                        first_comment=FALSE;
1962
1963      }
1964
1965   }
1966
1967}
1968
1969
1970
1971void docommline(int line,int *curcommline,int numcommline) {
1972
1973   BOOL didone=FALSE;
1974
1975   
1976
1977   if(!numcommline)
1978
1979      return;
1980
1981   if(*curcommline<numcommline) {     /* see if we've passed a comment line*/
1982
1983      while(((line-1)>commlinearray[*curcommline].comline)&&
1984
1985            (*curcommline<numcommline)) {
1986
1987         printf("Error: missed a comment line at %X, line=%X\n",
1988
1989               commlinearray[*curcommline].comline,line);
1990
1991         (*curcommline)++;
1992
1993      }
1994
1995
1996
1997      while(((line-1)==commlinearray[*curcommline].comline)) {
1998
1999         if(!didone)
2000
2001            printf("\n");
2002
2003         printf("*** %s\n",commlinearray[*curcommline].comment);
2004
2005         (*curcommline)++;
2006
2007         didone=TRUE;
2008
2009      }
2010
2011
2012
2013   }
2014
2015}
2016
2017
2018
2019BOOL newl=FALSE;
2020
2021char wchars[15]={
2022
2023   "<=-?!()',./&\":"
2024
2025};
2026
2027
2028
2029char wtext(int data) {
2030
2031   if(data>127) {
2032
2033      data-=128;
2034
2035      newl=TRUE;
2036
2037   } else
2038
2039      newl=FALSE;
2040
2041   if(data<10)
2042
2043      data+=48;
2044
2045   else if(data==10)
2046
2047      data=32;
2048
2049   else if(data<37)
2050
2051      data+=54;
2052
2053   else if(data<51)
2054
2055      data=(int)wchars[data-37];
2056
2057   else
2058
2059      data=39;
2060
2061   return((char)data);
2062
2063}
2064
2065
2066
2067BOOL diddata=FALSE;
2068
2069
2070
2071int dumpdata(int opcode,int *curdata,int numdata,FILE *fp,
2072
2073      int *curcomm,int numcomm,int *curcommline,int numcommline) {
2074
2075   int pnum,tnum;
2076
2077   int numoperands;
2078
2079   int k;
2080
2081   int numchars=0;
2082
2083   
2084
2085   numoperands=2+dataarray[*curdata].end-count;
2086
2087   pnum=dataarray[*curdata].per_line;       /* print up to pnum bytes data */
2088
2089   if(dataarray[*curdata].type==DATA) {
2090
2091      sprintf(out2,"%02X ",(UBYTE)opcode);
2092
2093      strcat(out,out2);
2094
2095      if((pnum<1)||(pnum>24))
2096
2097         pnum=16;                       /* no more than 24 bytes hex data */
2098
2099   } else {
2100
2101      if(dataarray[*curdata].type==ASCII)
2102
2103         out2[0]=opcode;
2104
2105      else
2106
2107         out2[0]=wtext(opcode);
2108
2109      out2[1]='\0';
2110
2111      strcat(out,out2);
2112
2113      if((pnum<1)||(pnum>70))
2114
2115         pnum=32;                      /* no more than 70 bytes ASCII data */
2116
2117   }
2118
2119   newl=FALSE;
2120
2121   tnum=(pnum>numoperands)?numoperands-2:pnum-1;
2122
2123   for(k=0;k<numoperands-1;k++) {                        /* print the data */
2124
2125      if((++numchars>=pnum)||newl) {
2126
2127         if((tnum)||(pnum==1)) {
2128
2129            docomment(count-tnum,curcomm,numcomm,tnum);
2130
2131            docommline(count-tnum,curcommline,numcommline);
2132
2133         }
2134
2135         printf("%s\n",out);
2136
2137         sprintf(out,"%04X: ",count);
2138
2139         numchars=0;
2140
2141         newl=FALSE;
2142
2143      }
2144
2145      if(dataarray[*curdata].type==DATA)
2146
2147         sprintf(out2,"%02X ",getbyte(fp));                         /* hex */
2148
2149      else {
2150
2151         if(dataarray[*curdata].type==ASCII)
2152
2153            out2[0]=getbyte(fp);                                  /* ASCII */
2154
2155         else
2156
2157            out2[0]=wtext(getbyte(fp));                            /* text */
2158
2159         out2[1]='\0';
2160
2161      }
2162
2163      strcat(out,out2);
2164
2165   }
2166
2167   if(*curdata<numdata)
2168
2169      (*curdata)++;
2170
2171   diddata=TRUE;
2172
2173   return(numoperands-2);
2174
2175}
2176
2177
2178
2179void freearrays(void) {
2180
2181   if(labarray)
2182
2183      free(labarray);
2184
2185   if(dataarray)
2186
2187      free(dataarray);
2188
2189   if(commarray)
2190
2191      free(commarray);
2192
2193   if(commlinearray)
2194
2195      free(commlinearray);
2196
2197}
2198
2199
2200
2201BOOL mallocarrays(void) {
2202
2203   BOOL gotit=FALSE;
2204
2205   
2206
2207   if(labarray=(struct lastruct *)malloc(sizeof(struct lastruct)*MAXLABEL))
2208
2209      if(dataarray=(struct dastruct *)
2210
2211            malloc(sizeof(struct dastruct)*MAXDATA))
2212
2213         if(commarray=(struct castruct *)
2214
2215               malloc(sizeof(struct castruct)*MAXCOMMENT))
2216
2217            if(commlinearray=(struct castruct *)
2218
2219                  malloc(sizeof(struct castruct)*MAXCOMMLINE))
2220
2221               gotit=TRUE;
2222
2223   if(!gotit)
2224
2225      freearrays();
2226
2227   return(gotit);
2228
2229}
2230
2231
2232
2233void main(int argc,char *argv[]) {
2234
2235   int i,j,k;
2236
2237   int opcode,page;
2238
2239   UBYTE operand[4];
2240
2241   FILE *fp;
2242
2243   int org=~0;
2244
2245   opcodeinfo *op;
2246
2247   int numoperands;
2248
2249   int sp;
2250
2251   int numcomm=0,numdata=0,numcommline=0;
2252
2253   int curcomm=0,curdata=0,curcommline=0;
2254
2255
2256
2257   if(argc>1) {
2258
2259      if(!stricmp(argv[1],"list")) {              /* show all instructions */
2260
2261         for(i=0;i<numops[0];i++)
2262
2263            printf("opcode %02X, operands %d, name %6s, mode %s, cycles %d\n",
2264
2265                  pg1opcodes[i].opcode,pg1opcodes[i].numoperands,
2266
2267                  pg1opcodes[i].name,
2268
2269                  modenames[pg1opcodes[i].mode],pg1opcodes[i].numcycles);
2270
2271         for(i=0;i<numops[1];i++)
2272
2273            printf("opcode 10 %02X, operands %d, name %6s, mode %s, cycles %d\n",
2274
2275                  pg2opcodes[i].opcode,pg2opcodes[i].numoperands,
2276
2277                  pg2opcodes[i].name,
2278
2279                  modenames[pg2opcodes[i].mode],pg2opcodes[i].numcycles);
2280
2281         for(i=0;i<numops[2];i++)
2282
2283            printf("opcode 11 %02X, operands %d, name %6s, mode %s, cycles %d\n",
2284
2285                  pg3opcodes[i].opcode,pg3opcodes[i].numoperands,
2286
2287                  pg3opcodes[i].name,
2288
2289                  modenames[pg3opcodes[i].mode],pg3opcodes[i].numcycles);
2290
2291      } else {
2292
2293         if(!mallocarrays()) {
2294
2295            printf("Can't get memory for arrays\n");
2296
2297            exit(20);
2298
2299         }
2300
2301         if(argc>2)
2302
2303            readdatafile(argv[2],&org,&numlab,&numdata,&numcomm,&numcommline);
2304
2305         if(org>-1)                              /* int PC to ORG val or 0 */
2306
2307            count=org;
2308
2309         else
2310
2311            count=0;
2312
2313
2314
2315         for(k=0;k<numlab;k++)                       /* print labels first */
2316
2317            printf("%s EQU $%04X\n",labarray[k].label,labarray[k].lab);
2318
2319         if(numlab)
2320
2321            printf("\n");
2322
2323         printf("ORG $%04X\n",count);
2324
2325                     
2326
2327         if(fp=fopen(argv[1],"rb")) {                  /* open binary file */
2328
2329            while((opcode=getbyte(fp))!=EOF) {
2330
2331               op=NULL;
2332
2333               docommline(count,&curcommline,numcommline);
2334
2335
2336
2337               printf(checklabs(count-1,TRUE,FALSE)); /* if add lab, print */
2338
2339               sprintf(out,"%04X: ",count-1);                  /* print PC */
2340
2341               
2342
2343               if(numdata) {
2344
2345                  while(((count-1)>dataarray[curdata].end)&&
2346
2347                        (curdata<numdata)) {
2348
2349                     printf("Error: missed a data line, start=%X, end=%X\n",
2350
2351                           dataarray[curdata].start,dataarray[curdata].end);
2352
2353                     curdata++;
2354
2355                  }
2356
2357               }
2358
2359               if(numdata&&
2360
2361                     ((count-1)>=dataarray[curdata].start)&&  /* data? */
2362
2363                     ((count-1)<=dataarray[curdata].end)) {
2364
2365                  numoperands=dumpdata(opcode,&curdata,numdata,fp,
2366
2367                        &curcomm,numcomm,&curcommline,numcommline);
2368
2369                  i=numops[0]+1;             /* skip decoding as an opcode */
2370
2371               } else {                    /* not data - search for opcode */
2372
2373                  sprintf(out2,"%02X ",(UBYTE)opcode);
2374
2375                  strcat(out,out2);
2376
2377                  for(i=0;(i<numops[0])&&(pg1opcodes[i].opcode!=opcode);i++)
2378
2379                     ;
2380
2381               }
2382
2383               if(i<numops[0]) {                           /* opcode found */
2384
2385                  if(pg1opcodes[i].mode>=PG2) {             /* page switch */
2386
2387                     opcode=getbyte(fp);
2388
2389                     sprintf(out2,"%02X ",(UBYTE)opcode);
2390
2391                     strcat(out,out2);
2392
2393                     page=pg1opcodes[i].mode-PG2+1;          /* get page # */
2394
2395                     for(k=0;(k<numops[page])&&(opcode!=
2396
2397                           pgpointers[page][k].opcode);k++)
2398
2399                        ;
2400
2401                     if(k!=numops[page]) {                 /* opcode found */
2402
2403                        op=(opcodeinfo *) &(pgpointers[page][k]);
2404
2405                        numoperands=pgpointers[page][k].numoperands;
2406
2407                        for(j=0;j<numoperands-1;j++) {
2408
2409                           sprintf(out2,"%02X ",(operand[j]=getbyte(fp)));
2410
2411                           strcat(out,out2);
2412
2413                        }
2414
2415                           
2416
2417                        if(pgpointers[page][k].mode!=IND) {
2418
2419                           for(sp=strlen(out);sp<OPNAMETAB;sp++)
2420
2421                              strcat(out," ");
2422
2423                           sprintf(out2,"%s",pgpointers[page][k].name);
2424
2425                           strcat(out,out2);
2426
2427                           for(sp=strlen(out);sp<OPERANDTAB;sp++)
2428
2429                              strcat(out," ");
2430
2431                        }
2432
2433                        printoperands(opcode,numoperands-1,
2434
2435                              operand,pgpointers[page][k].mode,fp,
2436
2437                              pgpointers[page][k].name,out);
2438
2439                     } else {               /* not found in alternate page */
2440
2441                        for(sp=strlen(out);sp<OPNAMETAB;sp++)
2442
2443                           strcat(out," ");
2444
2445                        strcat(out,"Illegal Opcode");
2446
2447                     }
2448
2449                  } else {                                /* page 1 opcode */
2450
2451                     op=(opcodeinfo *) &(pg1opcodes[i]);
2452
2453                     numoperands=pg1opcodes[i].numoperands;
2454
2455                     for(j=0;j<numoperands;j++) {
2456
2457                        sprintf(out2,"%02X ",(operand[j]=getbyte(fp)));
2458
2459                        strcat(out,out2);
2460
2461                     }
2462
2463                     if(pg1opcodes[i].mode!=IND) {
2464
2465                        for(sp=strlen(out);sp<OPNAMETAB;sp++)
2466
2467                           strcat(out," ");
2468
2469                        sprintf(out2,"%s",pg1opcodes[i].name);
2470
2471                        strcat(out,out2);
2472
2473                        for(sp=strlen(out);sp<OPERANDTAB;sp++)
2474
2475                           strcat(out," ");
2476
2477                     }
2478
2479                     printoperands(opcode,numoperands,operand,
2480
2481                           pg1opcodes[i].mode,fp,pg1opcodes[i].name,out);
2482
2483                  }
2484
2485               } else if(i==numops[0]) {            /* not found in page 1 */
2486
2487                  for(sp=strlen(out);sp<OPNAMETAB;sp++) 
2488
2489                     strcat(out," ");
2490
2491                  strcat(out,"Illegal Opcode");
2492
2493               }
2494
2495               docomment(count-1-numoperands,&curcomm,numcomm,numoperands+1);
2496
2497               if(op) {
2498
2499                  if((!stricmp(op->name,"BRA"))||   /* extra space - branch */
2500
2501                        (!stricmp(op->name,"LBRA"))||
2502
2503                        (!stricmp(op->name,"RTS"))||
2504
2505                        (!stricmp(op->name,"JMP"))||
2506
2507                        (!stricmp(op->name,"RTI"))||
2508
2509                        (!strnicmp(op->name,"PUL",3)&&PC)||  /* PUL? PC=RTS */
2510
2511                        (!stricmp(op->name,"WAI"))) {
2512
2513                     if(strlen(out)&&(out[strlen(out)-1]!='\n'))
2514
2515                        strcat(out,"\n");
2516
2517                  }
2518
2519               }
2520
2521               if(diddata) {
2522
2523                  if(strlen(out)&&(out[strlen(out)-1]!='\n'))
2524
2525                     strcat(out,"\n");
2526
2527               }
2528
2529               printf("%s\n",out);
2530
2531               PC=FALSE;
2532
2533               diddata=FALSE;
2534
2535            }
2536
2537            fclose(fp);
2538
2539         } else
2540
2541            fprintf(stderr,"Can't open file `%s'\n",argv[1]);
2542
2543      }
2544
2545   } else
2546
2547      printf("Usage: `%s <file> [<datafile>]'\n",argv[0]);
2548
2549   freearrays();
2550
2551}
2552
Note: See TracBrowser for help on using the repository browser.