summaryrefslogtreecommitdiff
path: root/tools/make_shim.py
blob: d5e6e00bb35f68fc3cb6a2b31b97085895a88285 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python

from __future__ import print_function
from sys import argv, stderr, exit


section_list = [
    {'end': 0x4000, 'invalid': False, 'name': "ROM0",  'banked': False},
    {'end': 0x8000, 'invalid': False, 'name': "ROMX",  'banked': True},
    {'end': 0xA000, 'invalid': False, 'name': "VRAM",  'banked': True},
    {'end': 0xC000, 'invalid': False, 'name': "SRAM",  'banked': True},
    {'end': 0xD000, 'invalid': False, 'name': "WRAM0", 'banked': False},
    {'end': 0xE000, 'invalid': False, 'name': "WRAMX", 'banked': True},
    {'end': 0xFE00, 'invalid': True , 'name': "Echo RAM"},
    {'end': 0xFEA0, 'invalid': False, 'name': "OAM",   'banked': False},
    {'end': 0xFF80, 'invalid': True , 'name': "FEXX / IO"},
    {'end': 0xFFFF, 'invalid': False, 'name': "HRAM",  'banked': False}
]


argv_id = 1
file_list = []
options = []
while argv_id < len(argv):
    arg = argv[argv_id]

    if arg[0] != '-':
        file_list.append(arg)
        argv_id += 1
        continue

    # An empty '--' stops parsing arguments
    if arg == '--':
        argv_id += 1
        break

    if arg[1] == '-':
        options.append(option[2:])
    elif arg[1] != '-':
        for option in arg[1:]:
            options.append(option)

    argv_id += 1

# Add remaining files to the list
for arg in argv[argv_id:]:
    file_list.append(arg)


if 'w' in options or 'd' in options:
    section_list[4]['end'] = 0xE000

if 't' in options:
    section_list[0]['end'] = 0x8000


for file_name in file_list:
    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:
            print("Error: Cannot parse line: %s" % line, file=stderr)
            exit(1)

        section = None
        for section_type in section_list:
            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 = section_type['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)