git @ Cat's Eye Technologies SixtyPical / 99a0d56
Awful binding, but at least this code isn't in main anymore. Chris Pressey 3 years ago
2 changed file(s) with 66 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
1919
2020 from sixtypical.parser import Parser, ParsingContext
2121 from sixtypical.analyzer import Analyzer
22 from sixtypical.emitter import Emitter, Byte, Word
22 from sixtypical.outputter import Outputter
2323 from sixtypical.compiler import Compiler
2424
2525
7878 if options.analyze_only:
7979 return
8080
81 fh = open(options.output, 'wb')
82
83 if options.output_format == 'raw':
84 start_addr = 0x0000
85 prelude = []
86 elif options.output_format == 'prg':
87 start_addr = 0xc000
88 prelude = []
89 elif options.output_format == 'c64-basic-prg':
90 start_addr = 0x0801
91 prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
92 0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
93 elif options.output_format == 'vic20-basic-prg':
94 start_addr = 0x1001
95 prelude = [0x0b, 0x10, 0xc9, 0x07, 0x9e, 0x34,
96 0x31, 0x30, 0x39, 0x00, 0x00, 0x00]
97 elif options.output_format == 'atari2600-cart':
98 start_addr = 0xf000
99 prelude = [0x78, 0xd8, 0xa2, 0xff, 0x9a, 0xa9,
100 0x00, 0x95, 0x00, 0xca, 0xd0, 0xfb]
101 else:
102 raise NotImplementedError("Unknown output format: {}".format(options.output_format))
81 outputter = Outputter(options.output_format)
10382
10483 if options.origin is not None:
10584 if options.origin.startswith('0x'):
106 start_addr = int(options.origin, 16)
85 outputter.set_start_addr(int(options.origin, 16))
10786 else:
108 start_addr = int(options.origin, 10)
87 outputter.set_start_addr(int(options.origin, 10))
10988
110 # If we are outputting a .PRG, we output the load address first.
111 # We don't use the Emitter for this b/c not part of addr space.
112 if options.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'):
113 fh.write(bytearray(Word(start_addr).serialize(0)))
89 with open(options.output, 'wb') as fh:
90 # this is *awful* binding
91 emitter = outputter.write_prelude(fh)
92 compiler = Compiler(emitter)
93 compiler.compile_program(program, compilation_roster=compilation_roster)
94 outputter.write_postlude(emitter)
11495
115 emitter = Emitter(start_addr)
116 for byte in prelude:
117 emitter.emit(Byte(byte))
118 compiler = Compiler(emitter)
119 compiler.compile_program(program, compilation_roster=compilation_roster)
120
121 # If we are outputting a cartridge with boot and BRK address
122 # at the end, pad to ROM size minus 4 bytes, and emit addresses.
123 if options.output_format == 'atari2600-cart':
124 emitter.pad_to_size(4096 - 4)
125 emitter.emit(Word(start_addr))
126 emitter.emit(Word(start_addr))
127
128 if options.debug:
129 pprint(emitter.accum)
130 else:
131 emitter.serialize_to(fh)
96 if options.debug:
97 pprint(emitter.accum)
98 else:
99 emitter.serialize_to(fh)
132100
133101
134102 if __name__ == '__main__':
0 """Executable file writer."""
1
2 from sixtypical.emitter import Emitter, Byte, Word
3
4
5 class Outputter(object):
6 def __init__(self, output_format):
7 self.output_format = output_format
8 if output_format == 'raw':
9 self.start_addr = 0x0000
10 self.prelude = []
11 elif output_format == 'prg':
12 self.start_addr = 0xc000
13 self.prelude = []
14 elif output_format == 'c64-basic-prg':
15 self.start_addr = 0x0801
16 self.prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
17 0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
18 elif output_format == 'vic20-basic-prg':
19 self.start_addr = 0x1001
20 self.prelude = [0x0b, 0x10, 0xc9, 0x07, 0x9e, 0x34,
21 0x31, 0x30, 0x39, 0x00, 0x00, 0x00]
22 elif output_format == 'atari2600-cart':
23 self.start_addr = 0xf000
24 self.prelude = [0x78, 0xd8, 0xa2, 0xff, 0x9a, 0xa9,
25 0x00, 0x95, 0x00, 0xca, 0xd0, 0xfb]
26 else:
27 raise NotImplementedError("Unknown output format: {}".format(output_format))
28
29 def set_start_addr(self, start_addr):
30 self.start_addr = start_addr
31
32 def write_prelude(self, fh):
33
34 # If we are outputting a .PRG, we output the load address first.
35 # We don't use the Emitter for this b/c not part of addr space.
36 if self.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'):
37 fh.write(bytearray(Word(self.start_addr).serialize(0)))
38
39 emitter = Emitter(self.start_addr)
40 for byte in self.prelude:
41 emitter.emit(Byte(byte))
42
43 return emitter
44
45 def write_postlude(self, emitter):
46 # If we are outputting a cartridge with boot and BRK address
47 # at the end, pad to ROM size minus 4 bytes, and emit addresses.
48 if self.output_format == 'atari2600-cart':
49 emitter.pad_to_size(4096 - 4)
50 emitter.emit(Word(self.start_addr))
51 emitter.emit(Word(self.start_addr))