1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2024-11-25 07:32:16 +00:00

Awful binding, but at least this code isn't in main anymore.

This commit is contained in:
Chris Pressey 2018-09-06 18:25:29 +01:00
parent 24f093b1db
commit 99a0d5624a
2 changed files with 66 additions and 46 deletions

View File

@ -20,7 +20,7 @@ import traceback
from sixtypical.parser import Parser, ParsingContext from sixtypical.parser import Parser, ParsingContext
from sixtypical.analyzer import Analyzer from sixtypical.analyzer import Analyzer
from sixtypical.emitter import Emitter, Byte, Word from sixtypical.outputter import Outputter
from sixtypical.compiler import Compiler from sixtypical.compiler import Compiler
@ -79,57 +79,25 @@ def process_input_files(filenames, options):
if options.analyze_only: if options.analyze_only:
return return
fh = open(options.output, 'wb') outputter = Outputter(options.output_format)
if options.output_format == 'raw':
start_addr = 0x0000
prelude = []
elif options.output_format == 'prg':
start_addr = 0xc000
prelude = []
elif options.output_format == 'c64-basic-prg':
start_addr = 0x0801
prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
elif options.output_format == 'vic20-basic-prg':
start_addr = 0x1001
prelude = [0x0b, 0x10, 0xc9, 0x07, 0x9e, 0x34,
0x31, 0x30, 0x39, 0x00, 0x00, 0x00]
elif options.output_format == 'atari2600-cart':
start_addr = 0xf000
prelude = [0x78, 0xd8, 0xa2, 0xff, 0x9a, 0xa9,
0x00, 0x95, 0x00, 0xca, 0xd0, 0xfb]
else:
raise NotImplementedError("Unknown output format: {}".format(options.output_format))
if options.origin is not None: if options.origin is not None:
if options.origin.startswith('0x'): if options.origin.startswith('0x'):
start_addr = int(options.origin, 16) outputter.set_start_addr(int(options.origin, 16))
else: else:
start_addr = int(options.origin, 10) outputter.set_start_addr(int(options.origin, 10))
# If we are outputting a .PRG, we output the load address first. with open(options.output, 'wb') as fh:
# We don't use the Emitter for this b/c not part of addr space. # this is *awful* binding
if options.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'): emitter = outputter.write_prelude(fh)
fh.write(bytearray(Word(start_addr).serialize(0))) compiler = Compiler(emitter)
compiler.compile_program(program, compilation_roster=compilation_roster)
outputter.write_postlude(emitter)
emitter = Emitter(start_addr) if options.debug:
for byte in prelude: pprint(emitter.accum)
emitter.emit(Byte(byte)) else:
compiler = Compiler(emitter) emitter.serialize_to(fh)
compiler.compile_program(program, compilation_roster=compilation_roster)
# If we are outputting a cartridge with boot and BRK address
# at the end, pad to ROM size minus 4 bytes, and emit addresses.
if options.output_format == 'atari2600-cart':
emitter.pad_to_size(4096 - 4)
emitter.emit(Word(start_addr))
emitter.emit(Word(start_addr))
if options.debug:
pprint(emitter.accum)
else:
emitter.serialize_to(fh)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -0,0 +1,52 @@
"""Executable file writer."""
from sixtypical.emitter import Emitter, Byte, Word
class Outputter(object):
def __init__(self, output_format):
self.output_format = output_format
if output_format == 'raw':
self.start_addr = 0x0000
self.prelude = []
elif output_format == 'prg':
self.start_addr = 0xc000
self.prelude = []
elif output_format == 'c64-basic-prg':
self.start_addr = 0x0801
self.prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
elif output_format == 'vic20-basic-prg':
self.start_addr = 0x1001
self.prelude = [0x0b, 0x10, 0xc9, 0x07, 0x9e, 0x34,
0x31, 0x30, 0x39, 0x00, 0x00, 0x00]
elif output_format == 'atari2600-cart':
self.start_addr = 0xf000
self.prelude = [0x78, 0xd8, 0xa2, 0xff, 0x9a, 0xa9,
0x00, 0x95, 0x00, 0xca, 0xd0, 0xfb]
else:
raise NotImplementedError("Unknown output format: {}".format(output_format))
def set_start_addr(self, start_addr):
self.start_addr = start_addr
def write_prelude(self, fh):
# If we are outputting a .PRG, we output the load address first.
# We don't use the Emitter for this b/c not part of addr space.
if self.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'):
fh.write(bytearray(Word(self.start_addr).serialize(0)))
emitter = Emitter(self.start_addr)
for byte in self.prelude:
emitter.emit(Byte(byte))
return emitter
def write_postlude(self, emitter):
# If we are outputting a cartridge with boot and BRK address
# at the end, pad to ROM size minus 4 bytes, and emit addresses.
if self.output_format == 'atari2600-cart':
emitter.pad_to_size(4096 - 4)
emitter.emit(Word(self.start_addr))
emitter.emit(Word(self.start_addr))