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
|
#!/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_rom_read(struct nes_state *state, uint32_t addr) {{
return 0;
}}
static void {filename}_prg_rom_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_rom_read = {filename}_prg_read;
state->mapper.prg_rom_write = {filename}_prg_rom_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)
|