Beispiel #1
0
def clock(quiet, prefix, operation, *args, **kwargs):
    go = time.time()
    result = operation(*args, **kwargs)
    stop = time.time()
    if not quiet:
        info('{} ({:0.2f}s)'.format(prefix, stop - go))
    return result
Beispiel #2
0
def run(skoolfile, options):
    # Read custom ASM templates
    t_parser = RefParser()
    if options.templates:
        t_parser.parse(options.templates, '#')
    templates = {t: t_parser.get_section(t, trim=False) for t in TEMPLATES if t_parser.has_section(t)}

    # Create the parser
    if skoolfile == '-':
        fname = 'stdin'
    else:
        fname = skoolfile
    asm_mode = options.asm_mode + 4 * int(options.force)
    parser = clock(options.quiet, 'Parsed {}'.format(fname), SkoolParser, skoolfile,
                   options.case, options.base, asm_mode, options.warn, options.fix_mode,
                   False, options.create_labels, True, options.start, options.end, options.variables)

    # Write the ASM file
    cls_name = options.writer or parser.asm_writer_class
    if cls_name:
        asm_writer_class = get_class(cls_name, os.path.dirname(skoolfile))
        if not options.quiet:
            info('Using ASM writer {0}'.format(cls_name))
    else:
        asm_writer_class = AsmWriter
    properties = dict(parser.properties)
    for spec in options.properties:
        name, sep, value = spec.partition('=')
        if sep:
            properties[name] = value
    if not options.warn:
        properties['warnings'] = '0'
    asm_writer = asm_writer_class(parser, properties, templates)
    clock(options.quiet, 'Wrote ASM to stdout', asm_writer.write)
Beispiel #3
0
 def _poke(self, instruction, data):
     address = instruction.real_address
     self.snapshot[address:address + len(data)] = data
     self.base_address = min(self.base_address, address)
     self.end_address = max(self.end_address, address + len(data))
     if self.verbose:
         info(str(instruction))
Beispiel #4
0
def clock(quiet, prefix, operation, *args, **kwargs):
    go = time.time()
    result = operation(*args, **kwargs)
    stop = time.time()
    if not quiet:
        info('{} ({:0.2f}s)'.format(prefix, stop - go))
    return result
Beispiel #5
0
def run(skoolfile, options):
    # Create the parser
    if skoolfile == '-':
        fname = 'stdin'
    else:
        fname = skoolfile
    parser = clock(options.quiet, 'Parsed {}'.format(fname), SkoolParser,
                   skoolfile, options.case, options.base, options.asm_mode,
                   options.warn, options.fix_mode, False,
                   options.create_labels, True, options.start, options.end)

    # Write the ASM file
    cls_name = options.writer or parser.asm_writer_class
    if cls_name:
        asm_writer_class = get_class(cls_name, os.path.dirname(skoolfile))
        if not options.quiet:
            info('Using ASM writer {0}'.format(cls_name))
    else:
        asm_writer_class = AsmWriter
    properties = dict(parser.properties)
    for spec in options.properties:
        name, sep, value = spec.partition('=')
        if sep:
            properties[name] = value
    if not options.warn:
        properties['warnings'] = '0'
    asm_writer = asm_writer_class(parser, properties)
    clock(options.quiet, 'Wrote ASM to stdout', asm_writer.write)
Beispiel #6
0
def run(skoolfile, options):
    # Create the parser
    if skoolfile == '-':
        fname = 'stdin'
    else:
        fname = skoolfile
    parser = clock(options.quiet, 'Parsed {}'.format(fname), SkoolParser, skoolfile,
                   options.case, options.base, options.asm_mode, options.warn, options.fix_mode,
                   False, options.create_labels, True, options.start, options.end)

    # Write the ASM file
    cls_name = options.writer or parser.asm_writer_class
    if cls_name:
        asm_writer_class = get_class(cls_name, os.path.dirname(skoolfile))
        if not options.quiet:
            info('Using ASM writer {0}'.format(cls_name))
    else:
        asm_writer_class = AsmWriter
    properties = dict(parser.properties)
    for spec in options.properties:
        name, sep, value = spec.partition('=')
        if sep:
            properties[name] = value
    if not options.warn:
        properties['warnings'] = '0'
    asm_writer = asm_writer_class(parser, properties, options.case == CASE_LOWER)
    clock(options.quiet, 'Wrote ASM to stdout', asm_writer.write)
Beispiel #7
0
def get_ctl_parser(ctls, infile, start, end, def_start, def_end):
    if infile[-4:].lower() in ('.bin', '.sna', '.szx', '.z80'):
        prefix = infile[:-4]
    else:
        prefix = infile
    if not ctls:
        ctls.extend(sorted(glob.glob(prefix + '*.ctl')))
    ctlfiles = []
    if ctls and '0' not in ctls:
        # Use control file(s)
        for ctl in ctls:
            if os.path.isdir(ctl):
                ctlfiles.extend(sorted(glob.glob(os.path.join(ctl, '*.ctl'))))
            else:
                ctlfiles.append(ctl)
    if ctlfiles:
        if len(ctlfiles) > 1:
            suffix = 's'
        else:
            suffix = ''
        info('Using control file{}: {}'.format(suffix, ', '.join(ctlfiles)))
        ctl_parser = CtlParser()
        ctl_parser.parse_ctls(ctlfiles, start, end)
    else:
        ctl_parser = CtlParser({def_start: 'c', def_end: 'i'})
    return ctl_parser
Beispiel #8
0
def run(skoolfile, options):
    # Read custom ASM templates
    t_parser = RefParser()
    if options.templates:
        t_parser.parse(options.templates, '#')
    templates = {t: t_parser.get_section(t, trim=False) for t in TEMPLATES if t_parser.has_section(t)}

    # Create the parser
    if skoolfile == '-':
        fname = 'stdin'
    else:
        fname = skoolfile
    asm_mode = options.asm_mode + 4 * int(options.force)
    parser = clock(options.quiet, 'Parsed {}'.format(fname), SkoolParser, skoolfile,
                   options.case, options.base, asm_mode, options.warn, options.fix_mode,
                   False, options.create_labels, True, options.start, options.end, options.variables)

    # Write the ASM file
    cls_name = options.writer or parser.asm_writer_class
    if cls_name:
        asm_writer_class = get_object(cls_name, os.path.dirname(skoolfile))
        if not options.quiet:
            info('Using ASM writer {0}'.format(cls_name))
    else:
        asm_writer_class = AsmWriter
    properties = dict(parser.properties)
    for spec in options.properties:
        name, sep, value = spec.partition('=')
        if sep:
            properties[name] = value
    if not options.warn:
        properties['warnings'] = '0'
    asm_writer = asm_writer_class(parser, properties, templates)
    clock(options.quiet, 'Wrote ASM to stdout', asm_writer.write)
Beispiel #9
0
 def write(self, binfile, start, end):
     if start is None:
         base_address = self.base_address
     else:
         base_address = start
     if end is None:
         end_address = self.end_address
     else:
         end_address = end
     data = self.snapshot[base_address:end_address]
     with open_file(binfile, 'wb') as f:
         f.write(bytearray(data))
     if binfile == '-':
         binfile = 'stdout'
     info("Wrote {}: start={}, end={}, size={}".format(binfile, base_address, end_address, len(data)))
Beispiel #10
0
 def write(self, binfile, start, end):
     if start is None:
         base_address = self.base_address
     else:
         base_address = start
     if end is None:
         end_address = self.end_address
     else:
         end_address = end
     data = self.snapshot[base_address:end_address]
     with open_file(binfile, 'wb') as f:
         f.write(bytearray(data))
     if binfile == '-':
         binfile = 'stdout'
     info("Wrote {}: start={}, end={}, size={}".format(binfile, base_address, end_address, len(data)))
Beispiel #11
0
def run(snafile, options, config):
    snapshot, start, end = make_snapshot(snafile, options.org, options.start, options.end, options.page)

    if options.ctlfiles:
        # Use control file(s)
        if len(options.ctlfiles) > 1:
            suffix = 's'
        else:
            suffix = ''
        info('Using control file{}: {}'.format(suffix, ', '.join(options.ctlfiles)))
        ctl_parser = CtlParser()
        ctl_parser.parse_ctls(options.ctlfiles, options.start, options.end)
    else:
        ctl_parser = CtlParser({start: 'c', end: 'i'})
    writer = SkoolWriter(snapshot, ctl_parser, options, config)
    writer.write_skool(config['ListRefs'], config['Text'])
Beispiel #12
0
 def write(self, binfile):
     if self.start < 0:
         base_address = self.base_address
     else:
         base_address = max(self.start, self.base_address)
     if self.end > 65536:
         end_address = self.end_address
     else:
         end_address = min(self.end, self.end_address)
     base_address = min(base_address, end_address)
     data = self.snapshot[base_address:end_address]
     with open_file(binfile, 'wb') as f:
         f.write(bytearray(data))
     if binfile == '-':
         binfile = 'stdout'
     info("Wrote {}: start={}, end={}, size={}".format(
         binfile, base_address, end_address, len(data)))
Beispiel #13
0
def run(snafile, options, config):
    words = set()
    dict_fname = config['Dictionary']
    if dict_fname:
        if find_file(dict_fname):
            info("Using dictionary file: {}".format(dict_fname))
            with open_file(config['Dictionary']) as f:
                for line in f:
                    word = line.strip().lower()
                    if word:
                        words.add(word)
        else:
            info("Dictionary file '{}' not found".format(dict_fname))
    ctl_config = Config(config['TextChars'], config['TextMinLengthCode'], config['TextMinLengthData'], words)
    snapshot, start, end = make_snapshot(snafile, options.org, options.start, options.end, options.page)
    ctls = get_component('ControlFileGenerator').generate_ctls(snapshot, start, end, options.code_map, ctl_config)
    write_ctl(ctls, options.ctl_hex)
Beispiel #14
0
def run(snafile, options):
    # Read the snapshot file
    if snafile[-4:].lower() in ('.sna', '.szx', '.z80'):
        snapshot = get_snapshot(snafile, options.page)
        start = max(START, options.start)
    else:
        ram = read_bin_file(snafile)
        if options.org is None:
            org = 65536 - len(ram)
        else:
            org = options.org
        snapshot = [0] * org
        snapshot.extend(ram)
        start = max(org, options.start)
    end = min(options.end, len(snapshot))

    # Pad out the end of the snapshot to avoid disassembly errors when an
    # instruction crosses the 64K boundary
    snapshot += [0] * (65539 - len(snapshot))

    if options.sftfile:
        # Use a skool file template
        info('Using skool file template: {}'.format(options.sftfile))
        writer = SftParser(snapshot, options.sftfile, options.zfill, options.asm_hex, options.asm_lower)
        writer.write_skool(options.start, options.end)
        return

    if options.genctlfile:
        # Generate a control file
        ctls = generate_ctls(snapshot, start, end, options.code_map)
        write_ctl(options.genctlfile, ctls, options.ctl_hex)
        ctl_parser = CtlParser(ctls)
    elif options.ctlfile:
        # Use a control file
        info('Using control file: {}'.format(options.ctlfile))
        ctl_parser = CtlParser()
        ctl_parser.parse_ctl(options.ctlfile, options.start, options.end)
    else:
        ctl_parser = CtlParser({start: 'c', end: 'i'})
    writer = SkoolWriter(snapshot, ctl_parser, options)
    writer.write_skool(options.write_refs, options.text)
Beispiel #15
0
def run(snafile, options, config):
    # Read the snapshot file
    if snafile[-4:].lower() in ('.sna', '.szx', '.z80'):
        snapshot = get_snapshot(snafile, options.page)
        start = max(START, options.start)
    else:
        ram = read_bin_file(snafile, 65536)
        if options.org is None:
            org = 65536 - len(ram)
        else:
            org = options.org
        snapshot = [0] * org
        snapshot.extend(ram)
        start = max(org, options.start)
    end = min(options.end, len(snapshot))

    snapshot += [0] * (65536 - len(snapshot))

    if options.sftfile:
        # Use a skool file template
        info('Using skool file template: {}'.format(options.sftfile))
        writer = SftParser(snapshot, options.sftfile, options.zfill,
                           options.base == 16, options.case == 1)
        writer.write_skool(options.start, options.end)
        return

    if options.genctlfile:
        # Generate a control file
        ctls = generate_ctls(snapshot, start, end, options.code_map)
        write_ctl(options.genctlfile, ctls, options.ctl_hex)
        ctl_parser = CtlParser(ctls)
    elif options.ctlfile:
        # Use a control file
        info('Using control file: {}'.format(options.ctlfile))
        ctl_parser = CtlParser()
        ctl_parser.parse_ctl(options.ctlfile, options.start, options.end)
    else:
        ctl_parser = CtlParser({start: 'c', end: 'i'})
    writer = SkoolWriter(snapshot, ctl_parser, options, config)
    writer.write_skool(options.write_refs, options.text)
Beispiel #16
0
def run(snafile, options, config):
    snapshot, start, end = make_snapshot(snafile, options.org, options.start, options.end, options.page)

    if options.sftfile:
        # Use a skool file template
        info('Using skool file template: {}'.format(options.sftfile))
        writer = SftParser(snapshot, options.sftfile, config['DefbZfill'], options.base == 16, options.case == 1)
        writer.write_skool(options.start, options.end)
        return

    if options.ctlfiles:
        # Use control file(s)
        if len(options.ctlfiles) > 1:
            suffix = 's'
        else:
            suffix = ''
        info('Using control file{}: {}'.format(suffix, ', '.join(options.ctlfiles)))
        ctl_parser = CtlParser()
        ctl_parser.parse_ctls(options.ctlfiles, options.start, options.end)
    else:
        ctl_parser = CtlParser({start: 'c', end: 'i'})
    writer = SkoolWriter(snapshot, ctl_parser, options, config)
    writer.write_skool(config['ListRefs'], config['Text'])