Finished mapping out all the single byte with word instructions
[uccvend-snackrom.git] / ROM / makeasm.pl
1 #!/usr/local/bin/perl -w
2
3 print "\tORG\t\$8000\n";
4
5 my %transtable = ( '02' => 'IDIV' ,
6                         '03' => 'FDIV' ,
7                         '08' => 'INX' , 
8                         '09' => 'DEX',
9                         '0A' => 'CLV',
10                         '0D' => 'SEC',
11                         '0E' => 'CLI',
12                         '0F' => 'SEI',
13                         '19' => 'DAA',
14                         '1B' => 'ABA',
15                         '30' => 'TSX',
16                         '31' => 'INS',
17                         '32' => 'PULA',
18                         '33' => 'PULB',
19                         '34' => 'DES',
20                         '35' => 'TXS',
21                         '36' => 'PSHA',
22                         '37' => 'PSHB',
23                         '3C' => 'PSHX',
24                         '38' => 'PULX',
25                         '3A' => 'ABX',
26                         '3B' => 'RTI',
27                         '3E' => 'WAI',
28                         '43' => 'COMA',
29                         '46' => 'RORA',
30                         '49' => 'ROLA',
31                         '4A' => 'DECA',
32                         '4C' => 'INCA',
33                         '4F' => 'CLRA',
34                         '53' => 'COMB',
35                         '56' => 'RORB',
36                         '59' => 'ROLB',
37                         '5A' => 'DECB',
38                         '5F' => 'CLRB',
39                         '8F' => 'XGDX',
40                         '39' => 'RTS' );
41
42                 #       '10' => 'SBA',
43
44
45 my %twobytefirst = ( '18' => '1' );
46
47 my %twobytesecond = ( '18 38' => 'PULY',
48                         '18 3A' => 'ABY',
49                         '18 3C' => 'PSHY');
50
51 my %onewitharg = ('63' => "COM\t\$%s,X",
52                         '6A' => "DEC\t\$%s,X",
53                         '6C' => "INC\t\$%s,X",
54                         '6D' => "TST\t\$%s,X",
55                         '6E' => "JMP\t\$%s,X",
56                         '6F' => "CLR\t\$%s,X",
57                         '80' => "SUBA\t#\$%s",
58                         '81' => "CMPA\t#\$%s",
59                         '82' => "SBCA\t#\$%s",
60                         '84' => "ANDA\t#\$%s",
61                         '85' => "BITA\t#\$%s",
62                         '86' => "LDAA\t#\$%s",
63                         '88' => "EORA\t#\$%s",
64                         '8A' => "ORA\t#\$%s",
65                         'A3' => "SUBD\t\$%s,X",
66                         'A7' => "STA\t\$%s,X",
67                         'C0' => "SUBB\t#\$%s",
68                         'C1' => "CMPB\t#\$%s",
69                         'C2' => "SBCB\t#\$%s",
70                         'C4' => "ANDB\t#\$%s",
71                         'C5' => "BITB\t#\$%s",
72                         'C6' => "LDAB\t#\$%s",
73                         'C8' => "EORB\t#\$%s",
74                         'CA' => "ORB\t#\$%s",
75                         'E3' => "ADDD\t\$%s,X",
76                         'EC' => "LDD\t\$%s,X",
77                         'ED' => "STAD\t\$%s,X" );
78
79
80 my %onewithword = (
81                    '70' => "NEG\t%s",
82                    '73' => "COM\t%s",
83                    '74' => "LSR\t%s",
84                    '76' => "ROR\t%s",
85                    '77' => "ASR\t%s",
86                    '78' => "LSL\t%s",
87 # LSL == ASL
88 #                  '78' => "ASL\t%s",
89                    '79' => "ROL\t%s",
90                    '7A' => "DEC\t%s",
91                    '7C' => "INC\t%s",
92                    '7D' => "TST\t%s",
93                    '7F' => "CLR\t%s",
94                    'B0' => "SUBA\t%s",
95                    'B1' => "CMPA\t%s",
96                    'B2' => "SBCA\t%s",
97                    'B3' => "SUBD\t%s",
98                    'B4' => "ANDA\t%s",
99                    'B5' => "BITA\t%s",
100                    'B6' => "LDAA\t%s",
101                    'B7' => "STAA\t%s",
102                    'B9' => "ADCA\t%s",
103                    'BA' => "ORAA\t%s",
104                    'BB' => "ADDA\t%s",
105                    'BC' => "CPX\t%s",
106                    'BD' => "JSR\t%s",
107                    'BE' => "LDS\t%s",
108                    'BF' => "STS\t%s",
109                    'F0' => "SUBB\t%s",
110                    'F1' => "CMPB\t%s",
111                    'F2' => "SBCB\t%s",
112                    'F3' => "ADDD\t%s",
113                    'F4' => "ANDB\t%s",
114                    'F5' => "BITB\t%s",
115                    'F6' => "LDAB\t%s",
116                    'F7' => "STAB\t%s",
117                    'FA' => "ORAB\t%s",
118                    'FB' => "ADDB\t%s",
119                    'F8' => "EORB\t%s",
120                    'F9' => "ADCB\t%s",
121                    'FC' => "LDD\t%s",
122                    'FD' => "STD\t%s",
123                    'FE' => "LDX\t%s",
124                    'FF' => "STX\t%s" );
125
126 LINE: while (<>) {
127
128 # if blank line, print blank line
129   if (/^\s*$/) { print "\n"; next LINE; }
130
131 # if comment line, pass straight through
132   if (/^\s*;/) { print; next LINE; }
133
134 # if duplicate label - rename uniquely
135   if (/^\s*((loop|jump82|goto91):)/) { print "${1}_$.\n"; next LINE; }
136
137 # if it's a label, print label
138   if (/^\s*(\w*:)/) { print "$1\n"; next LINE; }
139
140 # if it's a vector table, print words
141   if (/^\t\.word\t([a-f0-9]{4})$/) { print "\tFDB\t\$\U$1\n"; next LINE; }
142
143 # otherwise, catch all, print bytes
144   if (/^([A-F0-9]{4}) ((([A-F0-9]{2}) )*[A-F0-9]{2})/) {
145     instruction($_);
146     next LINE;
147   }
148
149 # if control Z, remove it
150   if (/^\cZ$/) { next LINE; }
151
152 # FINAL print commented out
153   print ";$_";
154 }
155
156 # Process one instruction
157 # Usage: instruction line
158 sub instruction {
159     my ($line) = @_;
160     print ";$line";
161
162     $instruction = substr($line,5,2);
163
164     if (defined $transtable{$instruction}) {
165         print "\t$transtable{$instruction}";
166         $line =~ /$transtable{$instruction}(.*)$/i;
167         print "$1\n";
168         return;
169     }
170    
171     if (defined $twobytefirst{$instruction}) {
172         $thing = substr($line,5,5);
173         if (defined $twobytesecond{$thing}) {
174                 print "\t" . $twobytesecond{$thing};
175                 $line =~ /$twobytesecond{$thing}(.*)$/i;
176                 print "$1\n";
177                 return;
178         }
179     }
180     
181     if (defined $onewitharg{$instruction}) {
182         $thing = substr($line,8,2);
183         
184         printf "\t$onewitharg{$instruction}", $thing;
185         $line =~ /$onewitharg{$instruction} [0-9A-F][0-9A-F](.*)$/i;
186         print "\t;$1\n";
187         return;
188     }
189    
190     if (defined $onewithword{$instruction}) {
191         $thing = substr($line,8,2);
192         $thing2 = substr($line,11,2);
193         if (!($thing2 =~ /\s+/)) {
194         $curraddr = substr($line,0,4);
195         printf "FOO%s\tEQU\t\$%s%s\n", $curraddr,$thing,$thing2;
196         printf "\t$onewithword{$instruction}", ">FOO$curraddr";
197         $line =~ /$onewithword{$instruction} [0-9A-F][0-9A-F][0-9A-F][0-9A-F](.*)$/i;
198         print "\t;$1\n";
199         return;
200         }
201     }
202    
203     if ($line =~ /^([A-F0-9]{4}) ((([A-F0-9]{2}) )*[A-F0-9]{2})/) {
204         @_=split(' ',$2);
205         print "\tFCB\t\$".join(', $',@_)."\n";
206     }
207 }

UCC git Repository :: git.ucc.asn.au