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 // fake only slave for now
98 if (a == 0x24 || a == 0x26)
99 return sh2_comm_faker(a);
102 return Pico32x.regs[a / 2];
105 static void p32x_reg_write8(u32 a, u32 d)
107 u16 *r = Pico32x.regs;
110 if (a == 1 && !(r[0] & 1)) {
121 r[0] = (r[0] & 0x83) | ((d << 8) & P32XS_FM);
133 static void p32x_reg_write16(u32 a, u32 d)
135 u16 *r = Pico32x.regs;
140 r[0] = (r[0] & 0x83) | (d & P32XS_FM);
144 if ((a & 0x30) == 0x20 && r[a / 2] != d) {
146 if (poll_undetect(&msh2_poll, P32XF_MSH2POLL))
147 // if SH2 is busy waiting, it needs to see the result ASAP
152 p32x_reg_write8(a + 1, d);
156 static u32 p32x_vdp_read16(u32 a)
160 return Pico32x.vdp_regs[a / 2];
163 static void p32x_vdp_write8(u32 a, u32 d)
165 u16 *r = Pico32x.vdp_regs;
168 // TODO: verify what's writeable
171 if (((r[0] & 3) == 0) != ((d & 3) == 0)) { // forced blanking changed
172 if (Pico.video.status & 8)
173 r[0x0a/2] |= P32XV_VBLK;
175 r[0x0a/2] &= ~P32XV_VBLK;
177 // priority inversion is handled in palette
178 if ((r[0] ^ d) & P32XV_PRI)
179 Pico32x.dirty_pal = 1;
180 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
184 Pico32x.pending_fb = d;
185 // if we are blanking and FS bit is changing
186 if ((r[0x0a/2] & P32XV_VBLK) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
188 Pico32xSwapDRAM(d ^ 1);
189 elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS);
195 static void p32x_vdp_write16(u32 a, u32 d)
197 p32x_vdp_write8(a | 1, d);
201 static u32 p32x_sh2reg_read16(u32 a)
205 if (poll_detect(&msh2_poll, a, ash2_pc(), P32XF_MSH2POLL))
209 return (Pico32x.regs[0] & P32XS_FM) | P32XS2_ADEN;
211 if ((a & 0x30) == 0x20)
212 return Pico32x.regs[a / 2];
217 static void p32x_sh2reg_write8(u32 a, u32 d)
221 static void p32x_sh2reg_write16(u32 a, u32 d)
225 if ((a & 0x30) == 0x20) {
226 Pico32x.regs[a/2] = d;
227 if (poll_undetect(&m68k_poll, P32XF_68KPOLL))
228 // dangerous, but let's just assume 68k program
229 // didn't issue STOP itself.
234 p32x_sh2reg_write8(a | 1, d);
237 // default 32x handlers
238 u32 PicoRead8_32x(u32 a)
241 if ((a & 0xffc0) == 0x5100) { // a15100
242 d = p32x_reg_read16(a);
246 if (!(Pico32x.regs[0] & 1))
249 if ((a & 0xfff0) == 0x5180) { // a15180
250 d = p32x_vdp_read16(a);
254 if ((a & 0xfe00) == 0x5200) { // a15200
255 d = Pico32xMem->pal[(a & 0x1ff) / 2];
260 if ((a & 0xfffc) == 0x30ec) { // a130ec
265 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
275 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
279 u32 PicoRead16_32x(u32 a)
282 if ((a & 0xffc0) == 0x5100) { // a15100
283 d = p32x_reg_read16(a);
287 if (!(Pico32x.regs[0] & 1))
290 if ((a & 0xfff0) == 0x5180) { // a15180
291 d = p32x_vdp_read16(a);
295 if ((a & 0xfe00) == 0x5200) { // a15200
296 d = Pico32xMem->pal[(a & 0x1ff) / 2];
301 if ((a & 0xfffc) == 0x30ec) { // a130ec
302 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
306 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
310 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
314 void PicoWrite8_32x(u32 a, u32 d)
316 if ((a & 0xfc00) == 0x5000)
317 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
319 if ((a & 0xffc0) == 0x5100) { // a15100
320 p32x_reg_write8(a, d);
324 if (!(Pico32x.regs[0] & 1))
327 if ((a & 0xfff0) == 0x5180) { // a15180
328 p32x_vdp_write8(a, d);
333 if ((a & 0xfe00) == 0x5200) { // a15200
334 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
335 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
336 Pico32x.dirty_pal = 1;
341 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
344 void PicoWrite16_32x(u32 a, u32 d)
346 if ((a & 0xfc00) == 0x5000)
347 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
349 if ((a & 0xffc0) == 0x5100) { // a15100
350 p32x_reg_write16(a, d);
354 if (!(Pico32x.regs[0] & 1))
357 if ((a & 0xfff0) == 0x5180) { // a15180
358 p32x_vdp_write16(a, d);
362 if ((a & 0xfe00) == 0x5200) { // a15200
363 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
364 Pico32x.dirty_pal = 1;
369 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
372 // hint vector is writeable
373 static void PicoWrite8_hint(u32 a, u32 d)
375 if ((a & 0xfffc) == 0x0070) {
376 Pico32xMem->m68k_rom[a ^ 1] = d;
380 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
383 static void PicoWrite16_hint(u32 a, u32 d)
385 if ((a & 0xfffc) == 0x0070) {
386 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
390 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
393 void Pico32xSwapDRAM(int b)
395 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
396 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
397 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
398 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
401 static void bank_switch(int b)
403 unsigned int rs, bank;
406 if (bank >= Pico.romsize) {
407 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
411 // 32X ROM (unbanked, XXX: consider mirroring?)
412 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
416 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
417 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
419 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
422 // -----------------------------------------------------------------
424 // -----------------------------------------------------------------
426 u32 pico32x_read8(u32 a)
429 if (a < sizeof(Pico32xMem->sh2_rom_m))
430 return Pico32xMem->sh2_rom_m[a ^ 1];
432 if ((a & 0x0ffc0000) == 0x06000000)
433 return Pico32xMem->sdram[(a & 0x3ffff) ^ 1];
435 if ((a & 0x0fc00000) == 0x02000000)
436 if ((a & 0x003fffff) < Pico.romsize)
437 return Pico.rom[(a & 0x3fffff) ^ 1];
439 if ((a & 0x0fffff00) == 0x4000) {
440 d = p32x_sh2reg_read16(a);
444 if ((a & 0x0fffff00) == 0x4100) {
445 d = p32x_vdp_read16(a);
449 if ((a & 0x0fffff00) == 0x4200) {
450 d = Pico32xMem->pal[(a & 0x1ff) / 2];
454 elprintf(EL_UIO, "sh2 unmapped r8 [%08x] %02x @%06x", a, d, ash2_pc());
463 elprintf(EL_32X, "sh2 r8 [%08x] %02x @%06x", a, d, ash2_pc());
467 u32 pico32x_read16(u32 a)
471 if (a < sizeof(Pico32xMem->sh2_rom_m))
472 return *(u16 *)(Pico32xMem->sh2_rom_m + a);
474 if ((a & 0x0ffc0000) == 0x06000000)
475 return ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2];
477 if ((a & 0x0fc00000) == 0x02000000)
478 if ((a & 0x003fffff) < Pico.romsize)
479 return ((u16 *)Pico.rom)[(a & 0x3fffff) / 2];
481 if ((a & 0x0fffff00) == 0x4000) {
482 d = p32x_sh2reg_read16(a);
486 if ((a & 0x0fffff00) == 0x4100) {
487 d = p32x_vdp_read16(a);
491 if ((a & 0x0fffff00) == 0x4200) {
492 d = Pico32xMem->pal[(a & 0x1ff) / 2];
496 elprintf(EL_UIO, "sh2 unmapped r16 [%08x] %04x @%06x", a, d, ash2_pc());
500 elprintf(EL_32X, "sh2 r16 [%08x] %04x @%06x", a, d, ash2_pc());
504 u32 pico32x_read32(u32 a)
506 // elprintf(EL_UIO, "sh2 r32 [%08x] %08x @%06x", a, d, ash2_pc());
507 return (pico32x_read16(a) << 16) | pico32x_read16(a + 2);
510 void pico32x_write8(u32 a, u32 d)
512 if ((a & 0x0ffffc00) == 0x4000)
513 elprintf(EL_32X, "sh2 w8 [%08x] %02x @%06x", a, d & 0xff, ash2_pc());
515 if ((a & 0x0ffc0000) == 0x06000000) {
516 Pico32xMem->sdram[(a & 0x3ffff) ^ 1] = d;
520 if ((a & 0x0ffe0000) == 0x04000000) {
521 u8 *dram = (u8 *)Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
522 dram[(a & 0x1ffff) ^ 1] = d;
526 if ((a & 0x0fffff00) == 0x4100) {
527 p32x_vdp_write8(a, d);
531 if ((a & 0x0fffff00) == 0x4000) {
532 p32x_sh2reg_write8(a, d);
536 elprintf(EL_UIO, "sh2 unmapped w8 [%08x] %02x @%06x", a, d & 0xff, ash2_pc());
539 void pico32x_write16(u32 a, u32 d)
541 if ((a & 0x0ffffc00) == 0x4000)
542 elprintf(EL_32X, "sh2 w16 [%08x] %04x @%06x", a, d & 0xffff, ash2_pc());
544 if ((a & 0x0ffc0000) == 0x06000000) {
545 ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2] = d;
549 if ((a & 0x0ffe0000) == 0x04000000) {
550 Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1][(a & 0x1ffff) / 2] = d;
554 if ((a & 0x0fffff00) == 0x4100) {
555 p32x_vdp_write16(a, d);
559 if ((a & 0x0ffffe00) == 0x4200) {
560 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
561 Pico32x.dirty_pal = 1;
565 if ((a & 0x0fffff00) == 0x4000) {
566 p32x_sh2reg_write16(a, d);
570 elprintf(EL_UIO, "sh2 unmapped w16 [%08x] %04x @%06x", a, d & 0xffff, ash2_pc());
573 void pico32x_write32(u32 a, u32 d)
575 // elprintf(EL_UIO, "sh2 w32 [%08x] %08x @%06x", a, d, ash2_pc());
576 pico32x_write16(a, d >> 16);
577 pico32x_write16(a + 2, d);
580 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
581 void PicoMemSetup32x(void)
588 Pico32xMem = calloc(1, sizeof(*Pico32xMem));
589 if (Pico32xMem == NULL) {
590 elprintf(EL_STATUS, "OOM");
595 ps = (unsigned short *)Pico32xMem->m68k_rom;
596 pl = (unsigned int *)Pico32xMem->m68k_rom;
597 for (i = 1; i < 0xc0/4; i++)
598 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
601 for (i = 0xc0/2; i < 0x100/2; i++)
606 ps[0xc2/2] = 0x2700; // move #0x2700,sr
607 ps[0xfe/2] = 0x60fe; // jump to self
609 ps[0xfe/2] = 0x4e75; // rts
612 // fill remaining mem with ROM
613 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
618 FILE *f = fopen("32X_M_BIOS.BIN", "rb");
621 printf("missing BIOS\n");
624 fread(Pico32xMem->sh2_rom_m, 1, sizeof(Pico32xMem->sh2_rom_m), f);
626 for (i = 0; i < sizeof(Pico32xMem->sh2_rom_m); i += 2) {
627 int t = Pico32xMem->sh2_rom_m[i];
628 Pico32xMem->sh2_rom_m[i] = Pico32xMem->sh2_rom_m[i + 1];
629 Pico32xMem->sh2_rom_m[i + 1] = t;
633 // cartridge area becomes unmapped
634 // XXX: we take the easy way and don't unmap ROM,
635 // so that we can avoid handling the RV bit.
636 // m68k_map_unmap(0x000000, 0x3fffff);
639 rs = sizeof(Pico32xMem->m68k_rom);
640 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
641 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
642 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
643 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
648 // 32X ROM (unbanked, XXX: consider mirroring?)
649 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
652 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
653 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);