summaryrefslogtreecommitdiff
path: root/tools/dump_string.py
blob: 85787773a07cab39d9740f0174cddd0450ad5617 (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
#!/usr/bin/env python3

from sys import argv

file = open("DMGAKVJ0.1", "rb").read()

multistring = False
if argv[1] == "-m":
    argv.pop(1)
    multistring = True

bank = int(argv[1], 16)
addr = int(argv[2], 16)
count = 1
if len(argv) > 3:
    count = int(argv[3], 0)

offset = addr
if bank > 0:
    offset += 0x4000 * (bank - 1)

charmap = {}
for line in open("include/charmap.inc"):
    if line.startswith("charmap "):
        split = line.split(";")[0].split(" ", 1)[1].split(",")
        if len(split) != 2:
            continue

        char = split[0].strip()
        value = int(split[1].strip())

        if not char.startswith("\"") or not char.endswith("\""):
            continue
        char = char[1:-1]

        if value not in charmap:
            charmap[value] = char

for x in range(count):
    bank = offset // 0x4000
    addr = offset % 0x4000
    if bank > 0:
        addr += 0x4000

    print("string_%02x_%04x::" % (bank, addr))
    while True:
        if multistring:
            if file[offset] == 0:
                offset += 1
                print("    db 0\n")
                break
            val = file[offset] | (file[offset + 1] << 8)
            print("    dw %d" % val)
            offset += 2

        print("    text \"", end="")
        while True:
            value = file[offset] | (file[offset + 1] << 8)
            offset += 2

            if value == 0xffff:
                break
            elif value == 0xfffe:
                print("\"\n    line \"")
                continue

            if value in charmap:
                print(charmap[value], end="")
            else:
                print("<%02x>" % value, end="")

        print("\"\n    done")
        if not multistring:
            print()
            break