1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2024-11-26 14:49:15 +00:00

{Low,High}AddressByte emittables to fix copy, make indirect call!

This commit is contained in:
Chris Pressey 2015-10-22 16:20:10 +01:00
parent 0b2b5d904e
commit 0d3ac8c247
3 changed files with 47 additions and 5 deletions

View File

@ -7,7 +7,7 @@ from sixtypical.model import (
RoutineType, VectorType, RoutineType, VectorType,
REG_A, REG_X, REG_Y, FLAG_C 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 ( from sixtypical.gen6502 import (
Immediate, Absolute, AbsoluteX, AbsoluteY, Indirect, Relative, Immediate, Absolute, AbsoluteX, AbsoluteY, Indirect, Relative,
LDA, LDX, LDY, STA, STX, STY, LDA, LDX, LDY, STA, STX, STY,
@ -267,14 +267,20 @@ class Compiler(object):
self.compile_block(instr.block) self.compile_block(instr.block)
self.emitter.emit(CLI()) self.emitter.emit(CLI())
elif opcode == 'copy': elif opcode == 'copy':
if isinstance(src.type, (RoutineType, VectorType)) and \ if isinstance(src.type, VectorType) and isinstance(dest.type, VectorType):
isinstance(dest.type, VectorType):
src_label = self.labels[src.name] src_label = self.labels[src.name]
dest_label = self.labels[dest.name] dest_label = self.labels[dest.name]
self.emitter.emit(LDA(Absolute(src_label))) self.emitter.emit(LDA(Absolute(src_label)))
self.emitter.emit(STA(Absolute(dest_label))) self.emitter.emit(STA(Absolute(dest_label)))
self.emitter.emit(LDA(Absolute(Offset(src_label, 1)))) self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
self.emitter.emit(STA(Absolute(Offset(dest_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: else:
raise NotImplementedError(src.type) raise NotImplementedError(src.type)
else: else:

View File

@ -82,6 +82,39 @@ class Offset(Emittable):
return "%s(%r, %r)" % (self.__class__.__name__, self.label, self.offset) 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): class Emitter(object):
def __init__(self, addr): def __init__(self, addr):
self.accum = [] self.accum = []

View File

@ -1,6 +1,6 @@
"""Emittables for 6502 machine code.""" """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): class AddressingMode(Emittable):
@ -28,7 +28,7 @@ class Implied(AddressingMode):
class Immediate(AddressingMode): class Immediate(AddressingMode):
def __init__(self, value): def __init__(self, value):
assert isinstance(value, Byte) assert isinstance(value, (Byte, LowAddressByte, HighAddressByte))
self.value = value self.value = value
def size(self): def size(self):
@ -82,6 +82,9 @@ class Relative(AddressingMode):
return self.value.serialize_relative_to(addr) return self.value.serialize_relative_to(addr)
# - - - -
class Instruction(Emittable): class Instruction(Emittable):
def __init__(self, operand=None): def __init__(self, operand=None):
self.operand = operand or Implied() self.operand = operand or Implied()