summaryrefslogtreecommitdiff
path: root/tools/make_shim.py
blob: 65d93624d7088cdd217f5f8aedaf69bbd7ffab9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python

from __future__ import print_function
import argparse
from sys import stderr
from collections import OrderedDict, namedtuple

Section = namedtuple('Section', ('end', 'invalud', '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'].end = 0xE000

if args.t:
    section_list['ROM0'].end = 0x8000


for file_name in args.files:
    for line in open(file_name, "rt"):

        # 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 from None

        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, section_type['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)