fix t574 reset
[picodrive.git] / pico / carthw / carthw.c
1 /*
2  * Support for a few cart mappers and some protection.
3  * (C) notaz, 2008-2011
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8
9 #include "../pico_int.h"
10 #include "../memory.h"
11 #include "eeprom_spi.h"
12
13
14 static 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
24 /* The SSFII mapper */
25 static unsigned char ssf2_banks[8];
26
27 static carthw_state_chunk carthw_ssf2_state[] =
28 {
29         { CHUNK_CARTHW, sizeof(ssf2_banks), &ssf2_banks },
30         { 0,            0,                  NULL }
31 };
32
33 static 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;
49         if (!have_bank(base))
50                 return;
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
56 static void carthw_ssf2_mem_setup(void)
57 {
58         cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1);
59 }
60
61 static 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
68 void 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
84 /* Common *-in-1 pirate mapper.
85  * Switches banks based on addr lines when /TIME is set.
86  * TODO: verify
87  */
88 static unsigned int carthw_Xin1_baddr = 0;
89
90 static void carthw_Xin1_do(u32 a, int mask, int shift)
91 {
92         int len;
93
94         carthw_Xin1_baddr = a;
95         a &= mask;
96         a <<= shift;
97         len = Pico.romsize - a;
98         if (len <= 0) {
99                 elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a);
100                 return;
101         }
102
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);
106 }
107
108 static carthw_state_chunk carthw_Xin1_state[] =
109 {
110         { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr },
111         { 0,            0,                         NULL }
112 };
113
114 // TODO: test a0, reads, w16
115 static 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);
123 }
124
125 static void carthw_Xin1_mem_setup(void)
126 {
127         cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1);
128 }
129
130 static void carthw_Xin1_reset(void)
131 {
132         carthw_Xin1_write8(0xa13000, 0);
133 }
134
135 static void carthw_Xin1_statef(void)
136 {
137         carthw_Xin1_write8(carthw_Xin1_baddr, 0);
138 }
139
140 void carthw_Xin1_startup(void)
141 {
142         elprintf(EL_STATUS, "X-in-1 mapper startup");
143
144         PicoCartMemSetup  = carthw_Xin1_mem_setup;
145         PicoResetHook     = carthw_Xin1_reset;
146         PicoLoadStateHook = carthw_Xin1_statef;
147         carthw_chunks     = carthw_Xin1_state;
148 }
149
150
151 /* Realtec, based on TascoDLX doc
152  * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt
153  */
154 static int realtec_bank = 0x80000000, realtec_size = 0x80000000;
155
156 static void carthw_realtec_write8(u32 a, u32 d)
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);
182
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);
187                 if (realtec_size > Pico.romsize - realtec_bank)
188                 {
189                         elprintf(EL_ANOMALY, "realtec: bank too large / out of range?");
190                         return;
191                 }
192
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                 }
197         }
198 }
199
200 static void carthw_realtec_reset(void)
201 {
202         int i;
203
204         /* map boot code */
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);
210         realtec_bank = realtec_size = 0x80000000;
211 }
212
213 void carthw_realtec_startup(void)
214 {
215         int i;
216
217         elprintf(EL_STATUS, "Realtec mapper startup");
218
219         // allocate additional bank for boot code
220         // (we know those ROMs have aligned size)
221         i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE);
222         if (i != 0) {
223                 elprintf(EL_STATUS, "OOM");
224                 return;
225         }
226
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
231         PicoResetHook = carthw_realtec_reset;
232 }
233
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?
237  */
238 static u32 carthw_radica_read16(u32 a)
239 {
240         if ((a & 0xffff00) != 0xa13000)
241                 return PicoRead16_io(a);
242
243         carthw_Xin1_do(a, 0x7e, 15);
244
245         return 0;
246 }
247
248 static void carthw_radica_mem_setup(void)
249 {
250         cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1);
251 }
252
253 static void carthw_radica_statef(void)
254 {
255         carthw_radica_read16(carthw_Xin1_baddr);
256 }
257
258 static void carthw_radica_reset(void)
259 {
260         carthw_radica_read16(0xa13000);
261 }
262
263 void carthw_radica_startup(void)
264 {
265         elprintf(EL_STATUS, "Radica mapper startup");
266
267         PicoCartMemSetup  = carthw_radica_mem_setup;
268         PicoResetHook     = carthw_radica_reset;
269         PicoLoadStateHook = carthw_radica_statef;
270         carthw_chunks     = carthw_Xin1_state;
271 }
272
273
274 /* Pier Solar. Based on my own research */
275 static unsigned char pier_regs[8];
276 static unsigned char pier_dump_prot;
277
278 static 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 },
282   { CHUNK_CARTHW + 2, 0,                      NULL }, // filled later
283   { 0,                0,                      NULL }
284 };
285
286 static 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:
321       Pico.sv.changed = 1;
322       eeprom_spi_write(d);
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
334 do_map:
335   if (!have_bank(base))
336     return;
337
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
342 static 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
353 static u32 carthw_pier_read8(u32 a)
354 {
355   if ((a & 0xffff00) != 0xa13000)
356     return PicoRead8_io(a);
357
358   if (a == 0xa1300b)
359     return eeprom_spi_read(a);
360
361   elprintf(EL_UIO, "pier r8  [%06x] @%06x", a, SekPc);
362   return 0;
363 }
364
365 static void carthw_pier_statef(void);
366
367 static 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
382 static 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
389 static 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
407 static 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
422 static void carthw_pier_reset(void)
423 {
424   pier_regs[0] = 1;
425   pier_regs[1] = pier_regs[2] = pier_regs[3] = 0;
426   carthw_pier_statef();
427   eeprom_spi_init(NULL);
428 }
429
430 void carthw_pier_startup(void)
431 {
432   void *eeprom_state;
433   int eeprom_size = 0;
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
445   pier_dump_prot = 3;
446
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
451   // save EEPROM
452   eeprom_state = eeprom_spi_init(&eeprom_size);
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;
458   carthw_pier_state[2].ptr = eeprom_state;
459   carthw_pier_state[2].size = eeprom_size;
460
461   PicoCartMemSetup  = carthw_pier_mem_setup;
462   PicoResetHook     = carthw_pier_reset;
463   PicoLoadStateHook = carthw_pier_statef;
464   carthw_chunks     = carthw_pier_state;
465 }
466
467 /* Simple unlicensed ROM protection emulation */
468 static struct {
469   u32 addr;
470   u32 mask;
471   u16 val;
472   u16 readonly;
473 } *sprot_items;
474 static int sprot_item_alloc;
475 static int sprot_item_count;
476
477 static u16 *carthw_sprot_get_val(u32 a, int rw_only)
478 {
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;
485
486   return NULL;
487 }
488
489 static 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
511 static 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
529 static 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
550 static 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
568 void 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
594 static void carthw_sprot_unload(void)
595 {
596   free(sprot_items);
597   sprot_items = NULL;
598   sprot_item_count = sprot_item_alloc = 0;
599 }
600
601 static 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
618 void 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 */
627 static u8 prot_lk3_cmd, prot_lk3_data;
628
629 static 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
657 static 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
666 static 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
682 static 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
689 void carthw_prot_lk3_startup(void)
690 {
691   int ret;
692
693   elprintf(EL_STATUS, "lk3 prot emu startup");
694
695   // allocate space for bank0 backup
696   ret = PicoCartResize(Pico.romsize + 0x8000);
697   if (ret != 0) {
698     elprintf(EL_STATUS, "OOM");
699     return;
700   }
701   memcpy(Pico.rom + Pico.romsize, Pico.rom, 0x8000);
702
703   PicoCartMemSetup = carthw_prot_lk3_mem_setup;
704 }
705
706 // vim:ts=2:sw=2:expandtab