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