new 32x renderers, auto fskip change, massive refactoring
[picodrive.git] / pico / 32x / memory.c
CommitLineData
83ff19ec 1/*
bcf65fd6 2 * SH2 addr lines:
3 * iii. .cc. ..xx * // Internal, Cs, x
4 *
83ff19ec 5 * Register map:
6 * a15100 F....... R.....EA F.....AC N...VHMP 4000 // Fm Ren nrEs Aden Cart heN V H cMd Pwm
7 * a15102 ........ ......SM ? 4002 // intS intM
8 * a15104 ........ ......10 ........ hhhhhhhh 4004 // bk1 bk0 Hint
9 * a15106 F....... .....SDR UE...... .....SDR 4006 // Full 68S Dma Rv fUll[fb] Empt[fb]
10 * a15108 (32bit DREQ src) 4008
11 * a1510c (32bit DREQ dst) 400c
12 * a15110 llllllll llllll00 4010 // DREQ Len
13 * a15112 (16bit FIFO reg) 4012
14 * a15114 ? (16bit VRES clr) 4014
15 * a15116 ? (16bit Vint clr) 4016
16 * a15118 ? (16bit Hint clr) 4018
17 * a1511a ........ .......C (16bit CMD clr) 401a // Cm
18 * a1511c ? (16bit PWM clr) 401c
19 * a1511e ? ? 401e
20 * a15120 (16 bytes comm) 2020
21 * a15130 (PWM) 2030
22 */
be2c4208 23#include "../pico_int.h"
24#include "../memory.h"
f4bb5d6b 25#ifdef DRC_SH2
26#include "../../cpu/sh2/compiler.h"
27#endif
be2c4208 28
236990cf 29#if 0
c987bb5c 30#undef ash2_end_run
31#undef SekEndRun
32#define ash2_end_run(x)
33#define SekEndRun(x)
34#endif
35
be2c4208 36static const char str_mars[] = "MARS";
37
83ff19ec 38void *p32x_bios_g, *p32x_bios_m, *p32x_bios_s;
974fdb5b 39struct Pico32xMem *Pico32xMem;
40
5e49c3a8 41static void bank_switch(int b);
42
266c6afa 43// poll detection
4ea707e1 44#define POLL_THRESHOLD 6
45
266c6afa 46struct poll_det {
be20816c 47 u32 addr, cycles, cyc_max;
48 int cnt, flag;
266c6afa 49};
b78efee2 50static struct poll_det m68k_poll, sh2_poll[2];
266c6afa 51
be20816c 52static int p32x_poll_detect(struct poll_det *pd, u32 a, u32 cycles, int is_vdp)
266c6afa 53{
b78efee2 54 int ret = 0, flag = pd->flag;
55
56 if (is_vdp)
57 flag <<= 3;
266c6afa 58
1d7a28a7 59 if (a - 2 <= pd->addr && pd->addr <= a + 2 && cycles - pd->cycles <= pd->cyc_max) {
266c6afa 60 pd->cnt++;
61 if (pd->cnt > POLL_THRESHOLD) {
62 if (!(Pico32x.emu_flags & flag)) {
be20816c 63 elprintf(EL_32X, "%s poll addr %08x, cyc %u",
64 flag & (P32XF_68KPOLL|P32XF_68KVPOLL) ? "m68k" :
65 (flag & (P32XF_MSH2POLL|P32XF_MSH2VPOLL) ? "msh2" : "ssh2"), a, cycles - pd->cycles);
266c6afa 66 ret = 1;
67 }
68 Pico32x.emu_flags |= flag;
69 }
70 }
c987bb5c 71 else {
266c6afa 72 pd->cnt = 0;
c987bb5c 73 pd->addr = a;
74 }
be20816c 75 pd->cycles = cycles;
266c6afa 76
77 return ret;
78}
79
b78efee2 80static int p32x_poll_undetect(struct poll_det *pd, int is_vdp)
266c6afa 81{
b78efee2 82 int ret = 0, flag = pd->flag;
83 if (is_vdp)
be20816c 84 flag <<= 3; // VDP only
85 else
86 flag |= flag << 3; // both
87 if (Pico32x.emu_flags & flag) {
88 elprintf(EL_32X, "poll %02x -> %02x", Pico32x.emu_flags, Pico32x.emu_flags & ~flag);
266c6afa 89 ret = 1;
be20816c 90 }
266c6afa 91 Pico32x.emu_flags &= ~flag;
be20816c 92 pd->addr = pd->cnt = 0;
266c6afa 93 return ret;
94}
95
87accdf7 96void p32x_poll_event(int cpu_mask, int is_vdp)
4ea707e1 97{
87accdf7 98 if (cpu_mask & 1)
99 p32x_poll_undetect(&sh2_poll[0], is_vdp);
100 if (cpu_mask & 2)
101 p32x_poll_undetect(&sh2_poll[1], is_vdp);
4ea707e1 102}
103
974fdb5b 104// SH2 faking
b78efee2 105//#define FAKE_SH2
acd35d4c 106int p32x_csum_faked;
107#ifdef FAKE_SH2
974fdb5b 108static const u16 comm_fakevals[] = {
109 0x4d5f, 0x4f4b, // M_OK
110 0x535f, 0x4f4b, // S_OK
5e49c3a8 111 0x4D41, 0x5346, // MASF - Brutal Unleashed
112 0x5331, 0x4d31, // Darxide
113 0x5332, 0x4d32,
114 0x5333, 0x4d33,
115 0x0000, 0x0000, // eq for doom
974fdb5b 116 0x0002, // Mortal Kombat
acd35d4c 117// 0, // pad
be2c4208 118};
acd35d4c 119
120static u32 sh2_comm_faker(u32 a)
121{
122 static int f = 0;
123 if (a == 0x28 && !p32x_csum_faked) {
124 p32x_csum_faked = 1;
125 return *(unsigned short *)(Pico.rom + 0x18e);
126 }
127 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
128 f = 0;
129 return comm_fakevals[f++];
130}
131#endif
be2c4208 132
4ea707e1 133// DMAC handling
134static struct {
135 unsigned int sar0, dar0, tcr0; // src addr, dst addr, transfer count
136 unsigned int chcr0; // chan ctl
137 unsigned int sar1, dar1, tcr1; // same for chan 1
138 unsigned int chcr1;
139 int pad[4];
140 unsigned int dmaor;
141} * dmac0;
142
143static void dma_68k2sh2_do(void)
144{
145 unsigned short *dreqlen = &Pico32x.regs[0x10 / 2];
146 int i;
147
148 if (dmac0->tcr0 != *dreqlen)
149 elprintf(EL_32X|EL_ANOMALY, "tcr0 and dreq len differ: %d != %d", dmac0->tcr0, *dreqlen);
150
1b3f5844 151 // HACK: assume bus is busy and SH2 is halted
152 // XXX: use different mechanism for this, not poll det
153 Pico32x.emu_flags |= P32XF_MSH2POLL; // id ? P32XF_SSH2POLL : P32XF_MSH2POLL;
154
4ea707e1 155 for (i = 0; i < Pico32x.dmac_ptr && dmac0->tcr0 > 0; i++) {
bcf65fd6 156 elprintf(EL_32X, "dmaw [%08x] %04x, left %d", dmac0->dar0, Pico32x.dmac_fifo[i], *dreqlen);
157 p32x_sh2_write16(dmac0->dar0, Pico32x.dmac_fifo[i], &msh2);
4ea707e1 158 dmac0->dar0 += 2;
159 dmac0->tcr0--;
160 (*dreqlen)--;
161 }
162
163 Pico32x.dmac_ptr = 0; // HACK
164 Pico32x.regs[6 / 2] &= ~P32XS_FULL;
165 if (*dreqlen == 0)
166 Pico32x.regs[6 / 2] &= ~P32XS_68S; // transfer complete
be20816c 167 if (dmac0->tcr0 == 0) {
4ea707e1 168 dmac0->chcr0 |= 2; // DMA has ended normally
be20816c 169 p32x_poll_undetect(&sh2_poll[0], 0);
170 }
4ea707e1 171}
172
173// ------------------------------------------------------------------
b78efee2 174// 68k regs
4ea707e1 175
be2c4208 176static u32 p32x_reg_read16(u32 a)
177{
178 a &= 0x3e;
179
87accdf7 180 if (a == 2) // INTM, INTS
181 return ((Pico32x.sh2irqi[0] & P32XI_CMD) >> 4) | ((Pico32x.sh2irqi[1] & P32XI_CMD) >> 3);
3cf9570b 182#if 0
974fdb5b 183 if ((a & 0x30) == 0x20)
acd35d4c 184 return sh2_comm_faker(a);
266c6afa 185#else
5fadfb1c 186 if ((a & 0x30) == 0x20) {
187 // evil X-Men proto polls in a dbra loop and expects it to expire..
188 static u32 dr2 = 0;
189 if (SekDar(2) != dr2)
190 m68k_poll.cnt = 0;
191 dr2 = SekDar(2);
192
193 if (p32x_poll_detect(&m68k_poll, a, SekCyclesDoneT(), 0)) {
194 SekSetStop(1);
195 SekEndTimeslice(16);
196 }
197 dr2 = SekDar(2);
266c6afa 198 }
acd35d4c 199#endif
87accdf7 200
db1d3564 201 if ((a & 0x30) == 0x30)
202 return p32x_pwm_read16(a);
974fdb5b 203
be2c4208 204 return Pico32x.regs[a / 2];
205}
206
be2c4208 207static void p32x_reg_write8(u32 a, u32 d)
208{
acd35d4c 209 u16 *r = Pico32x.regs;
be2c4208 210 a &= 0x3f;
211
97d3f47f 212 // for things like bset on comm port
213 m68k_poll.cnt = 0;
214
acd35d4c 215 switch (a) {
4ea707e1 216 case 0: // adapter ctl
83ff19ec 217 r[0] = (r[0] & ~P32XS_FM) | ((d << 8) & P32XS_FM);
218 return;
219 case 1: // adapter ctl, RES bit writeable
220 if ((d ^ r[0]) & d & P32XS_nRES)
221 p32x_reset_sh2s();
222 r[0] = (r[0] & ~P32XS_nRES) | (d & P32XS_nRES);
1b3f5844 223 return;
4ea707e1 224 case 3: // irq ctl
225 if ((d & 1) && !(Pico32x.sh2irqi[0] & P32XI_CMD)) {
226 Pico32x.sh2irqi[0] |= P32XI_CMD;
227 p32x_update_irls();
87accdf7 228 SekEndRun(16);
4ea707e1 229 }
b78efee2 230 if ((d & 2) && !(Pico32x.sh2irqi[1] & P32XI_CMD)) {
231 Pico32x.sh2irqi[1] |= P32XI_CMD;
232 p32x_update_irls();
87accdf7 233 SekEndRun(16);
b78efee2 234 }
1b3f5844 235 return;
4ea707e1 236 case 5: // bank
acd35d4c 237 d &= 7;
4ea707e1 238 if (r[4 / 2] != d) {
239 r[4 / 2] = d;
acd35d4c 240 bank_switch(d);
241 }
1b3f5844 242 return;
4ea707e1 243 case 7: // DREQ ctl
97d3f47f 244 r[6 / 2] = (r[6 / 2] & P32XS_FULL) | (d & (P32XS_68S|P32XS_DMA|P32XS_RV));
1b3f5844 245 return;
87accdf7 246 case 0x1b: // TV
247 r[0x1a / 2] = d;
1b3f5844 248 return;
249 }
250
251 if ((a & 0x30) == 0x20) {
252 u8 *r8 = (u8 *)r;
253 r8[a ^ 1] = d;
236990cf 254 p32x_poll_undetect(&sh2_poll[0], 0);
255 p32x_poll_undetect(&sh2_poll[1], 0);
256 // if some SH2 is busy waiting, it needs to see the result ASAP
257 if (SekCyclesLeftNoMCD > 32)
258 SekEndRun(32);
1b3f5844 259 return;
5e49c3a8 260 }
261}
262
263static void p32x_reg_write16(u32 a, u32 d)
264{
acd35d4c 265 u16 *r = Pico32x.regs;
266 a &= 0x3e;
267
97d3f47f 268 // for things like bset on comm port
269 m68k_poll.cnt = 0;
270
acd35d4c 271 switch (a) {
4ea707e1 272 case 0x00: // adapter ctl
83ff19ec 273 if ((d ^ r[0]) & d & P32XS_nRES)
274 p32x_reset_sh2s();
275 r[0] = (r[0] & ~(P32XS_FM|P32XS_nRES)) | (d & (P32XS_FM|P32XS_nRES));
acd35d4c 276 return;
4ea707e1 277 case 0x10: // DREQ len
278 r[a / 2] = d & ~3;
279 return;
280 case 0x12: // FIFO reg
281 if (!(r[6 / 2] & P32XS_68S)) {
282 elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO w16 without 68S?");
283 return;
284 }
285 if (Pico32x.dmac_ptr < DMAC_FIFO_LEN) {
286 Pico32x.dmac_fifo[Pico32x.dmac_ptr++] = d;
287 if ((Pico32x.dmac_ptr & 3) == 0 && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1))
288 dma_68k2sh2_do();
289 if (Pico32x.dmac_ptr == DMAC_FIFO_LEN)
290 r[6 / 2] |= P32XS_FULL;
291 }
292 break;
acd35d4c 293 }
294
4ea707e1 295 // DREQ src, dst
296 if ((a & 0x38) == 0x08) {
297 r[a / 2] = d;
298 return;
299 }
300 // comm port
301 else if ((a & 0x30) == 0x20 && r[a / 2] != d) {
acd35d4c 302 r[a / 2] = d;
236990cf 303 p32x_poll_undetect(&sh2_poll[0], 0);
304 p32x_poll_undetect(&sh2_poll[1], 0);
305 // same as for w8
306 if (SekCyclesLeftNoMCD > 32)
307 SekEndRun(32);
acd35d4c 308 return;
309 }
db1d3564 310 // PWM
311 else if ((a & 0x30) == 0x30) {
312 p32x_pwm_write16(a, d);
313 return;
314 }
acd35d4c 315
5e49c3a8 316 p32x_reg_write8(a + 1, d);
be2c4208 317}
318
4ea707e1 319// ------------------------------------------------------------------
be2c4208 320// VDP regs
321static u32 p32x_vdp_read16(u32 a)
322{
323 a &= 0x0e;
324
325 return Pico32x.vdp_regs[a / 2];
326}
327
be2c4208 328static void p32x_vdp_write8(u32 a, u32 d)
329{
974fdb5b 330 u16 *r = Pico32x.vdp_regs;
be2c4208 331 a &= 0x0f;
332
4ea707e1 333 // for FEN checks between writes
b78efee2 334 sh2_poll[0].cnt = 0;
4ea707e1 335
974fdb5b 336 // TODO: verify what's writeable
be2c4208 337 switch (a) {
974fdb5b 338 case 0x01:
5e49c3a8 339 // priority inversion is handled in palette
340 if ((r[0] ^ d) & P32XV_PRI)
341 Pico32x.dirty_pal = 1;
974fdb5b 342 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
be20816c 343 break;
344 case 0x05: // fill len
345 r[4 / 2] = d & 0xff;
974fdb5b 346 break;
be2c4208 347 case 0x0b:
974fdb5b 348 d &= 1;
349 Pico32x.pending_fb = d;
350 // if we are blanking and FS bit is changing
4ea707e1 351 if (((r[0x0a/2] & P32XV_VBLK) || (r[0] & P32XV_Mx) == 0) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
974fdb5b 352 r[0x0a/2] ^= 1;
353 Pico32xSwapDRAM(d ^ 1);
266c6afa 354 elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS);
be2c4208 355 }
356 break;
357 }
358}
359
974fdb5b 360static void p32x_vdp_write16(u32 a, u32 d)
361{
be20816c 362 a &= 0x0e;
363 if (a == 6) { // fill start
364 Pico32x.vdp_regs[6 / 2] = d;
365 return;
366 }
367 if (a == 8) { // fill data
368 u16 *dram = Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
1b3f5844 369 int len = Pico32x.vdp_regs[4 / 2] + 1;
be20816c 370 a = Pico32x.vdp_regs[6 / 2];
371 while (len--) {
372 dram[a] = d;
373 a = (a & 0xff00) | ((a + 1) & 0xff);
374 }
375 Pico32x.vdp_regs[6 / 2] = a;
376 Pico32x.vdp_regs[8 / 2] = d;
377 return;
378 }
379
974fdb5b 380 p32x_vdp_write8(a | 1, d);
381}
382
4ea707e1 383// ------------------------------------------------------------------
acd35d4c 384// SH2 regs
b78efee2 385
386static u32 p32x_sh2reg_read16(u32 a, int cpuid)
acd35d4c 387{
4ea707e1 388 u16 *r = Pico32x.regs;
389 a &= 0xfe; // ?
266c6afa 390
4ea707e1 391 switch (a) {
392 case 0x00: // adapter/irq ctl
87accdf7 393 return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0] | Pico32x.sh2irq_mask[cpuid];
c987bb5c 394 case 0x04: // H count (often as comm too)
395 if (p32x_poll_detect(&sh2_poll[cpuid], a, ash2_cycles_done(), 0))
396 ash2_end_run(8);
87accdf7 397 return Pico32x.sh2_regs[4 / 2];
4ea707e1 398 case 0x10: // DREQ len
399 return r[a / 2];
acd35d4c 400 }
4ea707e1 401
db1d3564 402 // DREQ src, dst
403 if ((a & 0x38) == 0x08)
4ea707e1 404 return r[a / 2];
db1d3564 405 // comm port
406 if ((a & 0x30) == 0x20) {
be20816c 407 if (p32x_poll_detect(&sh2_poll[cpuid], a, ash2_cycles_done(), 0))
db1d3564 408 ash2_end_run(8);
409 return r[a / 2];
410 }
411 if ((a & 0x30) == 0x30) {
412 sh2_poll[cpuid].cnt = 0;
413 return p32x_pwm_read16(a);
414 }
acd35d4c 415
416 return 0;
417}
418
b78efee2 419static void p32x_sh2reg_write8(u32 a, u32 d, int cpuid)
acd35d4c 420{
4ea707e1 421 a &= 0xff;
87accdf7 422 switch (a) {
423 case 0: // FM
424 Pico32x.regs[0] &= ~P32XS_FM;
425 Pico32x.regs[0] |= (d << 8) & P32XS_FM;
1b3f5844 426 return;
87accdf7 427 case 1: //
428 Pico32x.sh2irq_mask[cpuid] = d & 0x8f;
429 Pico32x.sh2_regs[0] &= ~0x80;
430 Pico32x.sh2_regs[0] |= d & 0x80;
431 p32x_update_irls();
1b3f5844 432 return;
87accdf7 433 case 5: // H count
434 Pico32x.sh2_regs[4 / 2] = d & 0xff;
c987bb5c 435 p32x_poll_undetect(&sh2_poll[cpuid ^ 1], 0);
1b3f5844 436 return;
437 }
438
439 if ((a & 0x30) == 0x20) {
440 u8 *r8 = (u8 *)Pico32x.regs;
441 r8[a ^ 1] = d;
5fadfb1c 442 if (p32x_poll_undetect(&m68k_poll, 0))
443 SekSetStop(0);
1b3f5844 444 p32x_poll_undetect(&sh2_poll[cpuid ^ 1], 0);
445 return;
4ea707e1 446 }
acd35d4c 447}
448
b78efee2 449static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
acd35d4c 450{
4ea707e1 451 a &= 0xfe;
acd35d4c 452
db1d3564 453 // comm
4ea707e1 454 if ((a & 0x30) == 0x20 && Pico32x.regs[a/2] != d) {
b78efee2 455 Pico32x.regs[a / 2] = d;
5fadfb1c 456 if (p32x_poll_undetect(&m68k_poll, 0))
457 SekSetStop(0);
b78efee2 458 p32x_poll_undetect(&sh2_poll[cpuid ^ 1], 0);
acd35d4c 459 return;
460 }
db1d3564 461 // PWM
462 else if ((a & 0x30) == 0x30) {
463 p32x_pwm_write16(a, d);
464 return;
465 }
acd35d4c 466
4ea707e1 467 switch (a) {
87accdf7 468 case 0: // FM
469 Pico32x.regs[0] &= ~P32XS_FM;
470 Pico32x.regs[0] |= d & P32XS_FM;
471 break;
4ea707e1 472 case 0x14: Pico32x.sh2irqs &= ~P32XI_VRES; goto irls;
473 case 0x16: Pico32x.sh2irqs &= ~P32XI_VINT; goto irls;
474 case 0x18: Pico32x.sh2irqs &= ~P32XI_HINT; goto irls;
b78efee2 475 case 0x1a: Pico32x.sh2irqi[cpuid] &= ~P32XI_CMD; goto irls;
be20816c 476 case 0x1c:
477 Pico32x.sh2irqs &= ~P32XI_PWM;
1d7a28a7 478 p32x_timers_do(0);
be20816c 479 goto irls;
4ea707e1 480 }
481
b78efee2 482 p32x_sh2reg_write8(a | 1, d, cpuid);
4ea707e1 483 return;
484
485irls:
486 p32x_update_irls();
487}
488
87accdf7 489// ------------------------------------------------------------------
490// SH2 internal peripherals
1d7a28a7 491// we keep them in little endian format
87accdf7 492static u32 sh2_peripheral_read8(u32 a, int id)
493{
494 u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
495 u32 d;
496
497 a &= 0x1ff;
1d7a28a7 498 d = PREG8(r, a);
87accdf7 499
500 elprintf(EL_32X, "%csh2 peri r8 [%08x] %02x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
501 return d;
502}
503
1d7a28a7 504static u32 sh2_peripheral_read16(u32 a, int id)
505{
506 u16 *r = (void *)Pico32xMem->sh2_peri_regs[id];
507 u32 d;
508
509 a &= 0x1ff;
510 d = r[(a / 2) ^ 1];
511
512 elprintf(EL_32X, "%csh2 peri r16 [%08x] %04x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
513 return d;
514}
515
87accdf7 516static u32 sh2_peripheral_read32(u32 a, int id)
4ea707e1 517{
518 u32 d;
519 a &= 0x1fc;
97d3f47f 520 d = Pico32xMem->sh2_peri_regs[id][a / 4];
4ea707e1 521
97d3f47f 522 elprintf(EL_32X, "%csh2 peri r32 [%08x] %08x @%06x", id ? 's' : 'm', a | ~0x1ff, d, sh2_pc(id));
4ea707e1 523 return d;
acd35d4c 524}
525
e05b81fc 526static int REGPARM(3) sh2_peripheral_write8(u32 a, u32 d, int id)
87accdf7 527{
528 u8 *r = (void *)Pico32xMem->sh2_peri_regs[id];
529 elprintf(EL_32X, "%csh2 peri w8 [%08x] %02x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
530
531 a &= 0x1ff;
1d7a28a7 532 PREG8(r, a) = d;
533
534 // X-men SCI hack
535 if ((a == 2 && (d & 0x20)) || // transmiter enabled
536 (a == 4 && !(d & 0x80))) { // valid data in TDR
537 void *oregs = Pico32xMem->sh2_peri_regs[id ^ 1];
538 if ((PREG8(oregs, 2) & 0x50) == 0x50) { // receiver + irq enabled
539 int level = PREG8(oregs, 0x60) >> 4;
540 int vector = PREG8(oregs, 0x63) & 0x7f;
541 elprintf(EL_32X, "%csh2 SCI recv irq (%d, %d)", (id ^ 1) ? 's' : 'm', level, vector);
542 sh2_internal_irq(&sh2s[id ^ 1], level, vector);
e05b81fc 543 return 1;
1d7a28a7 544 }
545 }
e05b81fc 546 return 0;
1d7a28a7 547}
548
e05b81fc 549static int REGPARM(3) sh2_peripheral_write16(u32 a, u32 d, int id)
1d7a28a7 550{
551 u16 *r = (void *)Pico32xMem->sh2_peri_regs[id];
552 elprintf(EL_32X, "%csh2 peri w16 [%08x] %04x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
553
554 a &= 0x1ff;
555
556 // evil WDT
557 if (a == 0x80) {
558 if ((d & 0xff00) == 0xa500) { // WTCSR
559 PREG8(r, 0x80) = d;
560 p32x_timers_recalc();
561 }
562 if ((d & 0xff00) == 0x5a00) // WTCNT
563 PREG8(r, 0x81) = d;
e05b81fc 564 return 0;
1d7a28a7 565 }
566
567 r[(a / 2) ^ 1] = d;
e05b81fc 568 return 0;
87accdf7 569}
570
571static void sh2_peripheral_write32(u32 a, u32 d, int id)
4ea707e1 572{
be20816c 573 u32 *r = Pico32xMem->sh2_peri_regs[id];
b78efee2 574 elprintf(EL_32X, "%csh2 peri w32 [%08x] %08x @%06x", id ? 's' : 'm', a, d, sh2_pc(id));
4ea707e1 575
576 a &= 0x1fc;
577 r[a / 4] = d;
578
97d3f47f 579 switch (a) {
be20816c 580 // division unit (TODO: verify):
97d3f47f 581 case 0x104: // DVDNT: divident L, starts divide
582 elprintf(EL_32X, "%csh2 divide %08x / %08x", id ? 's' : 'm', d, r[0x100 / 4]);
583 if (r[0x100 / 4]) {
be20816c 584 signed int divisor = r[0x100 / 4];
585 r[0x118 / 4] = r[0x110 / 4] = (signed int)d % divisor;
586 r[0x104 / 4] = r[0x11c / 4] = r[0x114 / 4] = (signed int)d / divisor;
97d3f47f 587 }
1625ed01 588 else
589 r[0x110 / 4] = r[0x114 / 4] = r[0x118 / 4] = r[0x11c / 4] = 0; // ?
97d3f47f 590 break;
591 case 0x114:
592 elprintf(EL_32X, "%csh2 divide %08x%08x / %08x @%08x",
593 id ? 's' : 'm', r[0x110 / 4], d, r[0x100 / 4], sh2_pc(id));
594 if (r[0x100 / 4]) {
be20816c 595 signed long long divident = (signed long long)r[0x110 / 4] << 32 | d;
596 signed int divisor = r[0x100 / 4];
97d3f47f 597 // XXX: undocumented mirroring to 0x118,0x11c?
be20816c 598 r[0x118 / 4] = r[0x110 / 4] = divident % divisor;
1625ed01 599 divident /= divisor;
600 r[0x11c / 4] = r[0x114 / 4] = divident;
601 divident >>= 31;
602 if ((unsigned long long)divident + 1 > 1) {
603 //elprintf(EL_32X, "%csh2 divide overflow! @%08x", id ? 's' : 'm', sh2_pc(id));
604 r[0x11c / 4] = r[0x114 / 4] = divident > 0 ? 0x7fffffff : 0x80000000; // overflow
605 }
97d3f47f 606 }
1625ed01 607 else
608 r[0x110 / 4] = r[0x114 / 4] = r[0x118 / 4] = r[0x11c / 4] = 0; // ?
97d3f47f 609 break;
610 }
611
4ea707e1 612 if ((a == 0x1b0 || a == 0x18c) && (dmac0->chcr0 & 3) == 1 && (dmac0->dmaor & 1)) {
613 elprintf(EL_32X, "sh2 DMA %08x -> %08x, cnt %d, chcr %04x @%06x",
b78efee2 614 dmac0->sar0, dmac0->dar0, dmac0->tcr0, dmac0->chcr0, sh2_pc(id));
4ea707e1 615 dmac0->tcr0 &= 0xffffff;
be20816c 616
1b3f5844 617 // HACK: assume 68k starts writing soon and end the timeslice
618 ash2_end_run(16);
be20816c 619
4ea707e1 620 // DREQ is only sent after first 4 words are written.
621 // we do multiple of 4 words to avoid messing up alignment
622 if (dmac0->sar0 == 0x20004012 && Pico32x.dmac_ptr && (Pico32x.dmac_ptr & 3) == 0) {
623 elprintf(EL_32X, "68k -> sh2 DMA");
624 dma_68k2sh2_do();
625 }
626 }
627}
628
629// ------------------------------------------------------------------
83ff19ec 630// 32x handlers
631
632// after ADEN
633static u32 PicoRead8_32x_on(u32 a)
be2c4208 634{
635 u32 d = 0;
636 if ((a & 0xffc0) == 0x5100) { // a15100
637 d = p32x_reg_read16(a);
638 goto out_16to8;
639 }
640
83ff19ec 641 if ((a & 0xfc00) != 0x5000)
642 return PicoRead8_io(a);
974fdb5b 643
644 if ((a & 0xfff0) == 0x5180) { // a15180
be2c4208 645 d = p32x_vdp_read16(a);
646 goto out_16to8;
647 }
648
974fdb5b 649 if ((a & 0xfe00) == 0x5200) { // a15200
650 d = Pico32xMem->pal[(a & 0x1ff) / 2];
651 goto out_16to8;
652 }
653
be2c4208 654 if ((a & 0xfffc) == 0x30ec) { // a130ec
655 d = str_mars[a & 3];
656 goto out;
657 }
658
659 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
660 return d;
661
662out_16to8:
663 if (a & 1)
664 d &= 0xff;
665 else
666 d >>= 8;
667
668out:
669 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
670 return d;
671}
672
83ff19ec 673static u32 PicoRead16_32x_on(u32 a)
be2c4208 674{
675 u32 d = 0;
676 if ((a & 0xffc0) == 0x5100) { // a15100
677 d = p32x_reg_read16(a);
678 goto out;
679 }
680
83ff19ec 681 if ((a & 0xfc00) != 0x5000)
682 return PicoRead16_io(a);
974fdb5b 683
684 if ((a & 0xfff0) == 0x5180) { // a15180
be2c4208 685 d = p32x_vdp_read16(a);
686 goto out;
687 }
688
974fdb5b 689 if ((a & 0xfe00) == 0x5200) { // a15200
690 d = Pico32xMem->pal[(a & 0x1ff) / 2];
691 goto out;
692 }
693
be2c4208 694 if ((a & 0xfffc) == 0x30ec) { // a130ec
695 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
696 goto out;
697 }
698
699 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
700 return d;
701
702out:
703 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
704 return d;
705}
706
83ff19ec 707static void PicoWrite8_32x_on(u32 a, u32 d)
be2c4208 708{
709 if ((a & 0xfc00) == 0x5000)
710 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
711
712 if ((a & 0xffc0) == 0x5100) { // a15100
713 p32x_reg_write8(a, d);
714 return;
715 }
716
83ff19ec 717 if ((a & 0xfc00) != 0x5000) {
718 PicoWrite8_io(a, d);
719 return;
720 }
974fdb5b 721
722 if ((a & 0xfff0) == 0x5180) { // a15180
be2c4208 723 p32x_vdp_write8(a, d);
724 return;
725 }
726
974fdb5b 727 // TODO: verify
728 if ((a & 0xfe00) == 0x5200) { // a15200
729 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
730 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
731 Pico32x.dirty_pal = 1;
732 return;
733 }
734
be2c4208 735 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
736}
737
83ff19ec 738static void PicoWrite16_32x_on(u32 a, u32 d)
be2c4208 739{
740 if ((a & 0xfc00) == 0x5000)
553c3eaa 741 elprintf(EL_32X, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
be2c4208 742
743 if ((a & 0xffc0) == 0x5100) { // a15100
744 p32x_reg_write16(a, d);
745 return;
746 }
747
83ff19ec 748 if ((a & 0xfc00) != 0x5000) {
749 PicoWrite16_io(a, d);
750 return;
751 }
974fdb5b 752
753 if ((a & 0xfff0) == 0x5180) { // a15180
be2c4208 754 p32x_vdp_write16(a, d);
755 return;
756 }
757
974fdb5b 758 if ((a & 0xfe00) == 0x5200) { // a15200
759 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
760 Pico32x.dirty_pal = 1;
761 return;
762 }
763
be2c4208 764 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
765}
766
83ff19ec 767// before ADEN
768u32 PicoRead8_32x(u32 a)
769{
770 u32 d = 0;
771 if ((a & 0xffc0) == 0x5100) { // a15100
772 // regs are always readable
773 d = ((u8 *)Pico32x.regs)[(a & 0x3f) ^ 1];
774 goto out;
775 }
776
777 if ((a & 0xfffc) == 0x30ec) { // a130ec
778 d = str_mars[a & 3];
779 goto out;
780 }
781
782 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
783 return d;
784
785out:
786 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
787 return d;
788}
789
790u32 PicoRead16_32x(u32 a)
791{
792 u32 d = 0;
793 if ((a & 0xffc0) == 0x5100) { // a15100
794 d = Pico32x.regs[(a & 0x3f) / 2];
795 goto out;
796 }
797
798 if ((a & 0xfffc) == 0x30ec) { // a130ec
799 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
800 goto out;
801 }
802
803 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
804 return d;
805
806out:
807 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
808 return d;
809}
810
811void PicoWrite8_32x(u32 a, u32 d)
812{
813 if ((a & 0xffc0) == 0x5100) { // a15100
814 u16 *r = Pico32x.regs;
815
816 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
817 a &= 0x3f;
818 if (a == 1) {
819 if ((d ^ r[0]) & d & P32XS_ADEN) {
820 Pico32xStartup();
821 r[0] &= ~P32XS_nRES; // causes reset if specified by this write
822 r[0] |= P32XS_ADEN;
823 p32x_reg_write8(a, d); // forward for reset processing
824 }
825 return;
826 }
827
828 // allow only COMM for now
829 if ((a & 0x30) == 0x20) {
830 u8 *r8 = (u8 *)r;
831 r8[a ^ 1] = d;
832 }
833 return;
834 }
835
836 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
837}
838
839void PicoWrite16_32x(u32 a, u32 d)
840{
841 if ((a & 0xffc0) == 0x5100) { // a15100
842 u16 *r = Pico32x.regs;
843
844 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
845 a &= 0x3e;
846 if (a == 0) {
847 if ((d ^ r[0]) & d & P32XS_ADEN) {
848 Pico32xStartup();
849 r[0] &= ~P32XS_nRES; // causes reset if specified by this write
850 r[0] |= P32XS_ADEN;
851 p32x_reg_write16(a, d); // forward for reset processing
852 }
853 return;
854 }
855
856 // allow only COMM for now
857 if ((a & 0x30) == 0x20)
858 r[a / 2] = d;
859 return;
860 }
861
862 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
863}
864
865// -----------------------------------------------------------------
866
be2c4208 867// hint vector is writeable
868static void PicoWrite8_hint(u32 a, u32 d)
869{
870 if ((a & 0xfffc) == 0x0070) {
871 Pico32xMem->m68k_rom[a ^ 1] = d;
872 return;
873 }
874
875 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
876}
877
878static void PicoWrite16_hint(u32 a, u32 d)
879{
880 if ((a & 0xfffc) == 0x0070) {
881 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
882 return;
883 }
884
885 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
886}
887
5e49c3a8 888static void bank_switch(int b)
889{
890 unsigned int rs, bank;
891
892 bank = b << 20;
893 if (bank >= Pico.romsize) {
894 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
895 return;
896 }
897
898 // 32X ROM (unbanked, XXX: consider mirroring?)
899 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
900 rs -= bank;
901 if (rs > 0x100000)
902 rs = 0x100000;
903 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
904 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
905
906 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
602c28ca 907
908#ifdef EMU_F68K
909 // setup FAME fetchmap
910 for (rs = 0x90; rs < 0xa0; rs++)
911 PicoCpuFM68k.Fetch[rs] = (u32)Pico.rom + bank - 0x900000;
912#endif
5e49c3a8 913}
914
acd35d4c 915// -----------------------------------------------------------------
916// SH2
917// -----------------------------------------------------------------
918
bcf65fd6 919// read8
920static u32 sh2_read8_unmapped(u32 a, int id)
acd35d4c 921{
bcf65fd6 922 elprintf(EL_UIO, "%csh2 unmapped r8 [%08x] %02x @%06x",
923 id ? 's' : 'm', a, 0, sh2_pc(id));
924 return 0;
925}
b78efee2 926
bcf65fd6 927static u32 sh2_read8_cs0(u32 a, int id)
928{
929 u32 d = 0;
97d3f47f 930
bcf65fd6 931 // 0x3ff00 is veridied
932 if ((a & 0x3ff00) == 0x4000) {
b78efee2 933 d = p32x_sh2reg_read16(a, id);
db1d3564 934 goto out_16to8;
acd35d4c 935 }
936
bcf65fd6 937 if ((a & 0x3ff00) == 0x4100) {
acd35d4c 938 d = p32x_vdp_read16(a);
be20816c 939 if (p32x_poll_detect(&sh2_poll[id], a, ash2_cycles_done(), 1))
db1d3564 940 ash2_end_run(8);
941 goto out_16to8;
acd35d4c 942 }
943
bcf65fd6 944 // TODO: mirroring?
945 if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
946 return Pico32xMem->sh2_rom_m[a ^ 1];
947 if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
948 return Pico32xMem->sh2_rom_s[a ^ 1];
949
950 if ((a & 0x3ff00) == 0x4200) {
acd35d4c 951 d = Pico32xMem->pal[(a & 0x1ff) / 2];
952 goto out_16to8;
953 }
954
bcf65fd6 955 return sh2_read8_unmapped(a, id);
acd35d4c 956
957out_16to8:
958 if (a & 1)
959 d &= 0xff;
960 else
961 d >>= 8;
962
b78efee2 963 elprintf(EL_32X, "%csh2 r8 [%08x] %02x @%06x",
964 id ? 's' : 'm', a, d, sh2_pc(id));
acd35d4c 965 return d;
966}
967
bcf65fd6 968static u32 sh2_read8_da(u32 a, int id)
acd35d4c 969{
bcf65fd6 970 return Pico32xMem->data_array[id][(a & 0xfff) ^ 1];
971}
acd35d4c 972
bcf65fd6 973// read16
974static u32 sh2_read16_unmapped(u32 a, int id)
975{
976 elprintf(EL_UIO, "%csh2 unmapped r16 [%08x] %04x @%06x",
977 id ? 's' : 'm', a, 0, sh2_pc(id));
978 return 0;
979}
b78efee2 980
bcf65fd6 981static u32 sh2_read16_cs0(u32 a, int id)
982{
983 u32 d = 0;
97d3f47f 984
bcf65fd6 985 if ((a & 0x3ff00) == 0x4000) {
b78efee2 986 d = p32x_sh2reg_read16(a, id);
1b3f5844 987 if (!(EL_LOGMASK & EL_PWM) && (a & 0x30) == 0x30) // hide PWM
988 return d;
db1d3564 989 goto out;
acd35d4c 990 }
991
bcf65fd6 992 if ((a & 0x3ff00) == 0x4100) {
acd35d4c 993 d = p32x_vdp_read16(a);
be20816c 994 if (p32x_poll_detect(&sh2_poll[id], a, ash2_cycles_done(), 1))
db1d3564 995 ash2_end_run(8);
996 goto out;
acd35d4c 997 }
998
bcf65fd6 999 if (id == 0 && a < sizeof(Pico32xMem->sh2_rom_m))
1000 return *(u16 *)(Pico32xMem->sh2_rom_m + a);
1001 if (id == 1 && a < sizeof(Pico32xMem->sh2_rom_s))
1002 return *(u16 *)(Pico32xMem->sh2_rom_s + a);
1003
1004 if ((a & 0x3ff00) == 0x4200) {
acd35d4c 1005 d = Pico32xMem->pal[(a & 0x1ff) / 2];
1006 goto out;
1007 }
1008
bcf65fd6 1009 return sh2_read16_unmapped(a, id);
acd35d4c 1010
1011out:
b78efee2 1012 elprintf(EL_32X, "%csh2 r16 [%08x] %04x @%06x",
1013 id ? 's' : 'm', a, d, sh2_pc(id));
acd35d4c 1014 return d;
1015}
1016
bcf65fd6 1017static u32 sh2_read16_da(u32 a, int id)
acd35d4c 1018{
bcf65fd6 1019 return ((u16 *)Pico32xMem->data_array[id])[(a & 0xfff) / 2];
acd35d4c 1020}
1021
e05b81fc 1022static int REGPARM(3) sh2_write_ignore(u32 a, u32 d, int id)
4b315c21 1023{
e05b81fc 1024 return 0;
4b315c21 1025}
1026
bcf65fd6 1027// write8
e05b81fc 1028static int REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, int id)
acd35d4c 1029{
bcf65fd6 1030 elprintf(EL_UIO, "%csh2 unmapped w8 [%08x] %02x @%06x",
1031 id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
e05b81fc 1032 return 0;
bcf65fd6 1033}
266c6afa 1034
e05b81fc 1035static int REGPARM(3) sh2_write8_cs0(u32 a, u32 d, int id)
bcf65fd6 1036{
1037 elprintf(EL_32X, "%csh2 w8 [%08x] %02x @%06x",
1038 id ? 's' : 'm', a, d & 0xff, sh2_pc(id));
b78efee2 1039
bcf65fd6 1040 if ((a & 0x3ff00) == 0x4100) {
acd35d4c 1041 p32x_vdp_write8(a, d);
e05b81fc 1042 return 0;
acd35d4c 1043 }
1044
bcf65fd6 1045 if ((a & 0x3ff00) == 0x4000) {
b78efee2 1046 p32x_sh2reg_write8(a, d, id);
e05b81fc 1047 return 1;
acd35d4c 1048 }
1049
e05b81fc 1050 return sh2_write8_unmapped(a, d, id);
bcf65fd6 1051}
1052
1053#define sh2_write8_dramN(n) \
1054 if (!(a & 0x20000) || d) { \
1055 u8 *dram = (u8 *)Pico32xMem->dram[n]; \
1056 dram[(a & 0x1ffff) ^ 1] = d; \
e05b81fc 1057 } \
1058 return 0;
87accdf7 1059
e05b81fc 1060static int REGPARM(3) sh2_write8_dram0(u32 a, u32 d, int id)
bcf65fd6 1061{
1062 sh2_write8_dramN(0);
acd35d4c 1063}
1064
e05b81fc 1065static int REGPARM(3) sh2_write8_dram1(u32 a, u32 d, int id)
acd35d4c 1066{
bcf65fd6 1067 sh2_write8_dramN(1);
1068}
87accdf7 1069
e05b81fc 1070static int REGPARM(3) sh2_write8_sdram(u32 a, u32 d, int id)
f4bb5d6b 1071{
1072 u32 a1 = a & 0x3ffff;
1073#ifdef DRC_SH2
1074 int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
1075 if (t)
1076 sh2_drc_wcheck_ram(a, t, id);
1077#endif
1078 Pico32xMem->sdram[a1 ^ 1] = d;
e05b81fc 1079 return 0;
f4bb5d6b 1080}
1081
e05b81fc 1082static int REGPARM(3) sh2_write8_da(u32 a, u32 d, int id)
bcf65fd6 1083{
f4bb5d6b 1084 u32 a1 = a & 0xfff;
1085#ifdef DRC_SH2
1086 int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
1087 if (t)
1088 sh2_drc_wcheck_da(a, t, id);
1089#endif
1090 Pico32xMem->data_array[id][a1 ^ 1] = d;
e05b81fc 1091 return 0;
bcf65fd6 1092}
acd35d4c 1093
bcf65fd6 1094// write16
e05b81fc 1095static int REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, int id)
bcf65fd6 1096{
1097 elprintf(EL_UIO, "%csh2 unmapped w16 [%08x] %04x @%06x",
1098 id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
e05b81fc 1099 return 0;
bcf65fd6 1100}
b78efee2 1101
e05b81fc 1102static int REGPARM(3) sh2_write16_cs0(u32 a, u32 d, int id)
bcf65fd6 1103{
1104 if (((EL_LOGMASK & EL_PWM) || (a & 0x30) != 0x30)) // hide PWM
1105 elprintf(EL_32X, "%csh2 w16 [%08x] %04x @%06x",
1106 id ? 's' : 'm', a, d & 0xffff, sh2_pc(id));
266c6afa 1107
bcf65fd6 1108 if ((a & 0x3ff00) == 0x4100) {
be20816c 1109 sh2_poll[id].cnt = 0; // for poll before VDP accesses
acd35d4c 1110 p32x_vdp_write16(a, d);
e05b81fc 1111 return 0;
acd35d4c 1112 }
1113
bcf65fd6 1114 if ((a & 0x3fe00) == 0x4200) {
acd35d4c 1115 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
1116 Pico32x.dirty_pal = 1;
e05b81fc 1117 return 0;
acd35d4c 1118 }
1119
bcf65fd6 1120 if ((a & 0x3ff00) == 0x4000) {
b78efee2 1121 p32x_sh2reg_write16(a, d, id);
e05b81fc 1122 return 1;
acd35d4c 1123 }
1124
e05b81fc 1125 return sh2_write16_unmapped(a, d, id);
bcf65fd6 1126}
1127
1128#define sh2_write16_dramN(n) \
1129 u16 *pd = &Pico32xMem->dram[n][(a & 0x1ffff) / 2]; \
1130 if (!(a & 0x20000)) { \
1131 *pd = d; \
e05b81fc 1132 return 0; \
bcf65fd6 1133 } \
1134 /* overwrite */ \
1135 if (!(d & 0xff00)) d |= *pd & 0xff00; \
1136 if (!(d & 0x00ff)) d |= *pd & 0x00ff; \
e05b81fc 1137 *pd = d; \
1138 return 0
bcf65fd6 1139
e05b81fc 1140static int REGPARM(3) sh2_write16_dram0(u32 a, u32 d, int id)
bcf65fd6 1141{
1142 sh2_write16_dramN(0);
1143}
1144
e05b81fc 1145static int REGPARM(3) sh2_write16_dram1(u32 a, u32 d, int id)
bcf65fd6 1146{
1147 sh2_write16_dramN(1);
1148}
1149
e05b81fc 1150static int REGPARM(3) sh2_write16_sdram(u32 a, u32 d, int id)
f4bb5d6b 1151{
1152 u32 a1 = a & 0x3ffff;
1153#ifdef DRC_SH2
1154 int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
1155 if (t)
1156 sh2_drc_wcheck_ram(a, t, id);
1157#endif
1158 ((u16 *)Pico32xMem->sdram)[a1 / 2] = d;
e05b81fc 1159 return 0;
f4bb5d6b 1160}
1161
e05b81fc 1162static int REGPARM(3) sh2_write16_da(u32 a, u32 d, int id)
bcf65fd6 1163{
f4bb5d6b 1164 u32 a1 = a & 0xfff;
1165#ifdef DRC_SH2
1166 int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
1167 if (t)
1168 sh2_drc_wcheck_da(a, t, id);
1169#endif
1170 ((u16 *)Pico32xMem->data_array[id])[a1 / 2] = d;
e05b81fc 1171 return 0;
bcf65fd6 1172}
1173
1174
1175typedef struct {
1176 uptr addr; // stores (membase >> 1) or ((handler >> 1) | (1<<31))
1177 u32 mask;
1178} sh2_memmap;
1179
e05b81fc 1180typedef u32 (sh2_read_handler)(u32 a, int id);
1181typedef int REGPARM(3) (sh2_write_handler)(u32 a, u32 d, int id);
bcf65fd6 1182
e05b81fc 1183#define SH2MAP_ADDR2OFFS_R(a) \
1184 ((((a) >> 25) & 3) | (((a) >> 27) & 0x1c))
1185
1186#define SH2MAP_ADDR2OFFS_W(a) \
1187 ((u32)(a) >> SH2_WRITE_SHIFT)
bcf65fd6 1188
80599a42 1189u32 REGPARM(2) p32x_sh2_read8(u32 a, SH2 *sh2)
bcf65fd6 1190{
1191 const sh2_memmap *sh2_map = sh2->read8_map;
1192 uptr p;
1193
e05b81fc 1194 sh2_map += SH2MAP_ADDR2OFFS_R(a);
bcf65fd6 1195 p = sh2_map->addr;
b8a1c09a 1196 if (map_flag_set(p))
bcf65fd6 1197 return ((sh2_read_handler *)(p << 1))(a, sh2->is_slave);
1198 else
1199 return *(u8 *)((p << 1) + ((a & sh2_map->mask) ^ 1));
1200}
1201
80599a42 1202u32 REGPARM(2) p32x_sh2_read16(u32 a, SH2 *sh2)
bcf65fd6 1203{
1204 const sh2_memmap *sh2_map = sh2->read16_map;
1205 uptr p;
1206
e05b81fc 1207 sh2_map += SH2MAP_ADDR2OFFS_R(a);
bcf65fd6 1208 p = sh2_map->addr;
b8a1c09a 1209 if (map_flag_set(p))
bcf65fd6 1210 return ((sh2_read_handler *)(p << 1))(a, sh2->is_slave);
1211 else
1212 return *(u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
1213}
1214
80599a42 1215u32 REGPARM(2) p32x_sh2_read32(u32 a, SH2 *sh2)
bcf65fd6 1216{
1217 const sh2_memmap *sh2_map = sh2->read16_map;
1218 sh2_read_handler *handler;
1219 u32 offs;
1220 uptr p;
1221
e05b81fc 1222 offs = SH2MAP_ADDR2OFFS_R(a);
bcf65fd6 1223 sh2_map += offs;
1224 p = sh2_map->addr;
b8a1c09a 1225 if (!map_flag_set(p)) {
bcf65fd6 1226 // XXX: maybe 32bit access instead with ror?
1227 u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
1228 return (pd[0] << 16) | pd[1];
1d7a28a7 1229 }
1230
bcf65fd6 1231 if (offs == 0x1f)
1232 return sh2_peripheral_read32(a, sh2->is_slave);
1233
1234 handler = (sh2_read_handler *)(p << 1);
1235 return (handler(a, sh2->is_slave) << 16) | handler(a + 2, sh2->is_slave);
1236}
1237
e05b81fc 1238// return nonzero if write potentially causes an interrupt (used by drc)
1239int REGPARM(3) p32x_sh2_write8(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1240{
f4bb5d6b 1241 const void **sh2_wmap = sh2->write8_tab;
1242 sh2_write_handler *wh;
bcf65fd6 1243
e05b81fc 1244 wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
1245 return wh(a, d, sh2->is_slave);
bcf65fd6 1246}
1247
e05b81fc 1248int REGPARM(3) p32x_sh2_write16(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1249{
f4bb5d6b 1250 const void **sh2_wmap = sh2->write16_tab;
1251 sh2_write_handler *wh;
bcf65fd6 1252
e05b81fc 1253 wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
1254 return wh(a, d, sh2->is_slave);
acd35d4c 1255}
1256
e05b81fc 1257int REGPARM(3) p32x_sh2_write32(u32 a, u32 d, SH2 *sh2)
acd35d4c 1258{
f4bb5d6b 1259 const void **sh2_wmap = sh2->write16_tab;
bcf65fd6 1260 sh2_write_handler *handler;
1261 u32 offs;
bcf65fd6 1262
e05b81fc 1263 offs = SH2MAP_ADDR2OFFS_W(a);
bcf65fd6 1264
e05b81fc 1265 if (offs == SH2MAP_ADDR2OFFS_W(0xffffc000)) {
bcf65fd6 1266 sh2_peripheral_write32(a, d, sh2->is_slave);
e05b81fc 1267 return 0;
4ea707e1 1268 }
1269
f4bb5d6b 1270 handler = sh2_wmap[offs];
bcf65fd6 1271 handler(a, d >> 16, sh2->is_slave);
1272 handler(a + 2, d, sh2->is_slave);
e05b81fc 1273 return 0;
acd35d4c 1274}
1275
bcf65fd6 1276// -----------------------------------------------------------------
1277
83ff19ec 1278static const u16 msh2_code[] = {
1279 // trap instructions
1280 0xaffe, // bra <self>
1281 0x0009, // nop
1282 // have to wait a bit until m68k initial program finishes clearing stuff
1283 // to avoid races with game SH2 code, like in Tempo
1284 0xd004, // mov.l @(_m_ok,pc), r0
1285 0xd105, // mov.l @(_cnt,pc), r1
1286 0xd205, // mov.l @(_start,pc), r2
1287 0x71ff, // add #-1, r1
1288 0x4115, // cmp/pl r1
1289 0x89fc, // bt -2
1290 0xc208, // mov.l r0, @(h'20,gbr)
1291 0x6822, // mov.l @r2, r8
1292 0x482b, // jmp @r8
1293 0x0009, // nop
1294 ('M'<<8)|'_', ('O'<<8)|'K',
1295 0x0001, 0x0000,
1296 0x2200, 0x03e0 // master start pointer in ROM
1297};
1298
1299static const u16 ssh2_code[] = {
1300 0xaffe, // bra <self>
1301 0x0009, // nop
1302 // code to wait for master, in case authentic master BIOS is used
1303 0xd104, // mov.l @(_m_ok,pc), r1
1304 0xd206, // mov.l @(_start,pc), r2
1305 0xc608, // mov.l @(h'20,gbr), r0
1306 0x3100, // cmp/eq r0, r1
1307 0x8bfc, // bf #-2
1308 0xd003, // mov.l @(_s_ok,pc), r0
1309 0xc209, // mov.l r0, @(h'24,gbr)
1310 0x6822, // mov.l @r2, r8
1311 0x482b, // jmp @r8
1312 0x0009, // nop
1313 ('M'<<8)|'_', ('O'<<8)|'K',
1314 ('S'<<8)|'_', ('O'<<8)|'K',
1315 0x2200, 0x03e4 // slave start pointer in ROM
1316};
1317
be2c4208 1318#define HWSWAP(x) (((x) << 16) | ((x) >> 16))
83ff19ec 1319static void get_bios(void)
be2c4208 1320{
83ff19ec 1321 u16 *ps;
1322 u32 *pl;
be2c4208 1323 int i;
1324
83ff19ec 1325 // M68K ROM
1326 if (p32x_bios_g != NULL) {
1327 elprintf(EL_STATUS|EL_32X, "32x: using supplied 68k BIOS");
1328 Byteswap(Pico32xMem->m68k_rom, p32x_bios_g, 0x100);
be2c4208 1329 }
83ff19ec 1330 else {
1331 // generate 68k ROM
1332 ps = (u16 *)Pico32xMem->m68k_rom;
1333 pl = (u32 *)ps;
1334 for (i = 1; i < 0xc0/4; i++)
1335 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
be2c4208 1336
83ff19ec 1337 // fill with nops
1338 for (i = 0xc0/2; i < 0x100/2; i++)
1339 ps[i] = 0x4e71;
be2c4208 1340
5e49c3a8 1341#if 0
83ff19ec 1342 ps[0xc0/2] = 0x46fc;
1343 ps[0xc2/2] = 0x2700; // move #0x2700,sr
1344 ps[0xfe/2] = 0x60fe; // jump to self
5e49c3a8 1345#else
83ff19ec 1346 ps[0xfe/2] = 0x4e75; // rts
5e49c3a8 1347#endif
83ff19ec 1348 }
1349 // fill remaining m68k_rom page with game ROM
974fdb5b 1350 memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100);
be2c4208 1351
83ff19ec 1352 // MSH2
1353 if (p32x_bios_m != NULL) {
1354 elprintf(EL_STATUS|EL_32X, "32x: using supplied master SH2 BIOS");
1355 Byteswap(Pico32xMem->sh2_rom_m, p32x_bios_m, sizeof(Pico32xMem->sh2_rom_m));
acd35d4c 1356 }
83ff19ec 1357 else {
1358 pl = (u32 *)Pico32xMem->sh2_rom_m;
1359
1360 // fill exception vector table to our trap address
1361 for (i = 0; i < 128; i++)
1362 pl[i] = HWSWAP(0x200);
1363
1364 // startup code
1365 memcpy(Pico32xMem->sh2_rom_m + 0x200, msh2_code, sizeof(msh2_code));
1366
1367 // reset SP
1368 pl[1] = pl[3] = HWSWAP(0x6040000);
1369 // start
1370 pl[0] = pl[2] = HWSWAP(0x204);
1371 }
1372
1373 // SSH2
1374 if (p32x_bios_s != NULL) {
1375 elprintf(EL_STATUS|EL_32X, "32x: using supplied slave SH2 BIOS");
1376 Byteswap(Pico32xMem->sh2_rom_s, p32x_bios_s, sizeof(Pico32xMem->sh2_rom_s));
1377 }
1378 else {
1379 pl = (u32 *)Pico32xMem->sh2_rom_s;
1380
1381 // fill exception vector table to our trap address
1382 for (i = 0; i < 128; i++)
1383 pl[i] = HWSWAP(0x200);
1384
1385 // startup code
1386 memcpy(Pico32xMem->sh2_rom_s + 0x200, ssh2_code, sizeof(ssh2_code));
1387
1388 // reset SP
1389 pl[1] = pl[3] = HWSWAP(0x603f800);
1390 // start
1391 pl[0] = pl[2] = HWSWAP(0x204);
1392 }
1393}
1394
bcf65fd6 1395#define MAP_MEMORY(m) ((uptr)(m) >> 1)
b8a1c09a 1396#define MAP_HANDLER(h) ( ((uptr)(h) >> 1) | ((uptr)1 << (sizeof(uptr) * 8 - 1)) )
bcf65fd6 1397
1398static sh2_memmap sh2_read8_map[0x20], sh2_read16_map[0x20];
f4bb5d6b 1399// for writes we are using handlers only
e05b81fc 1400static sh2_write_handler *sh2_write8_map[0x80], *sh2_write16_map[0x80];
bcf65fd6 1401
1402void Pico32xSwapDRAM(int b)
1403{
1404 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1405 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1406 cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1407 cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1408
1409 // SH2
1410 sh2_read8_map[2].addr = sh2_read8_map[6].addr =
1411 sh2_read16_map[2].addr = sh2_read16_map[6].addr = MAP_MEMORY(Pico32xMem->dram[b]);
1412
e05b81fc 1413 sh2_write8_map[0x04/2] = sh2_write8_map[0x24/2] = b ? sh2_write8_dram1 : sh2_write8_dram0;
1414 sh2_write16_map[0x04/2] = sh2_write16_map[0x24/2] = b ? sh2_write16_dram1 : sh2_write16_dram0;
bcf65fd6 1415}
1416
83ff19ec 1417void PicoMemSetup32x(void)
1418{
1419 unsigned int rs;
bcf65fd6 1420 int i;
83ff19ec 1421
b081408f 1422 Pico32xMem = plat_mmap(0x06000000, sizeof(*Pico32xMem));
83ff19ec 1423 if (Pico32xMem == NULL) {
1424 elprintf(EL_STATUS, "OOM");
1425 return;
1426 }
1427
1428 dmac0 = (void *)&Pico32xMem->sh2_peri_regs[0][0x180 / 4];
1429
1430 get_bios();
acd35d4c 1431
be2c4208 1432 // cartridge area becomes unmapped
1433 // XXX: we take the easy way and don't unmap ROM,
1434 // so that we can avoid handling the RV bit.
1435 // m68k_map_unmap(0x000000, 0x3fffff);
1436
1437 // MD ROM area
974fdb5b 1438 rs = sizeof(Pico32xMem->m68k_rom);
1439 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
1440 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0);
1441 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
1442 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
1443
be2c4208 1444 // 32X ROM (unbanked, XXX: consider mirroring?)
5e49c3a8 1445 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
1446 if (rs > 0x80000)
1447 rs = 0x80000;
1448 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1449 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
602c28ca 1450#ifdef EMU_F68K
1451 // setup FAME fetchmap
1452 PicoCpuFM68k.Fetch[0] = (u32)Pico32xMem->m68k_rom;
1453 for (rs = 0x88; rs < 0x90; rs++)
1454 PicoCpuFM68k.Fetch[rs] = (u32)Pico.rom - 0x880000;
1455#endif
be2c4208 1456
1457 // 32X ROM (banked)
5e49c3a8 1458 bank_switch(0);
b78efee2 1459
83ff19ec 1460 // SYS regs
1461 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_32x_on, 1);
1462 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_32x_on, 1);
1463 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_32x_on, 1);
1464 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_32x_on, 1);
1465
bcf65fd6 1466 // SH2 maps: A31,A30,A29,CS1,CS0
1467 // all unmapped by default
e05b81fc 1468 for (i = 0; i < ARRAY_SIZE(sh2_read8_map); i++) {
bcf65fd6 1469 sh2_read8_map[i].addr = MAP_HANDLER(sh2_read8_unmapped);
1470 sh2_read16_map[i].addr = MAP_HANDLER(sh2_read16_unmapped);
e05b81fc 1471 }
1472
1473 for (i = 0; i < ARRAY_SIZE(sh2_write8_map); i++) {
f4bb5d6b 1474 sh2_write8_map[i] = sh2_write8_unmapped;
1475 sh2_write16_map[i] = sh2_write16_unmapped;
bcf65fd6 1476 }
1477
4b315c21 1478 // "purge area"
e05b81fc 1479 for (i = 0x40; i <= 0x5f; i++) {
1480 sh2_write8_map[i >> 1] =
1481 sh2_write16_map[i >> 1] = sh2_write_ignore;
4b315c21 1482 }
1483
bcf65fd6 1484 // CS0
1485 sh2_read8_map[0].addr = sh2_read8_map[4].addr = MAP_HANDLER(sh2_read8_cs0);
1486 sh2_read16_map[0].addr = sh2_read16_map[4].addr = MAP_HANDLER(sh2_read16_cs0);
e05b81fc 1487 sh2_write8_map[0x00/2] = sh2_write8_map[0x20/2] = sh2_write8_cs0;
1488 sh2_write16_map[0x00/2] = sh2_write16_map[0x20/2] = sh2_write16_cs0;
bcf65fd6 1489 // CS1 - ROM
1490 sh2_read8_map[1].addr = sh2_read8_map[5].addr =
1491 sh2_read16_map[1].addr = sh2_read16_map[5].addr = MAP_MEMORY(Pico.rom);
1492 sh2_read8_map[1].mask = sh2_read8_map[5].mask =
1493 sh2_read16_map[1].mask = sh2_read16_map[5].mask = 0x3fffff; // FIXME
1494 // CS2 - DRAM - done by Pico32xSwapDRAM()
1495 sh2_read8_map[2].mask = sh2_read8_map[6].mask =
1496 sh2_read16_map[2].mask = sh2_read16_map[6].mask = 0x01ffff;
1497 // CS3 - SDRAM
1498 sh2_read8_map[3].addr = sh2_read8_map[7].addr =
f4bb5d6b 1499 sh2_read16_map[3].addr = sh2_read16_map[7].addr = MAP_MEMORY(Pico32xMem->sdram);
e05b81fc 1500 sh2_write8_map[0x06/2] = sh2_write8_map[0x26/2] = sh2_write8_sdram;
1501 sh2_write16_map[0x06/2] = sh2_write16_map[0x26/2] = sh2_write16_sdram;
bcf65fd6 1502 sh2_read8_map[3].mask = sh2_read8_map[7].mask =
f4bb5d6b 1503 sh2_read16_map[3].mask = sh2_read16_map[7].mask = 0x03ffff;
bcf65fd6 1504 // SH2 data array
1505 sh2_read8_map[0x18].addr = MAP_HANDLER(sh2_read8_da);
1506 sh2_read16_map[0x18].addr = MAP_HANDLER(sh2_read16_da);
e05b81fc 1507 sh2_write8_map[0xc0/2] = sh2_write8_da;
1508 sh2_write16_map[0xc0/2] = sh2_write16_da;
bcf65fd6 1509 // SH2 IO
1510 sh2_read8_map[0x1f].addr = MAP_HANDLER(sh2_peripheral_read8);
1511 sh2_read16_map[0x1f].addr = MAP_HANDLER(sh2_peripheral_read16);
e05b81fc 1512 sh2_write8_map[0xff/2] = sh2_peripheral_write8;
1513 sh2_write16_map[0xff/2] = sh2_peripheral_write16;
bcf65fd6 1514
1515 // map DRAM area, both 68k and SH2
1516 Pico32xSwapDRAM(1);
1517
1518 msh2.read8_map = ssh2.read8_map = sh2_read8_map;
1519 msh2.read16_map = ssh2.read16_map = sh2_read16_map;
f4bb5d6b 1520 msh2.write8_tab = ssh2.write8_tab = (const void **)sh2_write8_map;
1521 msh2.write16_tab = ssh2.write16_tab = (const void **)sh2_write16_map;
bcf65fd6 1522
b78efee2 1523 // setup poll detector
1524 m68k_poll.flag = P32XF_68KPOLL;
be20816c 1525 m68k_poll.cyc_max = 64;
b78efee2 1526 sh2_poll[0].flag = P32XF_MSH2POLL;
1d7a28a7 1527 sh2_poll[0].cyc_max = 21;
b78efee2 1528 sh2_poll[1].flag = P32XF_SSH2POLL;
be20816c 1529 sh2_poll[1].cyc_max = 16;
be2c4208 1530}
1531
bcf65fd6 1532// vim:shiftwidth=2:expandtab