From 0d3ac8c2475686089e77c61b39164a4696c2eec9 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Thu, 22 Oct 2015 16:20:10 +0100 Subject: [PATCH] {Low,High}AddressByte emittables to fix copy, make indirect call! --- src/sixtypical/compiler.py | 12 +++++++++--- src/sixtypical/emitter.py | 33 +++++++++++++++++++++++++++++++++ src/sixtypical/gen6502.py | 7 +++++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/sixtypical/compiler.py b/src/sixtypical/compiler.py index e49ee66..6123974 100644 --- a/src/sixtypical/compiler.py +++ b/src/sixtypical/compiler.py @@ -7,7 +7,7 @@ from sixtypical.model import ( RoutineType, VectorType, REG_A, REG_X, REG_Y, FLAG_C ) -from sixtypical.emitter import Byte, Label, Offset +from sixtypical.emitter import Byte, Label, Offset, LowAddressByte, HighAddressByte from sixtypical.gen6502 import ( Immediate, Absolute, AbsoluteX, AbsoluteY, Indirect, Relative, LDA, LDX, LDY, STA, STX, STY, @@ -267,14 +267,20 @@ class Compiler(object): self.compile_block(instr.block) self.emitter.emit(CLI()) elif opcode == 'copy': - if isinstance(src.type, (RoutineType, VectorType)) and \ - isinstance(dest.type, VectorType): + if isinstance(src.type, VectorType) and isinstance(dest.type, VectorType): src_label = self.labels[src.name] dest_label = self.labels[dest.name] self.emitter.emit(LDA(Absolute(src_label))) self.emitter.emit(STA(Absolute(dest_label))) self.emitter.emit(LDA(Absolute(Offset(src_label, 1)))) self.emitter.emit(STA(Absolute(Offset(dest_label, 1)))) + elif isinstance(src.type, RoutineType) and isinstance(dest.type, VectorType): + src_label = self.labels[src.name] + dest_label = self.labels[dest.name] + self.emitter.emit(LDA(Immediate(HighAddressByte(src_label)))) + self.emitter.emit(STA(Absolute(dest_label))) + self.emitter.emit(LDA(Immediate(LowAddressByte(src_label)))) + self.emitter.emit(STA(Absolute(Offset(dest_label, 1)))) else: raise NotImplementedError(src.type) else: diff --git a/src/sixtypical/emitter.py b/src/sixtypical/emitter.py index de088e6..5ae2225 100644 --- a/src/sixtypical/emitter.py +++ b/src/sixtypical/emitter.py @@ -82,6 +82,39 @@ class Offset(Emittable): return "%s(%r, %r)" % (self.__class__.__name__, self.label, self.offset) +class HighAddressByte(Emittable): + def __init__(self, label): + assert isinstance(label, Label) + self.label = label + + def size(self): + return 1 + + def serialize(self, addr=None): + return self.label.serialize()[0] + + def __repr__(self): + return "%s(%r)" % (self.__class__.__name__, self.label) + + +class LowAddressByte(Emittable): + def __init__(self, label): + assert isinstance(label, Label) + self.label = label + + def size(self): + return 1 + + def serialize(self, addr=None): + return self.label.serialize()[1] + + def __repr__(self): + return "%s(%r)" % (self.__class__.__name__, self.label) + + +# - - - - + + class Emitter(object): def __init__(self, addr): self.accum = [] diff --git a/src/sixtypical/gen6502.py b/src/sixtypical/gen6502.py index aefd82c..e0be7a4 100644 --- a/src/sixtypical/gen6502.py +++ b/src/sixtypical/gen6502.py @@ -1,6 +1,6 @@ """Emittables for 6502 machine code.""" -from sixtypical.emitter import Emittable, Byte, Label, Offset +from sixtypical.emitter import Emittable, Byte, Label, Offset, LowAddressByte, HighAddressByte class AddressingMode(Emittable): @@ -28,7 +28,7 @@ class Implied(AddressingMode): class Immediate(AddressingMode): def __init__(self, value): - assert isinstance(value, Byte) + assert isinstance(value, (Byte, LowAddressByte, HighAddressByte)) self.value = value def size(self): @@ -82,6 +82,9 @@ class Relative(AddressingMode): return self.value.serialize_relative_to(addr) +# - - - - + + class Instruction(Emittable): def __init__(self, operand=None): self.operand = operand or Implied()