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 /* The SSFII mapper */
25 static unsigned char ssf2_banks[8];
27 static carthw_state_chunk carthw_ssf2_state[] =
29 { CHUNK_CARTHW, sizeof(ssf2_banks), &ssf2_banks },
33 static void carthw_ssf2_write8(u32 a, u32 d)
37 if ((a & 0xfffff0) != 0xa130f0) {
46 ssf2_banks[a >> 1] = d;
52 cpu68k_map_set(m68k_read8_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
53 cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
56 static void carthw_ssf2_mem_setup(void)
58 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1);
61 static void carthw_ssf2_statef(void)
64 for (i = 1; i < 8; i++)
65 carthw_ssf2_write8(0xa130f0 | (i << 1), ssf2_banks[i]);
68 void carthw_ssf2_startup(void)
72 elprintf(EL_STATUS, "SSF2 mapper startup");
75 for (i = 0; i < 8; i++)
78 PicoCartMemSetup = carthw_ssf2_mem_setup;
79 PicoLoadStateHook = carthw_ssf2_statef;
80 carthw_chunks = carthw_ssf2_state;
84 /* Common *-in-1 pirate mapper.
85 * Switches banks based on addr lines when /TIME is set.
88 static unsigned int carthw_Xin1_baddr = 0;
90 static void carthw_Xin1_do(u32 a, int mask, int shift)
94 carthw_Xin1_baddr = a;
97 len = Pico.romsize - a;
99 elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a);
103 len = (len + M68K_BANK_MASK) & ~M68K_BANK_MASK;
104 cpu68k_map_set(m68k_read8_map, 0x000000, len - 1, Pico.rom + a, 0);
105 cpu68k_map_set(m68k_read16_map, 0x000000, len - 1, Pico.rom + a, 0);
108 static carthw_state_chunk carthw_Xin1_state[] =
110 { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr },
114 // TODO: test a0, reads, w16
115 static void carthw_Xin1_write8(u32 a, u32 d)
117 if ((a & 0xffff00) != 0xa13000) {
122 carthw_Xin1_do(a, 0x3f, 16);
125 static void carthw_Xin1_mem_setup(void)
127 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1);
130 static void carthw_Xin1_reset(void)
132 carthw_Xin1_write8(0xa13000, 0);
135 static void carthw_Xin1_statef(void)
137 carthw_Xin1_write8(carthw_Xin1_baddr, 0);
140 void carthw_Xin1_startup(void)
142 elprintf(EL_STATUS, "X-in-1 mapper startup");
144 PicoCartMemSetup = carthw_Xin1_mem_setup;
145 PicoResetHook = carthw_Xin1_reset;
146 PicoLoadStateHook = carthw_Xin1_statef;
147 carthw_chunks = carthw_Xin1_state;
151 /* Realtec, based on TascoDLX doc
152 * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt
154 static int realtec_bank = 0x80000000, realtec_size = 0x80000000;
156 static void carthw_realtec_write8(u32 a, u32 d)
158 int i, bank_old = realtec_bank, size_old = realtec_size;
162 realtec_bank &= 0x0e0000;
163 realtec_bank |= 0x300000 & (d << 19);
164 if (realtec_bank != bank_old)
165 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
167 else if (a == 0x402000)
169 realtec_size = (d << 17) & 0x3e0000;
170 if (realtec_size != size_old)
171 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
173 else if (a == 0x404000)
175 realtec_bank &= 0x300000;
176 realtec_bank |= 0x0e0000 & (d << 17);
177 if (realtec_bank != bank_old)
178 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
181 elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc);
183 if (realtec_bank >= 0 && realtec_size >= 0 &&
184 (realtec_bank != bank_old || realtec_size != size_old))
186 elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc);
187 if (realtec_size > Pico.romsize - realtec_bank)
189 elprintf(EL_ANOMALY, "realtec: bank too large / out of range?");
193 for (i = 0; i < 0x400000; i += realtec_size) {
194 cpu68k_map_set(m68k_read8_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0);
195 cpu68k_map_set(m68k_read16_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0);
200 static void carthw_realtec_reset(void)
205 for (i = 0; i < 0x400000; i += M68K_BANK_SIZE) {
206 cpu68k_map_set(m68k_read8_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0);
207 cpu68k_map_set(m68k_read16_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0);
209 cpu68k_map_set(m68k_write8_map, 0x400000, 0x400000 + M68K_BANK_SIZE - 1, carthw_realtec_write8, 1);
210 realtec_bank = realtec_size = 0x80000000;
213 void carthw_realtec_startup(void)
217 elprintf(EL_STATUS, "Realtec mapper startup");
219 // allocate additional bank for boot code
220 // (we know those ROMs have aligned size)
221 i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
223 elprintf(EL_STATUS, "OOM");
227 // create bank for boot code
228 for (i = 0; i < M68K_BANK_SIZE; i += 0x2000)
229 memcpy(Pico.rom + Pico.romsize + i, Pico.rom + Pico.romsize - 0x2000, 0x2000);
231 PicoResetHook = carthw_realtec_reset;
234 /* Radica mapper, based on DevSter's info
235 * http://devster.monkeeh.com/sega/radica/
236 * XXX: mostly the same as X-in-1, merge?
238 static u32 carthw_radica_read16(u32 a)
240 if ((a & 0xffff00) != 0xa13000)
241 return PicoRead16_io(a);
243 carthw_Xin1_do(a, 0x7e, 15);
248 static void carthw_radica_mem_setup(void)
250 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1);
253 static void carthw_radica_statef(void)
255 carthw_radica_read16(carthw_Xin1_baddr);
258 static void carthw_radica_reset(void)
260 carthw_radica_read16(0xa13000);
263 void carthw_radica_startup(void)
265 elprintf(EL_STATUS, "Radica mapper startup");
267 PicoCartMemSetup = carthw_radica_mem_setup;
268 PicoResetHook = carthw_radica_reset;
269 PicoLoadStateHook = carthw_radica_statef;
270 carthw_chunks = carthw_Xin1_state;
274 /* Pier Solar. Based on my own research */
275 static unsigned char pier_regs[8];
276 static unsigned char pier_dump_prot;
278 static carthw_state_chunk carthw_pier_state[] =
280 { CHUNK_CARTHW, sizeof(pier_regs), pier_regs },
281 { CHUNK_CARTHW + 1, sizeof(pier_dump_prot), &pier_dump_prot },
282 { CHUNK_CARTHW + 2, 0, NULL }, // filled later
286 static void carthw_pier_write8(u32 a, u32 d)
288 u32 a8, target, base;
290 if ((a & 0xffff00) != 0xa13000) {
296 pier_regs[a8 / 2] = d;
298 elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
303 if (!(pier_regs[0] & 2))
309 if (!(pier_regs[0] & 2))
315 if (!(pier_regs[0] & 2))
328 //elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
329 elprintf(EL_STATUS, "-- unmapped w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
335 if (!have_bank(base))
338 cpu68k_map_set(m68k_read8_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
339 cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0);
342 static void carthw_pier_write16(u32 a, u32 d)
344 if ((a & 0xffff00) != 0xa13000) {
345 PicoWrite16_io(a, d);
349 elprintf(EL_UIO, "pier w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
350 carthw_pier_write8(a + 1, d);
353 static u32 carthw_pier_read8(u32 a)
355 if ((a & 0xffff00) != 0xa13000)
356 return PicoRead8_io(a);
359 return eeprom_spi_read(a);
361 elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc);
365 static void carthw_pier_statef(void);
367 static u32 carthw_pier_prot_read8(u32 a)
369 /* it takes more than just these reads here to disable ROM protection,
370 * but for game emulation purposes this is enough. */
371 if (pier_dump_prot > 0)
373 if (pier_dump_prot == 0) {
374 carthw_pier_statef();
375 elprintf(EL_STATUS, "prot off on r8 @%06x", SekPc);
377 elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc);
379 return Pico.rom[(a & 0x7fff) ^ 1];
382 static void carthw_pier_mem_setup(void)
384 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_pier_write8, 1);
385 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, carthw_pier_write16, 1);
386 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, carthw_pier_read8, 1);
389 static void carthw_pier_prot_mem_setup(int prot_enable)
392 /* the dump protection.. */
394 for (a = 0x000000; a < 0x400000; a += M68K_BANK_SIZE) {
395 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, Pico.rom + Pico.romsize, 0);
396 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, Pico.rom + Pico.romsize, 0);
398 cpu68k_map_set(m68k_read8_map, M68K_BANK_SIZE, M68K_BANK_SIZE * 2 - 1,
399 carthw_pier_prot_read8, 1);
402 cpu68k_map_set(m68k_read8_map, 0, 0x27ffff, Pico.rom, 0);
403 cpu68k_map_set(m68k_read16_map, 0, 0x27ffff, Pico.rom, 0);
407 static void carthw_pier_statef(void)
409 carthw_pier_prot_mem_setup(pier_dump_prot);
411 if (!pier_dump_prot) {
412 /* setup all banks */
413 u32 r0 = pier_regs[0];
414 carthw_pier_write8(0xa13001, 3);
415 carthw_pier_write8(0xa13003, pier_regs[1]);
416 carthw_pier_write8(0xa13005, pier_regs[2]);
417 carthw_pier_write8(0xa13007, pier_regs[3]);
418 carthw_pier_write8(0xa13001, r0);
422 static void carthw_pier_reset(void)
425 pier_regs[1] = pier_regs[2] = pier_regs[3] = 0;
426 carthw_pier_statef();
427 eeprom_spi_init(NULL);
430 void carthw_pier_startup(void)
436 elprintf(EL_STATUS, "Pier Solar mapper startup");
438 // mostly same as for realtec..
439 i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
441 elprintf(EL_STATUS, "OOM");
447 // create dump protection bank
448 for (i = 0; i < M68K_BANK_SIZE; i += 0x8000)
449 memcpy(Pico.rom + Pico.romsize + i, Pico.rom, 0x8000);
452 eeprom_state = eeprom_spi_init(&eeprom_size);
454 Pico.sv.size = 0x10000;
455 Pico.sv.data = calloc(1, Pico.sv.size);
458 carthw_pier_state[2].ptr = eeprom_state;
459 carthw_pier_state[2].size = eeprom_size;
461 PicoCartMemSetup = carthw_pier_mem_setup;
462 PicoResetHook = carthw_pier_reset;
463 PicoLoadStateHook = carthw_pier_statef;
464 carthw_chunks = carthw_pier_state;
467 /* Simple unlicensed ROM protection emulation */
474 static int sprot_item_alloc;
475 static int sprot_item_count;
477 static u16 *carthw_sprot_get_val(u32 a, int rw_only)
481 for (i = 0; i < sprot_item_count; i++)
482 if ((a & sprot_items[i].mask) == sprot_items[i].addr)
483 if (!rw_only || !sprot_items[i].readonly)
484 return &sprot_items[i].val;
489 static u32 PicoRead8_sprot(u32 a)
494 if (0xa10000 <= a && a < 0xa12000)
495 return PicoRead8_io(a);
497 val = carthw_sprot_get_val(a, 0);
502 elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc);
506 elprintf(EL_UIO, "prot r8 [%06x] MISS @%06x", a, SekPc);
511 static u32 PicoRead16_sprot(u32 a)
515 if (0xa10000 <= a && a < 0xa12000)
516 return PicoRead16_io(a);
518 val = carthw_sprot_get_val(a, 0);
520 elprintf(EL_UIO, "prot r16 [%06x] %04x @%06x", a, *val, SekPc);
524 elprintf(EL_UIO, "prot r16 [%06x] MISS @%06x", a, SekPc);
529 static void PicoWrite8_sprot(u32 a, u32 d)
533 if (0xa10000 <= a && a < 0xa12000) {
538 val = carthw_sprot_get_val(a, 1);
541 *val = (*val & 0xff00) | (d | 0xff);
543 *val = (*val & 0x00ff) | (d << 8);
544 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
547 elprintf(EL_UIO, "prot w8 [%06x] %02x MISS @%06x", a, d & 0xff, SekPc);
550 static void PicoWrite16_sprot(u32 a, u32 d)
554 if (0xa10000 <= a && a < 0xa12000) {
555 PicoWrite16_io(a, d);
559 val = carthw_sprot_get_val(a, 1);
562 elprintf(EL_UIO, "prot w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
565 elprintf(EL_UIO, "prot w16 [%06x] %04x MISS @%06x", a, d & 0xffff, SekPc);
568 void carthw_sprot_new_location(unsigned int a, unsigned int mask, unsigned short val, int is_ro)
570 if (sprot_items == NULL) {
571 sprot_items = calloc(8, sizeof(sprot_items[0]));
572 sprot_item_alloc = 8;
573 sprot_item_count = 0;
576 if (sprot_item_count == sprot_item_alloc) {
578 sprot_item_alloc *= 2;
579 tmp = realloc(sprot_items, sprot_item_alloc);
581 elprintf(EL_STATUS, "OOM");
587 sprot_items[sprot_item_count].addr = a;
588 sprot_items[sprot_item_count].mask = mask;
589 sprot_items[sprot_item_count].val = val;
590 sprot_items[sprot_item_count].readonly = is_ro;
594 static void carthw_sprot_unload(void)
598 sprot_item_count = sprot_item_alloc = 0;
601 static void carthw_sprot_mem_setup(void)
605 // map ROM - 0x7fffff, /TIME areas (which are tipically used)
606 start = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
607 cpu68k_map_set(m68k_read8_map, start, 0x7fffff, PicoRead8_sprot, 1);
608 cpu68k_map_set(m68k_read16_map, start, 0x7fffff, PicoRead16_sprot, 1);
609 cpu68k_map_set(m68k_write8_map, start, 0x7fffff, PicoWrite8_sprot, 1);
610 cpu68k_map_set(m68k_write16_map, start, 0x7fffff, PicoWrite16_sprot, 1);
612 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_sprot, 1);
613 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_sprot, 1);
614 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_sprot, 1);
615 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_sprot, 1);
618 void carthw_sprot_startup(void)
620 elprintf(EL_STATUS, "Prot emu startup");
622 PicoCartMemSetup = carthw_sprot_mem_setup;
623 PicoCartUnloadHook = carthw_sprot_unload;
626 /* Protection emulation for Lion King 3. Credits go to Haze */
627 static u8 prot_lk3_cmd, prot_lk3_data;
629 static u32 PicoRead8_plk3(u32 a)
632 switch (prot_lk3_cmd) {
633 case 1: d = prot_lk3_data >> 1; break;
634 case 2: // nibble rotate
635 d = ((prot_lk3_data >> 4) | (prot_lk3_data << 4)) & 0xff;
637 case 3: // bit rotate
639 d = (d >> 4) | (d << 4);
640 d = ((d & 0xcc) >> 2) | ((d & 0x33) << 2);
641 d = ((d & 0xaa) >> 1) | ((d & 0x55) << 1);
643 /* Top Fighter 2000 MK VIII (Unl)
644 case 0x98: d = 0x50; break; // prot_lk3_data == a8 here
645 case 0x67: d = 0xde; break; // prot_lk3_data == 7b here (rot!)
646 case 0xb5: d = 0x9f; break; // prot_lk3_data == 4a
649 elprintf(EL_UIO, "unhandled prot cmd %02x @%06x", prot_lk3_cmd, SekPc);
653 elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc);
657 static void PicoWrite8_plk3p(u32 a, u32 d)
659 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
666 static void PicoWrite8_plk3b(u32 a, u32 d)
670 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
672 if (addr + 0x8000 > Pico.romsize) {
673 elprintf(EL_UIO|EL_ANOMALY, "prot_lk3: bank too large: %02x", d);
677 memcpy(Pico.rom, Pico.rom + Pico.romsize, 0x8000);
679 memcpy(Pico.rom, Pico.rom + addr, 0x8000);
682 static void carthw_prot_lk3_mem_setup(void)
684 cpu68k_map_set(m68k_read8_map, 0x600000, 0x7fffff, PicoRead8_plk3, 1);
685 cpu68k_map_set(m68k_write8_map, 0x600000, 0x6fffff, PicoWrite8_plk3p, 1);
686 cpu68k_map_set(m68k_write8_map, 0x700000, 0x7fffff, PicoWrite8_plk3b, 1);
689 void carthw_prot_lk3_startup(void)
693 elprintf(EL_STATUS, "lk3 prot emu startup");
695 // allocate space for bank0 backup
696 ret = PicoCartResize(Pico.romsize + 0x8000);
698 elprintf(EL_STATUS, "OOM");
701 memcpy(Pico.rom + Pico.romsize, Pico.rom, 0x8000);
703 PicoCartMemSetup = carthw_prot_lk3_mem_setup;
706 // vim:ts=2:sw=2:expandtab