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

Pass address when serializing. Still wrong, of course.

This commit is contained in:
Chris Pressey 2015-10-17 16:51:35 +01:00
parent b6bb64528f
commit a5c3b4725d
2 changed files with 25 additions and 18 deletions

View File

@ -1,9 +1,8 @@
class Emittable(object):
def size(self):
"""Default implementation may not be very efficient."""
return len(self.serialize())
raise NotImplementedError
def serialize(self):
def serialize(self, addr):
raise NotImplementedError
@ -18,7 +17,7 @@ class Byte(Emittable):
def size(self):
return 1
def serialize(self):
def serialize(self, addr):
return chr(self.value)
def __repr__(self):
@ -33,7 +32,7 @@ class Word(Emittable):
def size(self):
return 2
def serialize(self):
def serialize(self, addr):
word = self.value
low = word & 255
high = (word >> 8) & 255
@ -54,9 +53,9 @@ class Label(Emittable):
def size(self):
return 2
def serialize(self):
def serialize(self, addr):
assert self.addr is not None, "unresolved label: %s" % self.name
return Word(self.addr).serialize()
return Word(self.addr).serialize(addr)
def __repr__(self):
addrs = ', addr=%r' % self.addr if self.addr is not None else ''
@ -66,6 +65,7 @@ class Label(Emittable):
class Emitter(object):
def __init__(self, addr):
self.accum = []
self.start_addr = addr
self.addr = addr
self.name_counter = 0
@ -84,8 +84,11 @@ class Emitter(object):
self.accum.append(thing)
def serialize(self, stream):
addr = self.start_addr
for emittable in self.accum:
stream.write(emittable.serialize())
chunk = emittable.serialize(addr)
stream.write(chunk)
addr += len(chunk)
def make_label(self, name=None):
if name is None:

View File

@ -8,6 +8,9 @@ class AddressingMode(object):
"""Size of the operand for the mode (not including the opcode)"""
raise NotImplementedError
def serialize(self, addr):
raise NotImplementedError
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.value)
@ -16,7 +19,7 @@ class Implied(AddressingMode):
def size(self):
return 0
def serialize(self):
def serialize(self, addr):
return ''
def __repr__(self):
@ -31,8 +34,8 @@ class Immediate(AddressingMode):
def size(self):
return 1
def serialize(self):
return self.value.serialize()
def serialize(self, addr):
return self.value.serialize(addr)
class Absolute(AddressingMode):
@ -43,21 +46,22 @@ class Absolute(AddressingMode):
def size(self):
return 2
def serialize(self):
return self.value.serialize()
def serialize(self, addr):
return self.value.serialize(addr)
class Relative(AddressingMode):
def __init__(self, value):
assert isinstance(value, (Byte, Label))
assert isinstance(value, Label)
self.value = value
def size(self):
return 1
def serialize(self):
# HA
def serialize(self, addr):
# XXX serialize value relatively
return chr(0xff)
return self.value.serialize(addr)
class Opcode(Emittable):
@ -67,10 +71,10 @@ class Opcode(Emittable):
def size(self):
return 1 + self.operand.size() if self.operand else 0
def serialize(self):
def serialize(self, addr):
return (
chr(self.opcodes[self.operand.__class__]) +
self.operand.serialize()
self.operand.serialize(addr)
)
def __repr__(self):