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): class Emittable(object):
def size(self): def size(self):
"""Default implementation may not be very efficient.""" raise NotImplementedError
return len(self.serialize())
def serialize(self): def serialize(self, addr):
raise NotImplementedError raise NotImplementedError
@ -18,7 +17,7 @@ class Byte(Emittable):
def size(self): def size(self):
return 1 return 1
def serialize(self): def serialize(self, addr):
return chr(self.value) return chr(self.value)
def __repr__(self): def __repr__(self):
@ -33,7 +32,7 @@ class Word(Emittable):
def size(self): def size(self):
return 2 return 2
def serialize(self): def serialize(self, addr):
word = self.value word = self.value
low = word & 255 low = word & 255
high = (word >> 8) & 255 high = (word >> 8) & 255
@ -54,9 +53,9 @@ class Label(Emittable):
def size(self): def size(self):
return 2 return 2
def serialize(self): def serialize(self, addr):
assert self.addr is not None, "unresolved label: %s" % self.name 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): def __repr__(self):
addrs = ', addr=%r' % self.addr if self.addr is not None else '' addrs = ', addr=%r' % self.addr if self.addr is not None else ''
@ -66,6 +65,7 @@ class Label(Emittable):
class Emitter(object): class Emitter(object):
def __init__(self, addr): def __init__(self, addr):
self.accum = [] self.accum = []
self.start_addr = addr
self.addr = addr self.addr = addr
self.name_counter = 0 self.name_counter = 0
@ -84,8 +84,11 @@ class Emitter(object):
self.accum.append(thing) self.accum.append(thing)
def serialize(self, stream): def serialize(self, stream):
addr = self.start_addr
for emittable in self.accum: 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): def make_label(self, name=None):
if name is 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)""" """Size of the operand for the mode (not including the opcode)"""
raise NotImplementedError raise NotImplementedError
def serialize(self, addr):
raise NotImplementedError
def __repr__(self): def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.value) return "%s(%r)" % (self.__class__.__name__, self.value)
@ -16,7 +19,7 @@ class Implied(AddressingMode):
def size(self): def size(self):
return 0 return 0
def serialize(self): def serialize(self, addr):
return '' return ''
def __repr__(self): def __repr__(self):
@ -31,8 +34,8 @@ class Immediate(AddressingMode):
def size(self): def size(self):
return 1 return 1
def serialize(self): def serialize(self, addr):
return self.value.serialize() return self.value.serialize(addr)
class Absolute(AddressingMode): class Absolute(AddressingMode):
@ -43,21 +46,22 @@ class Absolute(AddressingMode):
def size(self): def size(self):
return 2 return 2
def serialize(self): def serialize(self, addr):
return self.value.serialize() return self.value.serialize(addr)
class Relative(AddressingMode): class Relative(AddressingMode):
def __init__(self, value): def __init__(self, value):
assert isinstance(value, (Byte, Label)) assert isinstance(value, Label)
self.value = value self.value = value
def size(self): def size(self):
return 1 return 1
def serialize(self): def serialize(self, addr):
# HA # XXX serialize value relatively
return chr(0xff) return chr(0xff)
return self.value.serialize(addr)
class Opcode(Emittable): class Opcode(Emittable):
@ -67,10 +71,10 @@ class Opcode(Emittable):
def size(self): def size(self):
return 1 + self.operand.size() if self.operand else 0 return 1 + self.operand.size() if self.operand else 0
def serialize(self): def serialize(self, addr):
return ( return (
chr(self.opcodes[self.operand.__class__]) + chr(self.opcodes[self.operand.__class__]) +
self.operand.serialize() self.operand.serialize(addr)
) )
def __repr__(self): def __repr__(self):