git @ Cat's Eye Technologies SixtyPical / a5c3b47
Pass address when serializing. Still wrong, of course. Chris Pressey 6 years ago
2 changed file(s) with 25 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
00 class Emittable(object):
11 def size(self):
2 """Default implementation may not be very efficient."""
3 return len(self.serialize())
2 raise NotImplementedError
43
5 def serialize(self):
4 def serialize(self, addr):
65 raise NotImplementedError
76
87
1716 def size(self):
1817 return 1
1918
20 def serialize(self):
19 def serialize(self, addr):
2120 return chr(self.value)
2221
2322 def __repr__(self):
3231 def size(self):
3332 return 2
3433
35 def serialize(self):
34 def serialize(self, addr):
3635 word = self.value
3736 low = word & 255
3837 high = (word >> 8) & 255
5352 def size(self):
5453 return 2
5554
56 def serialize(self):
55 def serialize(self, addr):
5756 assert self.addr is not None, "unresolved label: %s" % self.name
58 return Word(self.addr).serialize()
57 return Word(self.addr).serialize(addr)
5958
6059 def __repr__(self):
6160 addrs = ', addr=%r' % self.addr if self.addr is not None else ''
6564 class Emitter(object):
6665 def __init__(self, addr):
6766 self.accum = []
67 self.start_addr = addr
6868 self.addr = addr
6969 self.name_counter = 0
7070
8383 self.accum.append(thing)
8484
8585 def serialize(self, stream):
86 addr = self.start_addr
8687 for emittable in self.accum:
87 stream.write(emittable.serialize())
88 chunk = emittable.serialize(addr)
89 stream.write(chunk)
90 addr += len(chunk)
8891
8992 def make_label(self, name=None):
9093 if name is None:
77 """Size of the operand for the mode (not including the opcode)"""
88 raise NotImplementedError
99
10 def serialize(self, addr):
11 raise NotImplementedError
12
1013 def __repr__(self):
1114 return "%s(%r)" % (self.__class__.__name__, self.value)
1215
1518 def size(self):
1619 return 0
1720
18 def serialize(self):
21 def serialize(self, addr):
1922 return ''
2023
2124 def __repr__(self):
3033 def size(self):
3134 return 1
3235
33 def serialize(self):
34 return self.value.serialize()
36 def serialize(self, addr):
37 return self.value.serialize(addr)
3538
3639
3740 class Absolute(AddressingMode):
4245 def size(self):
4346 return 2
4447
45 def serialize(self):
46 return self.value.serialize()
48 def serialize(self, addr):
49 return self.value.serialize(addr)
4750
4851
4952 class Relative(AddressingMode):
5053 def __init__(self, value):
51 assert isinstance(value, (Byte, Label))
54 assert isinstance(value, Label)
5255 self.value = value
5356
5457 def size(self):
5558 return 1
5659
57 def serialize(self):
58 # HA
60 def serialize(self, addr):
61 # XXX serialize value relatively
5962 return chr(0xff)
63 return self.value.serialize(addr)
6064
6165
6266 class Opcode(Emittable):
6670 def size(self):
6771 return 1 + self.operand.size() if self.operand else 0
6872
69 def serialize(self):
73 def serialize(self, addr):
7074 return (
7175 chr(self.opcodes[self.operand.__class__]) +
72 self.operand.serialize()
76 self.operand.serialize(addr)
7377 )
7478
7579 def __repr__(self):