a12b1b29 |
1 | /* |
000f5335 |
2 | * Support for a few cart mappers and some protection. |
65ca3034 |
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 | |
000f5335 |
13 | /* The SSFII mapper */ |
14 | unsigned char ssf2_banks[8]; |
15 | |
16 | static carthw_state_chunk carthw_ssf2_state[] = |
17 | { |
18 | { CHUNK_CARTHW, sizeof(ssf2_banks), &ssf2_banks }, |
19 | { 0, 0, NULL } |
20 | }; |
21 | |
22 | static void carthw_ssf2_write8(u32 a, u32 d) |
23 | { |
24 | u32 target, base; |
25 | |
26 | if ((a & 0xfffff0) != 0xa130f0) { |
27 | PicoWrite8_io(a, d); |
28 | return; |
29 | } |
30 | |
31 | a &= 0x0e; |
32 | if (a == 0) |
33 | return; |
34 | |
35 | ssf2_banks[a >> 1] = d; |
36 | base = d << 19; |
37 | target = a << 18; |
38 | if (base + 0x80000 > Pico.romsize) { |
39 | elprintf(EL_ANOMALY|EL_STATUS, "ssf2: missing bank @ %06x", base); |
40 | return; |
41 | } |
42 | |
43 | cpu68k_map_set(m68k_read8_map, target, target + 0x80000 - 1, Pico.rom + base, 0); |
44 | cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0); |
45 | } |
46 | |
47 | static void carthw_ssf2_mem_setup(void) |
48 | { |
49 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1); |
50 | } |
51 | |
52 | static void carthw_ssf2_statef(void) |
53 | { |
54 | int i; |
55 | for (i = 1; i < 8; i++) |
56 | carthw_ssf2_write8(0xa130f0 | (i << 1), ssf2_banks[i]); |
57 | } |
58 | |
59 | void carthw_ssf2_startup(void) |
60 | { |
61 | int i; |
62 | |
63 | elprintf(EL_STATUS, "SSF2 mapper startup"); |
64 | |
65 | // default map |
66 | for (i = 0; i < 8; i++) |
67 | ssf2_banks[i] = i; |
68 | |
69 | PicoCartMemSetup = carthw_ssf2_mem_setup; |
70 | PicoLoadStateHook = carthw_ssf2_statef; |
71 | carthw_chunks = carthw_ssf2_state; |
72 | } |
73 | |
74 | |
45f2f245 |
75 | /* Common *-in-1 pirate mapper. |
76 | * Switches banks based on addr lines when /TIME is set. |
77 | * TODO: verify |
78 | */ |
79 | static unsigned int carthw_Xin1_baddr = 0; |
757f8dae |
80 | |
45f2f245 |
81 | static void carthw_Xin1_do(u32 a, int mask, int shift) |
757f8dae |
82 | { |
83 | int len; |
84 | |
45f2f245 |
85 | carthw_Xin1_baddr = a; |
86 | a &= mask; |
87 | a <<= shift; |
757f8dae |
88 | len = Pico.romsize - a; |
89 | if (len <= 0) { |
45f2f245 |
90 | elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a); |
757f8dae |
91 | return; |
92 | } |
93 | |
45f2f245 |
94 | len = (len + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
95 | cpu68k_map_set(m68k_read8_map, 0x000000, len - 1, Pico.rom + a, 0); |
96 | cpu68k_map_set(m68k_read16_map, 0x000000, len - 1, Pico.rom + a, 0); |
757f8dae |
97 | } |
98 | |
45f2f245 |
99 | static carthw_state_chunk carthw_Xin1_state[] = |
757f8dae |
100 | { |
45f2f245 |
101 | { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr }, |
102 | { 0, 0, NULL } |
103 | }; |
104 | |
105 | // TODO: test a0, reads, w16 |
106 | static void carthw_Xin1_write8(u32 a, u32 d) |
107 | { |
108 | if ((a & 0xffff00) != 0xa13000) { |
109 | PicoWrite8_io(a, d); |
110 | return; |
111 | } |
112 | |
113 | carthw_Xin1_do(a, 0x3f, 16); |
757f8dae |
114 | } |
115 | |
45f2f245 |
116 | static void carthw_Xin1_mem_setup(void) |
bdec53c9 |
117 | { |
45f2f245 |
118 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1); |
bdec53c9 |
119 | } |
120 | |
45f2f245 |
121 | static void carthw_Xin1_reset(void) |
757f8dae |
122 | { |
45f2f245 |
123 | carthw_Xin1_write8(0xa13000, 0); |
124 | } |
757f8dae |
125 | |
45f2f245 |
126 | static void carthw_Xin1_statef(void) |
127 | { |
128 | carthw_Xin1_write8(carthw_Xin1_baddr, 0); |
129 | } |
757f8dae |
130 | |
45f2f245 |
131 | void carthw_Xin1_startup(void) |
132 | { |
133 | elprintf(EL_STATUS, "X-in-1 mapper startup"); |
757f8dae |
134 | |
45f2f245 |
135 | PicoCartMemSetup = carthw_Xin1_mem_setup; |
136 | PicoResetHook = carthw_Xin1_reset; |
137 | PicoLoadStateHook = carthw_Xin1_statef; |
138 | carthw_chunks = carthw_Xin1_state; |
757f8dae |
139 | } |
140 | |
a12b1b29 |
141 | |
142 | /* Realtec, based on TascoDLX doc |
143 | * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt |
144 | */ |
145 | static int realtec_bank = 0x80000000, realtec_size = 0x80000000; |
a12b1b29 |
146 | |
45f2f245 |
147 | static void carthw_realtec_write8(u32 a, u32 d) |
a12b1b29 |
148 | { |
149 | int i, bank_old = realtec_bank, size_old = realtec_size; |
150 | |
151 | if (a == 0x400000) |
152 | { |
153 | realtec_bank &= 0x0e0000; |
154 | realtec_bank |= 0x300000 & (d << 19); |
155 | if (realtec_bank != bank_old) |
156 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
157 | } |
158 | else if (a == 0x402000) |
159 | { |
160 | realtec_size = (d << 17) & 0x3e0000; |
161 | if (realtec_size != size_old) |
162 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
163 | } |
164 | else if (a == 0x404000) |
165 | { |
166 | realtec_bank &= 0x300000; |
167 | realtec_bank |= 0x0e0000 & (d << 17); |
168 | if (realtec_bank != bank_old) |
169 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
170 | } |
171 | else |
172 | elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc); |
65ca3034 |
173 | |
a12b1b29 |
174 | if (realtec_bank >= 0 && realtec_size >= 0 && |
175 | (realtec_bank != bank_old || realtec_size != size_old)) |
176 | { |
177 | elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc); |
45f2f245 |
178 | if (realtec_size > Pico.romsize - realtec_bank) |
a12b1b29 |
179 | { |
180 | elprintf(EL_ANOMALY, "realtec: bank too large / out of range?"); |
181 | return; |
182 | } |
183 | |
45f2f245 |
184 | for (i = 0; i < 0x400000; i += realtec_size) { |
185 | cpu68k_map_set(m68k_read8_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0); |
186 | cpu68k_map_set(m68k_read16_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0); |
187 | } |
a12b1b29 |
188 | } |
189 | } |
190 | |
0b35350d |
191 | static void carthw_realtec_reset(void) |
a12b1b29 |
192 | { |
193 | int i; |
45f2f245 |
194 | |
a12b1b29 |
195 | /* map boot code */ |
45f2f245 |
196 | for (i = 0; i < 0x400000; i += M68K_BANK_SIZE) { |
197 | cpu68k_map_set(m68k_read8_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0); |
198 | cpu68k_map_set(m68k_read16_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0); |
199 | } |
200 | cpu68k_map_set(m68k_write8_map, 0x400000, 0x400000 + M68K_BANK_SIZE - 1, carthw_realtec_write8, 1); |
a12b1b29 |
201 | realtec_bank = realtec_size = 0x80000000; |
202 | } |
203 | |
204 | void carthw_realtec_startup(void) |
205 | { |
206 | void *tmp; |
45f2f245 |
207 | int i; |
a12b1b29 |
208 | |
45f2f245 |
209 | elprintf(EL_STATUS, "Realtec mapper startup"); |
a12b1b29 |
210 | |
45f2f245 |
211 | // allocate additional bank for boot code |
212 | // (we know those ROMs have aligned size) |
213 | tmp = realloc(Pico.rom, Pico.romsize + M68K_BANK_SIZE); |
a12b1b29 |
214 | if (tmp == NULL) |
215 | { |
216 | elprintf(EL_STATUS, "OOM"); |
217 | return; |
218 | } |
219 | Pico.rom = tmp; |
a12b1b29 |
220 | |
45f2f245 |
221 | // create bank for boot code |
222 | for (i = 0; i < M68K_BANK_SIZE; i += 0x2000) |
223 | memcpy(Pico.rom + Pico.romsize + i, Pico.rom + Pico.romsize - 0x2000, 0x2000); |
224 | |
a12b1b29 |
225 | PicoResetHook = carthw_realtec_reset; |
226 | } |
227 | |
0b35350d |
228 | /* Radica mapper, based on DevSter's info |
229 | * http://devster.monkeeh.com/sega/radica/ |
45f2f245 |
230 | * XXX: mostly the same as X-in-1, merge? |
0b35350d |
231 | */ |
45f2f245 |
232 | static u32 carthw_radica_read16(u32 a) |
0b35350d |
233 | { |
45f2f245 |
234 | if ((a & 0xffff00) != 0xa13000) |
235 | return PicoRead16_io(a); |
0b35350d |
236 | |
45f2f245 |
237 | carthw_Xin1_do(a, 0x7e, 15); |
0b35350d |
238 | |
239 | return 0; |
240 | } |
241 | |
45f2f245 |
242 | static void carthw_radica_mem_setup(void) |
243 | { |
244 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1); |
245 | } |
246 | |
0b35350d |
247 | static void carthw_radica_statef(void) |
248 | { |
45f2f245 |
249 | carthw_radica_read16(carthw_Xin1_baddr); |
0b35350d |
250 | } |
251 | |
252 | static void carthw_radica_reset(void) |
253 | { |
45f2f245 |
254 | carthw_radica_read16(0xa13000); |
0b35350d |
255 | } |
256 | |
257 | void carthw_radica_startup(void) |
258 | { |
45f2f245 |
259 | elprintf(EL_STATUS, "Radica mapper startup"); |
0b35350d |
260 | |
45f2f245 |
261 | PicoCartMemSetup = carthw_radica_mem_setup; |
262 | PicoResetHook = carthw_radica_reset; |
0b35350d |
263 | PicoLoadStateHook = carthw_radica_statef; |
45f2f245 |
264 | carthw_chunks = carthw_Xin1_state; |
0b35350d |
265 | } |
266 | |
000f5335 |
267 | /* Simple unlicensed ROM protection emulation */ |
268 | static struct { |
269 | u32 addr; |
270 | u32 mask; |
271 | u16 val; |
272 | u16 readonly; |
273 | } *sprot_items; |
274 | static int sprot_item_alloc; |
275 | static int sprot_item_count; |
276 | |
277 | static u16 *carthw_sprot_get_val(u32 a, int rw_only) |
278 | { |
279 | int i; |
280 | |
281 | for (i = 0; i < sprot_item_count; i++) |
282 | if ((a & sprot_items[i].mask) == sprot_items[i].addr) |
283 | if (!rw_only || !sprot_items[i].readonly) |
284 | return &sprot_items[i].val; |
285 | return NULL; |
286 | } |
287 | |
288 | static u32 PicoRead8_sprot(u32 a) |
289 | { |
290 | u16 *val; |
291 | u32 d; |
292 | |
293 | if (0xa10000 <= a && a < 0xa12000) |
294 | return PicoRead8_io(a); |
295 | |
296 | val = carthw_sprot_get_val(a, 0); |
297 | if (val != NULL) { |
298 | d = *val; |
299 | if (!(a & 1)) |
300 | d >>= 8; |
301 | elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc); |
302 | return d; |
303 | } |
304 | else { |
305 | elprintf(EL_UIO, "prot r8 [%06x] MISS @%06x", a, SekPc); |
306 | return 0; |
307 | } |
308 | } |
309 | |
310 | static u32 PicoRead16_sprot(u32 a) |
311 | { |
312 | u16 *val; |
313 | |
314 | if (0xa10000 <= a && a < 0xa12000) |
315 | return PicoRead16_io(a); |
316 | |
317 | val = carthw_sprot_get_val(a, 0); |
318 | if (val != NULL) { |
319 | elprintf(EL_UIO, "prot r16 [%06x] %04x @%06x", a, *val, SekPc); |
320 | return *val; |
321 | } |
322 | else { |
323 | elprintf(EL_UIO, "prot r16 [%06x] MISS @%06x", a, SekPc); |
324 | return 0; |
325 | } |
326 | } |
327 | |
328 | static void PicoWrite8_sprot(u32 a, u32 d) |
329 | { |
330 | u16 *val; |
331 | |
332 | if (0xa10000 <= a && a < 0xa12000) { |
333 | PicoWrite8_io(a, d); |
334 | return; |
335 | } |
336 | |
337 | val = carthw_sprot_get_val(a, 1); |
338 | if (val != NULL) { |
339 | if (a & 1) |
340 | *val = (*val & 0xff00) | (d | 0xff); |
341 | else |
342 | *val = (*val & 0x00ff) | (d << 8); |
343 | elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
344 | } |
345 | else |
346 | elprintf(EL_UIO, "prot w8 [%06x] %02x MISS @%06x", a, d & 0xff, SekPc); |
347 | } |
348 | |
349 | static void PicoWrite16_sprot(u32 a, u32 d) |
350 | { |
351 | u16 *val; |
352 | |
353 | if (0xa10000 <= a && a < 0xa12000) { |
354 | PicoWrite16_io(a, d); |
355 | return; |
356 | } |
357 | |
358 | val = carthw_sprot_get_val(a, 1); |
359 | if (val != NULL) { |
360 | *val = d; |
361 | elprintf(EL_UIO, "prot w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
362 | } |
363 | else |
364 | elprintf(EL_UIO, "prot w16 [%06x] %04x MISS @%06x", a, d & 0xffff, SekPc); |
365 | } |
366 | |
367 | void carthw_sprot_new_location(unsigned int a, unsigned int mask, unsigned short val, int is_ro) |
368 | { |
369 | if (sprot_items == NULL) { |
370 | sprot_items = calloc(8, sizeof(sprot_items[0])); |
371 | sprot_item_alloc = 8; |
372 | sprot_item_count = 0; |
373 | } |
374 | |
375 | if (sprot_item_count == sprot_item_alloc) { |
376 | void *tmp; |
377 | sprot_item_alloc *= 2; |
378 | tmp = realloc(sprot_items, sprot_item_alloc); |
379 | if (tmp == NULL) { |
380 | elprintf(EL_STATUS, "OOM"); |
381 | return; |
382 | } |
383 | sprot_items = tmp; |
384 | } |
385 | |
386 | sprot_items[sprot_item_count].addr = a; |
387 | sprot_items[sprot_item_count].mask = mask; |
388 | sprot_items[sprot_item_count].val = val; |
389 | sprot_items[sprot_item_count].readonly = is_ro; |
390 | sprot_item_count++; |
391 | } |
392 | |
393 | static void carthw_sprot_unload(void) |
394 | { |
395 | free(sprot_items); |
396 | sprot_items = NULL; |
397 | sprot_item_count = sprot_item_alloc = 0; |
398 | } |
399 | |
400 | static void carthw_sprot_mem_setup(void) |
401 | { |
402 | int start; |
403 | |
404 | // map ROM - 0x7fffff, /TIME areas (which are tipically used) |
405 | start = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
406 | cpu68k_map_set(m68k_read8_map, start, 0x7fffff, PicoRead8_sprot, 1); |
407 | cpu68k_map_set(m68k_read16_map, start, 0x7fffff, PicoRead16_sprot, 1); |
408 | cpu68k_map_set(m68k_write8_map, start, 0x7fffff, PicoWrite8_sprot, 1); |
409 | cpu68k_map_set(m68k_write16_map, start, 0x7fffff, PicoWrite16_sprot, 1); |
410 | |
411 | cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_sprot, 1); |
412 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_sprot, 1); |
413 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_sprot, 1); |
414 | cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_sprot, 1); |
415 | } |
416 | |
417 | void carthw_sprot_startup(void) |
418 | { |
419 | elprintf(EL_STATUS, "Prot emu startup"); |
420 | |
421 | PicoCartMemSetup = carthw_sprot_mem_setup; |
422 | PicoCartUnloadHook = carthw_sprot_unload; |
423 | } |
424 | |
425 | /* Protection emulation for Lion King 3. Credits go to Haze */ |
426 | static u8 prot_lk3_cmd, prot_lk3_data; |
427 | |
428 | static u32 PicoRead8_plk3(u32 a) |
429 | { |
430 | u32 d = 0; |
431 | switch (prot_lk3_cmd) { |
432 | case 1: d = prot_lk3_data >> 1; break; |
433 | case 2: // nibble rotate |
434 | d = ((prot_lk3_data >> 4) | (prot_lk3_data << 4)) & 0xff; |
435 | break; |
436 | case 3: // bit rotate |
437 | d = prot_lk3_data; |
438 | d = (d >> 4) | (d << 4); |
439 | d = ((d & 0xcc) >> 2) | ((d & 0x33) << 2); |
440 | d = ((d & 0xaa) >> 1) | ((d & 0x55) << 1); |
441 | break; |
442 | /* Top Fighter 2000 MK VIII (Unl) |
443 | case 0x98: d = 0x50; break; // prot_lk3_data == a8 here |
444 | case 0x67: d = 0xde; break; // prot_lk3_data == 7b here (rot!) |
445 | case 0xb5: d = 0x9f; break; // prot_lk3_data == 4a |
446 | */ |
447 | default: |
448 | elprintf(EL_UIO, "unhandled prot cmd %02x @%06x", prot_lk3_cmd, SekPc); |
449 | break; |
450 | } |
451 | |
452 | elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc); |
453 | return d; |
454 | } |
455 | |
456 | static void PicoWrite8_plk3p(u32 a, u32 d) |
457 | { |
458 | elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
459 | if (a & 2) |
460 | prot_lk3_cmd = d; |
461 | else |
462 | prot_lk3_data = d; |
463 | } |
464 | |
465 | static void PicoWrite8_plk3b(u32 a, u32 d) |
466 | { |
467 | int addr; |
468 | |
469 | elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
470 | addr = d << 15; |
471 | if (addr + 0x8000 > Pico.romsize) { |
472 | elprintf(EL_UIO|EL_ANOMALY, "prot_lk3: bank too large: %02x", d); |
473 | return; |
474 | } |
475 | if (addr == 0) |
476 | memcpy(Pico.rom, Pico.rom + Pico.romsize, 0x8000); |
477 | else |
478 | memcpy(Pico.rom, Pico.rom + addr, 0x8000); |
479 | } |
480 | |
481 | static void carthw_prot_lk3_mem_setup(void) |
482 | { |
483 | cpu68k_map_set(m68k_read8_map, 0x600000, 0x7fffff, PicoRead8_plk3, 1); |
484 | cpu68k_map_set(m68k_write8_map, 0x600000, 0x6fffff, PicoWrite8_plk3p, 1); |
485 | cpu68k_map_set(m68k_write8_map, 0x700000, 0x7fffff, PicoWrite8_plk3b, 1); |
486 | } |
487 | |
488 | void carthw_prot_lk3_startup(void) |
489 | { |
490 | void *tmp; |
491 | |
492 | elprintf(EL_STATUS, "lk3 prot emu startup"); |
493 | |
494 | // allocate space for bank0 backup |
495 | tmp = realloc(Pico.rom, Pico.romsize + 0x8000); |
496 | if (tmp == NULL) |
497 | { |
498 | elprintf(EL_STATUS, "OOM"); |
499 | return; |
500 | } |
501 | Pico.rom = tmp; |
502 | memcpy(Pico.rom + Pico.romsize, Pico.rom, 0x8000); |
503 | |
504 | PicoCartMemSetup = carthw_prot_lk3_mem_setup; |
505 | } |
506 | |