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

# Usage: ./dump_decompress.py 75:7d35 00b6 >compressed.bin 2>decompressed.bin

from sys import argv, stdout, stderr

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

bank_addr = argv[1].split(':')
bank = int(bank_addr[0], 16)
addr = int(bank_addr[1], 16)
length = int(argv[2], 16)
offset = bank * 0x4000 + addr - (0x4000 if bank else 0)

output = []
i = offset
v = 0x00
for _1 in range(length):
    byte = file[i]
    i += 1
    if byte < 0x80:
        for _2 in range(byte + 1):
            v ^= file[i]
            output.append(v)
            i += 1
    else:
        for _2 in range(byte - 0x7e):
            v ^= file[i]
            output.append(v)
        i += 1

stdout.buffer.write(bytes(file[offset:i])) # compressed
stderr.buffer.write(bytes(output)) # decompressed