1 #include "../pico_int.h"
4 static const char str_mars[] = "MARS";
6 struct Pico32xMem *Pico32xMem;
9 static const u16 comm_fakevals[] = {
10 0x4d5f, 0x4f4b, // M_OK
11 0x535f, 0x4f4b, // S_OK
12 0x0002, // Mortal Kombat
16 static u32 p32x_reg_read16(u32 a)
21 if ((a & 0x30) == 0x20)
23 static int f = 0, csum_faked = 0;
24 if (a == 0x28 && !csum_faked) {
26 return *(unsigned short *)(Pico.rom + 0x18e);
28 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
30 return comm_fakevals[f++];
33 return Pico32x.regs[a / 2];
36 static void p32x_reg_write16(u32 a, u32 d)
40 if (a == 0 && !(Pico32x.regs[0] & 1)) {
47 static void p32x_reg_write8(u32 a, u32 d)
51 if (a == 1 && !(Pico32x.regs[0] & 1)) {
59 static u32 p32x_vdp_read16(u32 a)
63 return Pico32x.vdp_regs[a / 2];
66 static void p32x_vdp_write8(u32 a, u32 d)
68 u16 *r = Pico32x.vdp_regs;
71 // TODO: verify what's writeable
74 if (((r[0] & 3) == 0) != ((d & 3) == 0)) { // forced blanking changed
75 if (Pico.video.status & 8)
76 r[0x0a/2] |= P32XV_VBLK;
78 r[0x0a/2] &= ~P32XV_VBLK;
80 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
84 Pico32x.pending_fb = d;
85 // if we are blanking and FS bit is changing
86 if ((r[0x0a/2] & P32XV_VBLK) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
88 Pico32xSwapDRAM(d ^ 1);
94 static void p32x_vdp_write16(u32 a, u32 d)
96 p32x_vdp_write8(a | 1, d);
99 // default 32x handlers
100 u32 PicoRead8_32x(u32 a)
103 if ((a & 0xffc0) == 0x5100) { // a15100
104 d = p32x_reg_read16(a);
108 if (!(Pico32x.regs[0] & 1))
111 if ((a & 0xfff0) == 0x5180) { // a15180
112 d = p32x_vdp_read16(a);
116 if ((a & 0xfe00) == 0x5200) { // a15200
117 d = Pico32xMem->pal[(a & 0x1ff) / 2];
122 if ((a & 0xfffc) == 0x30ec) { // a130ec
127 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
137 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
141 u32 PicoRead16_32x(u32 a)
144 if ((a & 0xffc0) == 0x5100) { // a15100
145 d = p32x_reg_read16(a);
149 if (!(Pico32x.regs[0] & 1))
152 if ((a & 0xfff0) == 0x5180) { // a15180
153 d = p32x_vdp_read16(a);
157 if ((a & 0xfe00) == 0x5200) { // a15200
158 d = Pico32xMem->pal[(a & 0x1ff) / 2];
163 if ((a & 0xfffc) == 0x30ec) { // a130ec
164 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
168 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
172 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
176 void PicoWrite8_32x(u32 a, u32 d)
178 if ((a & 0xfc00) == 0x5000)
179 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
181 if ((a & 0xffc0) == 0x5100) { // a15100
182 p32x_reg_write8(a, d);
186 if (!(Pico32x.regs[0] & 1))
189 if ((a & 0xfff0) == 0x5180) { // a15180
190 p32x_vdp_write8(a, d);
195 if ((a & 0xfe00) == 0x5200) { // a15200
196 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
197 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
198 Pico32x.dirty_pal = 1;
203 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
206 void PicoWrite16_32x(u32 a, u32 d)
208 if ((a & 0xfc00) == 0x5000)
209 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
211 if ((a & 0xffc0) == 0x5100) { // a15100
212 p32x_reg_write16(a, d);
216 if (!(Pico32x.regs[0] & 1))
219 if ((a & 0xfff0) == 0x5180) { // a15180
220 p32x_vdp_write16(a, d);
224 if ((a & 0xfe00) == 0x5200) { // a15200
225 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
226 Pico32x.dirty_pal = 1;
231 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
234 // hint vector is writeable
235 static void PicoWrite8_hint(u32 a, u32 d)
237 if ((a & 0xfffc) == 0x0070) {
238 Pico32xMem->m68k_rom[a ^ 1] = d;
242 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
245 static void PicoWrite16_hint(u32 a, u32 d)
247 if ((a & 0xfffc) == 0x0070) {
248 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
252 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
255 void Pico32xSwapDRAM(int b)
257 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
258 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
259 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
260 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
263 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
264 void PicoMemSetup32x(void)
268 unsigned int rs, rs1;
271 Pico32xMem = calloc(1, sizeof(*Pico32xMem));
272 if (Pico32xMem == NULL) {
273 elprintf(EL_STATUS, "OOM");
278 ps = (unsigned short *)Pico32xMem->m68k_rom;
279 pl = (unsigned int *)Pico32xMem->m68k_rom;
280 for (i = 1; i < 0xc0/4; i++)
281 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
284 for (i = 0xc0/2; i < 0x100/2; i++)
288 ps[0xc2/2] = 0x2700; // move #0x2700,sr
289 ps[0xfe/2] = 0x60fe; // jump to self
291 // fill remaining mem with ROM
292 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
294 // cartridge area becomes unmapped
295 // XXX: we take the easy way and don't unmap ROM,
296 // so that we can avoid handling the RV bit.
297 // m68k_map_unmap(0x000000, 0x3fffff);
300 rs = sizeof(Pico32xMem->m68k_rom);
301 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
302 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
303 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
304 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
309 // 32X ROM (unbanked, XXX: consider mirroring?)
310 rs1 = rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
313 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs1 - 1, Pico.rom, 0);
314 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs1 - 1, Pico.rom, 0);
319 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom, 0);
320 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom, 0);