diff options
| author | Peter Fors <peter.fors@mindkiller.com> | 2025-04-28 22:24:09 +0200 |
|---|---|---|
| committer | Peter Fors <peter.fors@mindkiller.com> | 2025-04-28 22:24:09 +0200 |
| commit | 9463faa436e1b981ef72000568445a83682f2658 (patch) | |
| tree | 3aa7f59a2199cbe7df4555dc2b4963d1b5a5b86f | |
| parent | e08b851c79ae9a7fc0a2066e49110dc7fb426bce (diff) | |
add a few python scripts for mapper generation and setup
| -rw-r--r-- | .gitignore | 3 | ||||
| -rwxr-xr-x | mapper_add.py | 80 | ||||
| -rwxr-xr-x | mapper_scan.py | 38 |
3 files changed, 121 insertions, 0 deletions
@@ -6,3 +6,6 @@ TODO dump_mappers fragment_shader.h vertex_shader.h +*.fm2 +fm2h.py +gmon.out
\ No newline at end of file diff --git a/mapper_add.py b/mapper_add.py new file mode 100755 index 0000000..151506b --- /dev/null +++ b/mapper_add.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +import sys + +def generate_mapper_files(mapper_id, submapper_id=0): + mapper_id = int(mapper_id) + submapper_id = int(submapper_id) + + # Format: mapper_<decimal>_<hexsub> + full_id = f"{mapper_id}_{submapper_id:x}" + filename = f"mapper_{full_id}" + + # Generate C file content + c_content = f"""#include "{filename}.h" + +static uint8_t {filename}_prg_read(struct nes_state *state, uint32_t addr) {{ + return 0; +}} + +static void {filename}_prg_write(struct nes_state *state, uint32_t addr, uint8_t value) {{ +}} + +static uint8_t {filename}_chr_read(struct nes_state *state, uint32_t addr) {{ + return 0; +}} + +static void {filename}_chr_write(struct nes_state *state, uint32_t addr, uint8_t value) {{ +}} + +static uint8_t {filename}_ciram_read(struct nes_state *state, uint32_t addr) {{ + return 0; +}} + +static void {filename}_ciram_write(struct nes_state *state, uint32_t addr, uint8_t value) {{ +}} + +static void {filename}_tick(struct nes_state *state) {{ +}} + +void {filename}_init(struct nes_state *state) {{ + state->mapper.prg_read = {filename}_prg_read; + state->mapper.prg_write = {filename}_prg_write; + state->mapper.chr_read = {filename}_chr_read; + state->mapper.chr_write = {filename}_chr_write; + state->mapper.ciram_read = {filename}_ciram_read; + state->mapper.ciram_write = {filename}_ciram_write; + state->mapper.tick = {filename}_tick; +}} +""" + + # Generate H file content + h_content = f"""#pragma once + +struct {filename} {{ +}}; +void {filename}_init(struct nes_state *state); +""" + + # Only generate mapper ID -> init pointer + table_entry = f"""\t{{ 0x{mapper_id:x}, {filename}_init }},""" + + # Write files + with open(f"{filename}.c", "w") as f: + f.write(c_content) + + with open(f"{filename}.h", "w") as f: + f.write(h_content) + + print(f"Generated files: {filename}.c, {filename}.h") + print("\nTable entry (copy-paste this into your mapper table):") + print(table_entry) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python3 newmapper.py <mapper_id> [submapper_id=0]") + print("Example: python3 newmapper.py 3 0 # Creates mapper_3_0.*") + sys.exit(1) + + mapper_id = sys.argv[1] + submapper_id = sys.argv[2] if len(sys.argv) > 2 else 0 + generate_mapper_files(mapper_id, submapper_id) diff --git a/mapper_scan.py b/mapper_scan.py new file mode 100755 index 0000000..5b6e96a --- /dev/null +++ b/mapper_scan.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +import glob +import re + +def scan_mappers(): + mappers = [] + + # Find all mapper_*.c files + for filename in sorted(glob.glob("mapper_*.c")): + # Extract mapper ID from filename + match = re.match(r'mapper_(\d+)_([0-9a-fA-F])\.c', filename) + if match: + mapper_id = int(match.group(1)) + submapper_id = int(match.group(2), 16) + full_id = (mapper_id << 4) | submapper_id + else: + # Legacy format: mapper_xxx.c without submapper + match = re.match(r'mapper_(\d+)\.c', filename) + if match: + mapper_id = int(match.group(1)) + full_id = (mapper_id << 4) | 0 + else: + continue + + init_fn = f"mapper_{mapper_id:03d}_{submapper_id:x}_init" if 'submapper_id' in locals() else f"mapper_{mapper_id:03d}_init" + mappers.append((full_id, init_fn)) + + return mappers + +def generate_table(mappers): + print("static struct mapper_entry mapper_table[] = {") + for full_id, init_fn in mappers: + print(f"\t{{ 0x{full_id:04x}, {init_fn} }},") + print("};") + +if __name__ == "__main__": + mappers = scan_mappers() + generate_table(mappers) |
