-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_texture_memory.py
218 lines (189 loc) · 6.11 KB
/
parse_texture_memory.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from dataclasses import dataclass
import sys
import struct
from pprint import pprint, pformat
import textwrap
with open(sys.argv[1], "rb") as f:
mem = memoryview(f.read())
@dataclass
class buf_parser:
mem: memoryview
address: int
def u32(buf):
value, = struct.unpack("<I", buf.mem[buf.address:buf.address+4])
buf.address += 4
return value
def f32(buf):
value, = struct.unpack("<f", buf.mem[buf.address:buf.address+4])
buf.address += 4
return value
@dataclass
class tile:
flags: set[str]
x: int
y: int
def region_array_tile(value):
last_region = (value >> 31) & 1
z_clear = (value >> 30) & 1
pre_sort = (value >> 29) & 1
flush_accumulate = (value >> 28) & 1
tile_y_position = (value >> 8) & 0x3f
tile_x_position = (value >> 2) & 0x3f
flags = set()
if last_region:
flags.add("last_region")
if z_clear:
flags.add("z_clear")
if pre_sort:
flags.add("pre_sort")
if flush_accumulate:
flags.add("flush_accumulate")
return tile(flags, tile_x_position, tile_y_position)
@dataclass
class region_array_entry:
tile: int
opaque_list_pointer: int
modifier_volume_list_pointer: int
translucent_list_pointer: int
translucent_modifier_volume_list_pointer: int
punch_through_list_pointer: int
def __init__(self):
pass
def parse_region_array_entry(buf):
entry = region_array_entry()
entry.tile = region_array_tile(u32(buf))
entry.opaque_list_pointer = u32(buf)
entry.modifier_volume_list_pointer = u32(buf)
entry.translucent_list_pointer = u32(buf)
entry.translucent_modifier_volume_list_pointer = u32(buf)
entry.punch_through_list_pointer = u32(buf)
return entry
def parse_region_array(mem, address):
buf = buf_parser(mem, address)
while True:
entry = parse_region_array_entry(buf)
yield entry
if "last_region" in entry.tile.flags:
break
@dataclass
class object_list_array:
number: int
shadow: int
skip: int
start: int
def print_array(value):
number = (value >> 25) & 0b1111
shadow = (value >> 24) & 1
skip = (value >> 21) & 0b111
start = value & ((1 << 21) - 1)
print(f' number: {number}')
print(f' shadow: {shadow}')
print(f' skip: {skip}')
print(f' start: {start}')
return object_list_array(number, shadow, skip, start)
@dataclass
class isp_tsp_instruction_word:
depth_compare_mode: str
culling_mode: str
z_write_disable: int
texture: int
offset: int
gouraud_shading: int
_16bit_uv: int
cache_bypass: int
dcalc_ctrl: int
def parse_isp_tsp_instruction_word(value):
def depth_compare_mode(value):
value = (value >> 29) & 0b111
if value == 0: return "never"
if value == 1: return "less"
if value == 2: return "equal"
if value == 3: return "less_or_equal"
if value == 4: return "greater"
if value == 5: return "not_equal"
if value == 6: return "greater_or_equal"
if value == 7: return "always"
def culling_mode(value):
value = (value >> 27) & 0b11
if value == 0: return "no_culling"
if value == 1: return "cull_if_small"
if value == 2: return "cull_if_negative"
if value == 3: return "cull_if_positive"
z_write_disable = (value >> 26) & 1
texture = (value >> 25) & 1
offset = (value >> 24) & 1
gouraud_shading = (value >> 23) & 1
_16bit_uv = (value >> 22) & 1
cache_bypass = (value >> 21) & 1
dcalc_ctrl = (value >> 20) & 1
return isp_tsp_instruction_word(
depth_compare_mode(value),
culling_mode(value),
z_write_disable,
texture,
offset,
gouraud_shading,
_16bit_uv,
cache_bypass,
dcalc_ctrl,
)
def print_params(mem, ol_array, num_vertices):
# skip
# This field specifies the data size (* 32 bits) for one vertex in the
# ISP/TSP Parameters. Normally, the actual data size is "skip + 3,"
buf = buf_parser(mem, ol_array.start)
isp_tsp = u32(buf)
tsp = u32(buf)
texture = u32(buf)
print(textwrap.indent(pformat(parse_isp_tsp_instruction_word(isp_tsp)), ' '))
vertex_size = (ol_array.skip + 3) * 4
while num_vertices > 0:
start = buf.address
x = f32(buf)
y = f32(buf)
z = f32(buf)
base_color = u32(buf)
print(f"{x:7.3f} {y:7.3f} {z:7.3f} {base_color:x}")
buf.address = start + vertex_size
num_vertices -= 1
def parse_object_list_data(mem, value):
if ((value >> 31) & 1) == 0:
print(' triangle:')
elif ((value >> 29) & 0b111) == 0b100:
print(' triangle array:')
ol_array = print_array(value)
print_params(mem, ol_array, 3)
elif ((value >> 29) & 0b111) == 0b101:
print(' quad array:')
ol_array = print_array(value)
print_params(mem, ol_array, 4)
elif ((value >> 29) & 0b111) == 0b111:
print(' block link:')
end_of_list = (value >> 28) & 1
if end_of_list:
print(' [end of list]')
return end_of_list
else:
assert False, value
def parse_object_list(mem, address):
if (address & (1 << 31)) != 0:
return
buf = buf_parser(mem, address)
while True:
end_of_list = parse_object_list_data(mem, u32(buf))
if end_of_list:
break
def parse_object_lists(mem, region_array_entry):
print(region_array_entry.tile)
parse_object_list(mem, region_array_entry.opaque_list_pointer)
parse_object_list(mem, region_array_entry.modifier_volume_list_pointer)
parse_object_list(mem, region_array_entry.translucent_list_pointer)
parse_object_list(mem, region_array_entry.translucent_modifier_volume_list_pointer)
parse_object_list(mem, region_array_entry.punch_through_list_pointer)
region_array = 0x296000
framebuffer = 0x200000
region_array_entries = list(parse_region_array(mem, region_array))
for region_array_entry in region_array_entries:
parse_object_lists(mem, region_array_entry)
#with open('framebuffer.data', 'wb') as f:
# f.write(mem[framebuffer:framebuffer + 640 * 480 * 2])