Skip to content

Commit ccfb359

Browse files
authored
Match iQue compression (#2389)
* Match iQue compression * Use a dict * Use Makefile variables for compression settings * Put COMPRESS_ARGS last * Pad iQue ROMs to 16 KiB * Replace --pad-rom with --pad-to and --fill-padding-bytes * Clarify --fill-padding-bytes
1 parent 1662ac7 commit ccfb359

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,17 @@ endif
706706
$(ROM): $(ELF)
707707
$(ELF2ROM) -cic 6105 $< $@
708708

709+
ifeq ($(PLATFORM),IQUE)
710+
COMPRESS_ARGS := --format gzip --pad-to 0x4000
711+
CIC = 6102
712+
else
713+
COMPRESS_ARGS := --format yaz0 --pad-to 0x800000 --fill-padding-bytes
714+
CIC = 6105
715+
endif
716+
709717
$(ROMC): $(ROM) $(ELF) $(BUILD_DIR)/compress_ranges.txt
710-
$(PYTHON) tools/compress.py --in $(ROM) --out $@ --dmadata-start `./tools/dmadata_start.sh $(NM) $(ELF)` --compress `cat $(BUILD_DIR)/compress_ranges.txt` --threads $(N_THREADS)
711-
$(PYTHON) -m ipl3checksum sum --cic 6105 --update $@
718+
$(PYTHON) tools/compress.py --in $(ROM) --out $@ --dmadata-start `./tools/dmadata_start.sh $(NM) $(ELF)` --compress `cat $(BUILD_DIR)/compress_ranges.txt` --threads $(N_THREADS) $(COMPRESS_ARGS)
719+
$(PYTHON) -m ipl3checksum sum --cic $(CIC) --update $@
712720

713721
$(ELF): $(TEXTURE_FILES_OUT) $(ASSET_FILES_OUT) $(O_FILES) $(OVL_RELOC_FILES) $(LDSCRIPT) $(BUILD_DIR)/undefined_syms.txt \
714722
$(SAMPLEBANK_O_FILES) $(SOUNDFONT_O_FILES) $(SEQUENCE_O_FILES) \

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Setup and compression
2-
crunch64>=0.3.1,<1.0.0
2+
crunch64>=0.5.1,<1.0.0
33
ipl3checksum>=1.2.0,<2.0.0
44
pyyaml>=6.0.1,<7.0.0
55

tools/compress.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
import dmadata
1818

1919

20+
COMPRESSION_METHODS = {
21+
"yaz0": crunch64.yaz0.compress,
22+
"gzip": crunch64.gzip.compress,
23+
}
24+
25+
2026
def align(v: int):
2127
v += 0xF
2228
return v // 0x10 * 0x10
@@ -48,15 +54,24 @@ def compress_rom(
4854
rom_data: memoryview,
4955
dmadata_start: int,
5056
compress_entries_indices: set[int],
57+
compression_format: str,
58+
pad_to_multiple_of: int,
59+
fill_padding_bytes: bool,
5160
n_threads: int = None,
5261
):
5362
"""
5463
rom_data: the uncompressed rom data
5564
dmadata_start: the offset in the rom where the dmadata starts
5665
compress_entries_indices: the indices in the dmadata of the segments that should be compressed
66+
compression_format: the compression format to use
67+
pad_to_multiple_of: pad the compressed rom to a multiple of this size, in bytes
68+
fill_padding_bytes: fill the padding bytes with a 0x00 0x01 0x02 ... pattern instead of zeros
5769
n_threads: how many cores to use for compression
5870
"""
5971

72+
# Compression function
73+
compress = COMPRESSION_METHODS[compression_format]
74+
6075
# Segments of the compressed rom (not all are compressed)
6176
compressed_rom_segments: list[RomSegment] = []
6277

@@ -80,7 +95,7 @@ def compress_rom(
8095
if is_compressed:
8196
segment_data = None
8297
segment_data_async = p.apply_async(
83-
crunch64.yaz0.compress,
98+
compress,
8499
(bytes(segment_data_uncompressed),),
85100
)
86101
else:
@@ -149,7 +164,6 @@ def compress_rom(
149164
compressed_rom_size = sum(
150165
align(len(segment.data)) for segment in compressed_rom_segments
151166
)
152-
pad_to_multiple_of = 8 * 2**20 # 8 MiB
153167
compressed_rom_size_padded = (
154168
(compressed_rom_size + pad_to_multiple_of - 1)
155169
// pad_to_multiple_of
@@ -186,9 +200,10 @@ def compress_rom(
186200
)
187201

188202
assert rom_offset == compressed_rom_size
189-
# Pad the compressed rom with the pattern matching the baseroms
190-
for i in range(compressed_rom_size, compressed_rom_size_padded):
191-
compressed_rom_data[i] = i % 256
203+
if fill_padding_bytes:
204+
# Pad the compressed rom with the pattern matching the baseroms
205+
for i in range(compressed_rom_size, compressed_rom_size_padded):
206+
compressed_rom_data[i] = i % 256
192207

193208
# Write the new dmadata
194209
offset = dmadata_start
@@ -233,6 +248,25 @@ def main():
233248
" e.g. '0-1,3,5,6-9' is all indices from 0 to 9 (included) except 2 and 4."
234249
),
235250
)
251+
parser.add_argument(
252+
"--format",
253+
dest="format",
254+
choices=COMPRESSION_METHODS.keys(),
255+
default="yaz0",
256+
help="compression format to use (default: yaz0)",
257+
)
258+
parser.add_argument(
259+
"--pad-to",
260+
dest="pad_to",
261+
type=lambda s: int(s, 16),
262+
help="pad the compressed rom to a multiple of this size, in hex (e.g. 0x800000 for 8 MiB)",
263+
)
264+
parser.add_argument(
265+
"--fill-padding-bytes",
266+
dest="fill_padding_bytes",
267+
action="store_true",
268+
help="fill the padding bytes with a 0x00 0x01 0x02 ... pattern instead of zeros",
269+
)
236270
parser.add_argument(
237271
"--threads",
238272
dest="n_threads",
@@ -269,13 +303,19 @@ def main():
269303
range(compress_range_first, compress_range_last + 1)
270304
)
271305

306+
compression_format = args.format
307+
pad_to_multiple_of = args.pad_to
308+
fill_padding_bytes = args.fill_padding_bytes
272309
n_threads = args.n_threads
273310

274311
in_rom_data = in_rom_p.read_bytes()
275312
out_rom_data = compress_rom(
276313
memoryview(in_rom_data),
277314
dmadata_start,
278315
compress_entries_indices,
316+
compression_format,
317+
pad_to_multiple_of,
318+
fill_padding_bytes,
279319
n_threads,
280320
)
281321
out_rom_p.write_bytes(out_rom_data)

0 commit comments

Comments
 (0)