3 * (C) notaz, 2009,2010,2013
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 * a15100 F....... R.....EA F.....AC N...VHMP 4000 // Fm Ren nrEs Aden Cart heN V H cMd Pwm
10 * a15102 ........ ......SM ? 4002 // intS intM
11 * a15104 ........ ......10 ........ hhhhhhhh 4004 // bk1 bk0 Hint
12 * a15106 F....... .....SDR UE...... .....SDR 4006 // Full 68S Dma Rv fUll[fb] Empt[fb]
13 * a15108 (32bit DREQ src) 4008
14 * a1510c (32bit DREQ dst) 400c
15 * a15110 llllllll llllll00 4010 // DREQ Len
16 * a15112 (16bit FIFO reg) 4012
17 * a15114 ? (16bit VRES clr) 4014
18 * a15116 ? (16bit Vint clr) 4016
19 * a15118 ? (16bit Hint clr) 4018
20 * a1511a ........ .......C (16bit CMD clr) 401a // Cm
21 * a1511c ? (16bit PWM clr) 401c
23 * a15120 (16 bytes comm) 2020
27 * iii. .cc. ..xx * // Internal, Cs, x
29 * sh2 map, wait/bus cycles (from docs):
31 * rom 0000000-0003fff 1 -
32 * sys reg 0004000-00040ff 1 1
33 * vdp reg 0004100-00041ff 5 5
34 * vdp pal 0004200-00043ff 5 5
35 * rom 2000000-23fffff 6-15
36 * dram/fb 4000000-401ffff 5-12 1-3
37 * fb ovr 4020000-403ffff
38 * sdram 6000000-603ffff 12 2 (cycles)
41 #include "../pico_int.h"
42 #include "../memory.h"
43 #include "../../cpu/sh2/compiler.h"
45 static const char str_mars[] = "MARS";
47 void *p32x_bios_g, *p32x_bios_m, *p32x_bios_s;
48 struct Pico32xMem *Pico32xMem;
50 static void bank_switch(int b);
53 #define POLL_THRESHOLD 3
60 static int m68k_poll_detect(u32 a, u32 cycles, u32 flags)
64 if (a - 2 <= m68k_poll.addr && m68k_poll.addr <= a + 2
65 && cycles - m68k_poll.cycles <= 64)
67 if (m68k_poll.cnt++ > POLL_THRESHOLD) {
68 if (!(Pico32x.emu_flags & flags)) {
69 elprintf(EL_32X, "m68k poll addr %08x, cyc %u",
70 a, cycles - m68k_poll.cycles);
73 Pico32x.emu_flags |= flags;
80 m68k_poll.cycles = cycles;
85 void p32x_m68k_poll_event(u32 flags)
87 if (Pico32x.emu_flags & flags) {
88 elprintf(EL_32X, "m68k poll %02x -> %02x", Pico32x.emu_flags,
89 Pico32x.emu_flags & ~flags);
90 Pico32x.emu_flags &= ~flags;
93 m68k_poll.addr = m68k_poll.cnt = 0;
96 static void sh2_poll_detect(SH2 *sh2, u32 a, u32 flags)
98 int cycles_left = sh2_cycles_left(sh2);
100 if (a == sh2->poll_addr && sh2->poll_cycles - cycles_left <= 10) {
101 if (sh2->poll_cnt++ > 3) {
102 if (!(sh2->state & flags))
103 elprintf(EL_32X, "%csh2 state: %02x->%02x", sh2->is_slave?'s':'m',
104 sh2->state, sh2->state | flags);
108 pevt_log_sh2(sh2, EVT_POLL_START);
115 sh2->poll_cycles = cycles_left;
118 void p32x_sh2_poll_event(SH2 *sh2, u32 flags, u32 m68k_cycles)
120 if (sh2->state & flags) {
121 elprintf(EL_32X, "%csh2 state: %02x->%02x", sh2->is_slave?'s':'m',
122 sh2->state, sh2->state & ~flags);
124 if (sh2->m68krcycles_done < m68k_cycles)
125 sh2->m68krcycles_done = m68k_cycles;
127 pevt_log_sh2_o(sh2, EVT_POLL_END);
130 sh2->state &= ~flags;
131 sh2->poll_addr = sh2->poll_cycles = sh2->poll_cnt = 0;
134 static void sh2s_sync_on_read(SH2 *sh2)
137 if (sh2->poll_cnt != 0)
140 cycles = sh2_cycles_done(sh2);
142 p32x_sync_other_sh2(sh2, sh2->m68krcycles_done + cycles / 3);
148 static int p32x_csum_faked;
149 static const u16 comm_fakevals[] = {
150 0x4d5f, 0x4f4b, // M_OK
151 0x535f, 0x4f4b, // S_OK
152 0x4D41, 0x5346, // MASF - Brutal Unleashed
153 0x5331, 0x4d31, // Darxide
156 0x0000, 0x0000, // eq for doom
157 0x0002, // Mortal Kombat
161 static u32 sh2_comm_faker(u32 a)
164 if (a == 0x28 && !p32x_csum_faked) {
166 return *(unsigned short *)(Pico.rom + 0x18e);
168 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
170 return comm_fakevals[f++];
176 unsigned int sar0, dar0, tcr0; // src addr, dst addr, transfer count
177 unsigned int chcr0; // chan ctl
178 unsigned int sar1, dar1, tcr1; // same for chan 1
184 static void dma_68k2sh2_do(void)
186 unsigned short *dreqlen = &Pico32x.regs[0x10 / 2];
189 if (dmac0->tcr0 != *dreqlen)
190 elprintf(EL_32X|EL_ANOMALY, "tcr0 and dreq len differ: %d != %d", dmac0->tcr0, *dreqlen);
192 // HACK: assume bus is busy and SH2 is halted
193 msh2.state |= SH2_STATE_SLEEP;
195 for (i = 0; i < Pico32x.dmac_ptr && dmac0->tcr0 > 0; i++) {
196 elprintf(EL_32X, "dmaw [%08x] %04x, left %d", dmac0->dar0, Pico32x.dmac_fifo[i], *dreqlen);
197 p32x_sh2_write16(dmac0->dar0, Pico32x.dmac_fifo[i], &msh2);
203 Pico32x.dmac_ptr = 0; // HACK
204 Pico32x.regs[6 / 2] &= ~P32XS_FULL;
206 Pico32x.regs[6 / 2] &= ~P32XS_68S; // transfer complete
207 if (dmac0->tcr0 == 0) {
208 dmac0->chcr0 |= 2; // DMA has ended normally
209 p32x_sh2_poll_event(&sh2s[0], SH2_STATE_SLEEP, SekCyclesDoneT());
213 // ------------------------------------------------------------------
216 static u32 p32x_reg_read16(u32 a)
221 if ((a & 0x30) == 0x20)
222 return sh2_comm_faker(a);
224 if ((a & 0x30) == 0x20) {
226 unsigned int cycles = SekCyclesDoneT();
227 int comreg = 1 << (a & 0x0f) / 2;
229 // evil X-Men proto polls in a dbra loop and expects it to expire..
230 if (SekDar(2) != dr2)
234 if (cycles - msh2.m68krcycles_done > 500)
235 p32x_sync_sh2s(cycles);
236 if (Pico32x.comm_dirty_sh2 & comreg)
237 Pico32x.comm_dirty_sh2 &= ~comreg;
238 else if (m68k_poll_detect(a, cycles, P32XF_68KCPOLL)) {
247 if (a == 2) { // INTM, INTS
248 unsigned int cycles = SekCyclesDoneT();
249 if (cycles - msh2.m68krcycles_done > 64)
250 p32x_sync_sh2s(cycles);
251 return ((Pico32x.sh2irqi[0] & P32XI_CMD) >> 4) | ((Pico32x.sh2irqi[1] & P32XI_CMD) >> 3);
254 if ((a & 0x30) == 0x30)
255 return p32x_pwm_read16(a);
258 return Pico32x.regs[a / 2];
261 static void p32x_reg_write8(u32 a, u32 d)
263 u16 *r = Pico32x.regs;
266 // for things like bset on comm port
270 case 0: // adapter ctl
271 r[0] = (r[0] & ~P32XS_FM) | ((d << 8) & P32XS_FM);
273 case 1: // adapter ctl, RES bit writeable
274 if ((d ^ r[0]) & d & P32XS_nRES)
276 r[0] = (r[0] & ~P32XS_nRES) | (d & P32XS_nRES);
279 if ((d & 1) && !(Pico32x.sh2irqi[0] & P32XI_CMD)) {
280 p32x_sync_sh2s(SekCyclesDoneT());
281 Pico32x.sh2irqi[0] |= P32XI_CMD;
282 p32x_update_irls(NULL);
284 if ((d & 2) && !(Pico32x.sh2irqi[1] & P32XI_CMD)) {
285 p32x_sync_sh2s(SekCyclesDoneT());
286 Pico32x.sh2irqi[1] |= P32XI_CMD;
287 p32x_update_irls(NULL);
298 r[6 / 2] = (r[6 / 2] & P32XS_FULL) | (d & (P32XS_68S|P32XS_DMA|P32XS_RV));
305 if ((a & 0x30) == 0x20) {
307 int cycles = SekCyclesDoneT();
313 comreg = 1 << (a & 0x0f) / 2;
314 if (Pico32x.comm_dirty_68k & comreg)
315 p32x_sync_sh2s(cycles);
318 p32x_sh2_poll_event(&sh2s[0], SH2_STATE_CPOLL, cycles);
319 p32x_sh2_poll_event(&sh2s[1], SH2_STATE_CPOLL, cycles);
320 Pico32x.comm_dirty_68k |= comreg;
322 if (cycles - (int)msh2.m68krcycles_done > 120)
323 p32x_sync_sh2s(cycles);
328 static void p32x_reg_write16(u32 a, u32 d)
330 u16 *r = Pico32x.regs;
333 // for things like bset on comm port
337 case 0x00: // adapter ctl
338 if ((d ^ r[0]) & d & P32XS_nRES)
340 r[0] = (r[0] & ~(P32XS_FM|P32XS_nRES)) | (d & (P32XS_FM|P32XS_nRES));
342 case 0x10: // DREQ len
345 case 0x12: // FIFO reg
346 if (!(r[6 / 2] & P32XS_68S)) {
347 elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO w16 without 68S?");
350 if (Pico32x.dmac_ptr < DMAC_FIFO_LEN) {
351 Pico32x.dmac_fifo[Pico32x.dmac_ptr++] = d;
352 if ((Pico32x.dmac_ptr & 3) == 0 && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1))
354 if (Pico32x.dmac_ptr == DMAC_FIFO_LEN)
355 r[6 / 2] |= P32XS_FULL;
361 if ((a & 0x38) == 0x08) {
366 else if ((a & 0x30) == 0x20) {
367 int cycles = SekCyclesDoneT();
373 comreg = 1 << (a & 0x0f) / 2;
374 if (Pico32x.comm_dirty_68k & comreg)
375 p32x_sync_sh2s(cycles);
378 p32x_sh2_poll_event(&sh2s[0], SH2_STATE_CPOLL, cycles);
379 p32x_sh2_poll_event(&sh2s[1], SH2_STATE_CPOLL, cycles);
380 Pico32x.comm_dirty_68k |= comreg;
382 if (cycles - (int)msh2.m68krcycles_done > 120)
383 p32x_sync_sh2s(cycles);
387 else if ((a & 0x30) == 0x30) {
388 p32x_pwm_write16(a, d);
392 p32x_reg_write8(a + 1, d);
395 // ------------------------------------------------------------------
397 static u32 p32x_vdp_read16(u32 a)
401 return Pico32x.vdp_regs[a / 2];
404 static void p32x_vdp_write8(u32 a, u32 d)
406 u16 *r = Pico32x.vdp_regs;
409 // TODO: verify what's writeable
412 // priority inversion is handled in palette
413 if ((r[0] ^ d) & P32XV_PRI)
414 Pico32x.dirty_pal = 1;
415 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
417 case 0x03: // shift (for pp mode)
420 case 0x05: // fill len
425 Pico32x.pending_fb = d;
426 // if we are blanking and FS bit is changing
427 if (((r[0x0a/2] & P32XV_VBLK) || (r[0] & P32XV_Mx) == 0) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
428 r[0x0a/2] ^= P32XV_FS;
429 Pico32xSwapDRAM(d ^ 1);
430 elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS);
436 static void p32x_vdp_write16(u32 a, u32 d, SH2 *sh2)
439 if (a == 6) { // fill start
440 Pico32x.vdp_regs[6 / 2] = d;
443 if (a == 8) { // fill data
444 u16 *dram = Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
445 int len = Pico32x.vdp_regs[4 / 2] + 1;
447 a = Pico32x.vdp_regs[6 / 2];
450 a = (a & 0xff00) | ((a + 1) & 0xff);
452 Pico32x.vdp_regs[0x06 / 2] = a;
453 Pico32x.vdp_regs[0x08 / 2] = d;
454 if (sh2 != NULL && len > 4) {
455 Pico32x.vdp_regs[0x0a / 2] |= P32XV_nFEN;
456 // supposedly takes 3 bus/6 sh2 cycles? or 3 sh2 cycles?
457 p32x_event_schedule_sh2(sh2, P32X_EVENT_FILLEND, 3 + len);
462 p32x_vdp_write8(a | 1, d);
465 // ------------------------------------------------------------------
468 static u32 p32x_sh2reg_read16(u32 a, int cpuid)
470 u16 *r = Pico32x.regs;
474 case 0x00: // adapter/irq ctl
475 return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0] | Pico32x.sh2irq_mask[cpuid];
476 case 0x04: // H count (often as comm too)
477 sh2_poll_detect(&sh2s[cpuid], a, SH2_STATE_CPOLL);
478 sh2s_sync_on_read(&sh2s[cpuid]);
479 return Pico32x.sh2_regs[4 / 2];
480 case 0x10: // DREQ len
485 if ((a & 0x38) == 0x08)
488 if ((a & 0x30) == 0x20) {
489 int comreg = 1 << (a & 0x0f) / 2;
490 if (Pico32x.comm_dirty_68k & comreg)
491 Pico32x.comm_dirty_68k &= ~comreg;
493 sh2_poll_detect(&sh2s[cpuid], a, SH2_STATE_CPOLL);
494 sh2s_sync_on_read(&sh2s[cpuid]);
497 if ((a & 0x30) == 0x30) {
498 return p32x_pwm_read16(a);
504 static void p32x_sh2reg_write8(u32 a, u32 d, int cpuid)
508 sh2s[cpuid].poll_addr = 0;
512 Pico32x.regs[0] &= ~P32XS_FM;
513 Pico32x.regs[0] |= (d << 8) & P32XS_FM;
515 case 1: // HEN/irq masks
516 if ((d ^ Pico32x.sh2_regs[0]) & 0x80)
517 elprintf(EL_ANOMALY|EL_32X, "HEN");
518 Pico32x.sh2irq_mask[cpuid] = d & 0x8f;
519 Pico32x.sh2_regs[0] &= ~0x80;
520 Pico32x.sh2_regs[0] |= d & 0x80;
522 p32x_pwm_schedule_sh2(&sh2s[cpuid]);
523 p32x_update_irls(&sh2s[cpuid]);
527 if (Pico32x.sh2_regs[4 / 2] != d) {
528 Pico32x.sh2_regs[4 / 2] = d;
529 p32x_sh2_poll_event(&sh2s[cpuid ^ 1], SH2_STATE_CPOLL,
530 sh2_cycles_done_m68k(&sh2s[cpuid]));
531 sh2_end_run(&sh2s[cpuid], 4);
536 if ((a & 0x30) == 0x20) {
537 u8 *r8 = (u8 *)Pico32x.regs;
543 p32x_m68k_poll_event(P32XF_68KCPOLL);
544 p32x_sh2_poll_event(&sh2s[cpuid ^ 1], SH2_STATE_CPOLL,
545 sh2_cycles_done_m68k(&sh2s[cpuid]));
546 comreg = 1 << (a & 0x0f) / 2;
547 Pico32x.comm_dirty_sh2 |= comreg;
552 static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
556 sh2s[cpuid].poll_addr = 0;
559 if ((a & 0x30) == 0x20) {
561 if (Pico32x.regs[a / 2] == d)
564 Pico32x.regs[a / 2] = d;
565 p32x_m68k_poll_event(P32XF_68KCPOLL);
566 p32x_sh2_poll_event(&sh2s[cpuid ^ 1], SH2_STATE_CPOLL,
567 sh2_cycles_done_m68k(&sh2s[cpuid]));
568 comreg = 1 << (a & 0x0f) / 2;
569 Pico32x.comm_dirty_sh2 |= comreg;
573 else if ((a & 0x30) == 0x30) {
574 p32x_pwm_write16(a, d);
580 Pico32x.regs[0] &= ~P32XS_FM;
581 Pico32x.regs[0] |= d & P32XS_FM;
583 case 0x14: Pico32x.sh2irqs &= ~P32XI_VRES; goto irls;
584 case 0x16: Pico32x.sh2irqs &= ~P32XI_VINT; goto irls;
585 case 0x18: Pico32x.sh2irqs &= ~P32XI_HINT; goto irls;
586 case 0x1a: Pico32x.sh2irqi[cpuid] &= ~P32XI_CMD; goto irls;
588 Pico32x.sh2irqs &= ~P32XI_PWM;
589 if (!(Pico32x.emu_flags & P32XF_PWM_PEND))
590 p32x_pwm_schedule_sh2(&sh2s[cpuid]);
594 p32x_sh2reg_write8(a | 1, d, cpuid);
598 p32x_update_irls(&sh2s[cpuid]);
601 // ------------------------------------------------------------------
602 // SH2 internal peripherals
603 // we keep them in little endian format
604 static u32 sh2_peripheral_read8(u32 a, int id)
606 u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
612 elprintf(EL_32X, "%csh2 peri r8 [%08x] %02x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
616 static u32 sh2_peripheral_read16(u32 a, int id)
618 u16 *r = (void *)Pico32xMem->sh2_peri_regs[id];
624 elprintf(EL_32X, "%csh2 peri r16 [%08x] %04x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
628 static u32 sh2_peripheral_read32(u32 a, int id)
632 d = Pico32xMem->sh2_peri_regs[id][a / 4];
634 elprintf(EL_32X, "%csh2 peri r32 [%08x] %08x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
638 static int REGPARM(3) sh2_peripheral_write8(u32 a, u32 d, int id)
640 u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
641 elprintf(EL_32X, "%csh2 peri w8 [%08x] %02x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
647 if ((a == 2 && (d & 0x20)) || // transmiter enabled
648 (a == 4 && !(d & 0x80))) { // valid data in TDR
649 void *oregs = Pico32xMem->sh2_peri_regs[id ^ 1];
650 if ((PREG8(oregs, 2) & 0x50) == 0x50) { // receiver + irq enabled
651 int level = PREG8(oregs, 0x60) >> 4;
652 int vector = PREG8(oregs, 0x63) & 0x7f;
653 elprintf(EL_32X, "%csh2 SCI recv irq (%d, %d)", (id ^ 1) ? 's' : 'm', level, vector);
654 sh2_internal_irq(&sh2s[id ^ 1], level, vector);
661 static int REGPARM(3) sh2_peripheral_write16(u32 a, u32 d, int id)
663 u16 *r = (void *)Pico32xMem->sh2_peri_regs[id];
664 elprintf(EL_32X, "%csh2 peri w16 [%08x] %04x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
670 if ((d & 0xff00) == 0xa500) { // WTCSR
672 p32x_timers_recalc();
674 if ((d & 0xff00) == 0x5a00) // WTCNT
683 static void sh2_peripheral_write32(u32 a, u32 d, int id)
685 u32 *r = Pico32xMem->sh2_peri_regs[id];
686 elprintf(EL_32X, "%csh2 peri w32 [%08x] %08x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
692 // division unit (TODO: verify):
693 case 0x104: // DVDNT: divident L, starts divide
694 elprintf(EL_32X, "%csh2 divide %08x / %08x", id ? 's' : 'm', d, r[0x100 / 4]);
696 signed int divisor = r[0x100 / 4];
697 r[0x118 / 4] = r[0x110 / 4] = (signed int)d % divisor;
698 r[0x104 / 4] = r[0x11c / 4] = r[0x114 / 4] = (signed int)d / divisor;
701 r[0x110 / 4] = r[0x114 / 4] = r[0x118 / 4] = r[0x11c / 4] = 0; // ?
704 elprintf(EL_32X, "%csh2 divide %08x%08x / %08x @%08x",
705 id ? 's' : 'm', r[0x110 / 4], d, r[0x100 / 4], sh2_pc(id));
707 signed long long divident = (signed long long)r[0x110 / 4] << 32 | d;
708 signed int divisor = r[0x100 / 4];
709 // XXX: undocumented mirroring to 0x118,0x11c?
710 r[0x118 / 4] = r[0x110 / 4] = divident % divisor;
712 r[0x11c / 4] = r[0x114 / 4] = divident;
714 if ((unsigned long long)divident + 1 > 1) {
715 //elprintf(EL_32X, "%csh2 divide overflow! @%08x", id ? 's' : 'm', sh2_pc(id));
716 r[0x11c / 4] = r[0x114 / 4] = divident > 0 ? 0x7fffffff : 0x80000000; // overflow
720 r[0x110 / 4] = r[0x114 / 4] = r[0x118 / 4] = r[0x11c / 4] = 0; // ?
724 if ((a == 0x1b0 || a == 0x18c) && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1)) {
725 elprintf(EL_32X, "sh2 DMA %08x -> %08x, cnt %d, chcr %04x @%06x",
726 dmac0->sar0, dmac0->dar0, dmac0->tcr0, dmac0->chcr0, sh2_pc(id));
727 dmac0->tcr0 &= 0xffffff;
729 // HACK: assume 68k starts writing soon and end the timeslice
730 sh2_end_run(&sh2s[id], 16);
732 // DREQ is only sent after first 4 words are written.
733 // we do multiple of 4 words to avoid messing up alignment
734 if (dmac0->sar0 == 0x20004012 && Pico32x.dmac_ptr && (Pico32x.dmac_ptr & 3) == 0) {
735 elprintf(EL_32X, "68k -> sh2 DMA");
741 // ------------------------------------------------------------------
745 static u32 PicoRead8_32x_on(u32 a)
748 if ((a & 0xffc0) == 0x5100) { // a15100
749 d = p32x_reg_read16(a);
753 if ((a & 0xfc00) != 0x5000)
754 return PicoRead8_io(a);
756 if ((a & 0xfff0) == 0x5180) { // a15180
757 d = p32x_vdp_read16(a);
761 if ((a & 0xfe00) == 0x5200) { // a15200
762 d = Pico32xMem->pal[(a & 0x1ff) / 2];
766 if ((a & 0xfffc) == 0x30ec) { // a130ec
771 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
781 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
785 static u32 PicoRead16_32x_on(u32 a)
788 if ((a & 0xffc0) == 0x5100) { // a15100
789 d = p32x_reg_read16(a);
793 if ((a & 0xfc00) != 0x5000)
794 return PicoRead16_io(a);
796 if ((a & 0xfff0) == 0x5180) { // a15180
797 d = p32x_vdp_read16(a);
801 if ((a & 0xfe00) == 0x5200) { // a15200
802 d = Pico32xMem->pal[(a & 0x1ff) / 2];
806 if ((a & 0xfffc) == 0x30ec) { // a130ec
807 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
811 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
815 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
819 static void PicoWrite8_32x_on(u32 a, u32 d)
821 if ((a & 0xfc00) == 0x5000)
822 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
824 if ((a & 0xffc0) == 0x5100) { // a15100
825 p32x_reg_write8(a, d);
829 if ((a & 0xfc00) != 0x5000) {
834 if ((a & 0xfff0) == 0x5180) { // a15180
835 p32x_vdp_write8(a, d);
840 if ((a & 0xfe00) == 0x5200) { // a15200
841 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
842 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
843 Pico32x.dirty_pal = 1;
847 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
850 static void PicoWrite16_32x_on(u32 a, u32 d)
852 if ((a & 0xfc00) == 0x5000)
853 elprintf(EL_32X, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
855 if ((a & 0xffc0) == 0x5100) { // a15100
856 p32x_reg_write16(a, d);
860 if ((a & 0xfc00) != 0x5000) {
861 PicoWrite16_io(a, d);
865 if ((a & 0xfff0) == 0x5180) { // a15180
866 p32x_vdp_write16(a, d, NULL); // FIXME?
870 if ((a & 0xfe00) == 0x5200) { // a15200
871 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
872 Pico32x.dirty_pal = 1;
876 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
880 u32 PicoRead8_32x(u32 a)
883 if ((a & 0xffc0) == 0x5100) { // a15100
884 // regs are always readable
885 d = ((u8 *)Pico32x.regs)[(a & 0x3f) ^ 1];
889 if ((a & 0xfffc) == 0x30ec) { // a130ec
894 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
898 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
902 u32 PicoRead16_32x(u32 a)
905 if ((a & 0xffc0) == 0x5100) { // a15100
906 d = Pico32x.regs[(a & 0x3f) / 2];
910 if ((a & 0xfffc) == 0x30ec) { // a130ec
911 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
915 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
919 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
923 void PicoWrite8_32x(u32 a, u32 d)
925 if ((a & 0xffc0) == 0x5100) { // a15100
926 u16 *r = Pico32x.regs;
928 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
931 if ((d ^ r[0]) & d & P32XS_ADEN) {
933 r[0] &= ~P32XS_nRES; // causes reset if specified by this write
935 p32x_reg_write8(a, d); // forward for reset processing
940 // allow only COMM for now
941 if ((a & 0x30) == 0x20) {
948 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
951 void PicoWrite16_32x(u32 a, u32 d)
953 if ((a & 0xffc0) == 0x5100) { // a15100
954 u16 *r = Pico32x.regs;
956 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
959 if ((d ^ r[0]) & d & P32XS_ADEN) {
961 r[0] &= ~P32XS_nRES; // causes reset if specified by this write
963 p32x_reg_write16(a, d); // forward for reset processing
968 // allow only COMM for now
969 if ((a & 0x30) == 0x20)
974 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
977 // -----------------------------------------------------------------
979 // hint vector is writeable
980 static void PicoWrite8_hint(u32 a, u32 d)
982 if ((a & 0xfffc) == 0x0070) {
983 Pico32xMem->m68k_rom[a ^ 1] = d;
987 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
990 static void PicoWrite16_hint(u32 a, u32 d)
992 if ((a & 0xfffc) == 0x0070) {
993 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
997 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
1000 static void bank_switch(int b)
1002 unsigned int rs, bank;
1005 if (bank >= Pico.romsize) {
1006 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
1010 // 32X ROM (unbanked, XXX: consider mirroring?)
1011 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
1015 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
1016 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
1018 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
1021 // setup FAME fetchmap
1022 for (rs = 0x90; rs < 0xa0; rs++)
1023 PicoCpuFM68k.Fetch[rs] = (unsigned long)Pico.rom + bank - 0x900000;
1027 // -----------------------------------------------------------------
1029 // -----------------------------------------------------------------
1032 static u32 sh2_read8_unmapped(u32 a, int id)
1034 elprintf(EL_UIO, "%csh2 unmapped r8 [%08x] %02x @%06x",
1035 id ? 's' : 'm', a, 0, sh2_pc(id));
1039 static u32 sh2_read8_cs0(u32 a, int id)
1043 // 0x3ff00 is veridied
1044 if ((a & 0x3ff00) == 0x4000) {
1045 d = p32x_sh2reg_read16(a, id);
1049 if ((a & 0x3ff00) == 0x4100) {
1050 d = p32x_vdp_read16(a);
1051 sh2_poll_detect(&sh2s[id], a, SH2_STATE_VPOLL);
1056 if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
1057 return Pico32xMem->sh2_rom_m[a ^ 1];
1058 if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
1059 return Pico32xMem->sh2_rom_s[a ^ 1];
1061 if ((a & 0x3fe00) == 0x4200) {
1062 d = Pico32xMem->pal[(a & 0x1ff) / 2];
1066 return sh2_read8_unmapped(a, id);
1074 elprintf(EL_32X, "%csh2 r8 [%08x] %02x @%06x",
1075 id ? 's' : 'm', a, d, sh2_pc(id));
1079 static u32 sh2_read8_da(u32 a, int id)
1081 return Pico32xMem->data_array[id][(a & 0xfff) ^ 1];
1085 static u32 sh2_read16_unmapped(u32 a, int id)
1087 elprintf(EL_UIO, "%csh2 unmapped r16 [%08x] %04x @%06x",
1088 id ? 's' : 'm', a, 0, sh2_pc(id));
1092 static u32 sh2_read16_cs0(u32 a, int id)
1096 if ((a & 0x3ff00) == 0x4000) {
1097 d = p32x_sh2reg_read16(a, id);
1098 if (!(EL_LOGMASK & EL_PWM) && (a & 0x30) == 0x30) // hide PWM
1103 if ((a & 0x3ff00) == 0x4100) {
1104 d = p32x_vdp_read16(a);
1105 sh2_poll_detect(&sh2s[id], a, SH2_STATE_VPOLL);
1109 if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
1110 return *(u16 *)(Pico32xMem->sh2_rom_m + a);
1111 if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
1112 return *(u16 *)(Pico32xMem->sh2_rom_s + a);
1114 if ((a & 0x3fe00) == 0x4200) {
1115 d = Pico32xMem->pal[(a & 0x1ff) / 2];
1119 return sh2_read16_unmapped(a, id);
1122 elprintf(EL_32X, "%csh2 r16 [%08x] %04x @%06x",
1123 id ? 's' : 'm', a, d, sh2_pc(id));
1127 static u32 sh2_read16_da(u32 a, int id)
1129 return ((u16 *)Pico32xMem->data_array[id])[(a & 0xfff) / 2];
1132 static int REGPARM(3) sh2_write_ignore(u32 a, u32 d, int id)
1138 static int REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, int id)
1140 elprintf(EL_UIO, "%csh2 unmapped w8 [%08x] %02x @%06x",
1141 id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
1145 static int REGPARM(3) sh2_write8_cs0(u32 a, u32 d, int id)
1147 elprintf(EL_32X, "%csh2 w8 [%08x] %02x @%06x",
1148 id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
1150 if ((a & 0x3ff00) == 0x4100) {
1151 sh2s[id].poll_addr = 0;
1152 p32x_vdp_write8(a, d);
1156 if ((a & 0x3ff00) == 0x4000) {
1157 p32x_sh2reg_write8(a, d, id);
1161 return sh2_write8_unmapped(a, d, id);
1164 /* quirk: in both normal and overwrite areas only nonzero values go through */
1165 #define sh2_write8_dramN(n) \
1166 if ((d & 0xff) != 0) { \
1167 u8 *dram = (u8 *)Pico32xMem->dram[n]; \
1168 dram[(a & 0x1ffff) ^ 1] = d; \
1172 static int REGPARM(3) sh2_write8_dram0(u32 a, u32 d, int id)
1174 sh2_write8_dramN(0);
1177 static int REGPARM(3) sh2_write8_dram1(u32 a, u32 d, int id)
1179 sh2_write8_dramN(1);
1182 static int REGPARM(3) sh2_write8_sdram(u32 a, u32 d, int id)
1184 u32 a1 = a & 0x3ffff;
1186 int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
1188 sh2_drc_wcheck_ram(a, t, id);
1190 Pico32xMem->sdram[a1 ^ 1] = d;
1194 static int REGPARM(3) sh2_write8_da(u32 a, u32 d, int id)
1198 int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
1200 sh2_drc_wcheck_da(a, t, id);
1202 Pico32xMem->data_array[id][a1 ^ 1] = d;
1207 static int REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, int id)
1209 elprintf(EL_UIO, "%csh2 unmapped w16 [%08x] %04x @%06x",
1210 id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
1214 static int REGPARM(3) sh2_write16_cs0(u32 a, u32 d, int id)
1216 if (((EL_LOGMASK & EL_PWM) || (a & 0x30) != 0x30)) // hide PWM
1217 elprintf(EL_32X, "%csh2 w16 [%08x] %04x @%06x",
1218 id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
1220 if ((a & 0x3ff00) == 0x4100) {
1221 sh2s[id].poll_addr = 0;
1222 p32x_vdp_write16(a, d, &sh2s[id]);
1226 if ((a & 0x3fe00) == 0x4200) {
1227 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
1228 Pico32x.dirty_pal = 1;
1232 if ((a & 0x3ff00) == 0x4000) {
1233 p32x_sh2reg_write16(a, d, id);
1237 return sh2_write16_unmapped(a, d, id);
1240 #define sh2_write16_dramN(n) \
1241 u16 *pd = &Pico32xMem->dram[n][(a & 0x1ffff) / 2]; \
1242 if (!(a & 0x20000)) { \
1247 if (!(d & 0xff00)) d |= *pd & 0xff00; \
1248 if (!(d & 0x00ff)) d |= *pd & 0x00ff; \
1252 static int REGPARM(3) sh2_write16_dram0(u32 a, u32 d, int id)
1254 sh2_write16_dramN(0);
1257 static int REGPARM(3) sh2_write16_dram1(u32 a, u32 d, int id)
1259 sh2_write16_dramN(1);
1262 static int REGPARM(3) sh2_write16_sdram(u32 a, u32 d, int id)
1264 u32 a1 = a & 0x3ffff;
1266 int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
1268 sh2_drc_wcheck_ram(a, t, id);
1270 ((u16 *)Pico32xMem->sdram)[a1 / 2] = d;
1274 static int REGPARM(3) sh2_write16_da(u32 a, u32 d, int id)
1278 int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
1280 sh2_drc_wcheck_da(a, t, id);
1282 ((u16 *)Pico32xMem->data_array[id])[a1 / 2] = d;
1287 typedef u32 (sh2_read_handler)(u32 a, int id);
1288 typedef int REGPARM(3) (sh2_write_handler)(u32 a, u32 d, int id);
1290 #define SH2MAP_ADDR2OFFS_R(a) \
1291 ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
1293 #define SH2MAP_ADDR2OFFS_W(a) \
1294 ((u32)(a) >> SH2_WRITE_SHIFT)
1296 u32 REGPARM(2) p32x_sh2_read8(u32 a, SH2 *sh2)
1298 const sh2_memmap *sh2_map = sh2->read8_map;
1301 sh2_map += SH2MAP_ADDR2OFFS_R(a);
1303 if (map_flag_set(p))
1304 return ((sh2_read_handler *)(p << 1))(a, sh2->is_slave);
1306 return *(u8 *)((p << 1) + ((a & sh2_map->mask) ^ 1));
1309 u32 REGPARM(2) p32x_sh2_read16(u32 a, SH2 *sh2)
1311 const sh2_memmap *sh2_map = sh2->read16_map;
1314 sh2_map += SH2MAP_ADDR2OFFS_R(a);
1316 if (map_flag_set(p))
1317 return ((sh2_read_handler *)(p << 1))(a, sh2->is_slave);
1319 return *(u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
1322 u32 REGPARM(2) p32x_sh2_read32(u32 a, SH2 *sh2)
1324 const sh2_memmap *sh2_map = sh2->read16_map;
1325 sh2_read_handler *handler;
1329 offs = SH2MAP_ADDR2OFFS_R(a);
1332 if (!map_flag_set(p)) {
1333 // XXX: maybe 32bit access instead with ror?
1334 u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
1335 return (pd[0] << 16) | pd[1];
1339 return sh2_peripheral_read32(a, sh2->is_slave);
1341 handler = (sh2_read_handler *)(p << 1);
1342 return (handler(a, sh2->is_slave) << 16) | handler(a + 2, sh2->is_slave);
1345 // return nonzero if write potentially causes an interrupt (used by drc)
1346 int REGPARM(3) p32x_sh2_write8(u32 a, u32 d, SH2 *sh2)
1348 const void **sh2_wmap = sh2->write8_tab;
1349 sh2_write_handler *wh;
1351 wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
1352 return wh(a, d, sh2->is_slave);
1355 int REGPARM(3) p32x_sh2_write16(u32 a, u32 d, SH2 *sh2)
1357 const void **sh2_wmap = sh2->write16_tab;
1358 sh2_write_handler *wh;
1360 wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
1361 return wh(a, d, sh2->is_slave);
1364 int REGPARM(3) p32x_sh2_write32(u32 a, u32 d, SH2 *sh2)
1366 const void **sh2_wmap = sh2->write16_tab;
1367 sh2_write_handler *handler;
1370 offs = SH2MAP_ADDR2OFFS_W(a);
1372 if (offs == SH2MAP_ADDR2OFFS_W(0xffffc000)) {
1373 sh2_peripheral_write32(a, d, sh2->is_slave);
1377 handler = sh2_wmap[offs];
1378 handler(a, d >> 16, sh2->is_slave);
1379 handler(a + 2, d, sh2->is_slave);
1383 // -----------------------------------------------------------------
1385 static const u16 msh2_code[] = {
1386 // trap instructions
1387 0xaffe, // bra <self>
1389 // have to wait a bit until m68k initial program finishes clearing stuff
1390 // to avoid races with game SH2 code, like in Tempo
1391 0xd004, // mov.l @(_m_ok,pc), r0
1392 0xd105, // mov.l @(_cnt,pc), r1
1393 0xd205, // mov.l @(_start,pc), r2
1394 0x71ff, // add #-1, r1
1395 0x4115, // cmp/pl r1
1397 0xc208, // mov.l r0, @(h'20,gbr)
1398 0x6822, // mov.l @r2, r8
1401 ('M'<<8)|'_', ('O'<<8)|'K',
1403 0x2200, 0x03e0 // master start pointer in ROM
1406 static const u16 ssh2_code[] = {
1407 0xaffe, // bra <self>
1409 // code to wait for master, in case authentic master BIOS is used
1410 0xd104, // mov.l @(_m_ok,pc), r1
1411 0xd206, // mov.l @(_start,pc), r2
1412 0xc608, // mov.l @(h'20,gbr), r0
1413 0x3100, // cmp/eq r0, r1
1415 0xd003, // mov.l @(_s_ok,pc), r0
1416 0xc209, // mov.l r0, @(h'24,gbr)
1417 0x6822, // mov.l @r2, r8
1420 ('M'<<8)|'_', ('O'<<8)|'K',
1421 ('S'<<8)|'_', ('O'<<8)|'K',
1422 0x2200, 0x03e4 // slave start pointer in ROM
1425 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
1426 static void get_bios(void)
1433 if (p32x_bios_g != NULL) {
1434 elprintf(EL_STATUS|EL_32X, "32x: using supplied 68k BIOS");
1435 Byteswap(Pico32xMem->m68k_rom, p32x_bios_g, sizeof(Pico32xMem->m68k_rom));
1439 ps = (u16 *)Pico32xMem->m68k_rom;
1441 for (i = 1; i < 0xc0/4; i++)
1442 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
1445 for (i = 0xc0/2; i < 0x100/2; i++)
1449 ps[0xc0/2] = 0x46fc;
1450 ps[0xc2/2] = 0x2700; // move #0x2700,sr
1451 ps[0xfe/2] = 0x60fe; // jump to self
1453 ps[0xfe/2] = 0x4e75; // rts
1456 // fill remaining m68k_rom page with game ROM
1457 memcpy(Pico32xMem->m68k_rom_bank + sizeof(Pico32xMem->m68k_rom),
1458 Pico.rom + sizeof(Pico32xMem->m68k_rom),
1459 sizeof(Pico32xMem->m68k_rom_bank) - sizeof(Pico32xMem->m68k_rom));
1462 if (p32x_bios_m != NULL) {
1463 elprintf(EL_STATUS|EL_32X, "32x: using supplied master SH2 BIOS");
1464 Byteswap(Pico32xMem->sh2_rom_m, p32x_bios_m, sizeof(Pico32xMem->sh2_rom_m));
1467 pl = (u32 *)Pico32xMem->sh2_rom_m;
1469 // fill exception vector table to our trap address
1470 for (i = 0; i < 128; i++)
1471 pl[i] = HWSWAP(0x200);
1474 memcpy(Pico32xMem->sh2_rom_m + 0x200, msh2_code, sizeof(msh2_code));
1477 pl[1] = pl[3] = HWSWAP(0x6040000);
1479 pl[0] = pl[2] = HWSWAP(0x204);
1483 if (p32x_bios_s != NULL) {
1484 elprintf(EL_STATUS|EL_32X, "32x: using supplied slave SH2 BIOS");
1485 Byteswap(Pico32xMem->sh2_rom_s, p32x_bios_s, sizeof(Pico32xMem->sh2_rom_s));
1488 pl = (u32 *)Pico32xMem->sh2_rom_s;
1490 // fill exception vector table to our trap address
1491 for (i = 0; i < 128; i++)
1492 pl[i] = HWSWAP(0x200);
1495 memcpy(Pico32xMem->sh2_rom_s + 0x200, ssh2_code, sizeof(ssh2_code));
1498 pl[1] = pl[3] = HWSWAP(0x603f800);
1500 pl[0] = pl[2] = HWSWAP(0x204);
1504 #define MAP_MEMORY(m) ((uptr)(m) >> 1)
1505 #define MAP_HANDLER(h) ( ((uptr)(h) >> 1) | ((uptr)1 << (sizeof(uptr) * 8 - 1)) )
1507 static sh2_memmap sh2_read8_map[0x20], sh2_read16_map[0x20];
1508 // for writes we are using handlers only
1509 static sh2_write_handler *sh2_write8_map[0x80], *sh2_write16_map[0x80];
1511 void Pico32xSwapDRAM(int b)
1513 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1514 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1515 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1516 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1519 sh2_read8_map[2].addr = sh2_read8_map[6].addr =
1520 sh2_read16_map[2].addr = sh2_read16_map[6].addr = MAP_MEMORY(Pico32xMem->dram[b]);
1522 sh2_write8_map[0x04/2] = sh2_write8_map[0x24/2] = b ? sh2_write8_dram1 : sh2_write8_dram0;
1523 sh2_write16_map[0x04/2] = sh2_write16_map[0x24/2] = b ? sh2_write16_dram1 : sh2_write16_dram0;
1526 void PicoMemSetup32x(void)
1531 Pico32xMem = plat_mmap(0x06000000, sizeof(*Pico32xMem), 0, 0);
1532 if (Pico32xMem == NULL) {
1533 elprintf(EL_STATUS, "OOM");
1537 dmac0 = (void *)&Pico32xMem->sh2_peri_regs[0][0x180 / 4];
1541 // cartridge area becomes unmapped
1542 // XXX: we take the easy way and don't unmap ROM,
1543 // so that we can avoid handling the RV bit.
1544 // m68k_map_unmap(0x000000, 0x3fffff);
1547 rs = sizeof(Pico32xMem->m68k_rom_bank);
1548 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom_bank, 0);
1549 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom_bank, 0);
1550 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
1551 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
1553 // 32X ROM (unbanked, XXX: consider mirroring?)
1554 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
1557 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1558 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1560 // setup FAME fetchmap
1561 PicoCpuFM68k.Fetch[0] = (unsigned long)Pico32xMem->m68k_rom;
1562 for (rs = 0x88; rs < 0x90; rs++)
1563 PicoCpuFM68k.Fetch[rs] = (unsigned long)Pico.rom - 0x880000;
1570 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_32x_on, 1);
1571 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_32x_on, 1);
1572 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_32x_on, 1);
1573 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_32x_on, 1);
1575 // SH2 maps: A31,A30,A29,CS1,CS0
1576 // all unmapped by default
1577 for (i = 0; i < ARRAY_SIZE(sh2_read8_map); i++) {
1578 sh2_read8_map[i].addr = MAP_HANDLER(sh2_read8_unmapped);
1579 sh2_read16_map[i].addr = MAP_HANDLER(sh2_read16_unmapped);
1582 for (i = 0; i < ARRAY_SIZE(sh2_write8_map); i++) {
1583 sh2_write8_map[i] = sh2_write8_unmapped;
1584 sh2_write16_map[i] = sh2_write16_unmapped;
1588 for (i = 0x40; i <= 0x5f; i++) {
1589 sh2_write8_map[i >> 1] =
1590 sh2_write16_map[i >> 1] = sh2_write_ignore;
1594 sh2_read8_map[0].addr = sh2_read8_map[4].addr = MAP_HANDLER(sh2_read8_cs0);
1595 sh2_read16_map[0].addr = sh2_read16_map[4].addr = MAP_HANDLER(sh2_read16_cs0);
1596 sh2_write8_map[0x00/2] = sh2_write8_map[0x20/2] = sh2_write8_cs0;
1597 sh2_write16_map[0x00/2] = sh2_write16_map[0x20/2] = sh2_write16_cs0;
1599 sh2_read8_map[1].addr = sh2_read8_map[5].addr =
1600 sh2_read16_map[1].addr = sh2_read16_map[5].addr = MAP_MEMORY(Pico.rom);
1601 sh2_read8_map[1].mask = sh2_read8_map[5].mask =
1602 sh2_read16_map[1].mask = sh2_read16_map[5].mask = 0x3fffff; // FIXME
1603 // CS2 - DRAM - done by Pico32xSwapDRAM()
1604 sh2_read8_map[2].mask = sh2_read8_map[6].mask =
1605 sh2_read16_map[2].mask = sh2_read16_map[6].mask = 0x01ffff;
1607 sh2_read8_map[3].addr = sh2_read8_map[7].addr =
1608 sh2_read16_map[3].addr = sh2_read16_map[7].addr = MAP_MEMORY(Pico32xMem->sdram);
1609 sh2_write8_map[0x06/2] = sh2_write8_map[0x26/2] = sh2_write8_sdram;
1610 sh2_write16_map[0x06/2] = sh2_write16_map[0x26/2] = sh2_write16_sdram;
1611 sh2_read8_map[3].mask = sh2_read8_map[7].mask =
1612 sh2_read16_map[3].mask = sh2_read16_map[7].mask = 0x03ffff;
1614 sh2_read8_map[0x18].addr = MAP_HANDLER(sh2_read8_da);
1615 sh2_read16_map[0x18].addr = MAP_HANDLER(sh2_read16_da);
1616 sh2_write8_map[0xc0/2] = sh2_write8_da;
1617 sh2_write16_map[0xc0/2] = sh2_write16_da;
1619 sh2_read8_map[0x1f].addr = MAP_HANDLER(sh2_peripheral_read8);
1620 sh2_read16_map[0x1f].addr = MAP_HANDLER(sh2_peripheral_read16);
1621 sh2_write8_map[0xff/2] = sh2_peripheral_write8;
1622 sh2_write16_map[0xff/2] = sh2_peripheral_write16;
1624 // map DRAM area, both 68k and SH2
1627 msh2.read8_map = ssh2.read8_map = sh2_read8_map;
1628 msh2.read16_map = ssh2.read16_map = sh2_read16_map;
1629 msh2.write8_tab = ssh2.write8_tab = (const void **)(void *)sh2_write8_map;
1630 msh2.write16_tab = ssh2.write16_tab = (const void **)(void *)sh2_write16_map;
1632 sh2_drc_mem_setup(&msh2);
1633 sh2_drc_mem_setup(&ssh2);
1636 void Pico32xMemStateLoaded(void)
1638 bank_switch(Pico32x.regs[4 / 2]);
1639 Pico32xSwapDRAM((Pico32x.vdp_regs[0x0a / 2] & P32XV_FS) ^ P32XV_FS);
1640 memset(Pico32xMem->pwm, 0, sizeof(Pico32xMem->pwm));
1641 Pico32x.dirty_pal = 1;
1643 Pico32x.emu_flags &= ~(P32XF_68KCPOLL | P32XF_68KVPOLL);
1644 memset(&m68k_poll, 0, sizeof(m68k_poll));
1646 msh2.poll_addr = msh2.poll_cycles = msh2.poll_cnt = 0;
1648 ssh2.poll_addr = ssh2.poll_cycles = ssh2.poll_cnt = 0;
1650 sh2_drc_flush_all();
1653 // vim:shiftwidth=2:ts=2:expandtab