fix t574 reset
[picodrive.git] / pico / carthw / carthw.c
CommitLineData
a12b1b29 1/*
000f5335 2 * Support for a few cart mappers and some protection.
30f0fdd4 3 * (C) notaz, 2008-2011
65ca3034 4 *
cff531af 5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
a12b1b29 7 */
8
efcba75f 9#include "../pico_int.h"
45f2f245 10#include "../memory.h"
6a47c2d4 11#include "eeprom_spi.h"
757f8dae 12
a12b1b29 13
f5080654 14static int have_bank(u32 base)
15{
16 // the loader allocs in 512K quantities
17 if (base >= Pico.romsize) {
18 elprintf(EL_ANOMALY|EL_STATUS, "carthw: missing bank @ %06x", base);
19 return 0;
20 }
21 return 1;
22}
23
000f5335 24/* The SSFII mapper */
30f0fdd4 25static unsigned char ssf2_banks[8];
000f5335 26
27static carthw_state_chunk carthw_ssf2_state[] =
28{
29 { CHUNK_CARTHW, sizeof(ssf2_banks), &ssf2_banks },
30 { 0, 0, NULL }
31};
32
33static void carthw_ssf2_write8(u32 a, u32 d)
34{
35 u32 target, base;
36
37 if ((a & 0xfffff0) != 0xa130f0) {
38 PicoWrite8_io(a, d);
39 return;
40 }
41
42 a &= 0x0e;
43 if (a == 0)
44 return;
45
46 ssf2_banks[a >> 1] = d;
47 base = d << 19;
48 target = a << 18;
f5080654 49 if (!have_bank(base))
000f5335 50 return;
000f5335 51
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);
54}
55
56static void carthw_ssf2_mem_setup(void)
57{
58 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1);
59}
60
61static void carthw_ssf2_statef(void)
62{
63 int i;
64 for (i = 1; i < 8; i++)
65 carthw_ssf2_write8(0xa130f0 | (i << 1), ssf2_banks[i]);
66}
67
68void carthw_ssf2_startup(void)
69{
70 int i;
71
72 elprintf(EL_STATUS, "SSF2 mapper startup");
73
74 // default map
75 for (i = 0; i < 8; i++)
76 ssf2_banks[i] = i;
77
78 PicoCartMemSetup = carthw_ssf2_mem_setup;
79 PicoLoadStateHook = carthw_ssf2_statef;
80 carthw_chunks = carthw_ssf2_state;
81}
82
83
45f2f245 84/* Common *-in-1 pirate mapper.
85 * Switches banks based on addr lines when /TIME is set.
86 * TODO: verify
87 */
88static unsigned int carthw_Xin1_baddr = 0;
757f8dae 89
45f2f245 90static void carthw_Xin1_do(u32 a, int mask, int shift)
757f8dae 91{
92 int len;
93
45f2f245 94 carthw_Xin1_baddr = a;
95 a &= mask;
96 a <<= shift;
757f8dae 97 len = Pico.romsize - a;
98 if (len <= 0) {
45f2f245 99 elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a);
757f8dae 100 return;
101 }
102
45f2f245 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);
757f8dae 106}
107
45f2f245 108static carthw_state_chunk carthw_Xin1_state[] =
757f8dae 109{
45f2f245 110 { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr },
111 { 0, 0, NULL }
112};
113
114// TODO: test a0, reads, w16
115static void carthw_Xin1_write8(u32 a, u32 d)
116{
117 if ((a & 0xffff00) != 0xa13000) {
118 PicoWrite8_io(a, d);
119 return;
120 }
121
122 carthw_Xin1_do(a, 0x3f, 16);
757f8dae 123}
124
45f2f245 125static void carthw_Xin1_mem_setup(void)
bdec53c9 126{
45f2f245 127 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1);
bdec53c9 128}
129
45f2f245 130static void carthw_Xin1_reset(void)
757f8dae 131{
45f2f245 132 carthw_Xin1_write8(0xa13000, 0);
133}
757f8dae 134
45f2f245 135static void carthw_Xin1_statef(void)
136{
137 carthw_Xin1_write8(carthw_Xin1_baddr, 0);
138}
757f8dae 139
45f2f245 140void carthw_Xin1_startup(void)
141{
142 elprintf(EL_STATUS, "X-in-1 mapper startup");
757f8dae 143
45f2f245 144 PicoCartMemSetup = carthw_Xin1_mem_setup;
145 PicoResetHook = carthw_Xin1_reset;
146 PicoLoadStateHook = carthw_Xin1_statef;
147 carthw_chunks = carthw_Xin1_state;
757f8dae 148}
149
a12b1b29 150
151/* Realtec, based on TascoDLX doc
152 * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt
153 */
154static int realtec_bank = 0x80000000, realtec_size = 0x80000000;
a12b1b29 155
45f2f245 156static void carthw_realtec_write8(u32 a, u32 d)
a12b1b29 157{
158 int i, bank_old = realtec_bank, size_old = realtec_size;
159
160 if (a == 0x400000)
161 {
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);
166 }
167 else if (a == 0x402000)
168 {
169 realtec_size = (d << 17) & 0x3e0000;
170 if (realtec_size != size_old)
171 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
172 }
173 else if (a == 0x404000)
174 {
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);
179 }
180 else
181 elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc);
65ca3034 182
a12b1b29 183 if (realtec_bank >= 0 && realtec_size >= 0 &&
184 (realtec_bank != bank_old || realtec_size != size_old))
185 {
186 elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc);
45f2f245 187 if (realtec_size > Pico.romsize - realtec_bank)
a12b1b29 188 {
189 elprintf(EL_ANOMALY, "realtec: bank too large / out of range?");
190 return;
191 }
192
45f2f245 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);
196 }
a12b1b29 197 }
198}
199
0b35350d 200static void carthw_realtec_reset(void)
a12b1b29 201{
202 int i;
45f2f245 203
a12b1b29 204 /* map boot code */
45f2f245 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);
208 }
209 cpu68k_map_set(m68k_write8_map, 0x400000, 0x400000 + M68K_BANK_SIZE - 1, carthw_realtec_write8, 1);
a12b1b29 210 realtec_bank = realtec_size = 0x80000000;
211}
212
213void carthw_realtec_startup(void)
214{
45f2f245 215 int i;
a12b1b29 216
45f2f245 217 elprintf(EL_STATUS, "Realtec mapper startup");
a12b1b29 218
45f2f245 219 // allocate additional bank for boot code
220 // (we know those ROMs have aligned size)
a736af3e 221 i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
222 if (i != 0) {
a12b1b29 223 elprintf(EL_STATUS, "OOM");
224 return;
225 }
a12b1b29 226
45f2f245 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);
230
a12b1b29 231 PicoResetHook = carthw_realtec_reset;
232}
233
0b35350d 234/* Radica mapper, based on DevSter's info
235 * http://devster.monkeeh.com/sega/radica/
45f2f245 236 * XXX: mostly the same as X-in-1, merge?
0b35350d 237 */
45f2f245 238static u32 carthw_radica_read16(u32 a)
0b35350d 239{
45f2f245 240 if ((a & 0xffff00) != 0xa13000)
241 return PicoRead16_io(a);
0b35350d 242
45f2f245 243 carthw_Xin1_do(a, 0x7e, 15);
0b35350d 244
245 return 0;
246}
247
45f2f245 248static void carthw_radica_mem_setup(void)
249{
250 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1);
251}
252
0b35350d 253static void carthw_radica_statef(void)
254{
45f2f245 255 carthw_radica_read16(carthw_Xin1_baddr);
0b35350d 256}
257
258static void carthw_radica_reset(void)
259{
45f2f245 260 carthw_radica_read16(0xa13000);
0b35350d 261}
262
263void carthw_radica_startup(void)
264{
45f2f245 265 elprintf(EL_STATUS, "Radica mapper startup");
0b35350d 266
45f2f245 267 PicoCartMemSetup = carthw_radica_mem_setup;
268 PicoResetHook = carthw_radica_reset;
0b35350d 269 PicoLoadStateHook = carthw_radica_statef;
45f2f245 270 carthw_chunks = carthw_Xin1_state;
0b35350d 271}
272
30f0fdd4 273
274/* Pier Solar. Based on my own research */
275static unsigned char pier_regs[8];
276static unsigned char pier_dump_prot;
277
278static carthw_state_chunk carthw_pier_state[] =
279{
280 { CHUNK_CARTHW, sizeof(pier_regs), pier_regs },
281 { CHUNK_CARTHW + 1, sizeof(pier_dump_prot), &pier_dump_prot },
6a47c2d4 282 { CHUNK_CARTHW + 2, 0, NULL }, // filled later
30f0fdd4 283 { 0, 0, NULL }
284};
285
286static void carthw_pier_write8(u32 a, u32 d)
287{
288 u32 a8, target, base;
289
290 if ((a & 0xffff00) != 0xa13000) {
291 PicoWrite8_io(a, d);
292 return;
293 }
294
295 a8 = a & 0x0f;
296 pier_regs[a8 / 2] = d;
297
298 elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc);
299 switch (a8) {
300 case 0x01:
301 break;
302 case 0x03:
303 if (!(pier_regs[0] & 2))
304 goto unmapped;
305 target = 0x280000;
306 base = d << 19;
307 goto do_map;
308 case 0x05:
309 if (!(pier_regs[0] & 2))
310 goto unmapped;
311 target = 0x300000;
312 base = d << 19;
313 goto do_map;
314 case 0x07:
315 if (!(pier_regs[0] & 2))
316 goto unmapped;
317 target = 0x380000;
318 base = d << 19;
319 goto do_map;
320 case 0x09:
88fd63ad 321 Pico.sv.changed = 1;
6a47c2d4 322 eeprom_spi_write(d);
30f0fdd4 323 break;
324 case 0x0b:
325 // eeprom read
326 default:
327 unmapped:
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);
330 break;
331 }
332 return;
333
334do_map:
f5080654 335 if (!have_bank(base))
30f0fdd4 336 return;
f5080654 337
30f0fdd4 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);
340}
341
342static void carthw_pier_write16(u32 a, u32 d)
343{
344 if ((a & 0xffff00) != 0xa13000) {
345 PicoWrite16_io(a, d);
346 return;
347 }
348
349 elprintf(EL_UIO, "pier w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
350 carthw_pier_write8(a + 1, d);
351}
352
353static u32 carthw_pier_read8(u32 a)
354{
355 if ((a & 0xffff00) != 0xa13000)
356 return PicoRead8_io(a);
357
358 if (a == 0xa1300b)
6a47c2d4 359 return eeprom_spi_read(a);
30f0fdd4 360
361 elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc);
362 return 0;
363}
364
365static void carthw_pier_statef(void);
366
367static u32 carthw_pier_prot_read8(u32 a)
368{
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)
372 pier_dump_prot--;
373 if (pier_dump_prot == 0) {
374 carthw_pier_statef();
375 elprintf(EL_STATUS, "prot off on r8 @%06x", SekPc);
376 }
377 elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc);
378
379 return Pico.rom[(a & 0x7fff) ^ 1];
380}
381
382static void carthw_pier_mem_setup(void)
383{
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);
387}
388
389static void carthw_pier_prot_mem_setup(int prot_enable)
390{
391 if (prot_enable) {
392 /* the dump protection.. */
393 int a;
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);
397 }
398 cpu68k_map_set(m68k_read8_map, M68K_BANK_SIZE, M68K_BANK_SIZE * 2 - 1,
399 carthw_pier_prot_read8, 1);
400 }
401 else {
402 cpu68k_map_set(m68k_read8_map, 0, 0x27ffff, Pico.rom, 0);
403 cpu68k_map_set(m68k_read16_map, 0, 0x27ffff, Pico.rom, 0);
404 }
405}
406
407static void carthw_pier_statef(void)
408{
409 carthw_pier_prot_mem_setup(pier_dump_prot);
410
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);
419 }
420}
421
422static void carthw_pier_reset(void)
423{
424 pier_regs[0] = 1;
425 pier_regs[1] = pier_regs[2] = pier_regs[3] = 0;
30f0fdd4 426 carthw_pier_statef();
6a47c2d4 427 eeprom_spi_init(NULL);
30f0fdd4 428}
429
430void carthw_pier_startup(void)
431{
6a47c2d4 432 void *eeprom_state;
433 int eeprom_size = 0;
30f0fdd4 434 int i;
435
436 elprintf(EL_STATUS, "Pier Solar mapper startup");
437
438 // mostly same as for realtec..
439 i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
440 if (i != 0) {
441 elprintf(EL_STATUS, "OOM");
442 return;
443 }
444
2b46e6c1 445 pier_dump_prot = 3;
446
30f0fdd4 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);
450
6a47c2d4 451 // save EEPROM
452 eeprom_state = eeprom_spi_init(&eeprom_size);
88fd63ad 453 Pico.sv.flags = 0;
454 Pico.sv.size = 0x10000;
455 Pico.sv.data = calloc(1, Pico.sv.size);
456 if (!Pico.sv.data)
457 Pico.sv.size = 0;
6a47c2d4 458 carthw_pier_state[2].ptr = eeprom_state;
459 carthw_pier_state[2].size = eeprom_size;
460
30f0fdd4 461 PicoCartMemSetup = carthw_pier_mem_setup;
462 PicoResetHook = carthw_pier_reset;
463 PicoLoadStateHook = carthw_pier_statef;
464 carthw_chunks = carthw_pier_state;
465}
466
000f5335 467/* Simple unlicensed ROM protection emulation */
468static struct {
30f0fdd4 469 u32 addr;
470 u32 mask;
471 u16 val;
472 u16 readonly;
000f5335 473} *sprot_items;
474static int sprot_item_alloc;
475static int sprot_item_count;
476
477static u16 *carthw_sprot_get_val(u32 a, int rw_only)
478{
30f0fdd4 479 int i;
480
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;
000f5335 485
30f0fdd4 486 return NULL;
000f5335 487}
488
489static u32 PicoRead8_sprot(u32 a)
490{
491 u16 *val;
492 u32 d;
493
494 if (0xa10000 <= a && a < 0xa12000)
495 return PicoRead8_io(a);
496
497 val = carthw_sprot_get_val(a, 0);
498 if (val != NULL) {
499 d = *val;
500 if (!(a & 1))
501 d >>= 8;
502 elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc);
503 return d;
504 }
505 else {
506 elprintf(EL_UIO, "prot r8 [%06x] MISS @%06x", a, SekPc);
507 return 0;
508 }
509}
510
511static u32 PicoRead16_sprot(u32 a)
512{
513 u16 *val;
514
515 if (0xa10000 <= a && a < 0xa12000)
516 return PicoRead16_io(a);
517
518 val = carthw_sprot_get_val(a, 0);
519 if (val != NULL) {
520 elprintf(EL_UIO, "prot r16 [%06x] %04x @%06x", a, *val, SekPc);
521 return *val;
522 }
523 else {
524 elprintf(EL_UIO, "prot r16 [%06x] MISS @%06x", a, SekPc);
525 return 0;
526 }
527}
528
529static void PicoWrite8_sprot(u32 a, u32 d)
530{
531 u16 *val;
532
533 if (0xa10000 <= a && a < 0xa12000) {
534 PicoWrite8_io(a, d);
535 return;
536 }
537
538 val = carthw_sprot_get_val(a, 1);
539 if (val != NULL) {
540 if (a & 1)
541 *val = (*val & 0xff00) | (d | 0xff);
542 else
543 *val = (*val & 0x00ff) | (d << 8);
544 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
545 }
546 else
547 elprintf(EL_UIO, "prot w8 [%06x] %02x MISS @%06x", a, d & 0xff, SekPc);
548}
549
550static void PicoWrite16_sprot(u32 a, u32 d)
551{
552 u16 *val;
553
554 if (0xa10000 <= a && a < 0xa12000) {
555 PicoWrite16_io(a, d);
556 return;
557 }
558
559 val = carthw_sprot_get_val(a, 1);
560 if (val != NULL) {
561 *val = d;
562 elprintf(EL_UIO, "prot w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
563 }
564 else
565 elprintf(EL_UIO, "prot w16 [%06x] %04x MISS @%06x", a, d & 0xffff, SekPc);
566}
567
568void carthw_sprot_new_location(unsigned int a, unsigned int mask, unsigned short val, int is_ro)
569{
570 if (sprot_items == NULL) {
571 sprot_items = calloc(8, sizeof(sprot_items[0]));
572 sprot_item_alloc = 8;
573 sprot_item_count = 0;
574 }
575
576 if (sprot_item_count == sprot_item_alloc) {
577 void *tmp;
578 sprot_item_alloc *= 2;
579 tmp = realloc(sprot_items, sprot_item_alloc);
580 if (tmp == NULL) {
581 elprintf(EL_STATUS, "OOM");
582 return;
583 }
584 sprot_items = tmp;
585 }
586
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;
591 sprot_item_count++;
592}
593
594static void carthw_sprot_unload(void)
595{
596 free(sprot_items);
597 sprot_items = NULL;
598 sprot_item_count = sprot_item_alloc = 0;
599}
600
601static void carthw_sprot_mem_setup(void)
602{
603 int start;
604
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);
611
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);
616}
617
618void carthw_sprot_startup(void)
619{
620 elprintf(EL_STATUS, "Prot emu startup");
621
622 PicoCartMemSetup = carthw_sprot_mem_setup;
623 PicoCartUnloadHook = carthw_sprot_unload;
624}
625
626/* Protection emulation for Lion King 3. Credits go to Haze */
627static u8 prot_lk3_cmd, prot_lk3_data;
628
629static u32 PicoRead8_plk3(u32 a)
630{
631 u32 d = 0;
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;
636 break;
637 case 3: // bit rotate
638 d = prot_lk3_data;
639 d = (d >> 4) | (d << 4);
640 d = ((d & 0xcc) >> 2) | ((d & 0x33) << 2);
641 d = ((d & 0xaa) >> 1) | ((d & 0x55) << 1);
642 break;
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
647*/
648 default:
649 elprintf(EL_UIO, "unhandled prot cmd %02x @%06x", prot_lk3_cmd, SekPc);
650 break;
651 }
652
653 elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc);
654 return d;
655}
656
657static void PicoWrite8_plk3p(u32 a, u32 d)
658{
659 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
660 if (a & 2)
661 prot_lk3_cmd = d;
662 else
663 prot_lk3_data = d;
664}
665
666static void PicoWrite8_plk3b(u32 a, u32 d)
667{
668 int addr;
669
670 elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
671 addr = d << 15;
672 if (addr + 0x8000 > Pico.romsize) {
673 elprintf(EL_UIO|EL_ANOMALY, "prot_lk3: bank too large: %02x", d);
674 return;
675 }
676 if (addr == 0)
677 memcpy(Pico.rom, Pico.rom + Pico.romsize, 0x8000);
678 else
679 memcpy(Pico.rom, Pico.rom + addr, 0x8000);
680}
681
682static void carthw_prot_lk3_mem_setup(void)
683{
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);
687}
688
689void carthw_prot_lk3_startup(void)
690{
a736af3e 691 int ret;
000f5335 692
693 elprintf(EL_STATUS, "lk3 prot emu startup");
694
695 // allocate space for bank0 backup
a736af3e 696 ret = PicoCartResize(Pico.romsize + 0x8000);
697 if (ret != 0) {
000f5335 698 elprintf(EL_STATUS, "OOM");
699 return;
700 }
000f5335 701 memcpy(Pico.rom + Pico.romsize, Pico.rom, 0x8000);
702
703 PicoCartMemSetup = carthw_prot_lk3_mem_setup;
704}
705
6a47c2d4 706// vim:ts=2:sw=2:expandtab