Get serialization of relative labels correct.
Chris Pressey
6 years ago
63 | 63 |
sys.exit(1)
|
64 | 64 |
|
65 | 65 |
if options.compile:
|
|
66 |
fh = sys.stdout
|
66 | 67 |
start_addr = 0xc000
|
67 | 68 |
prelude = []
|
68 | 69 |
if options.basic_prelude:
|
69 | 70 |
start_addr = 0x0801
|
70 | 71 |
prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
|
71 | 72 |
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
|
|
73 |
|
|
74 |
# we are outputting a .PRG, so we output the load address first
|
|
75 |
# we don't use the Emitter for this b/c not part of addr space
|
|
76 |
fh.write(Word(start_addr).serialize(0))
|
|
77 |
|
72 | 78 |
emitter = Emitter(start_addr)
|
73 | |
# we are outputting a .PRG, so output the load address first
|
74 | |
emitter.emit_header(Word(start_addr))
|
75 | 79 |
for byte in prelude:
|
76 | 80 |
emitter.emit(Byte(byte))
|
77 | 81 |
compiler = Compiler(emitter)
|
|
79 | 83 |
if options.debug:
|
80 | 84 |
print repr(emitter.accum)
|
81 | 85 |
else:
|
82 | |
emitter.serialize(sys.stdout)
|
|
86 |
emitter.serialize(fh)
|
83 | 87 |
|
84 | 88 |
if options.execute:
|
85 | 89 |
context = eval_program(program)
|
|
0 |
routine chrout
|
|
1 |
inputs a
|
|
2 |
trashes a
|
|
3 |
@ 65490
|
|
4 |
|
|
5 |
routine print
|
|
6 |
trashes a, z, n
|
|
7 |
{
|
|
8 |
ld a, 65
|
|
9 |
call chrout
|
|
10 |
}
|
|
11 |
|
|
12 |
routine main
|
|
13 |
trashes a, z, n
|
|
14 |
{
|
|
15 |
call print
|
|
16 |
call print
|
|
17 |
}
|
|
0 |
routine chrout
|
|
1 |
inputs a
|
|
2 |
trashes a
|
|
3 |
@ 65490
|
|
4 |
|
|
5 |
routine main
|
|
6 |
trashes a, x, y, z, n, c, v
|
|
7 |
{
|
|
8 |
ld a, 0
|
|
9 |
if z {
|
|
10 |
ld a, 89
|
|
11 |
call chrout
|
|
12 |
} else {
|
|
13 |
ld a, 78
|
|
14 |
call chrout
|
|
15 |
}
|
|
16 |
|
|
17 |
ld a, 1
|
|
18 |
if z {
|
|
19 |
ld a, 89
|
|
20 |
call chrout
|
|
21 |
} else {
|
|
22 |
ld a, 78
|
|
23 |
call chrout
|
|
24 |
}
|
|
25 |
}
|
|
0 |
routine chrout
|
|
1 |
inputs a
|
|
2 |
trashes a
|
|
3 |
@ 65490
|
|
4 |
|
|
5 |
routine main
|
|
6 |
trashes a, x, y, z, n, c, v
|
|
7 |
{
|
|
8 |
ld a, 0
|
|
9 |
if z {
|
|
10 |
ld a, 89
|
|
11 |
call chrout
|
|
12 |
ld a, 1
|
|
13 |
}
|
|
14 |
|
|
15 |
ld a, 65
|
|
16 |
call chrout
|
|
17 |
|
|
18 |
ld a, 1
|
|
19 |
if z {
|
|
20 |
ld a, 89
|
|
21 |
call chrout
|
|
22 |
ld a, 1
|
|
23 |
}
|
|
24 |
}
|
|
0 |
byte foo
|
|
1 |
|
|
2 |
routine chrout
|
|
3 |
inputs a
|
|
4 |
trashes a
|
|
5 |
@ 65490
|
|
6 |
|
|
7 |
routine print
|
|
8 |
inputs foo
|
|
9 |
trashes a, z, n
|
|
10 |
{
|
|
11 |
ld a, foo
|
|
12 |
call chrout
|
|
13 |
}
|
|
14 |
|
|
15 |
routine main
|
|
16 |
trashes a, y, z, n, foo
|
|
17 |
{
|
|
18 |
ld y, 65
|
|
19 |
st y, foo
|
|
20 |
call print
|
|
21 |
inc foo
|
|
22 |
call print
|
|
23 |
}
|
56 | 56 |
assert self.addr is not None, "unresolved label: %s" % self.name
|
57 | 57 |
return Word(self.addr).serialize(addr)
|
58 | 58 |
|
|
59 |
def serialize_relative_to(self, addr):
|
|
60 |
assert self.addr is not None, "unresolved label: %s" % self.name
|
|
61 |
return Byte(self.addr - (addr + 2)).serialize(addr)
|
|
62 |
|
59 | 63 |
def __repr__(self):
|
60 | 64 |
addrs = ', addr=%r' % self.addr if self.addr is not None else ''
|
61 | 65 |
return "%s(%r%s)" % (self.__class__.__name__, self.name, addrs)
|
|
70 | 74 |
|
71 | 75 |
def emit(self, *things):
|
72 | 76 |
for thing in things:
|
73 | |
if isinstance(thing, int):
|
74 | |
thing = Byte(thing)
|
75 | 77 |
self.accum.append(thing)
|
76 | 78 |
self.addr += thing.size()
|
77 | |
|
78 | |
def emit_header(self, *things):
|
79 | |
"""Does not advance the address counter"""
|
80 | |
for thing in things:
|
81 | |
if isinstance(thing, int):
|
82 | |
thing = Byte(thing)
|
83 | |
self.accum.append(thing)
|
84 | 79 |
|
85 | 80 |
def serialize(self, stream):
|
86 | 81 |
addr = self.start_addr
|
58 | 58 |
return 1
|
59 | 59 |
|
60 | 60 |
def serialize(self, addr):
|
61 | |
# XXX serialize value relatively
|
62 | |
return chr(0xff)
|
63 | |
return self.value.serialize(addr)
|
|
61 |
return self.value.serialize_relative_to(addr)
|
64 | 62 |
|
65 | 63 |
|
66 | 64 |
class Opcode(Emittable):
|