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