git @ Cat's Eye Technologies SixtyPical / a3160a5
Call defined routines. Chris Pressey 6 years ago
5 changed file(s) with 56 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
2727 optparser = OptionParser(__doc__.strip())
2828
2929 optparser.add_option("--analyze",
30 action="store_true", dest="analyze", default=False,
30 action="store_true",
3131 help="")
32 optparser.add_option("--basic-header",
33 action="store_true", dest="basic_header", default=False,
32 optparser.add_option("--basic-prelude",
33 action="store_true",
3434 help="")
3535 optparser.add_option("--compile",
36 action="store_true", dest="compile", default=False,
36 action="store_true",
3737 help="")
3838 optparser.add_option("--debug",
39 action="store_true", dest="debug", default=False,
39 action="store_true",
4040 help="")
4141 optparser.add_option("--traceback",
42 action="store_true", dest="traceback", default=False,
42 action="store_true",
4343 help="")
4444 optparser.add_option("--execute",
45 action="store_true", dest="execute", default=False,
45 action="store_true",
4646 help="")
4747
4848 (options, args) = optparser.parse_args(sys.argv[1:])
6464
6565 if options.compile:
6666 start_addr = 0xc000
67 header = []
68 if options.basic_header:
67 prelude = []
68 if options.basic_prelude:
6969 start_addr = 0x0801
70 header = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
71 0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
70 prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
71 0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
7272 emitter = Emitter(start_addr)
7373 # we are outputting a .PRG, so output the load address first
74 emitter.emit(Word(start_addr))
75 for byte in header:
74 emitter.emit_header(Word(start_addr))
75 for byte in prelude:
7676 emitter.emit(Byte(byte))
7777 compiler = Compiler(emitter)
7878 compiler.compile_program(program)
11
22 SRC=$1
33 OUT=/tmp/a-out.prg
4 bin/sixtypical --analyze --compile --basic-header $SRC > $OUT || exit 1
4 bin/sixtypical --analyze --compile --basic-prelude $SRC > $OUT || exit 1
55 x64 $OUT
66 rm -f $OUT
2929 if routine.addr is not None:
3030 label.set_addr(routine.addr)
3131 self.labels[routine.name] = label
32
33 self.compile_routine(self.routines['main'])
3234 for routine in program.routines:
33 self.compile_routine(routine)
35 if routine.name != 'main':
36 self.compile_routine(routine)
3437
3538 def compile_routine(self, routine):
3639 assert isinstance(routine, Routine)
3740 if routine.block:
41 self.emitter.resolve_label(self.labels[routine.name])
3842 self.compile_block(routine.block)
3943 self.emitter.emit(RTS())
4044
5660 if isinstance(src, ConstantRef):
5761 self.emitter.emit(LDA(Immediate(Byte(src.value))))
5862 else:
59 self.emitter.emit(LDA(Absolute(src.label)))
63 self.emitter.emit(LDA(Absolute(self.labels[src.name])))
6064 elif dest == REG_X:
61 pass
65 if isinstance(src, ConstantRef):
66 self.emitter.emit(LDX(Immediate(Byte(src.value))))
67 else:
68 self.emitter.emit(LDX(Absolute(self.labels[src.name])))
6269 elif dest == REG_Y:
63 pass
70 if isinstance(src, ConstantRef):
71 self.emitter.emit(LDY(Immediate(Byte(src.value))))
72 else:
73 self.emitter.emit(LDY(Absolute(self.labels[src.name])))
6474 else:
6575 raise UnsupportedOpcodeError(instr)
6676 elif opcode == 'st':
6979 elif dest == FLAG_C and src == ConstantRef(1):
7080 self.emitter.emit(SEC())
7181 elif src == REG_A:
72 self.emitter.emit(STA(Absolute(dest.label)))
82 self.emitter.emit(STA(Absolute(self.labels[dest.name])))
7383 elif src == REG_X:
74 self.emitter.emit(STX(Absolute(dest.label)))
84 self.emitter.emit(STX(Absolute(self.labels[dest.name])))
7585 elif src == REG_Y:
76 self.emitter.emit(STY(Absolute(dest.label)))
86 self.emitter.emit(STY(Absolute(self.labels[dest.name])))
7787 else:
7888 raise UnsupportedOpcodeError(instr)
7989 elif opcode == 'add':
7575 self.accum.append(thing)
7676 self.addr += thing.size()
7777
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
7885 def serialize(self, stream):
7986 for emittable in self.accum:
8087 stream.write(emittable.serialize())
4444 | call chrout
4545 | }
4646 = 00c0a94120d2ff60
47
48 Call defined routine.
49
50 | routine foo
51 | outputs a, x, y
52 | trashes z, n
53 | {
54 | ld a, 0
55 | ld x, 0
56 | ld y, 0
57 | }
58 |
59 | routine main
60 | trashes a, x, y, z, n
61 | {
62 | call foo
63 | }
64 = 00c02004c060a900a200a00060