diff options
author | Rangi <remy.oukaour+rangi@gmail.com> | 2020-09-20 17:43:24 -0400 |
---|---|---|
committer | Rangi <remy.oukaour+rangi@gmail.com> | 2020-09-20 17:43:24 -0400 |
commit | 2baaf80989de573c9c8d44407e1de697b543127e (patch) | |
tree | 5a9f06386d5ee31c06a7276fe71f29c53ac2220c /tools/make_shim.py | |
parent | d6ae961c64799d30586d46af94d10128070d3ee5 (diff) |
Port simpler tools/make_shim.py from pokepicross
Diffstat (limited to 'tools/make_shim.py')
-rw-r--r-- | tools/make_shim.py | 124 |
1 files changed, 47 insertions, 77 deletions
diff --git a/tools/make_shim.py b/tools/make_shim.py index e88e3fc..fe620ec 100644 --- a/tools/make_shim.py +++ b/tools/make_shim.py @@ -1,79 +1,49 @@ #!/usr/bin/env python3 -# -*- coding: utf-8 -*- -from __future__ import print_function -import argparse -from sys import stderr -from collections import OrderedDict, namedtuple - -Section = namedtuple('Section', ('end', 'invalid', 'banked')) -section_list = OrderedDict(( - ('ROM0', Section(0x4000, False, False)), - ('ROMX', Section(0x8000, False, True)), - ('VRAM', Section(0xA000, False, True)), - ('SRAM', Section(0xC000, False, True)), - ('WRAM0', Section(0xD000, False, False)), - ('WRAMX', Section(0xE000, False, True)), - ('EchoRAM', Section(0xFE00, True, False)), - ('OAM', Section(0xFEA0, False, False)), - ('IO', Section(0xFF80, True, False)), - ('HRAM', Section(0xFFFF, False, False)) -)) - -parser = argparse.ArgumentParser() -parser.add_argument('files', nargs='+', type=argparse.FileType()) -parser.add_argument('-w', action='store_true') -parser.add_argument('-d', action='store_true') -parser.add_argument('-t', action='store_true') -args = parser.parse_args() - - -if args.w or args.d: - section_list['WRAM0'] = Section(0xE000, *section_list['WRAM0'][1:]) - -if args.t: - section_list['ROM0'] = Section(0x8000, *section_list['ROM0'][1:]) - - -for f in args.files: - for line in f: - - # Strip out the comment - line = line.split(";")[0].strip() - if not line: - continue - - # Read the address - try: - address, symbol = line.split() - bank, pointer = address.split(":") - bank = int(bank, 16) - pointer = int(pointer, 16) - except ValueError: - print("Error: Cannot parse line: %s" % line, file=stderr) - raise - - section = None - for name, section_type in section_list.items(): - if pointer < section_type.end: - if section_type.invalid: - print("Warning: cannot shim '%s' in section type '%s'" % (symbol, name), file=stderr) - section = False - else: - section = name - if not section_type.banked: - bank = None - break - else: - # Didn't find a section - print("Unknown section for '%s'" % line, file=stderr) - continue - - if not section: - # Found section, but cannot shim it - continue - - print("SECTION \"Shim for %s\", %s[$%04X]" % (symbol, section, pointer), end='') - if bank: - print(", BANK[$%04X]" % bank, end='') - print("\n%s::\n\n" % symbol) +from sys import argv, stderr + +def warn(s): + print("WARNING:", s.rstrip(), file=stderr) + +for i, line in enumerate(open(argv[1])): + content = line.split(";", 1)[0].strip() + + bankaddr_name = content.split(" ", 1) + if len(bankaddr_name) != 2: + continue + name = bankaddr_name[1].strip() + + bank_addr = bankaddr_name[0].split(":") + if len(bank_addr) != 2: + warn("Can't shim line %d: %s" % (i+1, line)) + continue + bank = int(bank_addr[0], 16) + addr = int(bank_addr[1], 16) + + if 0x0000 <= addr < 0x4000 and bank == 0: + section = "ROM0" + bank = None + elif 0x4000 <= addr < 0x8000 and 1 <= bank < 128: + section = "ROMX" + elif 0x8000 <= addr < 0xA000 and 0 <= bank < 2: + section = "VRAM" + elif 0xA000 <= addr < 0xC000 and 0 <= bank < 16: + section = "SRAM" + elif 0xC000 <= addr < 0xD000 and bank == 0: + section = "WRAM0" + bank = None + elif 0xD000 <= addr < 0xE000 and 0 <= bank < 16: + section = "WRAMX" + elif 0xFE00 <= addr < 0xFEA0 and bank == 0: + section = "OAM" + bank = None + elif 0xFF80 <= addr < 0xFFFF and bank == 0: + section = "HRAM" + bank = None + else: + warn("Invalid bank/address on line %d: %s" % (i+1, line)) + continue + + bankdec = ", BANK[$%x]" % bank if bank is not None else "" + print('SECTION "Shim %s", %s[$%x]%s' % (name, section, addr, bankdec)) + print("%s::" % name) |