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 struct poll_det m68k_poll;
17 static struct poll_det msh2_poll;
19 #define POLL_THRESHOLD 6
21 static int poll_detect(struct poll_det *pd, u32 a, u32 pc, int flag)
25 if (a - 2 <= pd->addr && pd->addr <= a + 2 && pd->pc == pc) {
27 if (pd->cnt > POLL_THRESHOLD) {
28 if (!(Pico32x.emu_flags & flag)) {
29 elprintf(EL_32X, "%s poll addr %08x @ %06x",
30 flag == P32XF_68KPOLL ? "m68k" : (flag == P32XF_MSH2POLL ? "msh2" : "ssh2"), a, pc);
33 Pico32x.emu_flags |= flag;
44 static int poll_undetect(struct poll_det *pd, int flag)
47 if (pd->cnt > POLL_THRESHOLD)
49 pd->addr = pd->cnt = 0;
50 Pico32x.emu_flags &= ~flag;
58 static const u16 comm_fakevals[] = {
59 0x4d5f, 0x4f4b, // M_OK
60 0x535f, 0x4f4b, // S_OK
61 0x4D41, 0x5346, // MASF - Brutal Unleashed
62 0x5331, 0x4d31, // Darxide
65 0x0000, 0x0000, // eq for doom
66 0x0002, // Mortal Kombat
70 static u32 sh2_comm_faker(u32 a)
73 if (a == 0x28 && !p32x_csum_faked) {
75 return *(unsigned short *)(Pico.rom + 0x18e);
77 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
79 return comm_fakevals[f++];
83 static u32 p32x_reg_read16(u32 a)
88 if ((a & 0x30) == 0x20)
89 return sh2_comm_faker(a);
91 if (poll_detect(&m68k_poll, a, SekPc, P32XF_68KPOLL)) {
97 return Pico32x.regs[a / 2];
100 static void p32x_reg_write8(u32 a, u32 d)
102 u16 *r = Pico32x.regs;
105 if (a == 1 && !(r[0] & 1)) {
116 r[0] = (r[0] & 0x83) | ((d << 8) & P32XS_FM);
128 static void p32x_reg_write16(u32 a, u32 d)
130 u16 *r = Pico32x.regs;
135 r[0] = (r[0] & 0x83) | (d & P32XS_FM);
139 if ((a & 0x30) == 0x20) {
141 poll_undetect(&msh2_poll, P32XF_MSH2POLL);
145 p32x_reg_write8(a + 1, d);
149 static u32 p32x_vdp_read16(u32 a)
153 return Pico32x.vdp_regs[a / 2];
156 static void p32x_vdp_write8(u32 a, u32 d)
158 u16 *r = Pico32x.vdp_regs;
161 // TODO: verify what's writeable
164 if (((r[0] & 3) == 0) != ((d & 3) == 0)) { // forced blanking changed
165 if (Pico.video.status & 8)
166 r[0x0a/2] |= P32XV_VBLK;
168 r[0x0a/2] &= ~P32XV_VBLK;
170 // priority inversion is handled in palette
171 if ((r[0] ^ d) & P32XV_PRI)
172 Pico32x.dirty_pal = 1;
173 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
177 Pico32x.pending_fb = d;
178 // if we are blanking and FS bit is changing
179 if ((r[0x0a/2] & P32XV_VBLK) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
181 Pico32xSwapDRAM(d ^ 1);
182 elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS);
188 static void p32x_vdp_write16(u32 a, u32 d)
190 p32x_vdp_write8(a | 1, d);
194 static u32 p32x_sh2reg_read16(u32 a)
198 if (poll_detect(&msh2_poll, a, ash2_pc(), P32XF_MSH2POLL))
202 return (Pico32x.regs[0] & P32XS_FM) | P32XS2_ADEN;
204 if ((a & 0x30) == 0x20)
205 return Pico32x.regs[a / 2];
210 static void p32x_sh2reg_write8(u32 a, u32 d)
214 static void p32x_sh2reg_write16(u32 a, u32 d)
218 if ((a & 0x30) == 0x20) {
219 Pico32x.regs[a/2] = d;
220 if (poll_undetect(&m68k_poll, P32XF_68KPOLL))
221 // dangerous, but let's just assume 68k program
222 // didn't issue STOP itself.
227 p32x_sh2reg_write8(a | 1, d);
230 // default 32x handlers
231 u32 PicoRead8_32x(u32 a)
234 if ((a & 0xffc0) == 0x5100) { // a15100
235 d = p32x_reg_read16(a);
239 if (!(Pico32x.regs[0] & 1))
242 if ((a & 0xfff0) == 0x5180) { // a15180
243 d = p32x_vdp_read16(a);
247 if ((a & 0xfe00) == 0x5200) { // a15200
248 d = Pico32xMem->pal[(a & 0x1ff) / 2];
253 if ((a & 0xfffc) == 0x30ec) { // a130ec
258 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
268 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
272 u32 PicoRead16_32x(u32 a)
275 if ((a & 0xffc0) == 0x5100) { // a15100
276 d = p32x_reg_read16(a);
280 if (!(Pico32x.regs[0] & 1))
283 if ((a & 0xfff0) == 0x5180) { // a15180
284 d = p32x_vdp_read16(a);
288 if ((a & 0xfe00) == 0x5200) { // a15200
289 d = Pico32xMem->pal[(a & 0x1ff) / 2];
294 if ((a & 0xfffc) == 0x30ec) { // a130ec
295 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
299 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
303 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
307 void PicoWrite8_32x(u32 a, u32 d)
309 if ((a & 0xfc00) == 0x5000)
310 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
312 if ((a & 0xffc0) == 0x5100) { // a15100
313 p32x_reg_write8(a, d);
317 if (!(Pico32x.regs[0] & 1))
320 if ((a & 0xfff0) == 0x5180) { // a15180
321 p32x_vdp_write8(a, d);
326 if ((a & 0xfe00) == 0x5200) { // a15200
327 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
328 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
329 Pico32x.dirty_pal = 1;
334 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
337 void PicoWrite16_32x(u32 a, u32 d)
339 if ((a & 0xfc00) == 0x5000)
340 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
342 if ((a & 0xffc0) == 0x5100) { // a15100
343 p32x_reg_write16(a, d);
347 if (!(Pico32x.regs[0] & 1))
350 if ((a & 0xfff0) == 0x5180) { // a15180
351 p32x_vdp_write16(a, d);
355 if ((a & 0xfe00) == 0x5200) { // a15200
356 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
357 Pico32x.dirty_pal = 1;
362 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
365 // hint vector is writeable
366 static void PicoWrite8_hint(u32 a, u32 d)
368 if ((a & 0xfffc) == 0x0070) {
369 Pico32xMem->m68k_rom[a ^ 1] = d;
373 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
376 static void PicoWrite16_hint(u32 a, u32 d)
378 if ((a & 0xfffc) == 0x0070) {
379 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
383 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
386 void Pico32xSwapDRAM(int b)
388 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
389 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
390 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
391 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
394 static void bank_switch(int b)
396 unsigned int rs, bank;
399 if (bank >= Pico.romsize) {
400 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
404 // 32X ROM (unbanked, XXX: consider mirroring?)
405 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
409 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
410 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
412 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
415 // -----------------------------------------------------------------
417 // -----------------------------------------------------------------
419 u32 pico32x_read8(u32 a)
422 if (a < sizeof(Pico32xMem->sh2_rom_m))
423 return Pico32xMem->sh2_rom_m[a ^ 1];
425 if ((a & 0x0ffc0000) == 0x06000000)
426 return Pico32xMem->sdram[(a & 0x3ffff) ^ 1];
428 if ((a & 0x0fc00000) == 0x02000000)
429 if ((a & 0x003fffff) < Pico.romsize)
430 return Pico.rom[(a & 0x3fffff) ^ 1];
432 if ((a & 0x0fffff00) == 0x4000) {
433 d = p32x_sh2reg_read16(a);
437 if ((a & 0x0fffff00) == 0x4100) {
438 d = p32x_vdp_read16(a);
442 if ((a & 0x0fffff00) == 0x4200) {
443 d = Pico32xMem->pal[(a & 0x1ff) / 2];
447 elprintf(EL_UIO, "sh2 unmapped r8 [%08x] %02x @%06x", a, d, ash2_pc());
456 elprintf(EL_32X, "sh2 r8 [%08x] %02x @%06x", a, d, ash2_pc());
460 u32 pico32x_read16(u32 a)
463 if (a < sizeof(Pico32xMem->sh2_rom_m))
464 return *(u16 *)(Pico32xMem->sh2_rom_m + a);
466 if ((a & 0x0ffc0000) == 0x06000000)
467 return ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2];
469 if ((a & 0x0fc00000) == 0x02000000)
470 if ((a & 0x003fffff) < Pico.romsize)
471 return ((u16 *)Pico.rom)[(a & 0x3fffff) / 2];
473 if ((a & 0x0fffff00) == 0x4000) {
474 d = p32x_sh2reg_read16(a);
478 if ((a & 0x0fffff00) == 0x4100) {
479 d = p32x_vdp_read16(a);
483 if ((a & 0x0fffff00) == 0x4200) {
484 d = Pico32xMem->pal[(a & 0x1ff) / 2];
488 elprintf(EL_UIO, "sh2 unmapped r16 [%08x] %04x @%06x", a, d, ash2_pc());
492 elprintf(EL_32X, "sh2 r16 [%08x] %04x @%06x", a, d, ash2_pc());
496 u32 pico32x_read32(u32 a)
498 // elprintf(EL_UIO, "sh2 r32 [%08x] %08x @%06x", a, d, ash2_pc());
499 return (pico32x_read16(a) << 16) | pico32x_read16(a + 2);
502 void pico32x_write8(u32 a, u32 d)
504 if ((a & 0x0ffffc00) == 0x4000)
505 elprintf(EL_32X, "sh2 w8 [%08x] %02x @%06x", a, d & 0xff, ash2_pc());
507 if ((a & 0x0ffc0000) == 0x06000000) {
508 Pico32xMem->sdram[(a & 0x3ffff) ^ 1] = d;
512 if ((a & 0x0ffe0000) == 0x04000000) {
513 u8 *dram = (u8 *)Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
514 dram[(a & 0x1ffff) ^ 1] = d;
518 if ((a & 0x0fffff00) == 0x4100) {
519 p32x_vdp_write8(a, d);
523 if ((a & 0x0fffff00) == 0x4000) {
524 p32x_sh2reg_write8(a, d);
528 elprintf(EL_UIO, "sh2 unmapped w8 [%08x] %02x @%06x", a, d & 0xff, ash2_pc());
531 void pico32x_write16(u32 a, u32 d)
533 if ((a & 0x0ffffc00) == 0x4000)
534 elprintf(EL_32X, "sh2 w16 [%08x] %04x @%06x", a, d & 0xffff, ash2_pc());
536 if ((a & 0x0ffc0000) == 0x06000000) {
537 ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2] = d;
541 if ((a & 0x0ffe0000) == 0x04000000) {
542 Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1][(a & 0x1ffff) / 2] = d;
546 if ((a & 0x0fffff00) == 0x4100) {
547 p32x_vdp_write16(a, d);
551 if ((a & 0x0ffffe00) == 0x4200) {
552 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
553 Pico32x.dirty_pal = 1;
557 if ((a & 0x0fffff00) == 0x4000) {
558 p32x_sh2reg_write16(a, d);
562 elprintf(EL_UIO, "sh2 unmapped w16 [%08x] %04x @%06x", a, d & 0xffff, ash2_pc());
565 void pico32x_write32(u32 a, u32 d)
567 // elprintf(EL_UIO, "sh2 w32 [%08x] %08x @%06x", a, d, ash2_pc());
568 pico32x_write16(a, d >> 16);
569 pico32x_write16(a + 2, d);
572 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
573 void PicoMemSetup32x(void)
580 Pico32xMem = calloc(1, sizeof(*Pico32xMem));
581 if (Pico32xMem == NULL) {
582 elprintf(EL_STATUS, "OOM");
587 ps = (unsigned short *)Pico32xMem->m68k_rom;
588 pl = (unsigned int *)Pico32xMem->m68k_rom;
589 for (i = 1; i < 0xc0/4; i++)
590 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
593 for (i = 0xc0/2; i < 0x100/2; i++)
598 ps[0xc2/2] = 0x2700; // move #0x2700,sr
599 ps[0xfe/2] = 0x60fe; // jump to self
601 ps[0xfe/2] = 0x4e75; // rts
604 // fill remaining mem with ROM
605 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
610 FILE *f = fopen("32X_M_BIOS.BIN", "rb");
613 printf("missing BIOS\n");
616 fread(Pico32xMem->sh2_rom_m, 1, sizeof(Pico32xMem->sh2_rom_m), f);
618 for (i = 0; i < sizeof(Pico32xMem->sh2_rom_m); i += 2) {
619 int t = Pico32xMem->sh2_rom_m[i];
620 Pico32xMem->sh2_rom_m[i] = Pico32xMem->sh2_rom_m[i + 1];
621 Pico32xMem->sh2_rom_m[i + 1] = t;
625 // cartridge area becomes unmapped
626 // XXX: we take the easy way and don't unmap ROM,
627 // so that we can avoid handling the RV bit.
628 // m68k_map_unmap(0x000000, 0x3fffff);
631 rs = sizeof(Pico32xMem->m68k_rom);
632 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
633 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
634 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
635 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
640 // 32X ROM (unbanked, XXX: consider mirroring?)
641 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
644 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
645 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);