forked from pkmncoraldev/polishedcoral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank_ends.py
53 lines (43 loc) · 1.42 KB
/
bank_ends.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Find free space per bank.
Taken from Pokémon Prism.
"""
from __future__ import print_function
import sys
bank_diff = 0x4000
num_banks = 0x80
bank_ends = []
bank_space = []
rom_name = sys.argv[1]
with open(rom_name + '.gbc', 'rb') as f1, open(rom_name + '-0xff.gbc', 'rb') as f2:
for bank in range(num_banks):
empty_bank = True
for i in range(bank_diff):
f1.seek((bank + 1) * bank_diff - i - 1)
f2.seek((bank + 1) * bank_diff - i - 1)
if f1.read(1) == f2.read(1):
bank_space.append(i)
bank_ends.append(bank_diff * (1 + (bank != 0)) - i)
empty_bank = False
break
if empty_bank:
bank_space.append(bank_diff)
bank_ends.append(bank_diff)
print('''
> Back in the day, the programming used up all the space on a cartridge, so if
> we wanted to add something new, we had to clean it up first... First we'd
> find enough space, then we could make what we wanted... It was a pain!
>
> -- Morimoto, Pokémon Ultra Sun/Ultra Moon
> <https://www.serebii.net/ultrasunultramoon/virtualconsole.shtml>'''[1:])
print()
total_size = bank_diff * num_banks
free_space = sum(bank_space)
pct_free = free_space * 100.0 / total_size
print('Free space: {:.0f}/{:.0f} ({:.2f}%)'.format(free_space, total_size, pct_free))
print()
print('bank\tend\tfree')
for bank, end, space in zip(range(num_banks), bank_ends, bank_space):
print('${:02x}\t${:04x}\t${:04x}'.format(bank, end, space))