a12b1b29 |
1 | /* |
65ca3034 |
2 | * Support for a few cart mappers. |
3 | * |
45f2f245 |
4 | * (c) Copyright 2008-2009, Grazvydas "notaz" Ignotas |
65ca3034 |
5 | * Free for non-commercial use. |
6 | * |
a12b1b29 |
7 | */ |
8 | |
efcba75f |
9 | #include "../pico_int.h" |
45f2f245 |
10 | #include "../memory.h" |
757f8dae |
11 | |
a12b1b29 |
12 | |
45f2f245 |
13 | /* Common *-in-1 pirate mapper. |
14 | * Switches banks based on addr lines when /TIME is set. |
15 | * TODO: verify |
16 | */ |
17 | static unsigned int carthw_Xin1_baddr = 0; |
757f8dae |
18 | |
45f2f245 |
19 | static void carthw_Xin1_do(u32 a, int mask, int shift) |
757f8dae |
20 | { |
21 | int len; |
22 | |
45f2f245 |
23 | carthw_Xin1_baddr = a; |
24 | a &= mask; |
25 | a <<= shift; |
757f8dae |
26 | len = Pico.romsize - a; |
27 | if (len <= 0) { |
45f2f245 |
28 | elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a); |
757f8dae |
29 | return; |
30 | } |
31 | |
45f2f245 |
32 | len = (len + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
33 | cpu68k_map_set(m68k_read8_map, 0x000000, len - 1, Pico.rom + a, 0); |
34 | cpu68k_map_set(m68k_read16_map, 0x000000, len - 1, Pico.rom + a, 0); |
757f8dae |
35 | } |
36 | |
45f2f245 |
37 | static carthw_state_chunk carthw_Xin1_state[] = |
757f8dae |
38 | { |
45f2f245 |
39 | { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr }, |
40 | { 0, 0, NULL } |
41 | }; |
42 | |
43 | // TODO: test a0, reads, w16 |
44 | static void carthw_Xin1_write8(u32 a, u32 d) |
45 | { |
46 | if ((a & 0xffff00) != 0xa13000) { |
47 | PicoWrite8_io(a, d); |
48 | return; |
49 | } |
50 | |
51 | carthw_Xin1_do(a, 0x3f, 16); |
757f8dae |
52 | } |
53 | |
45f2f245 |
54 | static void carthw_Xin1_mem_setup(void) |
bdec53c9 |
55 | { |
45f2f245 |
56 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1); |
bdec53c9 |
57 | } |
58 | |
45f2f245 |
59 | static void carthw_Xin1_reset(void) |
757f8dae |
60 | { |
45f2f245 |
61 | carthw_Xin1_write8(0xa13000, 0); |
62 | } |
757f8dae |
63 | |
45f2f245 |
64 | static void carthw_Xin1_statef(void) |
65 | { |
66 | carthw_Xin1_write8(carthw_Xin1_baddr, 0); |
67 | } |
757f8dae |
68 | |
45f2f245 |
69 | void carthw_Xin1_startup(void) |
70 | { |
71 | elprintf(EL_STATUS, "X-in-1 mapper startup"); |
757f8dae |
72 | |
45f2f245 |
73 | PicoCartMemSetup = carthw_Xin1_mem_setup; |
74 | PicoResetHook = carthw_Xin1_reset; |
75 | PicoLoadStateHook = carthw_Xin1_statef; |
76 | carthw_chunks = carthw_Xin1_state; |
757f8dae |
77 | } |
78 | |
a12b1b29 |
79 | |
80 | /* Realtec, based on TascoDLX doc |
81 | * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt |
82 | */ |
83 | static int realtec_bank = 0x80000000, realtec_size = 0x80000000; |
a12b1b29 |
84 | |
45f2f245 |
85 | static void carthw_realtec_write8(u32 a, u32 d) |
a12b1b29 |
86 | { |
87 | int i, bank_old = realtec_bank, size_old = realtec_size; |
88 | |
89 | if (a == 0x400000) |
90 | { |
91 | realtec_bank &= 0x0e0000; |
92 | realtec_bank |= 0x300000 & (d << 19); |
93 | if (realtec_bank != bank_old) |
94 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
95 | } |
96 | else if (a == 0x402000) |
97 | { |
98 | realtec_size = (d << 17) & 0x3e0000; |
99 | if (realtec_size != size_old) |
100 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
101 | } |
102 | else if (a == 0x404000) |
103 | { |
104 | realtec_bank &= 0x300000; |
105 | realtec_bank |= 0x0e0000 & (d << 17); |
106 | if (realtec_bank != bank_old) |
107 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
108 | } |
109 | else |
110 | elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc); |
65ca3034 |
111 | |
a12b1b29 |
112 | if (realtec_bank >= 0 && realtec_size >= 0 && |
113 | (realtec_bank != bank_old || realtec_size != size_old)) |
114 | { |
115 | elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc); |
45f2f245 |
116 | if (realtec_size > Pico.romsize - realtec_bank) |
a12b1b29 |
117 | { |
118 | elprintf(EL_ANOMALY, "realtec: bank too large / out of range?"); |
119 | return; |
120 | } |
121 | |
45f2f245 |
122 | for (i = 0; i < 0x400000; i += realtec_size) { |
123 | cpu68k_map_set(m68k_read8_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0); |
124 | cpu68k_map_set(m68k_read16_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0); |
125 | } |
a12b1b29 |
126 | } |
127 | } |
128 | |
0b35350d |
129 | static void carthw_realtec_reset(void) |
a12b1b29 |
130 | { |
131 | int i; |
45f2f245 |
132 | |
a12b1b29 |
133 | /* map boot code */ |
45f2f245 |
134 | for (i = 0; i < 0x400000; i += M68K_BANK_SIZE) { |
135 | cpu68k_map_set(m68k_read8_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0); |
136 | cpu68k_map_set(m68k_read16_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0); |
137 | } |
138 | cpu68k_map_set(m68k_write8_map, 0x400000, 0x400000 + M68K_BANK_SIZE - 1, carthw_realtec_write8, 1); |
a12b1b29 |
139 | realtec_bank = realtec_size = 0x80000000; |
140 | } |
141 | |
142 | void carthw_realtec_startup(void) |
143 | { |
144 | void *tmp; |
45f2f245 |
145 | int i; |
a12b1b29 |
146 | |
45f2f245 |
147 | elprintf(EL_STATUS, "Realtec mapper startup"); |
a12b1b29 |
148 | |
45f2f245 |
149 | // allocate additional bank for boot code |
150 | // (we know those ROMs have aligned size) |
151 | tmp = realloc(Pico.rom, Pico.romsize + M68K_BANK_SIZE); |
a12b1b29 |
152 | if (tmp == NULL) |
153 | { |
154 | elprintf(EL_STATUS, "OOM"); |
155 | return; |
156 | } |
157 | Pico.rom = tmp; |
a12b1b29 |
158 | |
45f2f245 |
159 | // create bank for boot code |
160 | for (i = 0; i < M68K_BANK_SIZE; i += 0x2000) |
161 | memcpy(Pico.rom + Pico.romsize + i, Pico.rom + Pico.romsize - 0x2000, 0x2000); |
162 | |
a12b1b29 |
163 | PicoResetHook = carthw_realtec_reset; |
164 | } |
165 | |
0b35350d |
166 | /* Radica mapper, based on DevSter's info |
167 | * http://devster.monkeeh.com/sega/radica/ |
45f2f245 |
168 | * XXX: mostly the same as X-in-1, merge? |
0b35350d |
169 | */ |
45f2f245 |
170 | static u32 carthw_radica_read16(u32 a) |
0b35350d |
171 | { |
45f2f245 |
172 | if ((a & 0xffff00) != 0xa13000) |
173 | return PicoRead16_io(a); |
0b35350d |
174 | |
45f2f245 |
175 | carthw_Xin1_do(a, 0x7e, 15); |
0b35350d |
176 | |
177 | return 0; |
178 | } |
179 | |
45f2f245 |
180 | static void carthw_radica_mem_setup(void) |
181 | { |
182 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1); |
183 | } |
184 | |
0b35350d |
185 | static void carthw_radica_statef(void) |
186 | { |
45f2f245 |
187 | carthw_radica_read16(carthw_Xin1_baddr); |
0b35350d |
188 | } |
189 | |
190 | static void carthw_radica_reset(void) |
191 | { |
45f2f245 |
192 | carthw_radica_read16(0xa13000); |
0b35350d |
193 | } |
194 | |
195 | void carthw_radica_startup(void) |
196 | { |
45f2f245 |
197 | elprintf(EL_STATUS, "Radica mapper startup"); |
0b35350d |
198 | |
45f2f245 |
199 | PicoCartMemSetup = carthw_radica_mem_setup; |
200 | PicoResetHook = carthw_radica_reset; |
0b35350d |
201 | PicoLoadStateHook = carthw_radica_statef; |
45f2f245 |
202 | carthw_chunks = carthw_Xin1_state; |
0b35350d |
203 | } |
204 | |