3 # Script to convert M6800 source code to non-optimal M6809 source code.
4 # By Joel Matthew Rees, Amagasaki, Japan, September 2018.
5 # Copyright 2018 Joel Matthew Rees
7 # Permission to use current version for personal research, entertainment,
8 # and other non-commercial purposes hereby granted,
9 # on condition that authorship and copyright notice are left intact.
10 # For other uses, contact the author on social media.
18 # Including stuff we don't need, in case I get ambitious.
19 # Not including 6801 at this point.
21 # Branches are completely unchanged going from 6800 to 6809:
22 my $branchlist = "BCC|BCS|BEQ|BGE|BGT|BHI|BLE|BLS|BLT|BMI|BNE|BPL|BRA|BVC|BVS";
24 # These implicit ops are unchanged from 6800 to 6809:
25 my $implist = "DAA|NOP|RTS";
27 # 16-bit pseudo-binary, unchanged from 6800 to 6809:
28 my $jumplist ="JMP|JSR";
30 # 8-bit binary ops are unchanged going from 6800 to 6809,
31 # but we'll eliminate optional space for sociability:
32 my $binop8list = "ADC|ADD|AND|BIT|CMP|EOR|SBC|SUB";
34 # 8-bit unary ops are unchanged going from 6800to 6809,
35 # but we'll eliminate optional space for sociability:
36 my $unoplist = "ASL|ASR|CLR|COM|DEC|INC|LSR|LSL|NEG|ROL|ROR|TST";
38 # These binary 8-bits have one too many As from 6800 to 6809:
39 my $binopLDORST8list = "LDA|ORA|STA";
41 # These loads and stores are unchanged from 6800 to 6809,
42 # except for optional space:
43 my $binopLDST16list = "LDS|LDX|STS|STX";
45 # Form changes to CMPX
46 my $binop16list = "CPX";
48 # Push and pop (pull) are generalized:
49 # (6800 had no pshx! -- But fixed in 6801.)
50 my $pushmepullyoulist = "PSH|PUL";
52 # Transfers are generalized:
53 my $transferlist = "TAB|TAP|TBA|TPA|TSX|TXS";
55 # These convert to LEA instructions:
56 my $lealist = "DES|DEX|INS|INX";
58 # Processor status bit handling is generalized:
59 # my $flaghandlerlist = "CLC|CLI|CLV|SEC|SEI|SEV";
60 my $flaghandleroplist = "CL|SE";
61 my $flaghandlerbitlist = "[CIV]";
63 # Special handling for inter-accumulator:
64 my $b2alist = "ABA|CBA|SBA";
66 # Interrupt stuff, form remains the same, different register set,
67 # flag working on by hand:
68 my $interruptstufflist = "RTI|SWI";
70 # Interrupt wait, form changes, semantics change,
71 # flag for working on by hand:
72 my $waitstufflist = "WAI";
74 while ( my $line = <> )
76 if ( $line =~ m/^(\w*)\s+FCC\s+(\d+),(.*)$/ )
81 my $symbol = substr( $strfield, 0, $symlength );
82 my $leftovers = substr( $strfield, $symlength );
83 my $strlength = length( $symbol );
84 if ( $strlength < $symlength )
86 print "$label\tFCC error\t'$symbol' not complete to "
87 . "$symlength characters (only $strlength). ****error***";
91 my $fullsymbol = $symbol;
92 if ( $leftovers =~ m/^(\S+)(.*)$/ )
97 print "$label\tFCC\t'$symbol'\t; '$fullsymbol'";
100 if ( length( $leftovers ) > 0 )
102 print " : $leftovers";
106 elsif ( $line =~ m/^(\w*)\s+(${pushmepullyoulist})\s*(A|B)\s*(.*)$/ )
112 print "$label\t${operator}S $operand\t; $comments\n";
114 elsif ( $line =~ m/^(\w*)\s+(${binopLDORST8list})\s*(A|B)\s+(.*)$/ )
119 my $comments = $4; # Fudging, comments includes memory operand.
120 my $op2letter = substr( $operator, 0, 2 );
121 print "$label\t${op2letter}$operand $comments\n";
123 elsif ( $line =~ m/^(\w*)\s+(${lealist})(.*)$/ )
128 if ( $operator =~ m/IN(\w)/ )
129 { $operator = "LEA$1 1,$1";
131 elsif ( $operator =~ m/DE(\w)/ )
132 { $operator = "LEA$1 -1,$1";
134 print "$label\t${operator}\t; $comments\n";
136 elsif ( $line =~ m/^(\w*)\s+(${unoplist})\s*(A|B)(.*)$/ )
142 print "$label\t$operator$operand\t;$comments\n";
144 elsif ( $line =~ m/^(\w*)\s+(${unoplist})\s+(.*)$/ )
148 my $operand = $3; # Fudging, operand includes any comments.
149 print "$label\t$operator $operand\n";
151 elsif ( $line =~ m/^(\w*)\s+(${b2alist})(.*)$/ )
156 my $realoperator = $operator;
157 print "$label\tPSHS B\t; ** emulating $operator:\n";
158 if ( $operator eq "ABA" )
159 { $realoperator = "ADDA";
161 elsif ( $operator eq "CBA" )
162 { $realoperator = "CMPA";
164 elsif ( $operator eq "SBA" )
165 { $realoperator = "SUBA";
167 print "\t$realoperator ,S+\t; $comments\n";
169 elsif ( $line =~ m/^(\w*)\s+(${binop8list})\s*(A|B)\s+(.+)$/ )
174 my $comments = $4; # Fudging, comments includes memory operand.
175 print "$label\t${operator}$operand $comments\n";
177 elsif ( $line =~ m/^(\w*)\s+(${transferlist})(.*)$/ )
182 my $source = substr( $operator, 1, 1 );
183 my $destination = substr( $operator, 2, 1 );
184 if ( $source eq "P" )
187 if ( $destination eq "P" )
188 { $destination = "CCR";
190 print "$label\tTFR $source,$destination\t; $operator : $comments\n";
192 elsif ( $line =~ m/^(\w*)\s+(${flaghandleroplist})($flaghandlerbitlist)(.*)$/ )
193 { # Bits: EFHINZVC, thus I is 0x10, V is 0x02, and C is 0x01.
206 my $realoperator = $operator;
207 if ( $operator eq "CL" )
208 { $realoperator = "ANDCC";
209 $realbits = "~" . $realbits;
211 elsif ( $operator eq "SE" )
212 { $realoperator = "ORCC"
214 print "$label\t$realoperator #$realbits\t; ${operator}${bit} : $comments\n";
216 elsif ( $line =~ m/^(\w*)\s+($binop16list)\s+(.+)$/ )
220 my $operand = $3; # Fudging, operand includes any comments.
221 my $reg16bit = substr( $operator, 2, 1 );
222 print "$label\tCMP$reg16bit\t$operand\n";
224 elsif ( $line =~ m/^(\w*)\s+(${branchlist})\s+(\*[+-]\$?[0-9A-Fa-f]+)(.*)$/ )
230 print "$label\t$operator $operand\t; $comments\n"
231 . "\t****WARNING**** HARD OFFSET: $operand ****\n";
233 elsif ( $line =~ m/^(\w*)\s+(${interruptstufflist})(.*)$/ )
237 my $operand = $3; # Fudging, operand includes any comments.
238 print "$label\t$operator$operand\n"
239 . "\t****WARNING**** Interrupt routines must change!! ****\n";
241 elsif ( $line =~ m/^(\w*)\s+(${waitstufflist})(.*)$/ )
245 my $operand = $3; # Fudging, operand includes any comments.
246 print "$label\tC$operator #\$EF\t; $operand\n"
247 . "\t****WARNING**** WAI must change to CWAI!! ****\n";