git @ Cat's Eye Technologies SixtyPical / 0d3ac8c
{Low,High}AddressByte emittables to fix copy, make indirect call! Chris Pressey 6 years ago
3 changed file(s) with 47 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
66 RoutineType, VectorType,
77 REG_A, REG_X, REG_Y, FLAG_C
88 )
9 from sixtypical.emitter import Byte, Label, Offset
9 from sixtypical.emitter import Byte, Label, Offset, LowAddressByte, HighAddressByte
1010 from sixtypical.gen6502 import (
1111 Immediate, Absolute, AbsoluteX, AbsoluteY, Indirect, Relative,
1212 LDA, LDX, LDY, STA, STX, STY,
266266 self.compile_block(instr.block)
267267 self.emitter.emit(CLI())
268268 elif opcode == 'copy':
269 if isinstance(src.type, (RoutineType, VectorType)) and \
270 isinstance(dest.type, VectorType):
269 if isinstance(src.type, VectorType) and isinstance(dest.type, VectorType):
271270 src_label = self.labels[src.name]
272271 dest_label = self.labels[dest.name]
273272 self.emitter.emit(LDA(Absolute(src_label)))
274273 self.emitter.emit(STA(Absolute(dest_label)))
275274 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
276275 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
276 elif isinstance(src.type, RoutineType) and isinstance(dest.type, VectorType):
277 src_label = self.labels[src.name]
278 dest_label = self.labels[dest.name]
279 self.emitter.emit(LDA(Immediate(HighAddressByte(src_label))))
280 self.emitter.emit(STA(Absolute(dest_label)))
281 self.emitter.emit(LDA(Immediate(LowAddressByte(src_label))))
282 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
277283 else:
278284 raise NotImplementedError(src.type)
279285 else:
8181 return "%s(%r, %r)" % (self.__class__.__name__, self.label, self.offset)
8282
8383
84 class HighAddressByte(Emittable):
85 def __init__(self, label):
86 assert isinstance(label, Label)
87 self.label = label
88
89 def size(self):
90 return 1
91
92 def serialize(self, addr=None):
93 return self.label.serialize()[0]
94
95 def __repr__(self):
96 return "%s(%r)" % (self.__class__.__name__, self.label)
97
98
99 class LowAddressByte(Emittable):
100 def __init__(self, label):
101 assert isinstance(label, Label)
102 self.label = label
103
104 def size(self):
105 return 1
106
107 def serialize(self, addr=None):
108 return self.label.serialize()[1]
109
110 def __repr__(self):
111 return "%s(%r)" % (self.__class__.__name__, self.label)
112
113
114 # - - - -
115
116
84117 class Emitter(object):
85118 def __init__(self, addr):
86119 self.accum = []
00 """Emittables for 6502 machine code."""
11
2 from sixtypical.emitter import Emittable, Byte, Label, Offset
2 from sixtypical.emitter import Emittable, Byte, Label, Offset, LowAddressByte, HighAddressByte
33
44
55 class AddressingMode(Emittable):
2727
2828 class Immediate(AddressingMode):
2929 def __init__(self, value):
30 assert isinstance(value, Byte)
30 assert isinstance(value, (Byte, LowAddressByte, HighAddressByte))
3131 self.value = value
3232
3333 def size(self):
8181 return self.value.serialize_relative_to(addr)
8282
8383
84 # - - - -
85
86
8487 class Instruction(Emittable):
8588 def __init__(self, operand=None):
8689 self.operand = operand or Implied()