-
Notifications
You must be signed in to change notification settings - Fork 0
/
rom.py
90 lines (51 loc) · 1.9 KB
/
rom.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
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
# rom.py
import re
named_pointers = {}
def get_rom_address(data = None):
ptr = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)
return ptr & 0x1ffffff
def make_pointer(data = None, label="gUnknown"):
# 0x1ffffff
#ptr = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[4] << 24)
ptr = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)
return label + "_8" + hex(ptr & 0x1ffffff).replace("0x", "").upper()
def get_function_ptr(data=None):
addr = read_word(data)
if addr == 0:
return "0x0"
else:
addr -= 1
with open("pokefirered.map", "r") as map_file:
map_text = map_file.read()
addr_label = "0x0{:x}".format(addr)
regex = re.compile("\\s+{}\\s+(?P<func>[A-Za-z0-9_]*)".format(addr_label))
match = regex.search(map_text)
if match is not None:
func = match.group("func")
print("found function pointer {} in map file at 0x0{:X}".format(func, addr))
else:
func = "sub_{:X}".format(addr)
return func
def make_word(data = None):
word = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)
return hex(word).upper().replace("X", "x")
def make_half(data=None):
half = (data[0] << 0) | (data[1] << 8)
return hex(half).replace("X", "x")
def make_byte(data=None):
return hex(data).replace("X", "x")
def read_word(data = None):
word = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)
return word
def read_half(data=None):
half = (data[0] << 0) | (data[1] << 8)
return half
def read_byte(data=None):
try: return int(data[0])
except: return int(data)
def ptr_from_addr(address=0x0, label="gUnknown", suffix=""):
if address == 0:
return "NULL"
label = label + "_8" + hex(address).replace("0x", "").upper() + suffix
named_pointers[address] = label
return label