1 #include "../pico_int.h"
4 static const char str_mars[] = "MARS";
6 struct Pico32xMem *Pico32xMem;
8 static void bank_switch(int b);
11 static const u16 comm_fakevals[] = {
12 0x4d5f, 0x4f4b, // M_OK
13 0x535f, 0x4f4b, // S_OK
14 0x4D41, 0x5346, // MASF - Brutal Unleashed
15 0x5331, 0x4d31, // Darxide
18 0x0000, 0x0000, // eq for doom
19 0x0002, // Mortal Kombat
24 static u32 p32x_reg_read16(u32 a)
29 if ((a & 0x30) == 0x20)
32 if (a == 0x28 && !p32x_csum_faked) {
34 return *(unsigned short *)(Pico.rom + 0x18e);
36 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
38 return comm_fakevals[f++];
41 return Pico32x.regs[a / 2];
44 static void p32x_reg_write8(u32 a, u32 d)
48 if (a == 1 && !(Pico32x.regs[0] & 1)) {
54 if (!(Pico32x.regs[0] & 1))
59 if (Pico32x.regs[4/2] != d) {
60 Pico32x.regs[4/2] = d;
67 static void p32x_reg_write16(u32 a, u32 d)
69 p32x_reg_write8(a + 1, d);
73 static u32 p32x_vdp_read16(u32 a)
77 return Pico32x.vdp_regs[a / 2];
80 static void p32x_vdp_write8(u32 a, u32 d)
82 u16 *r = Pico32x.vdp_regs;
85 // TODO: verify what's writeable
88 if (((r[0] & 3) == 0) != ((d & 3) == 0)) { // forced blanking changed
89 if (Pico.video.status & 8)
90 r[0x0a/2] |= P32XV_VBLK;
92 r[0x0a/2] &= ~P32XV_VBLK;
94 // priority inversion is handled in palette
95 if ((r[0] ^ d) & P32XV_PRI)
96 Pico32x.dirty_pal = 1;
97 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
101 Pico32x.pending_fb = d;
102 // if we are blanking and FS bit is changing
103 if ((r[0x0a/2] & P32XV_VBLK) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
105 Pico32xSwapDRAM(d ^ 1);
111 static void p32x_vdp_write16(u32 a, u32 d)
113 p32x_vdp_write8(a | 1, d);
116 // default 32x handlers
117 u32 PicoRead8_32x(u32 a)
120 if ((a & 0xffc0) == 0x5100) { // a15100
121 d = p32x_reg_read16(a);
125 if (!(Pico32x.regs[0] & 1))
128 if ((a & 0xfff0) == 0x5180) { // a15180
129 d = p32x_vdp_read16(a);
133 if ((a & 0xfe00) == 0x5200) { // a15200
134 d = Pico32xMem->pal[(a & 0x1ff) / 2];
139 if ((a & 0xfffc) == 0x30ec) { // a130ec
144 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
154 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
158 u32 PicoRead16_32x(u32 a)
161 if ((a & 0xffc0) == 0x5100) { // a15100
162 d = p32x_reg_read16(a);
166 if (!(Pico32x.regs[0] & 1))
169 if ((a & 0xfff0) == 0x5180) { // a15180
170 d = p32x_vdp_read16(a);
174 if ((a & 0xfe00) == 0x5200) { // a15200
175 d = Pico32xMem->pal[(a & 0x1ff) / 2];
180 if ((a & 0xfffc) == 0x30ec) { // a130ec
181 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
185 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
189 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
193 void PicoWrite8_32x(u32 a, u32 d)
195 if ((a & 0xfc00) == 0x5000)
196 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
198 if ((a & 0xffc0) == 0x5100) { // a15100
199 p32x_reg_write8(a, d);
203 if (!(Pico32x.regs[0] & 1))
206 if ((a & 0xfff0) == 0x5180) { // a15180
207 p32x_vdp_write8(a, d);
212 if ((a & 0xfe00) == 0x5200) { // a15200
213 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
214 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
215 Pico32x.dirty_pal = 1;
220 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
223 void PicoWrite16_32x(u32 a, u32 d)
225 if ((a & 0xfc00) == 0x5000)
226 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
228 if ((a & 0xffc0) == 0x5100) { // a15100
229 p32x_reg_write16(a, d);
233 if (!(Pico32x.regs[0] & 1))
236 if ((a & 0xfff0) == 0x5180) { // a15180
237 p32x_vdp_write16(a, d);
241 if ((a & 0xfe00) == 0x5200) { // a15200
242 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
243 Pico32x.dirty_pal = 1;
248 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
251 // hint vector is writeable
252 static void PicoWrite8_hint(u32 a, u32 d)
254 if ((a & 0xfffc) == 0x0070) {
255 Pico32xMem->m68k_rom[a ^ 1] = d;
259 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
262 static void PicoWrite16_hint(u32 a, u32 d)
264 if ((a & 0xfffc) == 0x0070) {
265 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
269 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
272 void Pico32xSwapDRAM(int b)
274 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
275 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
276 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
277 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
280 static void bank_switch(int b)
282 unsigned int rs, bank;
285 if (bank >= Pico.romsize) {
286 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
290 // 32X ROM (unbanked, XXX: consider mirroring?)
291 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
295 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
296 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
298 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
301 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
302 void PicoMemSetup32x(void)
309 Pico32xMem = calloc(1, sizeof(*Pico32xMem));
310 if (Pico32xMem == NULL) {
311 elprintf(EL_STATUS, "OOM");
316 ps = (unsigned short *)Pico32xMem->m68k_rom;
317 pl = (unsigned int *)Pico32xMem->m68k_rom;
318 for (i = 1; i < 0xc0/4; i++)
319 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
322 for (i = 0xc0/2; i < 0x100/2; i++)
327 ps[0xc2/2] = 0x2700; // move #0x2700,sr
328 ps[0xfe/2] = 0x60fe; // jump to self
330 ps[0xfe/2] = 0x4e75; // rts
333 // fill remaining mem with ROM
334 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
336 // cartridge area becomes unmapped
337 // XXX: we take the easy way and don't unmap ROM,
338 // so that we can avoid handling the RV bit.
339 // m68k_map_unmap(0x000000, 0x3fffff);
342 rs = sizeof(Pico32xMem->m68k_rom);
343 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
344 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
345 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
346 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
351 // 32X ROM (unbanked, XXX: consider mirroring?)
352 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
355 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
356 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);