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

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