git @ Cat's Eye Technologies SixtyPical / 479b484
Compile the rest of the instructions. Chris Pressey 6 years ago
3 changed file(s) with 120 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
77 from sixtypical.emitter import Label, Byte
88 from sixtypical.gen6502 import (
99 Immediate, Absolute,
10 LDA, LDX, LDY, STA, STX, STY, CLC, SEC, ADC, RTS, JSR,
10 LDA, LDX, LDY, STA, STX, STY,
11 CLC, SEC, ADC, SBC, ROL, ROR,
12 RTS, JSR,
1113 INC, INX, INY, DEC, DEX, DEY,
14 CMP, CPX, CPY, AND, ORA, EOR,
1215 )
1316
1417
127130 else:
128131 self.emitter.emit(DEC(Absolute(self.labels[dest.name])))
129132 elif opcode == 'cmp':
130 raise NotImplementedError
131 elif opcode == 'and':
132 raise NotImplementedError
133 elif opcode == 'or':
134 raise NotImplementedError
135 elif opcode == 'xor':
136 raise NotImplementedError
137 elif opcode == 'shl':
138 raise NotImplementedError
139 elif opcode == 'shr':
140 raise NotImplementedError
133 cls = {
134 'a': CMP,
135 'x': CPX,
136 'y': CPY,
137 }.get(dest.name)
138 if cls is None:
139 raise UnsupportedOpcodeError(instr)
140 if isinstance(src, ConstantRef):
141 self.emitter.emit(cls(Immediate(Byte(src.value))))
142 else:
143 self.emitter.emit(cls(Absolute(self.labels[src.name])))
144 elif opcode in ('and', 'or', 'xor',):
145 cls = {
146 'and': AND,
147 'or': ORA,
148 'xor': EOR,
149 }[opcode]
150 if dest == REG_A:
151 if isinstance(src, ConstantRef):
152 self.emitter.emit(cls(Immediate(Byte(src.value))))
153 else:
154 self.emitter.emit(cls(Absolute(self.labels[src.name])))
155 else:
156 raise UnsupportedOpcodeError(instr)
157 elif opcode in ('shl', 'shr'):
158 cls = {
159 'shl': ROL,
160 'shr': ROR,
161 }[opcode]
162 if dest == REG_A:
163 self.emitter.emit(cls())
164 else:
165 raise UnsupportedOpcodeError(instr)
141166 elif opcode == 'call':
142167 label = self.labels[instr.name]
143168 self.emitter.emit(JSR(Absolute(label)))
7373 }
7474
7575
76 class ADD(Opcode):
76 class AND(Opcode):
7777 opcodes = {
7878 Immediate: 0x29,
7979 Absolute: 0x2d,
8686 }
8787
8888
89 class CMP(Opcode):
90 opcodes = {
91 Immediate: 0xc9,
92 Absolute: 0xcd,
93 }
94
95
96 class CPX(Opcode):
97 opcodes = {
98 Immediate: 0xe0,
99 Absolute: 0xec,
100 }
101
102
103 class CPY(Opcode):
104 opcodes = {
105 Immediate: 0xc0,
106 Absolute: 0xcc,
107 }
108
109
89110 class DEC(Opcode):
90111 opcodes = {
91112 Absolute: 0xce,
101122 class DEY(Opcode):
102123 opcodes = {
103124 Implied: 0x88,
125 }
126
127
128 class EOR(Opcode):
129 opcodes = {
130 Immediate: 0x49,
131 Absolute: 0x4d,
104132 }
105133
106134
156184 }
157185
158186
187 class ROL(Opcode):
188 opcodes = {
189 Implied: 0x2a, # Accumulator
190 }
191
192
193 class ROR(Opcode):
194 opcodes = {
195 Implied: 0x6a, # Accumulator
196 }
197
198
159199 class RTS(Opcode):
160200 opcodes = {
161201 Implied: 0x60,
7575 | ld a, foo
7676 | }
7777 = 00c0a0008c09c0ad09c060
78
79 Some instructions.
80
81 | byte foo
82 |
83 | routine main
84 | trashes a, x, y, z, n, c, v, foo
85 | {
86 | ld a, 0
87 | ld x, 0
88 | ld y, 0
89 | st a, foo
90 | st x, foo
91 | st y, foo
92 | st on, c
93 | st off, c
94 | add a, 1
95 | add a, foo
96 | sub a, 1
97 | sub a, foo
98 | inc foo
99 | inc x
100 | inc y
101 | dec foo
102 | dec x
103 | dec y
104 | and a, 255
105 | and a, foo
106 | or a, 255
107 | or a, foo
108 | xor a, 255
109 | xor a, foo
110 | cmp a, 1
111 | cmp a, foo
112 | cmp x, 1
113 | cmp x, foo
114 | cmp y, 1
115 | cmp y, foo
116 | shl a
117 | shr a
118 | }
119 = 00c0a900a200a0008d46c08e46c08c46c0381869016d46c0e901ed46c0ee46c0e8c8ce46c0ca8829ff2d46c009ff0d46c049ff4d46c0c901cd46c0e001ec46c0c001cc46c02a6a60