1 #include "../pico_int.h"
4 static const char str_mars[] = "MARS";
6 struct Pico32xMem *Pico32xMem;
8 static void bank_switch(int b);
10 #define MSB8(x) ((x) >> 8)
16 static const u16 comm_fakevals[] = {
17 0x4d5f, 0x4f4b, // M_OK
18 0x535f, 0x4f4b, // S_OK
19 0x4D41, 0x5346, // MASF - Brutal Unleashed
20 0x5331, 0x4d31, // Darxide
23 0x0000, 0x0000, // eq for doom
24 0x0002, // Mortal Kombat
28 static u32 sh2_comm_faker(u32 a)
31 if (a == 0x28 && !p32x_csum_faked) {
33 return *(unsigned short *)(Pico.rom + 0x18e);
35 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
37 return comm_fakevals[f++];
41 static u32 p32x_reg_read16(u32 a)
46 if ((a & 0x30) == 0x20)
47 return sh2_comm_faker(a);
50 return Pico32x.regs[a / 2];
53 static void p32x_reg_write8(u32 a, u32 d)
55 u16 *r = Pico32x.regs;
58 if (a == 1 && !(r[0] & 1)) {
69 r[0] = (r[0] & 0x83) | ((d << 8) & P32XS_FM);
81 static void p32x_reg_write16(u32 a, u32 d)
83 u16 *r = Pico32x.regs;
88 r[0] = (r[0] & 0x83) | (d & P32XS_FM);
92 if ((a & 0x30) == 0x20) {
97 p32x_reg_write8(a + 1, d);
101 static u32 p32x_vdp_read16(u32 a)
105 return Pico32x.vdp_regs[a / 2];
108 static void p32x_vdp_write8(u32 a, u32 d)
110 u16 *r = Pico32x.vdp_regs;
113 // TODO: verify what's writeable
116 if (((r[0] & 3) == 0) != ((d & 3) == 0)) { // forced blanking changed
117 if (Pico.video.status & 8)
118 r[0x0a/2] |= P32XV_VBLK;
120 r[0x0a/2] &= ~P32XV_VBLK;
122 // priority inversion is handled in palette
123 if ((r[0] ^ d) & P32XV_PRI)
124 Pico32x.dirty_pal = 1;
125 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
129 Pico32x.pending_fb = d;
130 // if we are blanking and FS bit is changing
131 if ((r[0x0a/2] & P32XV_VBLK) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
133 Pico32xSwapDRAM(d ^ 1);
139 static void p32x_vdp_write16(u32 a, u32 d)
141 p32x_vdp_write8(a | 1, d);
145 static u32 p32x_sh2reg_read16(u32 a)
149 return (Pico32x.regs[0] & P32XS_FM) | P32XS2_ADEN;
155 static void p32x_sh2reg_write8(u32 a, u32 d)
159 static void p32x_sh2reg_write16(u32 a, u32 d)
163 if ((a & 0x30) == 0x20) {
164 Pico32x.regs[a/2] = d;
168 p32x_sh2reg_write8(a | 1, d);
171 // default 32x handlers
172 u32 PicoRead8_32x(u32 a)
175 if ((a & 0xffc0) == 0x5100) { // a15100
176 d = p32x_reg_read16(a);
180 if (!(Pico32x.regs[0] & 1))
183 if ((a & 0xfff0) == 0x5180) { // a15180
184 d = p32x_vdp_read16(a);
188 if ((a & 0xfe00) == 0x5200) { // a15200
189 d = Pico32xMem->pal[(a & 0x1ff) / 2];
194 if ((a & 0xfffc) == 0x30ec) { // a130ec
199 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
209 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
213 u32 PicoRead16_32x(u32 a)
216 if ((a & 0xffc0) == 0x5100) { // a15100
217 d = p32x_reg_read16(a);
221 if (!(Pico32x.regs[0] & 1))
224 if ((a & 0xfff0) == 0x5180) { // a15180
225 d = p32x_vdp_read16(a);
229 if ((a & 0xfe00) == 0x5200) { // a15200
230 d = Pico32xMem->pal[(a & 0x1ff) / 2];
235 if ((a & 0xfffc) == 0x30ec) { // a130ec
236 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
240 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
244 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
248 void PicoWrite8_32x(u32 a, u32 d)
250 if ((a & 0xfc00) == 0x5000)
251 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
253 if ((a & 0xffc0) == 0x5100) { // a15100
254 p32x_reg_write8(a, d);
258 if (!(Pico32x.regs[0] & 1))
261 if ((a & 0xfff0) == 0x5180) { // a15180
262 p32x_vdp_write8(a, d);
267 if ((a & 0xfe00) == 0x5200) { // a15200
268 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
269 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
270 Pico32x.dirty_pal = 1;
275 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
278 void PicoWrite16_32x(u32 a, u32 d)
280 if ((a & 0xfc00) == 0x5000)
281 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
283 if ((a & 0xffc0) == 0x5100) { // a15100
284 p32x_reg_write16(a, d);
288 if (!(Pico32x.regs[0] & 1))
291 if ((a & 0xfff0) == 0x5180) { // a15180
292 p32x_vdp_write16(a, d);
296 if ((a & 0xfe00) == 0x5200) { // a15200
297 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
298 Pico32x.dirty_pal = 1;
303 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
306 // hint vector is writeable
307 static void PicoWrite8_hint(u32 a, u32 d)
309 if ((a & 0xfffc) == 0x0070) {
310 Pico32xMem->m68k_rom[a ^ 1] = d;
314 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
317 static void PicoWrite16_hint(u32 a, u32 d)
319 if ((a & 0xfffc) == 0x0070) {
320 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
324 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
327 void Pico32xSwapDRAM(int b)
329 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
330 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
331 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
332 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
335 static void bank_switch(int b)
337 unsigned int rs, bank;
340 if (bank >= Pico.romsize) {
341 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
345 // 32X ROM (unbanked, XXX: consider mirroring?)
346 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
350 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
351 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
353 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
356 // -----------------------------------------------------------------
358 // -----------------------------------------------------------------
360 u32 pico32x_read8(u32 a)
363 if (a < sizeof(Pico32xMem->sh2_rom_m))
364 return Pico32xMem->sh2_rom_m[a ^ 1];
366 if ((a & 0x0ffc0000) == 0x06000000)
367 return Pico32xMem->sdram[(a & 0x3ffff) ^ 1];
369 if ((a & 0x0fc00000) == 0x02000000)
370 if ((a & 0x003fffff) < Pico.romsize)
371 return Pico.rom[(a & 0x3fffff) ^ 1];
373 if ((a & 0x0fffff00) == 0x4000) {
374 d = p32x_sh2reg_read16(a);
378 if ((a & 0x0fffff00) == 0x4100) {
379 d = p32x_vdp_read16(a);
383 if ((a & 0x0fffff00) == 0x4200) {
384 d = Pico32xMem->pal[(a & 0x1ff) / 2];
388 elprintf(EL_UIO, "sh2 unmapped r8 [%08x] %02x @%06x", a, d, ash2_pc());
397 elprintf(EL_32X, "sh2 r8 [%08x] %02x @%06x", a, d, ash2_pc());
401 u32 pico32x_read16(u32 a)
404 if (a < sizeof(Pico32xMem->sh2_rom_m))
405 return *(u16 *)(Pico32xMem->sh2_rom_m + a);
407 if ((a & 0x0ffc0000) == 0x06000000)
408 return ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2];
410 if ((a & 0x0fc00000) == 0x02000000)
411 if ((a & 0x003fffff) < Pico.romsize)
412 return ((u16 *)Pico.rom)[(a & 0x3fffff) / 2];
414 if ((a & 0x0fffff00) == 0x4000) {
415 d = p32x_sh2reg_read16(a);
419 if ((a & 0x0fffff00) == 0x4100) {
420 d = p32x_vdp_read16(a);
424 if ((a & 0x0fffff00) == 0x4200) {
425 d = Pico32xMem->pal[(a & 0x1ff) / 2];
429 elprintf(EL_UIO, "sh2 unmapped r16 [%08x] %04x @%06x", a, d, ash2_pc());
433 elprintf(EL_32X, "sh2 r16 [%08x] %04x @%06x", a, d, ash2_pc());
437 u32 pico32x_read32(u32 a)
439 // elprintf(EL_UIO, "sh2 r32 [%08x] %08x @%06x", a, d, ash2_pc());
440 return (pico32x_read16(a) << 16) | pico32x_read16(a + 2);
443 void pico32x_write8(u32 a, u32 d)
445 if ((a & 0x0ffffc00) == 0x4000)
446 elprintf(EL_32X, "sh2 w8 [%08x] %02x @%06x", a, d & 0xff, ash2_pc());
448 if ((a & 0x0ffc0000) == 0x06000000) {
449 Pico32xMem->sdram[(a & 0x3ffff) ^ 1] = d;
453 if ((a & 0x0fffff00) == 0x4100) {
454 p32x_vdp_write8(a, d);
458 if ((a & 0x0fffff00) == 0x4000) {
459 p32x_sh2reg_write8(a, d);
463 elprintf(EL_UIO, "sh2 unmapped w8 [%08x] %02x @%06x", a, d & 0xff, ash2_pc());
466 void pico32x_write16(u32 a, u32 d)
468 if ((a & 0x0ffffc00) == 0x4000)
469 elprintf(EL_32X, "sh2 w16 [%08x] %04x @%06x", a, d & 0xffff, ash2_pc());
471 if ((a & 0x0ffc0000) == 0x06000000) {
472 ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2] = d;
476 if ((a & 0x0fffff00) == 0x4100) {
477 p32x_vdp_write16(a, d);
481 if ((a & 0x0ffffe00) == 0x4200) {
482 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
483 Pico32x.dirty_pal = 1;
487 if ((a & 0x0fffff00) == 0x4000) {
488 p32x_sh2reg_write16(a, d);
492 elprintf(EL_UIO, "sh2 unmapped w16 [%08x] %04x @%06x", a, d & 0xffff, ash2_pc());
495 void pico32x_write32(u32 a, u32 d)
497 // elprintf(EL_UIO, "sh2 w32 [%08x] %08x @%06x", a, d, ash2_pc());
498 pico32x_write16(a, d >> 16);
499 pico32x_write16(a + 2, d);
502 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
503 void PicoMemSetup32x(void)
510 Pico32xMem = calloc(1, sizeof(*Pico32xMem));
511 if (Pico32xMem == NULL) {
512 elprintf(EL_STATUS, "OOM");
517 ps = (unsigned short *)Pico32xMem->m68k_rom;
518 pl = (unsigned int *)Pico32xMem->m68k_rom;
519 for (i = 1; i < 0xc0/4; i++)
520 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
523 for (i = 0xc0/2; i < 0x100/2; i++)
528 ps[0xc2/2] = 0x2700; // move #0x2700,sr
529 ps[0xfe/2] = 0x60fe; // jump to self
531 ps[0xfe/2] = 0x4e75; // rts
534 // fill remaining mem with ROM
535 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
540 FILE *f = fopen("32X_M_BIOS.BIN", "rb");
543 printf("missing BIOS\n");
546 fread(Pico32xMem->sh2_rom_m, 1, sizeof(Pico32xMem->sh2_rom_m), f);
548 for (i = 0; i < sizeof(Pico32xMem->sh2_rom_m); i += 2) {
549 int t = Pico32xMem->sh2_rom_m[i];
550 Pico32xMem->sh2_rom_m[i] = Pico32xMem->sh2_rom_m[i + 1];
551 Pico32xMem->sh2_rom_m[i + 1] = t;
555 // cartridge area becomes unmapped
556 // XXX: we take the easy way and don't unmap ROM,
557 // so that we can avoid handling the RV bit.
558 // m68k_map_unmap(0x000000, 0x3fffff);
561 rs = sizeof(Pico32xMem->m68k_rom);
562 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
563 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
564 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
565 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
570 // 32X ROM (unbanked, XXX: consider mirroring?)
571 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
574 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
575 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);