2 * Support for a few cart mappers and some protection.
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
10 #include "../memory.h"
11 #include "eeprom_spi.h"
14 static int have_bank(u32 base)
16 // the loader allocs in 512K quantities
17 if (base >= Pico.romsize) {
18 elprintf(EL_ANOMALY|EL_STATUS, "carthw: missing bank @ %06x", base);
24 /* standard/ssf2 mapper */
25 int carthw_ssf2_active;
26 unsigned char carthw_ssf2_banks[8];
28 static carthw_state_chunk carthw_ssf2_state[] =
30 { CHUNK_CARTHW, sizeof(carthw_ssf2_banks), &carthw_ssf2_banks },
34 static void carthw_ssf2_write8(u32 a, u32 d)
38 if ((a & 0xfffff0) != 0xa130f0) {
46 if (carthw_ssf2_banks[a >> 1] == d)
53 carthw_ssf2_banks[a >> 1] = d;
55 cpu68k_map_set(m68k_read8_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
56 cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
57 if (PicoIn.AHW & PAHW_32X)
61 static void carthw_ssf2_mem_setup(void)
63 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1);
66 static void carthw_ssf2_statef(void)
69 for (i = 1; i < 8; i++)
70 carthw_ssf2_write8(0xa130f0 | (i << 1), carthw_ssf2_banks[i]);
73 static void carthw_ssf2_unload(void)
75 memset(carthw_ssf2_banks, 0, sizeof(carthw_ssf2_banks));
76 carthw_ssf2_active = 0;
79 void carthw_ssf2_startup(void)
83 elprintf(EL_STATUS, "SSF2 mapper startup");
86 for (i = 0; i < 8; i++)
87 carthw_ssf2_banks[i] = i;
89 PicoCartMemSetup = carthw_ssf2_mem_setup;
90 PicoLoadStateHook = carthw_ssf2_statef;
91 PicoCartUnloadHook = carthw_ssf2_unload;
92 carthw_chunks = carthw_ssf2_state;
93 carthw_ssf2_active = 1;
97 /* Common *-in-1 pirate mapper.
98 * Switches banks based on addr lines when /TIME is set.
101 static unsigned int carthw_Xin1_baddr = 0;
103 static void carthw_Xin1_do(u32 a, int mask, int shift)
107 carthw_Xin1_baddr = a;
110 len = Pico.romsize - a;
112 elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a);
116 len = (len + M68K_BANK_MASK) & ~M68K_BANK_MASK;
117 cpu68k_map_set(m68k_read8_map, 0x000000, len - 1, Pico.rom + a, 0);
118 cpu68k_map_set(m68k_read16_map, 0x000000, len - 1, Pico.rom + a, 0);
121 static carthw_state_chunk carthw_Xin1_state[] =
123 { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr },
127 // TODO: test a0, reads, w16
128 static void carthw_Xin1_write8(u32 a, u32 d)
130 if ((a & 0xffff00) != 0xa13000) {
135 carthw_Xin1_do(a, 0x3f, 16);
138 static void carthw_Xin1_mem_setup(void)
140 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1);
143 static void carthw_Xin1_reset(void)
145 carthw_Xin1_write8(0xa13000, 0);
148 static void carthw_Xin1_statef(void)
150 carthw_Xin1_write8(carthw_Xin1_baddr, 0);
153 void carthw_Xin1_startup(void)
155 elprintf(EL_STATUS, "X-in-1 mapper startup");
157 PicoCartMemSetup = carthw_Xin1_mem_setup;
158 PicoResetHook = carthw_Xin1_reset;
159 PicoLoadStateHook = carthw_Xin1_statef;
160 carthw_chunks = carthw_Xin1_state;
164 /* Realtec, based on TascoDLX doc
165 * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt
167 static int realtec_bank = 0x80000000, realtec_size = 0x80000000;
169 static void carthw_realtec_write8(u32 a, u32 d)
171 int i, bank_old = realtec_bank, size_old = realtec_size;
175 realtec_bank &= 0x0e0000;
176 realtec_bank |= 0x300000 & (d << 19);
177 if (realtec_bank != bank_old)
178 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
180 else if (a == 0x402000)
182 realtec_size = (d << 17) & 0x3e0000;
183 if (realtec_size != size_old)
184 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
186 else if (a == 0x404000)
188 realtec_bank &= 0x300000;
189 realtec_bank |= 0x0e0000 & (d << 17);
190 if (realtec_bank != bank_old)
191 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
194 elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc);
196 if (realtec_bank >= 0 && realtec_size >= 0 &&
197 (realtec_bank != bank_old || realtec_size != size_old))
199 elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc);
200 if (realtec_size > Pico.romsize - realtec_bank)
202 elprintf(EL_ANOMALY, "realtec: bank too large / out of range?");
206 for (i = 0; i < 0x400000; i += realtec_size) {
207 cpu68k_map_set(m68k_read8_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0);
208 cpu68k_map_set(m68k_read16_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0);
213 static void carthw_realtec_reset(void)
218 for (i = 0; i < 0x400000; i += M68K_BANK_SIZE) {
219 cpu68k_map_set(m68k_read8_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0);
220 cpu68k_map_set(m68k_read16_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0);
222 cpu68k_map_set(m68k_write8_map, 0x400000, 0x400000 + M68K_BANK_SIZE - 1, carthw_realtec_write8, 1);
223 realtec_bank = realtec_size = 0x80000000;
226 void carthw_realtec_startup(void)
230 elprintf(EL_STATUS, "Realtec mapper startup");
232 // allocate additional bank for boot code
233 // (we know those ROMs have aligned size)
234 i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
236 elprintf(EL_STATUS, "OOM");
240 // create bank for boot code
241 for (i = 0; i < M68K_BANK_SIZE; i += 0x2000)
242 memcpy(Pico.rom + Pico.romsize + i, Pico.rom + Pico.romsize - 0x2000, 0x2000);
244 PicoResetHook = carthw_realtec_reset;
247 /* Radica mapper, based on DevSter's info
248 * http://devster.monkeeh.com/sega/radica/
249 * XXX: mostly the same as X-in-1, merge?
251 static u32 carthw_radica_read16(u32 a)
253 if ((a & 0xffff00) != 0xa13000)
254 return PicoRead16_io(a);
256 carthw_Xin1_do(a, 0x7e, 15);
261 static void carthw_radica_mem_setup(void)
263 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1);
266 static void carthw_radica_statef(void)
268 carthw_radica_read16(carthw_Xin1_baddr);
271 static void carthw_radica_reset(void)
273 carthw_radica_read16(0xa13000);
276 void carthw_radica_startup(void)
278 elprintf(EL_STATUS, "Radica mapper startup");
280 PicoCartMemSetup = carthw_radica_mem_setup;
281 PicoResetHook = carthw_radica_reset;
282 PicoLoadStateHook = carthw_radica_statef;
283 carthw_chunks = carthw_Xin1_state;
287 /* Pier Solar. Based on my own research */
288 static unsigned char pier_regs[8];
289 static unsigned char pier_dump_prot;
291 static carthw_state_chunk carthw_pier_state[] =
293 { CHUNK_CARTHW, sizeof(pier_regs), pier_regs },
294 { CHUNK_CARTHW + 1, sizeof(pier_dump_prot), &pier_dump_prot },
295 { CHUNK_CARTHW + 2, 0, NULL }, // filled later
299 static void carthw_pier_write8(u32 a, u32 d)
301 u32 a8, target, base;
303 if ((a & 0xffff00) != 0xa13000) {
309 pier_regs[a8 / 2] = d;
311 elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
316 if (!(pier_regs[0] & 2))
322 if (!(pier_regs[0] & 2))
328 if (!(pier_regs[0] & 2))
341 //elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
342 elprintf(EL_STATUS, "-- unmapped w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
348 if (!have_bank(base))
351 cpu68k_map_set(m68k_read8_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
352 cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
355 static void carthw_pier_write16(u32 a, u32 d)
357 if ((a & 0xffff00) != 0xa13000) {
358 PicoWrite16_io(a, d);
362 elprintf(EL_UIO, "pier w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
363 carthw_pier_write8(a + 1, d);
366 static u32 carthw_pier_read8(u32 a)
368 if ((a & 0xffff00) != 0xa13000)
369 return PicoRead8_io(a);
372 return eeprom_spi_read(a);
374 elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc);
378 static void carthw_pier_statef(void);
380 static u32 carthw_pier_prot_read8(u32 a)
382 /* it takes more than just these reads here to disable ROM protection,
383 * but for game emulation purposes this is enough. */
384 if (pier_dump_prot > 0)
386 if (pier_dump_prot == 0) {
387 carthw_pier_statef();
388 elprintf(EL_STATUS, "prot off on r8 @%06x", SekPc);
390 elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc);
392 return Pico.rom[(a & 0x7fff) ^ 1];
395 static void carthw_pier_mem_setup(void)
397 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_pier_write8, 1);
398 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, carthw_pier_write16, 1);
399 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, carthw_pier_read8, 1);
402 static void carthw_pier_prot_mem_setup(int prot_enable)
405 /* the dump protection.. */
407 for (a = 0x000000; a < 0x400000; a += M68K_BANK_SIZE) {
408 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, Pico.rom + Pico.romsize, 0);
409 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, Pico.rom + Pico.romsize, 0);
411 cpu68k_map_set(m68k_read8_map, M68K_BANK_SIZE, M68K_BANK_SIZE * 2 - 1,
412 carthw_pier_prot_read8, 1);
415 cpu68k_map_set(m68k_read8_map, 0, 0x27ffff, Pico.rom, 0);
416 cpu68k_map_set(m68k_read16_map, 0, 0x27ffff, Pico.rom, 0);
420 static void carthw_pier_statef(void)
422 carthw_pier_prot_mem_setup(pier_dump_prot);
424 if (!pier_dump_prot) {
425 /* setup all banks */
426 u32 r0 = pier_regs[0];
427 carthw_pier_write8(0xa13001, 3);
428 carthw_pier_write8(0xa13003, pier_regs[1]);
429 carthw_pier_write8(0xa13005, pier_regs[2]);
430 carthw_pier_write8(0xa13007, pier_regs[3]);
431 carthw_pier_write8(0xa13001, r0);
435 static void carthw_pier_reset(void)
438 pier_regs[1] = pier_regs[2] = pier_regs[3] = 0;
439 carthw_pier_statef();
440 eeprom_spi_init(NULL);
443 void carthw_pier_startup(void)
449 elprintf(EL_STATUS, "Pier Solar mapper startup");
451 // mostly same as for realtec..
452 i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
454 elprintf(EL_STATUS, "OOM");
460 // create dump protection bank
461 for (i = 0; i < M68K_BANK_SIZE; i += 0x8000)
462 memcpy(Pico.rom + Pico.romsize + i, Pico.rom, 0x8000);
465 eeprom_state = eeprom_spi_init(&eeprom_size);
467 Pico.sv.size = 0x10000;
468 Pico.sv.data = calloc(1, Pico.sv.size);
471 carthw_pier_state[2].ptr = eeprom_state;
472 carthw_pier_state[2].size = eeprom_size;
474 PicoCartMemSetup = carthw_pier_mem_setup;
475 PicoResetHook = carthw_pier_reset;
476 PicoLoadStateHook = carthw_pier_statef;
477 carthw_chunks = carthw_pier_state;
480 /* Simple unlicensed ROM protection emulation */
487 static int sprot_item_alloc;
488 static int sprot_item_count;
490 static u16 *carthw_sprot_get_val(u32 a, int rw_only)
494 for (i = 0; i < sprot_item_count; i++)
495 if ((a & sprot_items[i].mask) == sprot_items[i].addr)
496 if (!rw_only || !sprot_items[i].readonly)
497 return &sprot_items[i].val;
502 static u32 PicoRead8_sprot(u32 a)
507 if (0xa10000 <= a && a < 0xa12000)
508 return PicoRead8_io(a);
510 val = carthw_sprot_get_val(a, 0);
515 elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc);
519 elprintf(EL_UIO, "prot r8 [%06x] MISS @%06x", a, SekPc);
524 static u32 PicoRead16_sprot(u32 a)
528 if (0xa10000 <= a && a < 0xa12000)
529 return PicoRead16_io(a);
531 val = carthw_sprot_get_val(a, 0);
533 elprintf(EL_UIO, "prot r16 [%06x] %04x @%06x", a, *val, SekPc);
537 elprintf(EL_UIO, "prot r16 [%06x] MISS @%06x", a, SekPc);
542 static void PicoWrite8_sprot(u32 a, u32 d)
546 if (0xa10000 <= a && a < 0xa12000) {
551 val = carthw_sprot_get_val(a, 1);
554 *val = (*val & 0xff00) | (d | 0xff);
556 *val = (*val & 0x00ff) | (d << 8);
557 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
560 elprintf(EL_UIO, "prot w8 [%06x] %02x MISS @%06x", a, d & 0xff, SekPc);
563 static void PicoWrite16_sprot(u32 a, u32 d)
567 if (0xa10000 <= a && a < 0xa12000) {
568 PicoWrite16_io(a, d);
572 val = carthw_sprot_get_val(a, 1);
575 elprintf(EL_UIO, "prot w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
578 elprintf(EL_UIO, "prot w16 [%06x] %04x MISS @%06x", a, d & 0xffff, SekPc);
581 void carthw_sprot_new_location(unsigned int a, unsigned int mask, unsigned short val, int is_ro)
583 if (sprot_items == NULL) {
584 sprot_items = calloc(8, sizeof(sprot_items[0]));
585 sprot_item_alloc = 8;
586 sprot_item_count = 0;
589 if (sprot_item_count == sprot_item_alloc) {
591 sprot_item_alloc *= 2;
592 tmp = realloc(sprot_items, sprot_item_alloc);
594 elprintf(EL_STATUS, "OOM");
600 sprot_items[sprot_item_count].addr = a;
601 sprot_items[sprot_item_count].mask = mask;
602 sprot_items[sprot_item_count].val = val;
603 sprot_items[sprot_item_count].readonly = is_ro;
607 static void carthw_sprot_unload(void)
611 sprot_item_count = sprot_item_alloc = 0;
614 static void carthw_sprot_mem_setup(void)
618 // map ROM - 0x7fffff, /TIME areas (which are tipically used)
619 start = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
620 cpu68k_map_set(m68k_read8_map, start, 0x7fffff, PicoRead8_sprot, 1);
621 cpu68k_map_set(m68k_read16_map, start, 0x7fffff, PicoRead16_sprot, 1);
622 cpu68k_map_set(m68k_write8_map, start, 0x7fffff, PicoWrite8_sprot, 1);
623 cpu68k_map_set(m68k_write16_map, start, 0x7fffff, PicoWrite16_sprot, 1);
625 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_sprot, 1);
626 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_sprot, 1);
627 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_sprot, 1);
628 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_sprot, 1);
631 void carthw_sprot_startup(void)
633 elprintf(EL_STATUS, "Prot emu startup");
635 PicoCartMemSetup = carthw_sprot_mem_setup;
636 PicoCartUnloadHook = carthw_sprot_unload;
639 /* Protection emulation for Lion King 3. Credits go to Haze */
640 static u8 prot_lk3_cmd, prot_lk3_data;
642 static u32 PicoRead8_plk3(u32 a)
645 switch (prot_lk3_cmd) {
646 case 1: d = prot_lk3_data >> 1; break;
647 case 2: // nibble rotate
648 d = ((prot_lk3_data >> 4) | (prot_lk3_data << 4)) & 0xff;
650 case 3: // bit rotate
652 d = (d >> 4) | (d << 4);
653 d = ((d & 0xcc) >> 2) | ((d & 0x33) << 2);
654 d = ((d & 0xaa) >> 1) | ((d & 0x55) << 1);
656 /* Top Fighter 2000 MK VIII (Unl)
657 case 0x98: d = 0x50; break; // prot_lk3_data == a8 here
658 case 0x67: d = 0xde; break; // prot_lk3_data == 7b here (rot!)
659 case 0xb5: d = 0x9f; break; // prot_lk3_data == 4a
662 elprintf(EL_UIO, "unhandled prot cmd %02x @%06x", prot_lk3_cmd, SekPc);
666 elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc);
670 static void PicoWrite8_plk3p(u32 a, u32 d)
672 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
679 static void PicoWrite8_plk3b(u32 a, u32 d)
683 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
685 if (addr + 0x8000 > Pico.romsize) {
686 elprintf(EL_UIO|EL_ANOMALY, "prot_lk3: bank too large: %02x", d);
690 memcpy(Pico.rom, Pico.rom + Pico.romsize, 0x8000);
692 memcpy(Pico.rom, Pico.rom + addr, 0x8000);
695 static void carthw_prot_lk3_mem_setup(void)
697 cpu68k_map_set(m68k_read8_map, 0x600000, 0x7fffff, PicoRead8_plk3, 1);
698 cpu68k_map_set(m68k_write8_map, 0x600000, 0x6fffff, PicoWrite8_plk3p, 1);
699 cpu68k_map_set(m68k_write8_map, 0x700000, 0x7fffff, PicoWrite8_plk3b, 1);
702 void carthw_prot_lk3_startup(void)
706 elprintf(EL_STATUS, "lk3 prot emu startup");
708 // allocate space for bank0 backup
709 ret = PicoCartResize(Pico.romsize + 0x8000);
711 elprintf(EL_STATUS, "OOM");
714 memcpy(Pico.rom + Pico.romsize, Pico.rom, 0x8000);
716 PicoCartMemSetup = carthw_prot_lk3_mem_setup;
719 // vim:ts=2:sw=2:expandtab