def make_snapshot(fname, org, start=0, end=65536, page=None): snapshot_reader = get_snapshot_reader() if snapshot_reader.can_read(fname): return snapshot_reader.get_snapshot(fname, page), max(16384, start), end ram = read_bin_file(fname, 65536) if org is None: org = 65536 - len(ram) mem = [0] * 65536 mem[org:org + len(ram)] = ram return mem, max(org, start), min(end, org + len(ram))
def run(infile, outfile, options): snapshot_reader = get_snapshot_reader() if options.binary or options.org is not None or snapshot_reader.can_read( infile): snapshot = make_snapshot(infile, options.org)[0] elif infile[-4:].lower() == '.scr': snapshot = make_snapshot(infile, 16384)[0] else: try: snapshot = BinWriter(infile, fix_mode=options.fix_mode).snapshot except SkoolKitError: raise except: raise SkoolKitError( 'Unable to parse {} as a skool file'.format(infile)) for spec in options.moves: move(snapshot, spec) for spec in options.pokes: poke(snapshot, spec) if options.macro is not None: match = re.match('(#?)(FONT|SCR|UDG|UDGARRAY)([^A-Z]|$)', options.macro) if match: macro = match.group(2) try: frame = MACROS[macro](snapshot, options.macro[match.end(2):]) except skoolmacro.MacroParsingError as e: raise SkoolKitError('Invalid #{} macro: {}'.format( macro, e.args[0])) else: raise SkoolKitError('Macro must be #FONT, #SCR, #UDG or #UDGARRAY') else: (x, y), (w, h) = options.origin, options.size frame = Frame(scr_udgs(snapshot, x, y, w, h), options.scale) if options.invert: for row in frame.udgs: for udg in row: if udg.attr & 128: udg.data = [b ^ 255 for b in udg.data] udg.attr &= 127 flip_udgs(frame.udgs, options.flip) rotate_udgs(frame.udgs, options.rotate) _write_image(frame, outfile, options.animated)
def main(args): parser = argparse.ArgumentParser( usage='bin2tap.py [options] FILE [file.tap]', description= "Convert a binary (raw memory) file or a SNA, SZX or Z80 snapshot into a TAP file. " "FILE may be a regular file, or '-' to read a binary file from standard input.", add_help=False) parser.add_argument('infile', help=argparse.SUPPRESS, nargs='?') parser.add_argument('outfile', help=argparse.SUPPRESS, nargs='?') group = parser.add_argument_group('Options') group.add_argument( '-c', '--clear', dest='clear', metavar='N', type=integer, help= "Use a 'CLEAR N' command in the BASIC loader and leave the stack pointer alone." ) group.add_argument('-e', '--end', dest='end', metavar='ADDR', type=integer, default=65536, help="Set the end address when reading a snapshot.") group.add_argument( '-o', '--org', dest='org', metavar='ORG', type=integer, help= "Set the origin address (default: 16384 for a snapshot, otherwise 65536 minus the length of FILE)." ) group.add_argument('-p', '--stack', dest='stack', metavar='STACK', type=integer, help="Set the stack pointer (default: ORG).") group.add_argument('-s', '--start', dest='start', metavar='START', type=integer, help="Set the start address to JP to (default: ORG).") group.add_argument( '-S', '--screen', dest='screen', metavar='FILE', help= "Add a loading screen to the TAP file. FILE may be a snapshot or a 6912-byte SCR file." ) group.add_argument('-V', '--version', action='version', version='SkoolKit {}'.format(VERSION), help='Show SkoolKit version number and exit.') namespace, unknown_args = parser.parse_known_args(args) infile = namespace.infile if unknown_args or infile is None: parser.exit(2, parser.format_help()) snapshot_reader = get_snapshot_reader() if snapshot_reader.can_read(infile): org = namespace.org or 16384 if org >= namespace.end: raise SkoolKitError( 'End address must be greater than {}'.format(org)) ram = snapshot_reader.get_snapshot(infile)[org:namespace.end] else: ram = read_bin_file(infile, 49152) if not ram: raise SkoolKitError('{} is empty'.format(infile)) org = namespace.org or 65536 - len(ram) clear = namespace.clear start = namespace.start or org stack = namespace.stack or org tapfile = namespace.outfile if tapfile is None: if infile.lower().endswith(('.bin', '.sna', '.szx', '.z80')): prefix = os.path.basename(infile)[:-4] elif infile == '-': prefix = 'program' else: prefix = os.path.basename(infile) tapfile = prefix + ".tap" scr = namespace.screen if scr is not None: if snapshot_reader.can_read(scr): scr = snapshot_reader.get_snapshot(scr)[16384:23296] else: scr = read_bin_file(scr, 6912) run(ram, clear, org, start, stack, tapfile, scr)