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" |
757f8dae |
11 | |
a12b1b29 |
12 | |
000f5335 |
13 | /* The SSFII mapper */ |
30f0fdd4 |
14 | static unsigned char ssf2_banks[8]; |
000f5335 |
15 | |
16 | static carthw_state_chunk carthw_ssf2_state[] = |
17 | { |
18 | { CHUNK_CARTHW, sizeof(ssf2_banks), &ssf2_banks }, |
19 | { 0, 0, NULL } |
20 | }; |
21 | |
22 | static 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 | |
47 | static void carthw_ssf2_mem_setup(void) |
48 | { |
49 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_ssf2_write8, 1); |
50 | } |
51 | |
52 | static 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 | |
59 | void 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 | |
45f2f245 |
75 | /* Common *-in-1 pirate mapper. |
76 | * Switches banks based on addr lines when /TIME is set. |
77 | * TODO: verify |
78 | */ |
79 | static unsigned int carthw_Xin1_baddr = 0; |
757f8dae |
80 | |
45f2f245 |
81 | static void carthw_Xin1_do(u32 a, int mask, int shift) |
757f8dae |
82 | { |
83 | int len; |
84 | |
45f2f245 |
85 | carthw_Xin1_baddr = a; |
86 | a &= mask; |
87 | a <<= shift; |
757f8dae |
88 | len = Pico.romsize - a; |
89 | if (len <= 0) { |
45f2f245 |
90 | elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a); |
757f8dae |
91 | return; |
92 | } |
93 | |
45f2f245 |
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); |
757f8dae |
97 | } |
98 | |
45f2f245 |
99 | static carthw_state_chunk carthw_Xin1_state[] = |
757f8dae |
100 | { |
45f2f245 |
101 | { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr }, |
102 | { 0, 0, NULL } |
103 | }; |
104 | |
105 | // TODO: test a0, reads, w16 |
106 | static 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); |
757f8dae |
114 | } |
115 | |
45f2f245 |
116 | static void carthw_Xin1_mem_setup(void) |
bdec53c9 |
117 | { |
45f2f245 |
118 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1); |
bdec53c9 |
119 | } |
120 | |
45f2f245 |
121 | static void carthw_Xin1_reset(void) |
757f8dae |
122 | { |
45f2f245 |
123 | carthw_Xin1_write8(0xa13000, 0); |
124 | } |
757f8dae |
125 | |
45f2f245 |
126 | static void carthw_Xin1_statef(void) |
127 | { |
128 | carthw_Xin1_write8(carthw_Xin1_baddr, 0); |
129 | } |
757f8dae |
130 | |
45f2f245 |
131 | void carthw_Xin1_startup(void) |
132 | { |
133 | elprintf(EL_STATUS, "X-in-1 mapper startup"); |
757f8dae |
134 | |
45f2f245 |
135 | PicoCartMemSetup = carthw_Xin1_mem_setup; |
136 | PicoResetHook = carthw_Xin1_reset; |
137 | PicoLoadStateHook = carthw_Xin1_statef; |
138 | carthw_chunks = carthw_Xin1_state; |
757f8dae |
139 | } |
140 | |
a12b1b29 |
141 | |
142 | /* Realtec, based on TascoDLX doc |
143 | * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt |
144 | */ |
145 | static int realtec_bank = 0x80000000, realtec_size = 0x80000000; |
a12b1b29 |
146 | |
45f2f245 |
147 | static void carthw_realtec_write8(u32 a, u32 d) |
a12b1b29 |
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); |
65ca3034 |
173 | |
a12b1b29 |
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); |
45f2f245 |
178 | if (realtec_size > Pico.romsize - realtec_bank) |
a12b1b29 |
179 | { |
180 | elprintf(EL_ANOMALY, "realtec: bank too large / out of range?"); |
181 | return; |
182 | } |
183 | |
45f2f245 |
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 | } |
a12b1b29 |
188 | } |
189 | } |
190 | |
0b35350d |
191 | static void carthw_realtec_reset(void) |
a12b1b29 |
192 | { |
193 | int i; |
45f2f245 |
194 | |
a12b1b29 |
195 | /* map boot code */ |
45f2f245 |
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); |
a12b1b29 |
201 | realtec_bank = realtec_size = 0x80000000; |
202 | } |
203 | |
204 | void carthw_realtec_startup(void) |
205 | { |
45f2f245 |
206 | int i; |
a12b1b29 |
207 | |
45f2f245 |
208 | elprintf(EL_STATUS, "Realtec mapper startup"); |
a12b1b29 |
209 | |
45f2f245 |
210 | // allocate additional bank for boot code |
211 | // (we know those ROMs have aligned size) |
a736af3e |
212 | i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE); |
213 | if (i != 0) { |
a12b1b29 |
214 | elprintf(EL_STATUS, "OOM"); |
215 | return; |
216 | } |
a12b1b29 |
217 | |
45f2f245 |
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 | |
a12b1b29 |
222 | PicoResetHook = carthw_realtec_reset; |
223 | } |
224 | |
0b35350d |
225 | /* Radica mapper, based on DevSter's info |
226 | * http://devster.monkeeh.com/sega/radica/ |
45f2f245 |
227 | * XXX: mostly the same as X-in-1, merge? |
0b35350d |
228 | */ |
45f2f245 |
229 | static u32 carthw_radica_read16(u32 a) |
0b35350d |
230 | { |
45f2f245 |
231 | if ((a & 0xffff00) != 0xa13000) |
232 | return PicoRead16_io(a); |
0b35350d |
233 | |
45f2f245 |
234 | carthw_Xin1_do(a, 0x7e, 15); |
0b35350d |
235 | |
236 | return 0; |
237 | } |
238 | |
45f2f245 |
239 | static void carthw_radica_mem_setup(void) |
240 | { |
241 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1); |
242 | } |
243 | |
0b35350d |
244 | static void carthw_radica_statef(void) |
245 | { |
45f2f245 |
246 | carthw_radica_read16(carthw_Xin1_baddr); |
0b35350d |
247 | } |
248 | |
249 | static void carthw_radica_reset(void) |
250 | { |
45f2f245 |
251 | carthw_radica_read16(0xa13000); |
0b35350d |
252 | } |
253 | |
254 | void carthw_radica_startup(void) |
255 | { |
45f2f245 |
256 | elprintf(EL_STATUS, "Radica mapper startup"); |
0b35350d |
257 | |
45f2f245 |
258 | PicoCartMemSetup = carthw_radica_mem_setup; |
259 | PicoResetHook = carthw_radica_reset; |
0b35350d |
260 | PicoLoadStateHook = carthw_radica_statef; |
45f2f245 |
261 | carthw_chunks = carthw_Xin1_state; |
0b35350d |
262 | } |
263 | |
30f0fdd4 |
264 | |
265 | /* Pier Solar. Based on my own research */ |
266 | static unsigned char pier_regs[8]; |
267 | static unsigned char pier_dump_prot; |
268 | |
269 | static carthw_state_chunk carthw_pier_state[] = |
270 | { |
271 | { CHUNK_CARTHW, sizeof(pier_regs), pier_regs }, |
272 | { CHUNK_CARTHW + 1, sizeof(pier_dump_prot), &pier_dump_prot }, |
273 | { 0, 0, NULL } |
274 | }; |
275 | |
276 | static void carthw_pier_write8(u32 a, u32 d) |
277 | { |
278 | u32 a8, target, base; |
279 | |
280 | if ((a & 0xffff00) != 0xa13000) { |
281 | PicoWrite8_io(a, d); |
282 | return; |
283 | } |
284 | |
285 | a8 = a & 0x0f; |
286 | pier_regs[a8 / 2] = d; |
287 | |
288 | elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc); |
289 | switch (a8) { |
290 | case 0x01: |
291 | break; |
292 | case 0x03: |
293 | if (!(pier_regs[0] & 2)) |
294 | goto unmapped; |
295 | target = 0x280000; |
296 | base = d << 19; |
297 | goto do_map; |
298 | case 0x05: |
299 | if (!(pier_regs[0] & 2)) |
300 | goto unmapped; |
301 | target = 0x300000; |
302 | base = d << 19; |
303 | goto do_map; |
304 | case 0x07: |
305 | if (!(pier_regs[0] & 2)) |
306 | goto unmapped; |
307 | target = 0x380000; |
308 | base = d << 19; |
309 | goto do_map; |
310 | case 0x09: |
311 | // TODO |
312 | break; |
313 | case 0x0b: |
314 | // eeprom read |
315 | default: |
316 | unmapped: |
317 | //elprintf(EL_UIO, "pier w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc); |
318 | elprintf(EL_STATUS, "-- unmapped w8 [%06x] %02x @%06x", a, d & 0xffff, SekPc); |
319 | break; |
320 | } |
321 | return; |
322 | |
323 | do_map: |
324 | if (base + 0x80000 > Pico.romsize) { |
325 | elprintf(EL_ANOMALY|EL_STATUS, "pier: missing bank @ %06x", base); |
326 | return; |
327 | } |
328 | cpu68k_map_set(m68k_read8_map, target, target + 0x80000 - 1, Pico.rom + base, 0); |
329 | cpu68k_map_set(m68k_read16_map, target, target + 0x80000 - 1, Pico.rom + base, 0); |
330 | } |
331 | |
332 | static void carthw_pier_write16(u32 a, u32 d) |
333 | { |
334 | if ((a & 0xffff00) != 0xa13000) { |
335 | PicoWrite16_io(a, d); |
336 | return; |
337 | } |
338 | |
339 | elprintf(EL_UIO, "pier w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
340 | carthw_pier_write8(a + 1, d); |
341 | } |
342 | |
343 | static u32 carthw_pier_read8(u32 a) |
344 | { |
345 | if ((a & 0xffff00) != 0xa13000) |
346 | return PicoRead8_io(a); |
347 | |
348 | if (a == 0xa1300b) |
349 | return 0; // TODO |
350 | |
351 | elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc); |
352 | return 0; |
353 | } |
354 | |
355 | static void carthw_pier_statef(void); |
356 | |
357 | static u32 carthw_pier_prot_read8(u32 a) |
358 | { |
359 | /* it takes more than just these reads here to disable ROM protection, |
360 | * but for game emulation purposes this is enough. */ |
361 | if (pier_dump_prot > 0) |
362 | pier_dump_prot--; |
363 | if (pier_dump_prot == 0) { |
364 | carthw_pier_statef(); |
365 | elprintf(EL_STATUS, "prot off on r8 @%06x", SekPc); |
366 | } |
367 | elprintf(EL_UIO, "pier r8 [%06x] @%06x", a, SekPc); |
368 | |
369 | return Pico.rom[(a & 0x7fff) ^ 1]; |
370 | } |
371 | |
372 | static void carthw_pier_mem_setup(void) |
373 | { |
374 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_pier_write8, 1); |
375 | cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, carthw_pier_write16, 1); |
376 | cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, carthw_pier_read8, 1); |
377 | } |
378 | |
379 | static void carthw_pier_prot_mem_setup(int prot_enable) |
380 | { |
381 | if (prot_enable) { |
382 | /* the dump protection.. */ |
383 | int a; |
384 | for (a = 0x000000; a < 0x400000; a += M68K_BANK_SIZE) { |
385 | cpu68k_map_set(m68k_read8_map, a, a + 0xffff, Pico.rom + Pico.romsize, 0); |
386 | cpu68k_map_set(m68k_read16_map, a, a + 0xffff, Pico.rom + Pico.romsize, 0); |
387 | } |
388 | cpu68k_map_set(m68k_read8_map, M68K_BANK_SIZE, M68K_BANK_SIZE * 2 - 1, |
389 | carthw_pier_prot_read8, 1); |
390 | } |
391 | else { |
392 | cpu68k_map_set(m68k_read8_map, 0, 0x27ffff, Pico.rom, 0); |
393 | cpu68k_map_set(m68k_read16_map, 0, 0x27ffff, Pico.rom, 0); |
394 | } |
395 | } |
396 | |
397 | static void carthw_pier_statef(void) |
398 | { |
399 | carthw_pier_prot_mem_setup(pier_dump_prot); |
400 | |
401 | if (!pier_dump_prot) { |
402 | /* setup all banks */ |
403 | u32 r0 = pier_regs[0]; |
404 | carthw_pier_write8(0xa13001, 3); |
405 | carthw_pier_write8(0xa13003, pier_regs[1]); |
406 | carthw_pier_write8(0xa13005, pier_regs[2]); |
407 | carthw_pier_write8(0xa13007, pier_regs[3]); |
408 | carthw_pier_write8(0xa13001, r0); |
409 | } |
410 | } |
411 | |
412 | static void carthw_pier_reset(void) |
413 | { |
414 | pier_regs[0] = 1; |
415 | pier_regs[1] = pier_regs[2] = pier_regs[3] = 0; |
416 | pier_dump_prot = 3; |
417 | carthw_pier_statef(); |
418 | } |
419 | |
420 | void carthw_pier_startup(void) |
421 | { |
422 | int i; |
423 | |
424 | elprintf(EL_STATUS, "Pier Solar mapper startup"); |
425 | |
426 | // mostly same as for realtec.. |
427 | i = PicoCartResize(Pico.romsize + M68K_BANK_SIZE); |
428 | if (i != 0) { |
429 | elprintf(EL_STATUS, "OOM"); |
430 | return; |
431 | } |
432 | |
433 | // create dump protection bank |
434 | for (i = 0; i < M68K_BANK_SIZE; i += 0x8000) |
435 | memcpy(Pico.rom + Pico.romsize + i, Pico.rom, 0x8000); |
436 | |
437 | PicoCartMemSetup = carthw_pier_mem_setup; |
438 | PicoResetHook = carthw_pier_reset; |
439 | PicoLoadStateHook = carthw_pier_statef; |
440 | carthw_chunks = carthw_pier_state; |
441 | } |
442 | |
000f5335 |
443 | /* Simple unlicensed ROM protection emulation */ |
444 | static struct { |
30f0fdd4 |
445 | u32 addr; |
446 | u32 mask; |
447 | u16 val; |
448 | u16 readonly; |
000f5335 |
449 | } *sprot_items; |
450 | static int sprot_item_alloc; |
451 | static int sprot_item_count; |
452 | |
453 | static u16 *carthw_sprot_get_val(u32 a, int rw_only) |
454 | { |
30f0fdd4 |
455 | int i; |
456 | |
457 | for (i = 0; i < sprot_item_count; i++) |
458 | if ((a & sprot_items[i].mask) == sprot_items[i].addr) |
459 | if (!rw_only || !sprot_items[i].readonly) |
460 | return &sprot_items[i].val; |
000f5335 |
461 | |
30f0fdd4 |
462 | return NULL; |
000f5335 |
463 | } |
464 | |
465 | static u32 PicoRead8_sprot(u32 a) |
466 | { |
467 | u16 *val; |
468 | u32 d; |
469 | |
470 | if (0xa10000 <= a && a < 0xa12000) |
471 | return PicoRead8_io(a); |
472 | |
473 | val = carthw_sprot_get_val(a, 0); |
474 | if (val != NULL) { |
475 | d = *val; |
476 | if (!(a & 1)) |
477 | d >>= 8; |
478 | elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc); |
479 | return d; |
480 | } |
481 | else { |
482 | elprintf(EL_UIO, "prot r8 [%06x] MISS @%06x", a, SekPc); |
483 | return 0; |
484 | } |
485 | } |
486 | |
487 | static u32 PicoRead16_sprot(u32 a) |
488 | { |
489 | u16 *val; |
490 | |
491 | if (0xa10000 <= a && a < 0xa12000) |
492 | return PicoRead16_io(a); |
493 | |
494 | val = carthw_sprot_get_val(a, 0); |
495 | if (val != NULL) { |
496 | elprintf(EL_UIO, "prot r16 [%06x] %04x @%06x", a, *val, SekPc); |
497 | return *val; |
498 | } |
499 | else { |
500 | elprintf(EL_UIO, "prot r16 [%06x] MISS @%06x", a, SekPc); |
501 | return 0; |
502 | } |
503 | } |
504 | |
505 | static void PicoWrite8_sprot(u32 a, u32 d) |
506 | { |
507 | u16 *val; |
508 | |
509 | if (0xa10000 <= a && a < 0xa12000) { |
510 | PicoWrite8_io(a, d); |
511 | return; |
512 | } |
513 | |
514 | val = carthw_sprot_get_val(a, 1); |
515 | if (val != NULL) { |
516 | if (a & 1) |
517 | *val = (*val & 0xff00) | (d | 0xff); |
518 | else |
519 | *val = (*val & 0x00ff) | (d << 8); |
520 | elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
521 | } |
522 | else |
523 | elprintf(EL_UIO, "prot w8 [%06x] %02x MISS @%06x", a, d & 0xff, SekPc); |
524 | } |
525 | |
526 | static void PicoWrite16_sprot(u32 a, u32 d) |
527 | { |
528 | u16 *val; |
529 | |
530 | if (0xa10000 <= a && a < 0xa12000) { |
531 | PicoWrite16_io(a, d); |
532 | return; |
533 | } |
534 | |
535 | val = carthw_sprot_get_val(a, 1); |
536 | if (val != NULL) { |
537 | *val = d; |
538 | elprintf(EL_UIO, "prot w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
539 | } |
540 | else |
541 | elprintf(EL_UIO, "prot w16 [%06x] %04x MISS @%06x", a, d & 0xffff, SekPc); |
542 | } |
543 | |
544 | void carthw_sprot_new_location(unsigned int a, unsigned int mask, unsigned short val, int is_ro) |
545 | { |
546 | if (sprot_items == NULL) { |
547 | sprot_items = calloc(8, sizeof(sprot_items[0])); |
548 | sprot_item_alloc = 8; |
549 | sprot_item_count = 0; |
550 | } |
551 | |
552 | if (sprot_item_count == sprot_item_alloc) { |
553 | void *tmp; |
554 | sprot_item_alloc *= 2; |
555 | tmp = realloc(sprot_items, sprot_item_alloc); |
556 | if (tmp == NULL) { |
557 | elprintf(EL_STATUS, "OOM"); |
558 | return; |
559 | } |
560 | sprot_items = tmp; |
561 | } |
562 | |
563 | sprot_items[sprot_item_count].addr = a; |
564 | sprot_items[sprot_item_count].mask = mask; |
565 | sprot_items[sprot_item_count].val = val; |
566 | sprot_items[sprot_item_count].readonly = is_ro; |
567 | sprot_item_count++; |
568 | } |
569 | |
570 | static void carthw_sprot_unload(void) |
571 | { |
572 | free(sprot_items); |
573 | sprot_items = NULL; |
574 | sprot_item_count = sprot_item_alloc = 0; |
575 | } |
576 | |
577 | static void carthw_sprot_mem_setup(void) |
578 | { |
579 | int start; |
580 | |
581 | // map ROM - 0x7fffff, /TIME areas (which are tipically used) |
582 | start = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
583 | cpu68k_map_set(m68k_read8_map, start, 0x7fffff, PicoRead8_sprot, 1); |
584 | cpu68k_map_set(m68k_read16_map, start, 0x7fffff, PicoRead16_sprot, 1); |
585 | cpu68k_map_set(m68k_write8_map, start, 0x7fffff, PicoWrite8_sprot, 1); |
586 | cpu68k_map_set(m68k_write16_map, start, 0x7fffff, PicoWrite16_sprot, 1); |
587 | |
588 | cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_sprot, 1); |
589 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_sprot, 1); |
590 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_sprot, 1); |
591 | cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_sprot, 1); |
592 | } |
593 | |
594 | void carthw_sprot_startup(void) |
595 | { |
596 | elprintf(EL_STATUS, "Prot emu startup"); |
597 | |
598 | PicoCartMemSetup = carthw_sprot_mem_setup; |
599 | PicoCartUnloadHook = carthw_sprot_unload; |
600 | } |
601 | |
602 | /* Protection emulation for Lion King 3. Credits go to Haze */ |
603 | static u8 prot_lk3_cmd, prot_lk3_data; |
604 | |
605 | static u32 PicoRead8_plk3(u32 a) |
606 | { |
607 | u32 d = 0; |
608 | switch (prot_lk3_cmd) { |
609 | case 1: d = prot_lk3_data >> 1; break; |
610 | case 2: // nibble rotate |
611 | d = ((prot_lk3_data >> 4) | (prot_lk3_data << 4)) & 0xff; |
612 | break; |
613 | case 3: // bit rotate |
614 | d = prot_lk3_data; |
615 | d = (d >> 4) | (d << 4); |
616 | d = ((d & 0xcc) >> 2) | ((d & 0x33) << 2); |
617 | d = ((d & 0xaa) >> 1) | ((d & 0x55) << 1); |
618 | break; |
619 | /* Top Fighter 2000 MK VIII (Unl) |
620 | case 0x98: d = 0x50; break; // prot_lk3_data == a8 here |
621 | case 0x67: d = 0xde; break; // prot_lk3_data == 7b here (rot!) |
622 | case 0xb5: d = 0x9f; break; // prot_lk3_data == 4a |
623 | */ |
624 | default: |
625 | elprintf(EL_UIO, "unhandled prot cmd %02x @%06x", prot_lk3_cmd, SekPc); |
626 | break; |
627 | } |
628 | |
629 | elprintf(EL_UIO, "prot r8 [%06x] %02x @%06x", a, d, SekPc); |
630 | return d; |
631 | } |
632 | |
633 | static void PicoWrite8_plk3p(u32 a, u32 d) |
634 | { |
635 | elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
636 | if (a & 2) |
637 | prot_lk3_cmd = d; |
638 | else |
639 | prot_lk3_data = d; |
640 | } |
641 | |
642 | static void PicoWrite8_plk3b(u32 a, u32 d) |
643 | { |
644 | int addr; |
645 | |
646 | elprintf(EL_UIO, "prot w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
647 | addr = d << 15; |
648 | if (addr + 0x8000 > Pico.romsize) { |
649 | elprintf(EL_UIO|EL_ANOMALY, "prot_lk3: bank too large: %02x", d); |
650 | return; |
651 | } |
652 | if (addr == 0) |
653 | memcpy(Pico.rom, Pico.rom + Pico.romsize, 0x8000); |
654 | else |
655 | memcpy(Pico.rom, Pico.rom + addr, 0x8000); |
656 | } |
657 | |
658 | static void carthw_prot_lk3_mem_setup(void) |
659 | { |
660 | cpu68k_map_set(m68k_read8_map, 0x600000, 0x7fffff, PicoRead8_plk3, 1); |
661 | cpu68k_map_set(m68k_write8_map, 0x600000, 0x6fffff, PicoWrite8_plk3p, 1); |
662 | cpu68k_map_set(m68k_write8_map, 0x700000, 0x7fffff, PicoWrite8_plk3b, 1); |
663 | } |
664 | |
665 | void carthw_prot_lk3_startup(void) |
666 | { |
a736af3e |
667 | int ret; |
000f5335 |
668 | |
669 | elprintf(EL_STATUS, "lk3 prot emu startup"); |
670 | |
671 | // allocate space for bank0 backup |
a736af3e |
672 | ret = PicoCartResize(Pico.romsize + 0x8000); |
673 | if (ret != 0) { |
000f5335 |
674 | elprintf(EL_STATUS, "OOM"); |
675 | return; |
676 | } |
000f5335 |
677 | memcpy(Pico.rom + Pico.romsize, Pico.rom, 0x8000); |
678 | |
679 | PicoCartMemSetup = carthw_prot_lk3_mem_setup; |
680 | } |
681 | |