A little sanity checker.
[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 %twobytesecondword = ( '18 FE' => 'LDY\t>%s',
52                         '18 FF' => 'STY\t>%s');
53
54 my %onewitharg = ('60' => "NEG\t\$%s,X",
55                         '63' => "COM\t\$%s,X",
56                         '64' => "LSR\t\$%s,X",
57                         '67' => "ASR\t\$%s,X",
58                         '68' => "ASL\t\$%s,X",
59                         '6A' => "DEC\t\$%s,X",
60                         '6C' => "INC\t\$%s,X",
61                         '6D' => "TST\t\$%s,X",
62                         '6E' => "JMP\t\$%s,X",
63                         '6F' => "CLR\t\$%s,X",
64                         '80' => "SUBA\t#\$%s",
65                         '81' => "CMPA\t#\$%s",
66                         '82' => "SBCA\t#\$%s",
67                         '84' => "ANDA\t#\$%s",
68                         '85' => "BITA\t#\$%s",
69                         '86' => "LDAA\t#\$%s",
70                         '88' => "EORA\t#\$%s",
71                         '8A' => "ORA\t#\$%s",
72                         '8B' => "ADDA\t#\$%s",
73                         'A0' => "SUBA\t\$%s,X",
74                         'A1' => "CMPA\t\$%s,X",
75                         'A2' => "SBCA\t\$%s,X",
76                         'A3' => "SUBD\t\$%s,X",
77                         'A4' => "ANDA\t\$%s,X",
78                         'A6' => "LDAA\t\$%s,X",
79                         'A7' => "STA\t\$%s,X",
80                         'A8' => "EORA\t\$%s,X",
81                         'A9' => "ADCA\t\$%s,X",
82                         'AA' => "ORAA\t\$%s,X",
83                         'AB' => "ADDA\t\$%s,X",
84                         'AC' => "CPX\t\$%s,X",
85                         'AD' => "JSR\t\$%s,X",
86                         'AE' => "LDS\t\$%s,X",
87                         'AF' => "STS\t\$%s,X",
88                         'C0' => "SUBB\t#\$%s",
89                         'C1' => "CMPB\t#\$%s",
90                         'C2' => "SBCB\t#\$%s",
91                         'C4' => "ANDB\t#\$%s",
92                         'C5' => "BITB\t#\$%s",
93                         'C6' => "LDAB\t#\$%s",
94                         'C8' => "EORB\t#\$%s",
95                         'C9' => "ADCB\t#\$%s",
96                         'CA' => "ORB\t#\$%s",
97                         'CB' => "ADDB\t#\$%s",
98                         'E0' => "SUBB\t\$%s,X",
99                         'E1' => "CMPB\t\$%s,X",
100                         'E2' => "SBCB\t\$%s,X",
101                         'E3' => "ADDD\t\$%s,X",
102                         'E4' => "ANDB\t\$%s,X",
103                         'E6' => "LDAB\t\$%s,X",
104                         'E7' => "STAB\t\$%s,X",
105                         'E8' => "EORB\t\$%s,X",
106                         'E9' => "ADCB\t\$%s,X",
107                         'EA' => "ORAB\t\$%s,X",
108                         'EB' => "ADDB\t\$%s,X",
109                         'EC' => "LDD\t\$%s,X",
110                         'ED' => "STD\t\$%s,X",
111                         'EE' => "LDX\t\$%s,X",
112                         'EF' => "STX\t\$%s,X" );
113
114
115 my %onewithword = (
116                    '70' => "NEG\t>%s",
117                    '73' => "COM\t>%s",
118                    '74' => "LSR\t>%s",
119                    '76' => "ROR\t>%s",
120                    '77' => "ASR\t>%s",
121                    '78' => "LSL\t>%s",
122 # LSL == ASL
123 #                  '78' => "ASL\t>%s",
124                    '79' => "ROL\t>%s",
125                    '7A' => "DEC\t>%s",
126                    '7C' => "INC\t>%s",
127                    '7D' => "TST\t>%s",
128                    '7F' => "CLR\t>%s",
129 #                  '95' => "BITA\t<%s",
130                    'B0' => "SUBA\t>%s",
131                    'B1' => "CMPA\t>%s",
132                    'B2' => "SBCA\t>%s",
133                    'B3' => "SUBD\t>%s",
134                    'B4' => "ANDA\t>%s",
135                    'B5' => "BITA\t>%s",
136                    'B6' => "LDAA\t>%s",
137                    'B7' => "STAA\t>%s",
138                    'B9' => "ADCA\t>%s",
139                    'BA' => "ORAA\t>%s",
140                    'BB' => "ADDA\t>%s",
141                    'BC' => "CPX\t>%s",
142                    'BD' => "JSR\t>%s",
143                    'BE' => "LDS\t>%s",
144                    'BF' => "STS\t>%s",
145                    'D5' => "BITB\t>%s",
146                    'F0' => "SUBB\t>%s",
147                    'F1' => "CMPB\t>%s",
148                    'F2' => "SBCB\t>%s",
149                    'F3' => "ADDD\t>%s",
150                    'F4' => "ANDB\t>%s",
151                    'F5' => "BITB\t>%s",
152                    'F6' => "LDAB\t>%s",
153                    'F7' => "STAB\t>%s",
154                    'FA' => "ORAB\t>%s",
155                    'FB' => "ADDB\t>%s",
156                    'F8' => "EORB\t>%s",
157                    'F9' => "ADCB\t>%s",
158                    'FC' => "LDD\t>%s",
159                    'FD' => "STD\t>%s",
160                    'FE' => "LDX\t>%s",
161                    'FF' => "STX\t>%s" );
162
163 LINE: while (<>) {
164
165 # if blank line, print blank line
166   if (/^\s*$/) { print "\n"; next LINE; }
167
168 # if comment line, pass straight through
169   if (/^\s*;/) { print; next LINE; }
170
171 # if duplicate label - rename uniquely
172   if (/^\s*((loop|jump82|goto91):)/) { print "${1}_$.\n"; next LINE; }
173
174 # if it's a label, print label
175   if (/^\s*(\w*:)/) { print "$1\n"; next LINE; }
176
177 # if it's a vector table, print words
178   if (/^\t\.word\t([a-f0-9]{4})$/) { print "\tFDB\t\$\U$1\n"; next LINE; }
179
180 # otherwise, catch all, print bytes
181   if (/^([A-F0-9]{4}) ((([A-F0-9]{2}) )*[A-F0-9]{2})/) {
182     instruction($_);
183     next LINE;
184   }
185
186 # if control Z, remove it
187   if (/^\cZ$/) { next LINE; }
188
189 # FINAL print commented out
190   print ";$_";
191 }
192
193 # Process one instruction
194 # Usage: instruction line
195 sub instruction {
196     my ($line) = @_;
197
198     $instruction = substr($line,5,2);
199
200     if (defined $transtable{$instruction}) {
201     print ";$line";
202         print "\t$transtable{$instruction}";
203         $line =~ /$transtable{$instruction}(.*)$/i;
204         print "$1\n";
205         return;
206     }
207    
208     if (defined $twobytefirst{$instruction}) {
209     print ";$line";
210         $thing = substr($line,5,5);
211         if (defined $twobytesecond{$thing}) {
212                 print "\t" . $twobytesecond{$thing};
213                 $line =~ /$twobytesecond{$thing}(.*)$/i;
214                 print "$1\n";
215                 return;
216         }
217     
218         if (defined $twobytesecondword{$thing}) {
219                 $athing = substr($line,11,2);
220                 $athing2 = substr($line,14,2);
221                 if (!($athing2 =~ /\s+/)) {
222                  $curraddr = substr($line,0,4);
223                  printf "FOO%s\tEQU\t\$%s%s\n", $curraddr,$athing,$athing2;
224                  printf "\t$twobytesecondword{$thing}", "FOO$curraddr";
225                  $line =~ /$twobytesecondword{$thing} [0-9A-F][0-9A-F][0-9A-F][0-9A-F](.*)$/i;
226                  print "\t;$1\n";
227                 return;
228                 }
229         }
230     }
231     
232     if (defined $onewitharg{$instruction}) {
233     print ";$line";
234         $thing = substr($line,8,2);
235         
236         printf "\t$onewitharg{$instruction}", $thing;
237         $line =~ /$onewitharg{$instruction} [0-9A-F][0-9A-F](.*)$/i;
238         print "\t;$1\n";
239         return;
240     }
241    
242     if (defined $onewithword{$instruction}) {
243     print ";$line";
244         $thing = substr($line,8,2);
245         $thing2 = substr($line,11,2);
246         if (!($thing2 =~ /\s+/)) {
247         $curraddr = substr($line,0,4);
248         printf "FOO%s\tEQU\t\$%s%s\n", $curraddr,$thing,$thing2;
249         printf "\t$onewithword{$instruction}", "FOO$curraddr";
250         $line =~ /$onewithword{$instruction} [0-9A-F][0-9A-F][0-9A-F][0-9A-F](.*)$/i;
251         print "\t;$1\n";
252         return;
253         }
254     }
255    
256     print ";$line";
257     if ($line =~ /^([A-F0-9]{4}) ((([A-F0-9]{2}) )*[A-F0-9]{2})/) {
258         @_=split(' ',$2);
259         print "\tFCB\t\$".join(', $',@_)."\n";
260     }
261 }

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