1 #include "../pico_int.h"
4 static const char str_mars[] = "MARS";
6 struct Pico32xMem *Pico32xMem;
8 static void bank_switch(int b);
11 #define POLL_THRESHOLD 6
14 u32 addr, cycles, cyc_max;
17 static struct poll_det m68k_poll, sh2_poll[2];
19 static int p32x_poll_detect(struct poll_det *pd, u32 a, u32 cycles, int is_vdp)
21 int ret = 0, flag = pd->flag;
26 if (a - 2 <= pd->addr && pd->addr <= a + 2 && cycles - pd->cycles < pd->cyc_max) {
28 if (pd->cnt > POLL_THRESHOLD) {
29 if (!(Pico32x.emu_flags & flag)) {
30 elprintf(EL_32X, "%s poll addr %08x, cyc %u",
31 flag & (P32XF_68KPOLL|P32XF_68KVPOLL) ? "m68k" :
32 (flag & (P32XF_MSH2POLL|P32XF_MSH2VPOLL) ? "msh2" : "ssh2"), a, cycles - pd->cycles);
35 Pico32x.emu_flags |= flag;
46 static int p32x_poll_undetect(struct poll_det *pd, int is_vdp)
48 int ret = 0, flag = pd->flag;
50 flag <<= 3; // VDP only
52 flag |= flag << 3; // both
53 if (Pico32x.emu_flags & flag) {
54 elprintf(EL_32X, "poll %02x -> %02x", Pico32x.emu_flags, Pico32x.emu_flags & ~flag);
57 Pico32x.emu_flags &= ~flag;
58 pd->addr = pd->cnt = 0;
62 void p32x_poll_event(int cpu_mask, int is_vdp)
65 p32x_poll_undetect(&sh2_poll[0], is_vdp);
67 p32x_poll_undetect(&sh2_poll[1], is_vdp);
74 static const u16 comm_fakevals[] = {
75 0x4d5f, 0x4f4b, // M_OK
76 0x535f, 0x4f4b, // S_OK
77 0x4D41, 0x5346, // MASF - Brutal Unleashed
78 0x5331, 0x4d31, // Darxide
81 0x0000, 0x0000, // eq for doom
82 0x0002, // Mortal Kombat
86 static u32 sh2_comm_faker(u32 a)
89 if (a == 0x28 && !p32x_csum_faked) {
91 return *(unsigned short *)(Pico.rom + 0x18e);
93 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
95 return comm_fakevals[f++];
101 unsigned int sar0, dar0, tcr0; // src addr, dst addr, transfer count
102 unsigned int chcr0; // chan ctl
103 unsigned int sar1, dar1, tcr1; // same for chan 1
109 static void dma_68k2sh2_do(void)
111 unsigned short *dreqlen = &Pico32x.regs[0x10 / 2];
114 if (dmac0->tcr0 != *dreqlen)
115 elprintf(EL_32X|EL_ANOMALY, "tcr0 and dreq len differ: %d != %d", dmac0->tcr0, *dreqlen);
117 // HACK: assume bus is busy and SH2 is halted
118 // XXX: use different mechanism for this, not poll det
119 Pico32x.emu_flags |= P32XF_MSH2POLL; // id ? P32XF_SSH2POLL : P32XF_MSH2POLL;
121 for (i = 0; i < Pico32x.dmac_ptr && dmac0->tcr0 > 0; i++) {
122 extern void p32x_sh2_write16(u32 a, u32 d, int id);
123 elprintf(EL_32X, "dmaw [%08x] %04x, left %d", dmac0->dar0, Pico32x.dmac_fifo[i], *dreqlen);
124 p32x_sh2_write16(dmac0->dar0, Pico32x.dmac_fifo[i], 0);
130 Pico32x.dmac_ptr = 0; // HACK
131 Pico32x.regs[6 / 2] &= ~P32XS_FULL;
133 Pico32x.regs[6 / 2] &= ~P32XS_68S; // transfer complete
134 if (dmac0->tcr0 == 0) {
135 dmac0->chcr0 |= 2; // DMA has ended normally
136 p32x_poll_undetect(&sh2_poll[0], 0);
140 // ------------------------------------------------------------------
143 static u32 p32x_reg_read16(u32 a)
147 if (a == 2) // INTM, INTS
148 return ((Pico32x.sh2irqi[0] & P32XI_CMD) >> 4) | ((Pico32x.sh2irqi[1] & P32XI_CMD) >> 3);
150 if ((a & 0x30) == 0x20)
151 return sh2_comm_faker(a);
153 if ((a & 0x30) == 0x20 && p32x_poll_detect(&m68k_poll, a, SekCyclesDoneT(), 0)) {
158 if ((a & 0x30) == 0x30)
159 return p32x_pwm_read16(a);
161 return Pico32x.regs[a / 2];
164 static void p32x_reg_write8(u32 a, u32 d)
166 u16 *r = Pico32x.regs;
169 // for things like bset on comm port
172 if (a == 1 && !(r[0] & 1)) {
182 case 0: // adapter ctl
183 r[0] = (r[0] & 0x83) | ((d << 8) & P32XS_FM);
186 if ((d & 1) && !(Pico32x.sh2irqi[0] & P32XI_CMD)) {
187 Pico32x.sh2irqi[0] |= P32XI_CMD;
191 if ((d & 2) && !(Pico32x.sh2irqi[1] & P32XI_CMD)) {
192 Pico32x.sh2irqi[1] |= P32XI_CMD;
205 r[6 / 2] = (r[6 / 2] & P32XS_FULL) | (d & (P32XS_68S|P32XS_DMA|P32XS_RV));
212 if ((a & 0x30) == 0x20) {
215 if (p32x_poll_undetect(&sh2_poll[0], 0) || p32x_poll_undetect(&sh2_poll[1], 0))
216 // if some SH2 is busy waiting, it needs to see the result ASAP
222 static void p32x_reg_write16(u32 a, u32 d)
224 u16 *r = Pico32x.regs;
227 // for things like bset on comm port
231 case 0x00: // adapter ctl
232 r[0] = (r[0] & 0x83) | (d & P32XS_FM);
234 case 0x10: // DREQ len
237 case 0x12: // FIFO reg
238 if (!(r[6 / 2] & P32XS_68S)) {
239 elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO w16 without 68S?");
242 if (Pico32x.dmac_ptr < DMAC_FIFO_LEN) {
243 Pico32x.dmac_fifo[Pico32x.dmac_ptr++] = d;
244 if ((Pico32x.dmac_ptr & 3) == 0 && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1))
246 if (Pico32x.dmac_ptr == DMAC_FIFO_LEN)
247 r[6 / 2] |= P32XS_FULL;
253 if ((a & 0x38) == 0x08) {
258 else if ((a & 0x30) == 0x20 && r[a / 2] != d) {
260 if (p32x_poll_undetect(&sh2_poll[0], 0) || p32x_poll_undetect(&sh2_poll[1], 0))
261 // if some SH2 is busy waiting, it needs to see the result ASAP
266 else if ((a & 0x30) == 0x30) {
267 p32x_pwm_write16(a, d);
271 p32x_reg_write8(a + 1, d);
274 // ------------------------------------------------------------------
276 static u32 p32x_vdp_read16(u32 a)
280 return Pico32x.vdp_regs[a / 2];
283 static void p32x_vdp_write8(u32 a, u32 d)
285 u16 *r = Pico32x.vdp_regs;
288 // for FEN checks between writes
291 // TODO: verify what's writeable
294 // priority inversion is handled in palette
295 if ((r[0] ^ d) & P32XV_PRI)
296 Pico32x.dirty_pal = 1;
297 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
299 elprintf(EL_32X|EL_ANOMALY, "TODO: mode3");
301 case 0x05: // fill len
306 Pico32x.pending_fb = d;
307 // if we are blanking and FS bit is changing
308 if (((r[0x0a/2] & P32XV_VBLK) || (r[0] & P32XV_Mx) == 0) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
310 Pico32xSwapDRAM(d ^ 1);
311 elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS);
317 static void p32x_vdp_write16(u32 a, u32 d)
320 if (a == 6) { // fill start
321 Pico32x.vdp_regs[6 / 2] = d;
324 if (a == 8) { // fill data
325 u16 *dram = Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
326 int len = Pico32x.vdp_regs[4 / 2] + 1;
327 a = Pico32x.vdp_regs[6 / 2];
330 a = (a & 0xff00) | ((a + 1) & 0xff);
332 Pico32x.vdp_regs[6 / 2] = a;
333 Pico32x.vdp_regs[8 / 2] = d;
337 p32x_vdp_write8(a | 1, d);
340 // ------------------------------------------------------------------
343 static u32 p32x_sh2reg_read16(u32 a, int cpuid)
345 u16 *r = Pico32x.regs;
349 case 0x00: // adapter/irq ctl
350 return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0] | Pico32x.sh2irq_mask[cpuid];
351 case 0x04: // H count
352 return Pico32x.sh2_regs[4 / 2];
353 case 0x10: // DREQ len
358 if ((a & 0x38) == 0x08)
361 if ((a & 0x30) == 0x20) {
362 if (p32x_poll_detect(&sh2_poll[cpuid], a, ash2_cycles_done(), 0))
366 if ((a & 0x30) == 0x30) {
367 sh2_poll[cpuid].cnt = 0;
368 return p32x_pwm_read16(a);
374 static void p32x_sh2reg_write8(u32 a, u32 d, int cpuid)
379 Pico32x.regs[0] &= ~P32XS_FM;
380 Pico32x.regs[0] |= (d << 8) & P32XS_FM;
383 Pico32x.sh2irq_mask[cpuid] = d & 0x8f;
384 Pico32x.sh2_regs[0] &= ~0x80;
385 Pico32x.sh2_regs[0] |= d & 0x80;
389 Pico32x.sh2_regs[4 / 2] = d & 0xff;
393 if ((a & 0x30) == 0x20) {
394 u8 *r8 = (u8 *)Pico32x.regs;
396 p32x_poll_undetect(&m68k_poll, 0);
397 p32x_poll_undetect(&sh2_poll[cpuid ^ 1], 0);
402 static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
407 if ((a & 0x30) == 0x20 && Pico32x.regs[a/2] != d) {
408 Pico32x.regs[a / 2] = d;
409 p32x_poll_undetect(&m68k_poll, 0);
410 p32x_poll_undetect(&sh2_poll[cpuid ^ 1], 0);
414 else if ((a & 0x30) == 0x30) {
415 p32x_pwm_write16(a, d);
421 Pico32x.regs[0] &= ~P32XS_FM;
422 Pico32x.regs[0] |= d & P32XS_FM;
424 case 0x14: Pico32x.sh2irqs &= ~P32XI_VRES; goto irls;
425 case 0x16: Pico32x.sh2irqs &= ~P32XI_VINT; goto irls;
426 case 0x18: Pico32x.sh2irqs &= ~P32XI_HINT; goto irls;
427 case 0x1a: Pico32x.sh2irqi[cpuid] &= ~P32XI_CMD; goto irls;
429 Pico32x.sh2irqs &= ~P32XI_PWM;
430 p32x_pwm_irq_check(0);
434 p32x_sh2reg_write8(a | 1, d, cpuid);
441 // ------------------------------------------------------------------
442 // SH2 internal peripherals
443 static u32 sh2_peripheral_read8(u32 a, int id)
445 u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
453 elprintf(EL_32X, "%csh2 peri r8 [%08x] %02x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
457 static u32 sh2_peripheral_read32(u32 a, int id)
461 d = Pico32xMem->sh2_peri_regs[id][a / 4];
463 elprintf(EL_32X, "%csh2 peri r32 [%08x] %08x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
467 static void sh2_peripheral_write8(u32 a, u32 d, int id)
469 u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
470 elprintf(EL_32X, "%csh2 peri w8 [%08x] %02x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
476 static void sh2_peripheral_write32(u32 a, u32 d, int id)
478 u32 *r = Pico32xMem->sh2_peri_regs[id];
479 elprintf(EL_32X, "%csh2 peri w32 [%08x] %08x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
485 // division unit (TODO: verify):
486 case 0x104: // DVDNT: divident L, starts divide
487 elprintf(EL_32X, "%csh2 divide %08x / %08x", id ? 's' : 'm', d, r[0x100 / 4]);
489 signed int divisor = r[0x100 / 4];
490 r[0x118 / 4] = r[0x110 / 4] = (signed int)d % divisor;
491 r[0x104 / 4] = r[0x11c / 4] = r[0x114 / 4] = (signed int)d / divisor;
495 elprintf(EL_32X, "%csh2 divide %08x%08x / %08x @%08x",
496 id ? 's' : 'm', r[0x110 / 4], d, r[0x100 / 4], sh2_pc(id));
498 signed long long divident = (signed long long)r[0x110 / 4] << 32 | d;
499 signed int divisor = r[0x100 / 4];
500 // XXX: undocumented mirroring to 0x118,0x11c?
501 r[0x118 / 4] = r[0x110 / 4] = divident % divisor;
502 r[0x11c / 4] = r[0x114 / 4] = divident / divisor;
507 if ((a == 0x1b0 || a == 0x18c) && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1)) {
508 elprintf(EL_32X, "sh2 DMA %08x -> %08x, cnt %d, chcr %04x @%06x",
509 dmac0->sar0, dmac0->dar0, dmac0->tcr0, dmac0->chcr0, sh2_pc(id));
510 dmac0->tcr0 &= 0xffffff;
512 // HACK: assume 68k starts writing soon and end the timeslice
515 // DREQ is only sent after first 4 words are written.
516 // we do multiple of 4 words to avoid messing up alignment
517 if (dmac0->sar0 == 0x20004012 && Pico32x.dmac_ptr && (Pico32x.dmac_ptr & 3) == 0) {
518 elprintf(EL_32X, "68k -> sh2 DMA");
524 // ------------------------------------------------------------------
525 // default 32x handlers
526 u32 PicoRead8_32x(u32 a)
529 if ((a & 0xffc0) == 0x5100) { // a15100
530 d = p32x_reg_read16(a);
534 if (!(Pico32x.regs[0] & 1))
537 if ((a & 0xfff0) == 0x5180) { // a15180
538 d = p32x_vdp_read16(a);
542 if ((a & 0xfe00) == 0x5200) { // a15200
543 d = Pico32xMem->pal[(a & 0x1ff) / 2];
548 if ((a & 0xfffc) == 0x30ec) { // a130ec
553 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
563 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
567 u32 PicoRead16_32x(u32 a)
570 if ((a & 0xffc0) == 0x5100) { // a15100
571 d = p32x_reg_read16(a);
575 if (!(Pico32x.regs[0] & 1))
578 if ((a & 0xfff0) == 0x5180) { // a15180
579 d = p32x_vdp_read16(a);
583 if ((a & 0xfe00) == 0x5200) { // a15200
584 d = Pico32xMem->pal[(a & 0x1ff) / 2];
589 if ((a & 0xfffc) == 0x30ec) { // a130ec
590 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
594 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
598 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
602 void PicoWrite8_32x(u32 a, u32 d)
604 if ((a & 0xfc00) == 0x5000)
605 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
607 if ((a & 0xffc0) == 0x5100) { // a15100
608 p32x_reg_write8(a, d);
612 if (!(Pico32x.regs[0] & 1))
615 if ((a & 0xfff0) == 0x5180) { // a15180
616 p32x_vdp_write8(a, d);
621 if ((a & 0xfe00) == 0x5200) { // a15200
622 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
623 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
624 Pico32x.dirty_pal = 1;
629 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
632 void PicoWrite16_32x(u32 a, u32 d)
634 if ((a & 0xfc00) == 0x5000)
635 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
637 if ((a & 0xffc0) == 0x5100) { // a15100
638 p32x_reg_write16(a, d);
642 if (!(Pico32x.regs[0] & 1))
645 if ((a & 0xfff0) == 0x5180) { // a15180
646 p32x_vdp_write16(a, d);
650 if ((a & 0xfe00) == 0x5200) { // a15200
651 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
652 Pico32x.dirty_pal = 1;
657 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
660 // hint vector is writeable
661 static void PicoWrite8_hint(u32 a, u32 d)
663 if ((a & 0xfffc) == 0x0070) {
664 Pico32xMem->m68k_rom[a ^ 1] = d;
668 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
671 static void PicoWrite16_hint(u32 a, u32 d)
673 if ((a & 0xfffc) == 0x0070) {
674 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
678 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
681 void Pico32xSwapDRAM(int b)
683 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
684 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
685 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
686 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
689 static void bank_switch(int b)
691 unsigned int rs, bank;
694 if (bank >= Pico.romsize) {
695 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
699 // 32X ROM (unbanked, XXX: consider mirroring?)
700 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
704 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
705 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
707 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
710 // -----------------------------------------------------------------
712 // -----------------------------------------------------------------
714 u32 p32x_sh2_read8(u32 a, int id)
718 if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
719 return Pico32xMem->sh2_rom_m[a ^ 1];
720 if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
721 return Pico32xMem->sh2_rom_s[a ^ 1];
723 if ((a & 0xdffc0000) == 0x06000000)
724 return Pico32xMem->sdram[(a & 0x3ffff) ^ 1];
726 if ((a & 0xdfc00000) == 0x02000000)
727 if ((a & 0x003fffff) < Pico.romsize)
728 return Pico.rom[(a & 0x3fffff) ^ 1];
730 if ((a & ~0xfff) == 0xc0000000)
731 return Pico32xMem->data_array[id][(a & 0xfff) ^ 1];
733 if ((a & 0xdffe0000) == 0x04000000) {
734 u8 *dram = (u8 *)Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
735 return dram[(a & 0x1ffff) ^ 1];
738 if ((a & 0xdfffff00) == 0x4000) {
739 d = p32x_sh2reg_read16(a, id);
743 if ((a & 0xdfffff00) == 0x4100) {
744 d = p32x_vdp_read16(a);
745 if (p32x_poll_detect(&sh2_poll[id], a, ash2_cycles_done(), 1))
750 if ((a & 0xdfffff00) == 0x4200) {
751 d = Pico32xMem->pal[(a & 0x1ff) / 2];
755 if ((a & 0xfffffe00) == 0xfffffe00)
756 return sh2_peripheral_read8(a, id);
758 elprintf(EL_UIO, "%csh2 unmapped r8 [%08x] %02x @%06x",
759 id ? 's' : 'm', a, d, sh2_pc(id));
768 elprintf(EL_32X, "%csh2 r8 [%08x] %02x @%06x",
769 id ? 's' : 'm', a, d, sh2_pc(id));
773 u32 p32x_sh2_read16(u32 a, int id)
777 if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
778 return *(u16 *)(Pico32xMem->sh2_rom_m + a);
779 if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
780 return *(u16 *)(Pico32xMem->sh2_rom_s + a);
782 if ((a & 0xdffc0000) == 0x06000000)
783 return ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2];
785 if ((a & 0xdfc00000) == 0x02000000)
786 if ((a & 0x003fffff) < Pico.romsize)
787 return ((u16 *)Pico.rom)[(a & 0x3fffff) / 2];
789 if ((a & ~0xfff) == 0xc0000000)
790 return ((u16 *)Pico32xMem->data_array[id])[(a & 0xfff) / 2];
792 if ((a & 0xdffe0000) == 0x04000000)
793 return Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1][(a & 0x1ffff) / 2];
795 if ((a & 0xdfffff00) == 0x4000) {
796 d = p32x_sh2reg_read16(a, id);
797 if (!(EL_LOGMASK & EL_PWM) && (a & 0x30) == 0x30) // hide PWM
802 if ((a & 0xdfffff00) == 0x4100) {
803 d = p32x_vdp_read16(a);
804 if (p32x_poll_detect(&sh2_poll[id], a, ash2_cycles_done(), 1))
809 if ((a & 0xdfffff00) == 0x4200) {
810 d = Pico32xMem->pal[(a & 0x1ff) / 2];
814 elprintf(EL_UIO, "%csh2 unmapped r16 [%08x] %04x @%06x",
815 id ? 's' : 'm', a, d, sh2_pc(id));
819 elprintf(EL_32X, "%csh2 r16 [%08x] %04x @%06x",
820 id ? 's' : 'm', a, d, sh2_pc(id));
824 u32 p32x_sh2_read32(u32 a, int id)
826 if ((a & 0xfffffe00) == 0xfffffe00)
827 return sh2_peripheral_read32(a, id);
829 // elprintf(EL_UIO, "sh2 r32 [%08x] %08x @%06x", a, d, ash2_pc());
830 return (p32x_sh2_read16(a, id) << 16) | p32x_sh2_read16(a + 2, id);
833 void p32x_sh2_write8(u32 a, u32 d, int id)
835 if ((a & 0xdffffc00) == 0x4000)
836 elprintf(EL_32X, "%csh2 w8 [%08x] %02x @%06x",
837 id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
839 if ((a & 0xdffc0000) == 0x06000000) {
840 Pico32xMem->sdram[(a & 0x3ffff) ^ 1] = d;
844 if ((a & 0xdffc0000) == 0x04000000) {
846 if (!(a & 0x20000) || d) {
847 dram = (u8 *)Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
848 dram[(a & 0x1ffff) ^ 1] = d;
853 if ((a & ~0xfff) == 0xc0000000) {
854 Pico32xMem->data_array[id][(a & 0xfff) ^ 1] = d;
858 if ((a & 0xdfffff00) == 0x4100) {
859 p32x_vdp_write8(a, d);
863 if ((a & 0xdfffff00) == 0x4000) {
864 p32x_sh2reg_write8(a, d, id);
868 if ((a & 0xfffffe00) == 0xfffffe00) {
869 sh2_peripheral_write8(a, d, id);
873 elprintf(EL_UIO, "%csh2 unmapped w8 [%08x] %02x @%06x",
874 id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
877 void p32x_sh2_write16(u32 a, u32 d, int id)
879 if ((a & 0xdffffc00) == 0x4000 && ((EL_LOGMASK & EL_PWM) || (a & 0x30) != 0x30)) // hide PWM
880 elprintf(EL_32X, "%csh2 w16 [%08x] %04x @%06x",
881 id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
883 // ignore "Associative purge space"
884 if ((a & 0xf8000000) == 0x40000000)
887 if ((a & 0xdffc0000) == 0x06000000) {
888 ((u16 *)Pico32xMem->sdram)[(a & 0x3ffff) / 2] = d;
892 if ((a & ~0xfff) == 0xc0000000) {
893 ((u16 *)Pico32xMem->data_array[id])[(a & 0xfff) / 2] = d;
897 if ((a & 0xdffc0000) == 0x04000000) {
898 u16 *pd = &Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1][(a & 0x1ffff) / 2];
899 if (!(a & 0x20000)) {
904 if (!(d & 0xff00)) d |= *pd & 0xff00;
905 if (!(d & 0x00ff)) d |= *pd & 0x00ff;
910 if ((a & 0xdfffff00) == 0x4100) {
911 sh2_poll[id].cnt = 0; // for poll before VDP accesses
912 p32x_vdp_write16(a, d);
916 if ((a & 0xdffffe00) == 0x4200) {
917 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
918 Pico32x.dirty_pal = 1;
922 if ((a & 0xdfffff00) == 0x4000) {
923 p32x_sh2reg_write16(a, d, id);
927 elprintf(EL_UIO, "%csh2 unmapped w16 [%08x] %04x @%06x",
928 id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
931 void p32x_sh2_write32(u32 a, u32 d, int id)
933 if ((a & 0xfffffe00) == 0xfffffe00) {
934 sh2_peripheral_write32(a, d, id);
938 p32x_sh2_write16(a, d >> 16, id);
939 p32x_sh2_write16(a + 2, d, id);
942 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
943 void PicoMemSetup32x(void)
950 Pico32xMem = calloc(1, sizeof(*Pico32xMem));
951 if (Pico32xMem == NULL) {
952 elprintf(EL_STATUS, "OOM");
956 dmac0 = (void *)&Pico32xMem->sh2_peri_regs[0][0x180 / 4];
959 ps = (unsigned short *)Pico32xMem->m68k_rom;
960 pl = (unsigned int *)Pico32xMem->m68k_rom;
961 for (i = 1; i < 0xc0/4; i++)
962 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
965 for (i = 0xc0/2; i < 0x100/2; i++)
970 ps[0xc2/2] = 0x2700; // move #0x2700,sr
971 ps[0xfe/2] = 0x60fe; // jump to self
973 ps[0xfe/2] = 0x4e75; // rts
976 // fill remaining mem with ROM
977 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
982 FILE *f = fopen("32X_M_BIOS.BIN", "rb");
985 printf("missing 32X_M_BIOS.BIN\n");
988 fread(Pico32xMem->sh2_rom_m, 1, sizeof(Pico32xMem->sh2_rom_m), f);
990 f = fopen("32X_S_BIOS.BIN", "rb");
992 printf("missing 32X_S_BIOS.BIN\n");
995 fread(Pico32xMem->sh2_rom_s, 1, sizeof(Pico32xMem->sh2_rom_s), f);
998 for (i = 0; i < sizeof(Pico32xMem->sh2_rom_m); i += 2) {
999 int t = Pico32xMem->sh2_rom_m[i];
1000 Pico32xMem->sh2_rom_m[i] = Pico32xMem->sh2_rom_m[i + 1];
1001 Pico32xMem->sh2_rom_m[i + 1] = t;
1003 for (i = 0; i < sizeof(Pico32xMem->sh2_rom_s); i += 2) {
1004 int t = Pico32xMem->sh2_rom_s[i];
1005 Pico32xMem->sh2_rom_s[i] = Pico32xMem->sh2_rom_s[i + 1];
1006 Pico32xMem->sh2_rom_s[i + 1] = t;
1010 // cartridge area becomes unmapped
1011 // XXX: we take the easy way and don't unmap ROM,
1012 // so that we can avoid handling the RV bit.
1013 // m68k_map_unmap(0x000000, 0x3fffff);
1016 rs = sizeof(Pico32xMem->m68k_rom);
1017 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
1018 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
1019 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
1020 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
1025 // 32X ROM (unbanked, XXX: consider mirroring?)
1026 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
1029 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1030 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1035 // setup poll detector
1036 m68k_poll.flag = P32XF_68KPOLL;
1037 m68k_poll.cyc_max = 64;
1038 sh2_poll[0].flag = P32XF_MSH2POLL;
1039 sh2_poll[0].cyc_max = 16;
1040 sh2_poll[1].flag = P32XF_SSH2POLL;
1041 sh2_poll[1].cyc_max = 16;