@@ -1467,7 +1467,8 @@ def assemble(input_):
14671467 return gl .has_errors
14681468
14691469
1470- def generate_binary (outputfname , format_ , progname = '' , binary_files = None , headless_binary_files = None ):
1470+ def generate_binary (outputfname , format_ , progname = '' , binary_files = None , headless_binary_files = None ,
1471+ emitter = None ):
14711472 """ Outputs the memory binary to the
14721473 output filename using one of the given
14731474 formats: tap, tzx or bin
@@ -1512,23 +1513,24 @@ def generate_binary(outputfname, format_, progname='', binary_files=None, headle
15121513 else :
15131514 program .add_line ([['REM' ], ['RANDOMIZE' , program .token ('USR' ), AUTORUN_ADDR ]])
15141515
1515- if format_ in ('tap' , 'tzx' ):
1516- t = {'tap' : outfmt .TAP , 'tzx' : outfmt .TZX }[format_ ]()
1517-
1518- if OPTIONS .use_loader .value :
1519- t .save_program ('loader' , program .bytes , line = 1 ) # Put line 0 to protect against MERGE
1520-
1521- t .save_code (progname , org , binary )
1522- for name , block in bin_blocks :
1523- t .save_code (name , 0 , block )
1524- for block in headless_bin_blocks :
1525- t .standard_block (block )
1526-
1527- t .dump (outputfname )
1516+ if emitter is None :
1517+ if format_ in ('tap' , 'tzx' ):
1518+ emitter = {'tap' : outfmt .TAP , 'tzx' : outfmt .TZX }[format_ ]()
1519+ else :
1520+ emitter = outfmt .BinaryEmitter ()
15281521
1529- else :
1530- with open (outputfname , 'wb' ) as f :
1531- f .write (bytearray (binary ))
1522+ loader_bytes = None
1523+ if OPTIONS .use_loader .value :
1524+ loader_bytes = program .bytes
1525+
1526+ assert isinstance (emitter , outfmt .CodeEmitter )
1527+ emitter .emit (output_filename = outputfname ,
1528+ program_name = progname ,
1529+ loader_bytes = loader_bytes ,
1530+ entry_point = AUTORUN_ADDR ,
1531+ program_bytes = binary ,
1532+ aux_bin_blocks = bin_blocks ,
1533+ aux_headless_bin_blocks = headless_bin_blocks )
15321534
15331535
15341536def main (argv ):
0 commit comments