forked from zeldaret/oot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmadata.py
83 lines (64 loc) · 2.15 KB
/
dmadata.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
# SPDX-FileCopyrightText: © 2024 ZeldaRET
# SPDX-License-Identifier: CC0-1.0
from __future__ import annotations
import dataclasses
import struct
STRUCT_IIII = struct.Struct(">IIII")
@dataclasses.dataclass
class DmaEntry:
"""
A Python counterpart to the dmadata entry struct:
```c
typedef struct {
/* 0x00 */ uintptr_t vromStart;
/* 0x04 */ uintptr_t vromEnd;
/* 0x08 */ uintptr_t romStart;
/* 0x0C */ uintptr_t romEnd;
} DmaEntry;
```
"""
vrom_start: int
vrom_end: int
rom_start: int
rom_end: int
def __repr__(self):
return (
"DmaEntry("
f"vrom_start=0x{self.vrom_start:08X}, "
f"vrom_end=0x{self.vrom_end:08X}, "
f"rom_start=0x{self.rom_start:08X}, "
f"rom_end=0x{self.rom_end:08X}"
")"
)
SIZE_BYTES = STRUCT_IIII.size
def to_bin(self, data: memoryview):
STRUCT_IIII.pack_into(
data,
0,
self.vrom_start,
self.vrom_end,
self.rom_start,
self.rom_end,
)
@staticmethod
def from_bin(data: memoryview):
return DmaEntry(*STRUCT_IIII.unpack_from(data))
def is_compressed(self) -> bool:
return self.rom_end != 0
def is_syms(self) -> bool:
"""
"SYMS" DMA entries describe segments that are always filled with 0's in the ROM.
The DMA entry has both rom_start and rom_end set to 0xFFFFFFFF, where the actual rom start and end is given by vrom_start and vrom_end instead.
These zeroed segments are used to record the offsets/sizes of subfiles in compressed yaz0 archive files but are not used by the game directly.
"""
return self.rom_start == 0xFFFFFFFF and self.rom_end == 0xFFFFFFFF
DMA_ENTRY_END = DmaEntry(0, 0, 0, 0)
def read_dmadata(rom_data: memoryview, start_offset: int) -> list[DmaEntry]:
result = []
offset = start_offset
while (
entry := DmaEntry.from_bin(rom_data[offset : offset + DmaEntry.SIZE_BYTES])
) != DMA_ENTRY_END:
result.append(entry)
offset += DmaEntry.SIZE_BYTES
return result