update libpicofe
[picodrive.git] / pico / 32x / memory.c
CommitLineData
83ff19ec 1/*
cff531af 2 * PicoDrive
65514d85 3 * (C) notaz, 2009,2010,2013
cff531af 4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 *
83ff19ec 8 * Register map:
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
7eaa3812 12 * a15106 ........ F....SDR UE...... .....SDR 4006 // Full 68S Dma Rv fUll[fb] Empt[fb]
83ff19ec 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
7eaa3812 17 * a15114 0 (16bit VRES clr) 4014
18 * a15116 0 (16bit Vint clr) 4016
19 * a15118 0 (16bit Hint clr) 4018
20 * a1511a .......? .......C (16bit CMD clr) 401a // TV Cm
21 * a1511c 0 (16bit PWM clr) 401c
22 * a1511e 0 ? 401e
83ff19ec 23 * a15120 (16 bytes comm) 2020
24 * a15130 (PWM) 2030
65514d85 25 *
26 * SH2 addr lines:
27 * iii. .cc. ..xx * // Internal, Cs, x
28 *
29 * sh2 map, wait/bus cycles (from docs):
30 * r w
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
8a847c12 35 * cart 2000000-23fffff 6-15
65514d85 36 * dram/fb 4000000-401ffff 5-12 1-3
37 * fb ovr 4020000-403ffff
38 * sdram 6000000-603ffff 12 2 (cycles)
39 * d.a. c0000000-?
83ff19ec 40 */
be2c4208 41#include "../pico_int.h"
42#include "../memory.h"
f4bb5d6b 43#include "../../cpu/sh2/compiler.h"
be2c4208 44
45static const char str_mars[] = "MARS";
46
83ff19ec 47void *p32x_bios_g, *p32x_bios_m, *p32x_bios_s;
974fdb5b 48struct Pico32xMem *Pico32xMem;
49
5e49c3a8 50static void bank_switch(int b);
51
7eaa3812 52// addressing byte in 16bit reg
53#define REG8IN16(ptr, offs) ((u8 *)ptr)[(offs) ^ 1]
54
266c6afa 55// poll detection
19886062 56#define POLL_THRESHOLD 3
4ea707e1 57
19886062 58static struct {
59 u32 addr, cycles;
60 int cnt;
61} m68k_poll;
266c6afa 62
19886062 63static int m68k_poll_detect(u32 a, u32 cycles, u32 flags)
266c6afa 64{
19886062 65 int ret = 0;
66
67 if (a - 2 <= m68k_poll.addr && m68k_poll.addr <= a + 2
ecc8036e 68 && cycles - m68k_poll.cycles <= 64 && !SekNotPolling)
19886062 69 {
70 if (m68k_poll.cnt++ > POLL_THRESHOLD) {
71 if (!(Pico32x.emu_flags & flags)) {
72 elprintf(EL_32X, "m68k poll addr %08x, cyc %u",
73 a, cycles - m68k_poll.cycles);
266c6afa 74 ret = 1;
75 }
19886062 76 Pico32x.emu_flags |= flags;
266c6afa 77 }
78 }
c987bb5c 79 else {
19886062 80 m68k_poll.cnt = 0;
81 m68k_poll.addr = a;
ecc8036e 82 SekNotPolling = 0;
c987bb5c 83 }
19886062 84 m68k_poll.cycles = cycles;
266c6afa 85
86 return ret;
87}
88
19886062 89void p32x_m68k_poll_event(u32 flags)
90{
91 if (Pico32x.emu_flags & flags) {
92 elprintf(EL_32X, "m68k poll %02x -> %02x", Pico32x.emu_flags,
93 Pico32x.emu_flags & ~flags);
94 Pico32x.emu_flags &= ~flags;
95 SekSetStop(0);
96 }
97 m68k_poll.addr = m68k_poll.cnt = 0;
98}
99
4a1fb183 100static void sh2_poll_detect(SH2 *sh2, u32 a, u32 flags, int maxcnt)
266c6afa 101{
19886062 102 int cycles_left = sh2_cycles_left(sh2);
103
104 if (a == sh2->poll_addr && sh2->poll_cycles - cycles_left <= 10) {
4a1fb183 105 if (sh2->poll_cnt++ > maxcnt) {
19886062 106 if (!(sh2->state & flags))
f8675e28 107 elprintf_sh2(sh2, EL_32X, "state: %02x->%02x",
19886062 108 sh2->state, sh2->state | flags);
109
110 sh2->state |= flags;
111 sh2_end_run(sh2, 1);
112 pevt_log_sh2(sh2, EVT_POLL_START);
113 return;
114 }
115 }
be20816c 116 else
19886062 117 sh2->poll_cnt = 0;
118 sh2->poll_addr = a;
119 sh2->poll_cycles = cycles_left;
120}
121
122void p32x_sh2_poll_event(SH2 *sh2, u32 flags, u32 m68k_cycles)
123{
124 if (sh2->state & flags) {
f8675e28 125 elprintf_sh2(sh2, EL_32X, "state: %02x->%02x", sh2->state,
126 sh2->state & ~flags);
19886062 127
128 if (sh2->m68krcycles_done < m68k_cycles)
129 sh2->m68krcycles_done = m68k_cycles;
130
131 pevt_log_sh2_o(sh2, EVT_POLL_END);
be20816c 132 }
19886062 133
134 sh2->state &= ~flags;
135 sh2->poll_addr = sh2->poll_cycles = sh2->poll_cnt = 0;
266c6afa 136}
137
19886062 138static void sh2s_sync_on_read(SH2 *sh2)
4ea707e1 139{
19886062 140 int cycles;
141 if (sh2->poll_cnt != 0)
142 return;
143
144 cycles = sh2_cycles_done(sh2);
145 if (cycles > 600)
146 p32x_sync_other_sh2(sh2, sh2->m68krcycles_done + cycles / 3);
4ea707e1 147}
148
974fdb5b 149// SH2 faking
b78efee2 150//#define FAKE_SH2
acd35d4c 151#ifdef FAKE_SH2
27e26273 152static int p32x_csum_faked;
974fdb5b 153static const u16 comm_fakevals[] = {
154 0x4d5f, 0x4f4b, // M_OK
155 0x535f, 0x4f4b, // S_OK
5e49c3a8 156 0x4D41, 0x5346, // MASF - Brutal Unleashed
157 0x5331, 0x4d31, // Darxide
158 0x5332, 0x4d32,
159 0x5333, 0x4d33,
160 0x0000, 0x0000, // eq for doom
974fdb5b 161 0x0002, // Mortal Kombat
acd35d4c 162// 0, // pad
be2c4208 163};
acd35d4c 164
165static u32 sh2_comm_faker(u32 a)
166{
167 static int f = 0;
168 if (a == 0x28 && !p32x_csum_faked) {
169 p32x_csum_faked = 1;
170 return *(unsigned short *)(Pico.rom + 0x18e);
171 }
172 if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0]))
173 f = 0;
174 return comm_fakevals[f++];
175}
176#endif
be2c4208 177
4ea707e1 178// ------------------------------------------------------------------
b78efee2 179// 68k regs
4ea707e1 180
be2c4208 181static u32 p32x_reg_read16(u32 a)
182{
183 a &= 0x3e;
184
3cf9570b 185#if 0
974fdb5b 186 if ((a & 0x30) == 0x20)
acd35d4c 187 return sh2_comm_faker(a);
266c6afa 188#else
5fadfb1c 189 if ((a & 0x30) == 0x20) {
ae214f1c 190 unsigned int cycles = SekCyclesDone();
a8fd6e37 191 int comreg = 1 << (a & 0x0f) / 2;
192
419973a6 193 if (cycles - msh2.m68krcycles_done > 244
194 || (Pico32x.comm_dirty_68k & comreg))
a8fd6e37 195 p32x_sync_sh2s(cycles);
419973a6 196
a8fd6e37 197 if (Pico32x.comm_dirty_sh2 & comreg)
198 Pico32x.comm_dirty_sh2 &= ~comreg;
19886062 199 else if (m68k_poll_detect(a, cycles, P32XF_68KCPOLL)) {
5fadfb1c 200 SekSetStop(1);
3e5b912c 201 SekEndRun(16);
5fadfb1c 202 }
a8fd6e37 203 goto out;
266c6afa 204 }
acd35d4c 205#endif
87accdf7 206
a8fd6e37 207 if (a == 2) { // INTM, INTS
ae214f1c 208 unsigned int cycles = SekCyclesDone();
a8fd6e37 209 if (cycles - msh2.m68krcycles_done > 64)
210 p32x_sync_sh2s(cycles);
9e1fa0a6 211 goto out;
a8fd6e37 212 }
213
db1d3564 214 if ((a & 0x30) == 0x30)
ae214f1c 215 return p32x_pwm_read16(a, NULL, SekCyclesDone());
974fdb5b 216
a8fd6e37 217out:
be2c4208 218 return Pico32x.regs[a / 2];
219}
220
7eaa3812 221static void dreq0_write(u16 *r, u32 d)
222{
223 if (!(r[6 / 2] & P32XS_68S)) {
224 elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO w16 without 68S?");
225 return; // ignored - tested
226 }
227 if (Pico32x.dmac0_fifo_ptr < DMAC_FIFO_LEN) {
228 Pico32x.dmac_fifo[Pico32x.dmac0_fifo_ptr++] = d;
229 if (Pico32x.dmac0_fifo_ptr == DMAC_FIFO_LEN)
230 r[6 / 2] |= P32XS_FULL;
231 // tested: len register decrements and 68S clears
232 // even if SH2s/DMAC aren't active..
233 r[0x10 / 2]--;
234 if (r[0x10 / 2] == 0)
235 r[6 / 2] &= ~P32XS_68S;
236
237 if ((Pico32x.dmac0_fifo_ptr & 3) == 0) {
ae214f1c 238 p32x_sync_sh2s(SekCyclesDone());
7eaa3812 239 p32x_dreq0_trigger();
240 }
241 }
242 else
243 elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO overflow!");
244}
245
246// writable bits tested
be2c4208 247static void p32x_reg_write8(u32 a, u32 d)
248{
acd35d4c 249 u16 *r = Pico32x.regs;
be2c4208 250 a &= 0x3f;
251
97d3f47f 252 // for things like bset on comm port
253 m68k_poll.cnt = 0;
254
acd35d4c 255 switch (a) {
7eaa3812 256 case 0x00: // adapter ctl: FM writable
257 REG8IN16(r, 0x00) = d & 0x80;
83ff19ec 258 return;
7eaa3812 259 case 0x01: // adapter ctl: RES and ADEN writable
83ff19ec 260 if ((d ^ r[0]) & d & P32XS_nRES)
261 p32x_reset_sh2s();
7eaa3812 262 REG8IN16(r, 0x01) &= ~(P32XS_nRES|P32XS_ADEN);
263 REG8IN16(r, 0x01) |= d & (P32XS_nRES|P32XS_ADEN);
264 return;
265 case 0x02: // ignored, always 0
1b3f5844 266 return;
7eaa3812 267 case 0x03: // irq ctl
9e1fa0a6 268 if ((d ^ r[0x02 / 2]) & 3) {
ae214f1c 269 int cycles = SekCyclesDone();
9e1fa0a6 270 p32x_sync_sh2s(cycles);
271 r[0x02 / 2] = d & 3;
272 p32x_update_cmd_irq(NULL, cycles);
b78efee2 273 }
1b3f5844 274 return;
7eaa3812 275 case 0x04: // ignored, always 0
276 return;
277 case 0x05: // bank
278 d &= 3;
279 if (r[0x04 / 2] != d) {
280 r[0x04 / 2] = d;
acd35d4c 281 bank_switch(d);
282 }
1b3f5844 283 return;
7eaa3812 284 case 0x06: // ignored, always 0
285 return;
286 case 0x07: // DREQ ctl
287 REG8IN16(r, 0x07) &= ~(P32XS_68S|P32XS_DMA|P32XS_RV);
288 if (!(d & P32XS_68S)) {
289 Pico32x.dmac0_fifo_ptr = 0;
290 REG8IN16(r, 0x07) &= ~P32XS_FULL;
291 }
292 REG8IN16(r, 0x07) |= d & (P32XS_68S|P32XS_DMA|P32XS_RV);
293 return;
294 case 0x08: // ignored, always 0
295 return;
296 case 0x09: // DREQ src
297 REG8IN16(r, 0x09) = d;
298 return;
299 case 0x0a:
300 REG8IN16(r, 0x0a) = d;
301 return;
302 case 0x0b:
303 REG8IN16(r, 0x0b) = d & 0xfe;
304 return;
305 case 0x0c: // ignored, always 0
306 return;
307 case 0x0d: // DREQ dest
308 case 0x0e:
309 case 0x0f:
310 case 0x10: // DREQ len
311 REG8IN16(r, a) = d;
312 return;
313 case 0x11:
314 REG8IN16(r, a) = d & 0xfc;
315 return;
316 // DREQ FIFO - writes to odd addr go to fifo
317 // do writes to even work? Reads return 0
318 case 0x12:
319 REG8IN16(r, a) = d;
320 return;
321 case 0x13:
322 d = (REG8IN16(r, 0x12) << 8) | (d & 0xff);
323 REG8IN16(r, 0x12) = 0;
324 dreq0_write(r, d);
325 return;
326 case 0x14: // ignored, always 0
327 case 0x15:
328 case 0x16:
329 case 0x17:
330 case 0x18:
331 case 0x19:
332 return;
333 case 0x1a: // what's this?
334 elprintf(EL_32X|EL_ANOMALY, "mystery w8 %02x %02x", a, d);
335 REG8IN16(r, a) = d & 0x01;
1b3f5844 336 return;
87accdf7 337 case 0x1b: // TV
7eaa3812 338 REG8IN16(r, a) = d & 0x01;
339 return;
340 case 0x1c: // ignored, always 0
341 case 0x1d:
342 case 0x1e:
343 case 0x1f:
344 case 0x30:
345 return;
346 case 0x31: // PWM control
347 REG8IN16(r, a) &= ~0x0f;
348 REG8IN16(r, a) |= d & 0x0f;
77e58d93 349 d = r[0x30 / 2];
7eaa3812 350 goto pwm_write;
351 case 0x32: // PWM cycle
352 REG8IN16(r, a) = d & 0x0f;
77e58d93 353 d = r[0x32 / 2];
7eaa3812 354 goto pwm_write;
355 case 0x33:
356 REG8IN16(r, a) = d;
77e58d93 357 d = r[0x32 / 2];
7eaa3812 358 goto pwm_write;
359 // PWM pulse regs.. Only writes to odd address send a value
360 // to FIFO; reads are 0 (except status bits)
361 case 0x34:
362 case 0x36:
363 case 0x38:
364 REG8IN16(r, a) = d;
365 return;
366 case 0x35:
367 case 0x37:
368 case 0x39:
77e58d93 369 d = (REG8IN16(r, a ^ 1) << 8) | (d & 0xff);
370 REG8IN16(r, a ^ 1) = 0;
7eaa3812 371 goto pwm_write;
372 case 0x3a: // ignored, always 0
373 case 0x3b:
374 case 0x3c:
375 case 0x3d:
376 case 0x3e:
377 case 0x3f:
378 return;
379 pwm_write:
ae214f1c 380 p32x_pwm_write16(a & ~1, d, NULL, SekCyclesDone());
1b3f5844 381 return;
382 }
383
384 if ((a & 0x30) == 0x20) {
ae214f1c 385 int cycles = SekCyclesDone();
a8fd6e37 386 int comreg;
387
7eaa3812 388 if (REG8IN16(r, a) == d)
a8fd6e37 389 return;
19886062 390
a8fd6e37 391 comreg = 1 << (a & 0x0f) / 2;
392 if (Pico32x.comm_dirty_68k & comreg)
393 p32x_sync_sh2s(cycles);
394
7eaa3812 395 REG8IN16(r, a) = d;
19886062 396 p32x_sh2_poll_event(&sh2s[0], SH2_STATE_CPOLL, cycles);
397 p32x_sh2_poll_event(&sh2s[1], SH2_STATE_CPOLL, cycles);
a8fd6e37 398 Pico32x.comm_dirty_68k |= comreg;
399
400 if (cycles - (int)msh2.m68krcycles_done > 120)
401 p32x_sync_sh2s(cycles);
1b3f5844 402 return;
5e49c3a8 403 }
404}
405
406static void p32x_reg_write16(u32 a, u32 d)
407{
acd35d4c 408 u16 *r = Pico32x.regs;
409 a &= 0x3e;
410
97d3f47f 411 // for things like bset on comm port
412 m68k_poll.cnt = 0;
413
acd35d4c 414 switch (a) {
4ea707e1 415 case 0x00: // adapter ctl
83ff19ec 416 if ((d ^ r[0]) & d & P32XS_nRES)
417 p32x_reset_sh2s();
7eaa3812 418 r[0] &= ~(P32XS_FM|P32XS_nRES|P32XS_ADEN);
419 r[0] |= d & (P32XS_FM|P32XS_nRES|P32XS_ADEN);
420 return;
421 case 0x08: // DREQ src
422 r[a / 2] = d & 0xff;
423 return;
424 case 0x0a:
425 r[a / 2] = d & ~1;
426 return;
427 case 0x0c: // DREQ dest
428 r[a / 2] = d & 0xff;
429 return;
430 case 0x0e:
431 r[a / 2] = d;
acd35d4c 432 return;
4ea707e1 433 case 0x10: // DREQ len
434 r[a / 2] = d & ~3;
435 return;
436 case 0x12: // FIFO reg
7eaa3812 437 dreq0_write(r, d);
438 return;
439 case 0x1a: // TV + mystery bit
440 r[a / 2] = d & 0x0101;
441 return;
77e58d93 442 case 0x30: // PWM control
443 d = (r[a / 2] & ~0x0f) | (d & 0x0f);
444 r[a / 2] = d;
ae214f1c 445 p32x_pwm_write16(a, d, NULL, SekCyclesDone());
77e58d93 446 return;
acd35d4c 447 }
448
4ea707e1 449 // comm port
7eaa3812 450 if ((a & 0x30) == 0x20) {
ae214f1c 451 int cycles = SekCyclesDone();
a8fd6e37 452 int comreg;
453
454 if (r[a / 2] == d)
455 return;
456
457 comreg = 1 << (a & 0x0f) / 2;
458 if (Pico32x.comm_dirty_68k & comreg)
459 p32x_sync_sh2s(cycles);
460
acd35d4c 461 r[a / 2] = d;
19886062 462 p32x_sh2_poll_event(&sh2s[0], SH2_STATE_CPOLL, cycles);
463 p32x_sh2_poll_event(&sh2s[1], SH2_STATE_CPOLL, cycles);
a8fd6e37 464 Pico32x.comm_dirty_68k |= comreg;
465
466 if (cycles - (int)msh2.m68krcycles_done > 120)
467 p32x_sync_sh2s(cycles);
acd35d4c 468 return;
469 }
db1d3564 470 // PWM
471 else if ((a & 0x30) == 0x30) {
ae214f1c 472 p32x_pwm_write16(a, d, NULL, SekCyclesDone());
db1d3564 473 return;
474 }
acd35d4c 475
5e49c3a8 476 p32x_reg_write8(a + 1, d);
be2c4208 477}
478
4ea707e1 479// ------------------------------------------------------------------
be2c4208 480// VDP regs
481static u32 p32x_vdp_read16(u32 a)
482{
4a1fb183 483 u32 d;
be2c4208 484 a &= 0x0e;
485
4a1fb183 486 d = Pico32x.vdp_regs[a / 2];
487 if (a == 0x0a) {
488 // tested: FEN seems to be randomly pulsing on hcnt 0x80-0xf0,
489 // most often at 0xb1-0xb5, even during vblank,
490 // what's the deal with that?
491 // we'll just fake it along with hblank for now
492 Pico32x.vdp_fbcr_fake++;
493 if (Pico32x.vdp_fbcr_fake & 4)
494 d |= P32XV_HBLK;
495 if ((Pico32x.vdp_fbcr_fake & 7) == 0)
496 d |= P32XV_nFEN;
497 }
498 return d;
be2c4208 499}
500
be2c4208 501static void p32x_vdp_write8(u32 a, u32 d)
502{
974fdb5b 503 u16 *r = Pico32x.vdp_regs;
be2c4208 504 a &= 0x0f;
505
974fdb5b 506 // TODO: verify what's writeable
be2c4208 507 switch (a) {
974fdb5b 508 case 0x01:
5e49c3a8 509 // priority inversion is handled in palette
510 if ((r[0] ^ d) & P32XV_PRI)
511 Pico32x.dirty_pal = 1;
974fdb5b 512 r[0] = (r[0] & P32XV_nPAL) | (d & 0xff);
be20816c 513 break;
e51e5983 514 case 0x03: // shift (for pp mode)
515 r[2 / 2] = d & 1;
516 break;
be20816c 517 case 0x05: // fill len
518 r[4 / 2] = d & 0xff;
974fdb5b 519 break;
be2c4208 520 case 0x0b:
974fdb5b 521 d &= 1;
522 Pico32x.pending_fb = d;
523 // if we are blanking and FS bit is changing
4ea707e1 524 if (((r[0x0a/2] & P32XV_VBLK) || (r[0] & P32XV_Mx) == 0) && ((r[0x0a/2] ^ d) & P32XV_FS)) {
b4db550e 525 r[0x0a/2] ^= P32XV_FS;
5609d343 526 Pico32xSwapDRAM(d ^ 1);
266c6afa 527 elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS);
be2c4208 528 }
529 break;
530 }
531}
532
19886062 533static void p32x_vdp_write16(u32 a, u32 d, SH2 *sh2)
974fdb5b 534{
be20816c 535 a &= 0x0e;
536 if (a == 6) { // fill start
537 Pico32x.vdp_regs[6 / 2] = d;
538 return;
539 }
540 if (a == 8) { // fill data
541 u16 *dram = Pico32xMem->dram[(Pico32x.vdp_regs[0x0a/2] & P32XV_FS) ^ 1];
1b3f5844 542 int len = Pico32x.vdp_regs[4 / 2] + 1;
a8fd6e37 543 int len1 = len;
be20816c 544 a = Pico32x.vdp_regs[6 / 2];
a8fd6e37 545 while (len1--) {
be20816c 546 dram[a] = d;
547 a = (a & 0xff00) | ((a + 1) & 0xff);
548 }
a8fd6e37 549 Pico32x.vdp_regs[0x06 / 2] = a;
550 Pico32x.vdp_regs[0x08 / 2] = d;
19886062 551 if (sh2 != NULL && len > 4) {
a8fd6e37 552 Pico32x.vdp_regs[0x0a / 2] |= P32XV_nFEN;
19886062 553 // supposedly takes 3 bus/6 sh2 cycles? or 3 sh2 cycles?
554 p32x_event_schedule_sh2(sh2, P32X_EVENT_FILLEND, 3 + len);
a8fd6e37 555 }
be20816c 556 return;
557 }
558
974fdb5b 559 p32x_vdp_write8(a | 1, d);
560}
561
4ea707e1 562// ------------------------------------------------------------------
acd35d4c 563// SH2 regs
b78efee2 564
f81107f5 565static u32 p32x_sh2reg_read16(u32 a, SH2 *sh2)
acd35d4c 566{
4ea707e1 567 u16 *r = Pico32x.regs;
eb35ce15 568 a &= 0x3e;
266c6afa 569
4ea707e1 570 switch (a) {
571 case 0x00: // adapter/irq ctl
f81107f5 572 return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0]
573 | Pico32x.sh2irq_mask[sh2->is_slave];
c987bb5c 574 case 0x04: // H count (often as comm too)
4a1fb183 575 sh2_poll_detect(sh2, a, SH2_STATE_CPOLL, 3);
f81107f5 576 sh2s_sync_on_read(sh2);
87accdf7 577 return Pico32x.sh2_regs[4 / 2];
7eaa3812 578 case 0x06:
579 return (r[a / 2] & ~P32XS_FULL) | 0x4000;
580 case 0x08: // DREQ src
581 case 0x0a:
582 case 0x0c: // DREQ dst
583 case 0x0e:
4ea707e1 584 case 0x10: // DREQ len
585 return r[a / 2];
7eaa3812 586 case 0x12: // DREQ FIFO - does this work on hw?
587 if (Pico32x.dmac0_fifo_ptr > 0) {
588 Pico32x.dmac0_fifo_ptr--;
589 r[a / 2] = Pico32x.dmac_fifo[0];
590 memmove(&Pico32x.dmac_fifo[0], &Pico32x.dmac_fifo[1],
591 Pico32x.dmac0_fifo_ptr * 2);
592 }
593 return r[a / 2];
594 case 0x14:
595 case 0x16:
596 case 0x18:
597 case 0x1a:
598 case 0x1c:
599 return 0; // ?
acd35d4c 600 }
4ea707e1 601
db1d3564 602 // comm port
603 if ((a & 0x30) == 0x20) {
a8fd6e37 604 int comreg = 1 << (a & 0x0f) / 2;
605 if (Pico32x.comm_dirty_68k & comreg)
606 Pico32x.comm_dirty_68k &= ~comreg;
19886062 607 else
4a1fb183 608 sh2_poll_detect(sh2, a, SH2_STATE_CPOLL, 3);
f81107f5 609 sh2s_sync_on_read(sh2);
db1d3564 610 return r[a / 2];
611 }
7eaa3812 612 if ((a & 0x30) == 0x30)
f81107f5 613 return p32x_pwm_read16(a, sh2, sh2_cycles_done_m68k(sh2));
acd35d4c 614
7eaa3812 615 elprintf_sh2(sh2, EL_32X|EL_ANOMALY,
77e58d93 616 "unhandled sysreg r16 [%02x] @%08x", a, sh2_pc(sh2));
acd35d4c 617 return 0;
618}
619
f81107f5 620static void p32x_sh2reg_write8(u32 a, u32 d, SH2 *sh2)
acd35d4c 621{
77e58d93 622 u16 *r = Pico32x.regs;
9e1fa0a6 623 u32 old;
19886062 624
eb35ce15 625 a &= 0x3f;
f81107f5 626 sh2->poll_addr = 0;
19886062 627
87accdf7 628 switch (a) {
77e58d93 629 case 0x00: // FM
630 r[0] &= ~P32XS_FM;
631 r[0] |= (d << 8) & P32XS_FM;
1b3f5844 632 return;
77e58d93 633 case 0x01: // HEN/irq masks
9e1fa0a6 634 old = Pico32x.sh2irq_mask[sh2->is_slave];
635 if ((d ^ old) & 1)
636 p32x_pwm_sync_to_sh2(sh2);
637
5ac99d9a 638 Pico32x.sh2irq_mask[sh2->is_slave] = d & 0x0f;
87accdf7 639 Pico32x.sh2_regs[0] &= ~0x80;
640 Pico32x.sh2_regs[0] |= d & 0x80;
9e1fa0a6 641
642 if ((d ^ old) & 1)
f81107f5 643 p32x_pwm_schedule_sh2(sh2);
9e1fa0a6 644 if ((old ^ d) & 2)
645 p32x_update_cmd_irq(sh2, 0);
646 if ((old ^ d) & 4)
5ac99d9a 647 p32x_schedule_hint(sh2, 0);
1b3f5844 648 return;
77e58d93 649 case 0x04: // ignored?
650 return;
651 case 0x05: // H count
19886062 652 d &= 0xff;
653 if (Pico32x.sh2_regs[4 / 2] != d) {
654 Pico32x.sh2_regs[4 / 2] = d;
f81107f5 655 p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL,
656 sh2_cycles_done_m68k(sh2));
657 sh2_end_run(sh2, 4);
19886062 658 }
1b3f5844 659 return;
77e58d93 660 case 0x30:
661 REG8IN16(r, a) = d & 0x0f;
662 d = r[0x30 / 2];
663 goto pwm_write;
664 case 0x31: // PWM control
665 REG8IN16(r, a) = d & 0x8f;
666 d = r[0x30 / 2];
667 goto pwm_write;
668 case 0x32: // PWM cycle
669 REG8IN16(r, a) = d & 0x0f;
670 d = r[0x32 / 2];
671 goto pwm_write;
672 case 0x33:
673 REG8IN16(r, a) = d;
674 d = r[0x32 / 2];
675 goto pwm_write;
676 // PWM pulse regs.. Only writes to odd address send a value
677 // to FIFO; reads are 0 (except status bits)
678 case 0x34:
679 case 0x36:
680 case 0x38:
681 REG8IN16(r, a) = d;
682 return;
683 case 0x35:
684 case 0x37:
685 case 0x39:
686 d = (REG8IN16(r, a ^ 1) << 8) | (d & 0xff);
687 REG8IN16(r, a ^ 1) = 0;
688 goto pwm_write;
689 case 0x3a: // ignored, always 0?
690 case 0x3b:
691 case 0x3c:
692 case 0x3d:
693 case 0x3e:
694 case 0x3f:
695 return;
696 pwm_write:
697 p32x_pwm_write16(a & ~1, d, sh2, 0);
698 return;
1b3f5844 699 }
700
701 if ((a & 0x30) == 0x20) {
a8fd6e37 702 int comreg;
77e58d93 703 if (REG8IN16(r, a) == d)
a8fd6e37 704 return;
705
77e58d93 706 REG8IN16(r, a) = d;
19886062 707 p32x_m68k_poll_event(P32XF_68KCPOLL);
f81107f5 708 p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL,
709 sh2_cycles_done_m68k(sh2));
a8fd6e37 710 comreg = 1 << (a & 0x0f) / 2;
711 Pico32x.comm_dirty_sh2 |= comreg;
1b3f5844 712 return;
4ea707e1 713 }
77e58d93 714
715 elprintf(EL_32X|EL_ANOMALY,
716 "unhandled sysreg w8 [%02x] %02x @%08x", a, d, sh2_pc(sh2));
acd35d4c 717}
718
f81107f5 719static void p32x_sh2reg_write16(u32 a, u32 d, SH2 *sh2)
acd35d4c 720{
eb35ce15 721 a &= 0x3e;
acd35d4c 722
f81107f5 723 sh2->poll_addr = 0;
19886062 724
db1d3564 725 // comm
a8fd6e37 726 if ((a & 0x30) == 0x20) {
727 int comreg;
728 if (Pico32x.regs[a / 2] == d)
729 return;
730
b78efee2 731 Pico32x.regs[a / 2] = d;
19886062 732 p32x_m68k_poll_event(P32XF_68KCPOLL);
f81107f5 733 p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL,
734 sh2_cycles_done_m68k(sh2));
a8fd6e37 735 comreg = 1 << (a & 0x0f) / 2;
736 Pico32x.comm_dirty_sh2 |= comreg;
acd35d4c 737 return;
738 }
db1d3564 739 // PWM
740 else if ((a & 0x30) == 0x30) {
f81107f5 741 p32x_pwm_write16(a, d, sh2, sh2_cycles_done_m68k(sh2));
db1d3564 742 return;
743 }
acd35d4c 744
4ea707e1 745 switch (a) {
87accdf7 746 case 0: // FM
747 Pico32x.regs[0] &= ~P32XS_FM;
748 Pico32x.regs[0] |= d & P32XS_FM;
749 break;
9e1fa0a6 750 case 0x14:
751 Pico32x.sh2irqs &= ~P32XI_VRES;
752 goto irls;
753 case 0x16:
754 Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_VINT;
755 goto irls;
756 case 0x18:
757 Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_HINT;
758 goto irls;
759 case 0x1a:
760 Pico32x.regs[2 / 2] &= ~(1 << sh2->is_slave);
761 p32x_update_cmd_irq(sh2, 0);
762 return;
be20816c 763 case 0x1c:
9e1fa0a6 764 p32x_pwm_sync_to_sh2(sh2);
765 Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_PWM;
f81107f5 766 p32x_pwm_schedule_sh2(sh2);
be20816c 767 goto irls;
4ea707e1 768 }
769
f81107f5 770 p32x_sh2reg_write8(a | 1, d, sh2);
4ea707e1 771 return;
772
773irls:
f81107f5 774 p32x_update_irls(sh2, 0);
4ea707e1 775}
776
4ea707e1 777// ------------------------------------------------------------------
34280f9b 778// 32x 68k handlers
83ff19ec 779
780// after ADEN
781static u32 PicoRead8_32x_on(u32 a)
be2c4208 782{
783 u32 d = 0;
784 if ((a & 0xffc0) == 0x5100) { // a15100
785 d = p32x_reg_read16(a);
786 goto out_16to8;
787 }
788
fa8fb754 789 if ((a & 0xfc00) != 0x5000) {
790 if (PicoAHW & PAHW_MCD)
791 return PicoRead8_mcd_io(a);
792 else
793 return PicoRead8_io(a);
794 }
974fdb5b 795
796 if ((a & 0xfff0) == 0x5180) { // a15180
be2c4208 797 d = p32x_vdp_read16(a);
798 goto out_16to8;
799 }
800
974fdb5b 801 if ((a & 0xfe00) == 0x5200) { // a15200
802 d = Pico32xMem->pal[(a & 0x1ff) / 2];
803 goto out_16to8;
804 }
805
be2c4208 806 if ((a & 0xfffc) == 0x30ec) { // a130ec
807 d = str_mars[a & 3];
808 goto out;
809 }
810
811 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
812 return d;
813
814out_16to8:
815 if (a & 1)
816 d &= 0xff;
817 else
818 d >>= 8;
819
820out:
821 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
822 return d;
823}
824
83ff19ec 825static u32 PicoRead16_32x_on(u32 a)
be2c4208 826{
827 u32 d = 0;
828 if ((a & 0xffc0) == 0x5100) { // a15100
829 d = p32x_reg_read16(a);
830 goto out;
831 }
832
fa8fb754 833 if ((a & 0xfc00) != 0x5000) {
834 if (PicoAHW & PAHW_MCD)
835 return PicoRead16_mcd_io(a);
836 else
837 return PicoRead16_io(a);
838 }
974fdb5b 839
840 if ((a & 0xfff0) == 0x5180) { // a15180
be2c4208 841 d = p32x_vdp_read16(a);
842 goto out;
843 }
844
974fdb5b 845 if ((a & 0xfe00) == 0x5200) { // a15200
846 d = Pico32xMem->pal[(a & 0x1ff) / 2];
847 goto out;
848 }
849
be2c4208 850 if ((a & 0xfffc) == 0x30ec) { // a130ec
851 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
852 goto out;
853 }
854
855 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
856 return d;
857
858out:
859 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
860 return d;
861}
862
83ff19ec 863static void PicoWrite8_32x_on(u32 a, u32 d)
be2c4208 864{
865 if ((a & 0xfc00) == 0x5000)
866 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
867
868 if ((a & 0xffc0) == 0x5100) { // a15100
869 p32x_reg_write8(a, d);
870 return;
871 }
872
83ff19ec 873 if ((a & 0xfc00) != 0x5000) {
fa8fb754 874 if (PicoAHW & PAHW_MCD)
875 PicoWrite8_mcd_io(a, d);
876 else
877 PicoWrite8_io(a, d);
77e58d93 878 if (a == 0xa130f1)
879 bank_switch(Pico32x.regs[4 / 2]);
83ff19ec 880 return;
881 }
974fdb5b 882
5609d343 883 if (!(Pico32x.regs[0] & P32XS_FM)) {
884 if ((a & 0xfff0) == 0x5180) { // a15180
885 p32x_vdp_write8(a, d);
886 return;
887 }
be2c4208 888
5609d343 889 // TODO: verify
890 if ((a & 0xfe00) == 0x5200) { // a15200
891 elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
892 ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d;
893 Pico32x.dirty_pal = 1;
894 return;
895 }
974fdb5b 896 }
897
be2c4208 898 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
899}
900
83ff19ec 901static void PicoWrite16_32x_on(u32 a, u32 d)
be2c4208 902{
903 if ((a & 0xfc00) == 0x5000)
553c3eaa 904 elprintf(EL_32X, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
be2c4208 905
906 if ((a & 0xffc0) == 0x5100) { // a15100
907 p32x_reg_write16(a, d);
908 return;
909 }
910
83ff19ec 911 if ((a & 0xfc00) != 0x5000) {
fa8fb754 912 if (PicoAHW & PAHW_MCD)
913 PicoWrite16_mcd_io(a, d);
914 else
915 PicoWrite16_io(a, d);
77e58d93 916 if (a == 0xa130f0)
917 bank_switch(Pico32x.regs[4 / 2]);
83ff19ec 918 return;
919 }
974fdb5b 920
5609d343 921 if (!(Pico32x.regs[0] & P32XS_FM)) {
922 if ((a & 0xfff0) == 0x5180) { // a15180
923 p32x_vdp_write16(a, d, NULL); // FIXME?
924 return;
925 }
be2c4208 926
5609d343 927 if ((a & 0xfe00) == 0x5200) { // a15200
928 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
929 Pico32x.dirty_pal = 1;
930 return;
931 }
974fdb5b 932 }
933
be2c4208 934 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
935}
936
83ff19ec 937// before ADEN
938u32 PicoRead8_32x(u32 a)
939{
940 u32 d = 0;
941 if ((a & 0xffc0) == 0x5100) { // a15100
942 // regs are always readable
943 d = ((u8 *)Pico32x.regs)[(a & 0x3f) ^ 1];
944 goto out;
945 }
946
947 if ((a & 0xfffc) == 0x30ec) { // a130ec
948 d = str_mars[a & 3];
949 goto out;
950 }
951
952 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
953 return d;
954
955out:
956 elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc);
957 return d;
958}
959
960u32 PicoRead16_32x(u32 a)
961{
962 u32 d = 0;
963 if ((a & 0xffc0) == 0x5100) { // a15100
964 d = Pico32x.regs[(a & 0x3f) / 2];
965 goto out;
966 }
967
968 if ((a & 0xfffc) == 0x30ec) { // a130ec
969 d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S';
970 goto out;
971 }
972
973 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
974 return d;
975
976out:
977 elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc);
978 return d;
979}
980
981void PicoWrite8_32x(u32 a, u32 d)
982{
983 if ((a & 0xffc0) == 0x5100) { // a15100
984 u16 *r = Pico32x.regs;
985
986 elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
987 a &= 0x3f;
988 if (a == 1) {
989 if ((d ^ r[0]) & d & P32XS_ADEN) {
990 Pico32xStartup();
991 r[0] &= ~P32XS_nRES; // causes reset if specified by this write
992 r[0] |= P32XS_ADEN;
993 p32x_reg_write8(a, d); // forward for reset processing
994 }
995 return;
996 }
997
998 // allow only COMM for now
999 if ((a & 0x30) == 0x20) {
1000 u8 *r8 = (u8 *)r;
1001 r8[a ^ 1] = d;
1002 }
1003 return;
1004 }
1005
1006 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
1007}
1008
1009void PicoWrite16_32x(u32 a, u32 d)
1010{
1011 if ((a & 0xffc0) == 0x5100) { // a15100
1012 u16 *r = Pico32x.regs;
1013
1014 elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
1015 a &= 0x3e;
1016 if (a == 0) {
1017 if ((d ^ r[0]) & d & P32XS_ADEN) {
1018 Pico32xStartup();
1019 r[0] &= ~P32XS_nRES; // causes reset if specified by this write
1020 r[0] |= P32XS_ADEN;
1021 p32x_reg_write16(a, d); // forward for reset processing
1022 }
1023 return;
1024 }
1025
1026 // allow only COMM for now
1027 if ((a & 0x30) == 0x20)
1028 r[a / 2] = d;
1029 return;
1030 }
1031
1032 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
1033}
1034
34280f9b 1035/* quirk: in both normal and overwrite areas only nonzero values go through */
1036#define sh2_write8_dramN(n) \
1037 if ((d & 0xff) != 0) { \
1038 u8 *dram = (u8 *)Pico32xMem->dram[n]; \
1039 dram[(a & 0x1ffff) ^ 1] = d; \
1040 }
1041
1042static void m68k_write8_dram0_ow(u32 a, u32 d)
1043{
1044 sh2_write8_dramN(0);
1045}
1046
1047static void m68k_write8_dram1_ow(u32 a, u32 d)
1048{
1049 sh2_write8_dramN(1);
1050}
1051
f81107f5 1052#define sh2_write16_dramN(n) \
34280f9b 1053 u16 *pd = &Pico32xMem->dram[n][(a & 0x1ffff) / 2]; \
1054 if (!(a & 0x20000)) { \
1055 *pd = d; \
f81107f5 1056 return; \
34280f9b 1057 } \
1058 /* overwrite */ \
1059 if (!(d & 0xff00)) d |= *pd & 0xff00; \
1060 if (!(d & 0x00ff)) d |= *pd & 0x00ff; \
f81107f5 1061 *pd = d;
34280f9b 1062
1063static void m68k_write16_dram0_ow(u32 a, u32 d)
1064{
f81107f5 1065 sh2_write16_dramN(0);
34280f9b 1066}
1067
1068static void m68k_write16_dram1_ow(u32 a, u32 d)
1069{
f81107f5 1070 sh2_write16_dramN(1);
34280f9b 1071}
1072
83ff19ec 1073// -----------------------------------------------------------------
1074
be2c4208 1075// hint vector is writeable
1076static void PicoWrite8_hint(u32 a, u32 d)
1077{
1078 if ((a & 0xfffc) == 0x0070) {
1079 Pico32xMem->m68k_rom[a ^ 1] = d;
1080 return;
1081 }
1082
77e58d93 1083 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x",
1084 a, d & 0xff, SekPc);
be2c4208 1085}
1086
1087static void PicoWrite16_hint(u32 a, u32 d)
1088{
1089 if ((a & 0xfffc) == 0x0070) {
1090 ((u16 *)Pico32xMem->m68k_rom)[a/2] = d;
1091 return;
1092 }
1093
77e58d93 1094 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x",
1095 a, d & 0xffff, SekPc);
1096}
1097
1098// normally not writable, but somebody could make a RAM cart
1099static void PicoWrite8_cart(u32 a, u32 d)
1100{
1101 elprintf(EL_UIO, "m68k w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
1102
1103 a &= 0xfffff;
1104 m68k_write8(a, d);
1105}
1106
1107static void PicoWrite16_cart(u32 a, u32 d)
1108{
1109 elprintf(EL_UIO, "m68k w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
1110
1111 a &= 0xfffff;
1112 m68k_write16(a, d);
1113}
1114
1115// same with bank, but save ram is sometimes here
1116static u32 PicoRead8_bank(u32 a)
1117{
1118 a = (Pico32x.regs[4 / 2] << 20) | (a & 0xfffff);
1119 return m68k_read8(a);
1120}
1121
1122static u32 PicoRead16_bank(u32 a)
1123{
1124 a = (Pico32x.regs[4 / 2] << 20) | (a & 0xfffff);
1125 return m68k_read16(a);
1126}
1127
1128static void PicoWrite8_bank(u32 a, u32 d)
1129{
1130 if (!(Pico.m.sram_reg & SRR_MAPPED))
1131 elprintf(EL_UIO, "m68k w8 [%06x] %02x @%06x",
1132 a, d & 0xff, SekPc);
1133
1134 a = (Pico32x.regs[4 / 2] << 20) | (a & 0xfffff);
1135 m68k_write8(a, d);
1136}
1137
1138static void PicoWrite16_bank(u32 a, u32 d)
1139{
1140 if (!(Pico.m.sram_reg & SRR_MAPPED))
1141 elprintf(EL_UIO, "m68k w16 [%06x] %04x @%06x",
1142 a, d & 0xffff, SekPc);
1143
1144 a = (Pico32x.regs[4 / 2] << 20) | (a & 0xfffff);
1145 m68k_write16(a, d);
1146}
1147
1148static void bank_map_handler(void)
1149{
1150 cpu68k_map_set(m68k_read8_map, 0x900000, 0x9fffff, PicoRead8_bank, 1);
1151 cpu68k_map_set(m68k_read16_map, 0x900000, 0x9fffff, PicoRead16_bank, 1);
be2c4208 1152}
1153
5e49c3a8 1154static void bank_switch(int b)
1155{
1156 unsigned int rs, bank;
1157
fa8fb754 1158 if (Pico.m.ncart_in)
1159 return;
1160
5e49c3a8 1161 bank = b << 20;
77e58d93 1162 if ((Pico.m.sram_reg & SRR_MAPPED) && bank == SRam.start) {
1163 bank_map_handler();
1164 return;
1165 }
1166
5e49c3a8 1167 if (bank >= Pico.romsize) {
1168 elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank);
77e58d93 1169 bank_map_handler();
5e49c3a8 1170 return;
1171 }
1172
1173 // 32X ROM (unbanked, XXX: consider mirroring?)
1174 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
1175 rs -= bank;
1176 if (rs > 0x100000)
1177 rs = 0x100000;
1178 cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
1179 cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom + bank, 0);
1180
1181 elprintf(EL_32X, "bank %06x-%06x -> %06x", 0x900000, 0x900000 + rs - 1, bank);
602c28ca 1182
1183#ifdef EMU_F68K
1184 // setup FAME fetchmap
1185 for (rs = 0x90; rs < 0xa0; rs++)
be26eb23 1186 PicoCpuFM68k.Fetch[rs] = (unsigned long)Pico.rom + bank - 0x900000;
602c28ca 1187#endif
5e49c3a8 1188}
1189
acd35d4c 1190// -----------------------------------------------------------------
1191// SH2
1192// -----------------------------------------------------------------
1193
bcf65fd6 1194// read8
f81107f5 1195static u32 sh2_read8_unmapped(u32 a, SH2 *sh2)
acd35d4c 1196{
f8675e28 1197 elprintf_sh2(sh2, EL_32X, "unmapped r8 [%08x] %02x @%06x",
1198 a, 0, sh2_pc(sh2));
bcf65fd6 1199 return 0;
1200}
b78efee2 1201
f81107f5 1202static u32 sh2_read8_cs0(u32 a, SH2 *sh2)
bcf65fd6 1203{
1204 u32 d = 0;
97d3f47f 1205
8a847c12 1206 sh2_burn_cycles(sh2, 1*2);
1207
eb35ce15 1208 // 0x3ffc0 is veridied
1209 if ((a & 0x3ffc0) == 0x4000) {
f81107f5 1210 d = p32x_sh2reg_read16(a, sh2);
db1d3564 1211 goto out_16to8;
acd35d4c 1212 }
1213
eb35ce15 1214 if ((a & 0x3fff0) == 0x4100) {
acd35d4c 1215 d = p32x_vdp_read16(a);
4a1fb183 1216 sh2_poll_detect(sh2, a, SH2_STATE_VPOLL, 7);
db1d3564 1217 goto out_16to8;
acd35d4c 1218 }
1219
bcf65fd6 1220 // TODO: mirroring?
f81107f5 1221 if (!sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_m))
895d1512 1222 return Pico32xMem->sh2_rom_m.b[a ^ 1];
f81107f5 1223 if (sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_s))
895d1512 1224 return Pico32xMem->sh2_rom_s.b[a ^ 1];
bcf65fd6 1225
1f1ff763 1226 if ((a & 0x3fe00) == 0x4200) {
acd35d4c 1227 d = Pico32xMem->pal[(a & 0x1ff) / 2];
1228 goto out_16to8;
1229 }
1230
f81107f5 1231 return sh2_read8_unmapped(a, sh2);
acd35d4c 1232
1233out_16to8:
1234 if (a & 1)
1235 d &= 0xff;
1236 else
1237 d >>= 8;
1238
f8675e28 1239 elprintf_sh2(sh2, EL_32X, "r8 [%08x] %02x @%06x",
1240 a, d, sh2_pc(sh2));
acd35d4c 1241 return d;
1242}
1243
f81107f5 1244static u32 sh2_read8_da(u32 a, SH2 *sh2)
acd35d4c 1245{
f81107f5 1246 return sh2->data_array[(a & 0xfff) ^ 1];
bcf65fd6 1247}
acd35d4c 1248
bcf65fd6 1249// read16
f81107f5 1250static u32 sh2_read16_unmapped(u32 a, SH2 *sh2)
bcf65fd6 1251{
f8675e28 1252 elprintf_sh2(sh2, EL_32X, "unmapped r16 [%08x] %04x @%06x",
1253 a, 0, sh2_pc(sh2));
bcf65fd6 1254 return 0;
1255}
b78efee2 1256
f81107f5 1257static u32 sh2_read16_cs0(u32 a, SH2 *sh2)
bcf65fd6 1258{
1259 u32 d = 0;
97d3f47f 1260
8a847c12 1261 sh2_burn_cycles(sh2, 1*2);
1262
eb35ce15 1263 if ((a & 0x3ffc0) == 0x4000) {
f81107f5 1264 d = p32x_sh2reg_read16(a, sh2);
1b3f5844 1265 if (!(EL_LOGMASK & EL_PWM) && (a & 0x30) == 0x30) // hide PWM
1266 return d;
db1d3564 1267 goto out;
acd35d4c 1268 }
1269
eb35ce15 1270 if ((a & 0x3fff0) == 0x4100) {
acd35d4c 1271 d = p32x_vdp_read16(a);
4a1fb183 1272 sh2_poll_detect(sh2, a, SH2_STATE_VPOLL, 7);
db1d3564 1273 goto out;
acd35d4c 1274 }
1275
f81107f5 1276 if (!sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_m))
895d1512 1277 return Pico32xMem->sh2_rom_m.w[a / 2];
f81107f5 1278 if (sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_s))
895d1512 1279 return Pico32xMem->sh2_rom_s.w[a / 2];
bcf65fd6 1280
1f1ff763 1281 if ((a & 0x3fe00) == 0x4200) {
acd35d4c 1282 d = Pico32xMem->pal[(a & 0x1ff) / 2];
1283 goto out;
1284 }
1285
f81107f5 1286 return sh2_read16_unmapped(a, sh2);
acd35d4c 1287
1288out:
f8675e28 1289 elprintf_sh2(sh2, EL_32X, "r16 [%08x] %04x @%06x",
1290 a, d, sh2_pc(sh2));
acd35d4c 1291 return d;
1292}
1293
f81107f5 1294static u32 sh2_read16_da(u32 a, SH2 *sh2)
acd35d4c 1295{
f81107f5 1296 return ((u16 *)sh2->data_array)[(a & 0xfff) / 2];
acd35d4c 1297}
1298
f81107f5 1299// writes
1300static void REGPARM(3) sh2_write_ignore(u32 a, u32 d, SH2 *sh2)
4b315c21 1301{
1302}
1303
bcf65fd6 1304// write8
f81107f5 1305static void REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, SH2 *sh2)
acd35d4c 1306{
f8675e28 1307 elprintf_sh2(sh2, EL_32X, "unmapped w8 [%08x] %02x @%06x",
1308 a, d & 0xff, sh2_pc(sh2));
bcf65fd6 1309}
266c6afa 1310
f81107f5 1311static void REGPARM(3) sh2_write8_cs0(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1312{
f8675e28 1313 elprintf_sh2(sh2, EL_32X, "w8 [%08x] %02x @%06x",
1314 a, d & 0xff, sh2_pc(sh2));
b78efee2 1315
5609d343 1316 if (Pico32x.regs[0] & P32XS_FM) {
eb35ce15 1317 if ((a & 0x3fff0) == 0x4100) {
f81107f5 1318 sh2->poll_addr = 0;
5609d343 1319 p32x_vdp_write8(a, d);
f81107f5 1320 return;
5609d343 1321 }
acd35d4c 1322 }
1323
eb35ce15 1324 if ((a & 0x3ffc0) == 0x4000) {
f81107f5 1325 p32x_sh2reg_write8(a, d, sh2);
1326 return;
acd35d4c 1327 }
1328
f81107f5 1329 sh2_write8_unmapped(a, d, sh2);
bcf65fd6 1330}
1331
f81107f5 1332static void REGPARM(3) sh2_write8_dram0(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1333{
1334 sh2_write8_dramN(0);
acd35d4c 1335}
1336
f81107f5 1337static void REGPARM(3) sh2_write8_dram1(u32 a, u32 d, SH2 *sh2)
acd35d4c 1338{
bcf65fd6 1339 sh2_write8_dramN(1);
1340}
87accdf7 1341
f81107f5 1342static void REGPARM(3) sh2_write8_sdram(u32 a, u32 d, SH2 *sh2)
f4bb5d6b 1343{
1344 u32 a1 = a & 0x3ffff;
1345#ifdef DRC_SH2
1346 int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
1347 if (t)
f81107f5 1348 sh2_drc_wcheck_ram(a, t, sh2->is_slave);
f4bb5d6b 1349#endif
1350 Pico32xMem->sdram[a1 ^ 1] = d;
1351}
1352
8a847c12 1353static void REGPARM(3) sh2_write8_sdram_wt(u32 a, u32 d, SH2 *sh2)
1354{
1355 // xmen sync hack..
1356 if (a < 0x26000200)
1357 sh2_end_run(sh2, 32);
1358
1359 sh2_write8_sdram(a, d, sh2);
1360}
1361
f81107f5 1362static void REGPARM(3) sh2_write8_da(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1363{
f4bb5d6b 1364 u32 a1 = a & 0xfff;
1365#ifdef DRC_SH2
f81107f5 1366 int id = sh2->is_slave;
f4bb5d6b 1367 int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
1368 if (t)
1369 sh2_drc_wcheck_da(a, t, id);
1370#endif
f81107f5 1371 sh2->data_array[a1 ^ 1] = d;
bcf65fd6 1372}
acd35d4c 1373
bcf65fd6 1374// write16
f81107f5 1375static void REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1376{
f8675e28 1377 elprintf_sh2(sh2, EL_32X, "unmapped w16 [%08x] %04x @%06x",
1378 a, d & 0xffff, sh2_pc(sh2));
bcf65fd6 1379}
b78efee2 1380
f81107f5 1381static void REGPARM(3) sh2_write16_cs0(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1382{
1383 if (((EL_LOGMASK & EL_PWM) || (a & 0x30) != 0x30)) // hide PWM
f8675e28 1384 elprintf_sh2(sh2, EL_32X, "w16 [%08x] %04x @%06x",
1385 a, d & 0xffff, sh2_pc(sh2));
266c6afa 1386
5609d343 1387 if (Pico32x.regs[0] & P32XS_FM) {
eb35ce15 1388 if ((a & 0x3fff0) == 0x4100) {
f81107f5 1389 sh2->poll_addr = 0;
1390 p32x_vdp_write16(a, d, sh2);
1391 return;
5609d343 1392 }
acd35d4c 1393
5609d343 1394 if ((a & 0x3fe00) == 0x4200) {
1395 Pico32xMem->pal[(a & 0x1ff) / 2] = d;
1396 Pico32x.dirty_pal = 1;
f81107f5 1397 return;
5609d343 1398 }
acd35d4c 1399 }
1400
eb35ce15 1401 if ((a & 0x3ffc0) == 0x4000) {
f81107f5 1402 p32x_sh2reg_write16(a, d, sh2);
1403 return;
acd35d4c 1404 }
1405
f81107f5 1406 sh2_write16_unmapped(a, d, sh2);
bcf65fd6 1407}
1408
f81107f5 1409static void REGPARM(3) sh2_write16_dram0(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1410{
f81107f5 1411 sh2_write16_dramN(0);
bcf65fd6 1412}
1413
f81107f5 1414static void REGPARM(3) sh2_write16_dram1(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1415{
f81107f5 1416 sh2_write16_dramN(1);
bcf65fd6 1417}
1418
f81107f5 1419static void REGPARM(3) sh2_write16_sdram(u32 a, u32 d, SH2 *sh2)
f4bb5d6b 1420{
1421 u32 a1 = a & 0x3ffff;
1422#ifdef DRC_SH2
1423 int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT];
1424 if (t)
f81107f5 1425 sh2_drc_wcheck_ram(a, t, sh2->is_slave);
f4bb5d6b 1426#endif
1427 ((u16 *)Pico32xMem->sdram)[a1 / 2] = d;
1428}
1429
f81107f5 1430static void REGPARM(3) sh2_write16_da(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1431{
f4bb5d6b 1432 u32 a1 = a & 0xfff;
1433#ifdef DRC_SH2
f81107f5 1434 int id = sh2->is_slave;
f4bb5d6b 1435 int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT];
1436 if (t)
1437 sh2_drc_wcheck_da(a, t, id);
1438#endif
f81107f5 1439 ((u16 *)sh2->data_array)[a1 / 2] = d;
bcf65fd6 1440}
1441
1442
f81107f5 1443typedef u32 (sh2_read_handler)(u32 a, SH2 *sh2);
1444typedef void REGPARM(3) (sh2_write_handler)(u32 a, u32 d, SH2 *sh2);
bcf65fd6 1445
e05b81fc 1446#define SH2MAP_ADDR2OFFS_R(a) \
f81107f5 1447 ((u32)(a) >> SH2_READ_SHIFT)
e05b81fc 1448
1449#define SH2MAP_ADDR2OFFS_W(a) \
1450 ((u32)(a) >> SH2_WRITE_SHIFT)
bcf65fd6 1451
80599a42 1452u32 REGPARM(2) p32x_sh2_read8(u32 a, SH2 *sh2)
bcf65fd6 1453{
1454 const sh2_memmap *sh2_map = sh2->read8_map;
1455 uptr p;
1456
e05b81fc 1457 sh2_map += SH2MAP_ADDR2OFFS_R(a);
bcf65fd6 1458 p = sh2_map->addr;
b8a1c09a 1459 if (map_flag_set(p))
f81107f5 1460 return ((sh2_read_handler *)(p << 1))(a, sh2);
bcf65fd6 1461 else
1462 return *(u8 *)((p << 1) + ((a & sh2_map->mask) ^ 1));
1463}
1464
80599a42 1465u32 REGPARM(2) p32x_sh2_read16(u32 a, SH2 *sh2)
bcf65fd6 1466{
1467 const sh2_memmap *sh2_map = sh2->read16_map;
1468 uptr p;
1469
e05b81fc 1470 sh2_map += SH2MAP_ADDR2OFFS_R(a);
bcf65fd6 1471 p = sh2_map->addr;
b8a1c09a 1472 if (map_flag_set(p))
f81107f5 1473 return ((sh2_read_handler *)(p << 1))(a, sh2);
bcf65fd6 1474 else
1475 return *(u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
1476}
1477
80599a42 1478u32 REGPARM(2) p32x_sh2_read32(u32 a, SH2 *sh2)
bcf65fd6 1479{
1480 const sh2_memmap *sh2_map = sh2->read16_map;
1481 sh2_read_handler *handler;
1482 u32 offs;
1483 uptr p;
1484
e05b81fc 1485 offs = SH2MAP_ADDR2OFFS_R(a);
bcf65fd6 1486 sh2_map += offs;
1487 p = sh2_map->addr;
b8a1c09a 1488 if (!map_flag_set(p)) {
bcf65fd6 1489 // XXX: maybe 32bit access instead with ror?
1490 u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
1491 return (pd[0] << 16) | pd[1];
1d7a28a7 1492 }
1493
77e58d93 1494 if (offs == SH2MAP_ADDR2OFFS_R(0xffffc000))
f81107f5 1495 return sh2_peripheral_read32(a, sh2);
bcf65fd6 1496
1497 handler = (sh2_read_handler *)(p << 1);
f81107f5 1498 return (handler(a, sh2) << 16) | handler(a + 2, sh2);
bcf65fd6 1499}
1500
f81107f5 1501void REGPARM(3) p32x_sh2_write8(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1502{
f4bb5d6b 1503 const void **sh2_wmap = sh2->write8_tab;
1504 sh2_write_handler *wh;
bcf65fd6 1505
e05b81fc 1506 wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
f81107f5 1507 wh(a, d, sh2);
bcf65fd6 1508}
1509
f81107f5 1510void REGPARM(3) p32x_sh2_write16(u32 a, u32 d, SH2 *sh2)
bcf65fd6 1511{
f4bb5d6b 1512 const void **sh2_wmap = sh2->write16_tab;
1513 sh2_write_handler *wh;
bcf65fd6 1514
e05b81fc 1515 wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)];
f81107f5 1516 wh(a, d, sh2);
acd35d4c 1517}
1518
f81107f5 1519void REGPARM(3) p32x_sh2_write32(u32 a, u32 d, SH2 *sh2)
acd35d4c 1520{
f4bb5d6b 1521 const void **sh2_wmap = sh2->write16_tab;
f81107f5 1522 sh2_write_handler *wh;
bcf65fd6 1523 u32 offs;
bcf65fd6 1524
e05b81fc 1525 offs = SH2MAP_ADDR2OFFS_W(a);
bcf65fd6 1526
e05b81fc 1527 if (offs == SH2MAP_ADDR2OFFS_W(0xffffc000)) {
f81107f5 1528 sh2_peripheral_write32(a, d, sh2);
1529 return;
4ea707e1 1530 }
1531
f81107f5 1532 wh = sh2_wmap[offs];
1533 wh(a, d >> 16, sh2);
1534 wh(a + 2, d, sh2);
acd35d4c 1535}
1536
bcf65fd6 1537// -----------------------------------------------------------------
1538
419973a6 1539static void z80_md_bank_write_32x(unsigned int a, unsigned char d)
1540{
1541 unsigned int addr68k;
1542
1543 addr68k = Pico.m.z80_bank68k << 15;
1544 addr68k += a & 0x7fff;
1545 if ((addr68k & 0xfff000) == 0xa15000)
1546 Pico32x.emu_flags |= P32XF_Z80_32X_IO;
1547
1548 elprintf(EL_Z80BNK, "z80->68k w8 [%06x] %02x", addr68k, d);
1549 m68k_write8(addr68k, d);
1550}
1551
1552// -----------------------------------------------------------------
1553
83ff19ec 1554static const u16 msh2_code[] = {
1555 // trap instructions
fa8fb754 1556 0xaffe, // 200 bra <self>
1557 0x0009, // 202 nop
83ff19ec 1558 // have to wait a bit until m68k initial program finishes clearing stuff
1559 // to avoid races with game SH2 code, like in Tempo
fa8fb754 1560 0xd406, // 204 mov.l @(_m_ok,pc), r4
1561 0xc400, // 206 mov.b @(h'0,gbr),r0
1562 0xc801, // 208 tst #1, r0
1563 0x8b0f, // 20a bf cd_start
1564 0xd105, // 20c mov.l @(_cnt,pc), r1
1565 0xd206, // 20e mov.l @(_start,pc), r2
1566 0x71ff, // 210 add #-1, r1
1567 0x4115, // 212 cmp/pl r1
1568 0x89fc, // 214 bt -2
1569 0x6043, // 216 mov r4, r0
1570 0xc208, // 218 mov.l r0, @(h'20,gbr)
1571 0x6822, // 21a mov.l @r2, r8
1572 0x482b, // 21c jmp @r8
1573 0x0009, // 21e nop
1574 ('M'<<8)|'_', ('O'<<8)|'K', // 220 _m_ok
1575 0x0001, 0x0000, // 224 _cnt
1576 0x2200, 0x03e0, // master start pointer in ROM
1577 // cd_start:
1578 0xd20d, // 22c mov.l @(__cd_,pc), r2
1579 0xc608, // 22e mov.l @(h'20,gbr), r0
1580 0x3200, // 230 cmp/eq r0, r2
1581 0x8bfc, // 232 bf #-2
1582 0xe000, // 234 mov #0, r0
1583 0xcf80, // 236 or.b #0x80,@(r0,gbr)
1584 0xd80b, // 238 mov.l @(_start_cd,pc), r8 // 24000018
1585 0xd30c, // 23a mov.l @(_max_len,pc), r3
1586 0x5b84, // 23c mov.l @(h'10,r8), r11 // master vbr
1587 0x5a82, // 23e mov.l @(8,r8), r10 // entry
1588 0x5081, // 240 mov.l @(4,r8), r0 // len
1589 0x5980, // 242 mov.l @(0,r8), r9 // dst
1590 0x3036, // 244 cmp/hi r3,r0
1591 0x8b00, // 246 bf #1
1592 0x6033, // 248 mov r3,r0
1593 0x7820, // 24a add #0x20, r8
1594 // ipl_copy:
1595 0x6286, // 24c mov.l @r8+, r2
1596 0x2922, // 24e mov.l r2, @r9
1597 0x7904, // 250 add #4, r9
1598 0x70fc, // 252 add #-4, r0
1599 0x8800, // 254 cmp/eq #0, r0
1600 0x8bf9, // 256 bf #-5
1601 //
1602 0x4b2e, // 258 ldc r11, vbr
1603 0x6043, // 25a mov r4, r0 // M_OK
1604 0xc208, // 25c mov.l r0, @(h'20,gbr)
1605 0x4a2b, // 25e jmp @r10
1606 0x0009, // 260 nop
1607 0x0009, // 262 nop // pad
1608 ('_'<<8)|'C', ('D'<<8)|'_', // 264 __cd_
1609 0x2400, 0x0018, // 268 _start_cd
1610 0x0001, 0xffe0, // 26c _max_len
83ff19ec 1611};
1612
1613static const u16 ssh2_code[] = {
fa8fb754 1614 0xaffe, // 200 bra <self>
1615 0x0009, // 202 nop
83ff19ec 1616 // code to wait for master, in case authentic master BIOS is used
fa8fb754 1617 0xd106, // 204 mov.l @(_m_ok,pc), r1
1618 0xd208, // 206 mov.l @(_start,pc), r2
1619 0xc608, // 208 mov.l @(h'20,gbr), r0
1620 0x3100, // 20a cmp/eq r0, r1
1621 0x8bfc, // 20c bf #-2
1622 0xc400, // 20e mov.b @(h'0,gbr),r0
1623 0xc801, // 210 tst #1, r0
1624 0xd004, // 212 mov.l @(_s_ok,pc), r0
1625 0x8b0a, // 214 bf cd_start
1626 0xc209, // 216 mov.l r0, @(h'24,gbr)
1627 0x6822, // 218 mov.l @r2, r8
1628 0x482b, // 21a jmp @r8
1629 0x0009, // 21c nop
1630 0x0009, // 21e nop
1631 ('M'<<8)|'_', ('O'<<8)|'K', // 220
1632 ('S'<<8)|'_', ('O'<<8)|'K', // 224
1633 0x2200, 0x03e4, // slave start pointer in ROM
1634 // cd_start:
1635 0xd803, // 22c mov.l @(_start_cd,pc), r8 // 24000018
1636 0x5b85, // 22e mov.l @(h'14,r8), r11 // slave vbr
1637 0x5a83, // 230 mov.l @(h'0c,r8), r10 // entry
1638 0x4b2e, // 232 ldc r11, vbr
1639 0xc209, // 234 mov.l r0, @(h'24,gbr) // write S_OK
1640 0x4a2b, // 236 jmp @r10
1641 0x0009, // 238 nop
1642 0x0009, // 23a nop
1643 0x2400, 0x0018, // 23c _start_cd
83ff19ec 1644};
1645
da77daa9 1646#define HWSWAP(x) (((u16)(x) << 16) | ((x) >> 16))
83ff19ec 1647static void get_bios(void)
be2c4208 1648{
83ff19ec 1649 u16 *ps;
1650 u32 *pl;
be2c4208 1651 int i;
1652
83ff19ec 1653 // M68K ROM
1654 if (p32x_bios_g != NULL) {
1655 elprintf(EL_STATUS|EL_32X, "32x: using supplied 68k BIOS");
b4db550e 1656 Byteswap(Pico32xMem->m68k_rom, p32x_bios_g, sizeof(Pico32xMem->m68k_rom));
be2c4208 1657 }
83ff19ec 1658 else {
1659 // generate 68k ROM
1660 ps = (u16 *)Pico32xMem->m68k_rom;
1661 pl = (u32 *)ps;
1662 for (i = 1; i < 0xc0/4; i++)
1663 pl[i] = HWSWAP(0x880200 + (i - 1) * 6);
be2c4208 1664
83ff19ec 1665 // fill with nops
1666 for (i = 0xc0/2; i < 0x100/2; i++)
1667 ps[i] = 0x4e71;
be2c4208 1668
5e49c3a8 1669#if 0
83ff19ec 1670 ps[0xc0/2] = 0x46fc;
1671 ps[0xc2/2] = 0x2700; // move #0x2700,sr
1672 ps[0xfe/2] = 0x60fe; // jump to self
5e49c3a8 1673#else
83ff19ec 1674 ps[0xfe/2] = 0x4e75; // rts
5e49c3a8 1675#endif
83ff19ec 1676 }
1677 // fill remaining m68k_rom page with game ROM
b4db550e 1678 memcpy(Pico32xMem->m68k_rom_bank + sizeof(Pico32xMem->m68k_rom),
1679 Pico.rom + sizeof(Pico32xMem->m68k_rom),
1680 sizeof(Pico32xMem->m68k_rom_bank) - sizeof(Pico32xMem->m68k_rom));
be2c4208 1681
83ff19ec 1682 // MSH2
1683 if (p32x_bios_m != NULL) {
1684 elprintf(EL_STATUS|EL_32X, "32x: using supplied master SH2 BIOS");
895d1512 1685 Byteswap(&Pico32xMem->sh2_rom_m, p32x_bios_m, sizeof(Pico32xMem->sh2_rom_m));
acd35d4c 1686 }
83ff19ec 1687 else {
895d1512 1688 pl = (u32 *)&Pico32xMem->sh2_rom_m;
83ff19ec 1689
1690 // fill exception vector table to our trap address
1691 for (i = 0; i < 128; i++)
1692 pl[i] = HWSWAP(0x200);
1693
83ff19ec 1694 // start
1695 pl[0] = pl[2] = HWSWAP(0x204);
fa8fb754 1696 // reset SP
1697 pl[1] = pl[3] = HWSWAP(0x6040000);
1698
1699 // startup code
1700 memcpy(&Pico32xMem->sh2_rom_m.b[0x200], msh2_code, sizeof(msh2_code));
83ff19ec 1701 }
1702
1703 // SSH2
1704 if (p32x_bios_s != NULL) {
1705 elprintf(EL_STATUS|EL_32X, "32x: using supplied slave SH2 BIOS");
895d1512 1706 Byteswap(&Pico32xMem->sh2_rom_s, p32x_bios_s, sizeof(Pico32xMem->sh2_rom_s));
83ff19ec 1707 }
1708 else {
895d1512 1709 pl = (u32 *)&Pico32xMem->sh2_rom_s;
83ff19ec 1710
1711 // fill exception vector table to our trap address
1712 for (i = 0; i < 128; i++)
1713 pl[i] = HWSWAP(0x200);
1714
83ff19ec 1715 // start
1716 pl[0] = pl[2] = HWSWAP(0x204);
fa8fb754 1717 // reset SP
1718 pl[1] = pl[3] = HWSWAP(0x603f800);
1719
1720 // startup code
1721 memcpy(&Pico32xMem->sh2_rom_s.b[0x200], ssh2_code, sizeof(ssh2_code));
83ff19ec 1722 }
1723}
1724
bcf65fd6 1725#define MAP_MEMORY(m) ((uptr)(m) >> 1)
b8a1c09a 1726#define MAP_HANDLER(h) ( ((uptr)(h) >> 1) | ((uptr)1 << (sizeof(uptr) * 8 - 1)) )
bcf65fd6 1727
f81107f5 1728static sh2_memmap sh2_read8_map[0x80], sh2_read16_map[0x80];
f4bb5d6b 1729// for writes we are using handlers only
e05b81fc 1730static sh2_write_handler *sh2_write8_map[0x80], *sh2_write16_map[0x80];
bcf65fd6 1731
1732void Pico32xSwapDRAM(int b)
1733{
1734 cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
1735 cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0);
34280f9b 1736 cpu68k_map_set(m68k_read8_map, 0x860000, 0x87ffff, Pico32xMem->dram[b], 0);
1737 cpu68k_map_set(m68k_read16_map, 0x860000, 0x87ffff, Pico32xMem->dram[b], 0);
1738 cpu68k_map_set(m68k_write8_map, 0x840000, 0x87ffff,
1739 b ? m68k_write8_dram1_ow : m68k_write8_dram0_ow, 1);
1740 cpu68k_map_set(m68k_write16_map, 0x840000, 0x87ffff,
1741 b ? m68k_write16_dram1_ow : m68k_write16_dram0_ow, 1);
bcf65fd6 1742
1743 // SH2
f81107f5 1744 sh2_read8_map[0x04/2].addr = sh2_read8_map[0x24/2].addr =
1745 sh2_read16_map[0x04/2].addr = sh2_read16_map[0x24/2].addr = MAP_MEMORY(Pico32xMem->dram[b]);
bcf65fd6 1746
e05b81fc 1747 sh2_write8_map[0x04/2] = sh2_write8_map[0x24/2] = b ? sh2_write8_dram1 : sh2_write8_dram0;
1748 sh2_write16_map[0x04/2] = sh2_write16_map[0x24/2] = b ? sh2_write16_dram1 : sh2_write16_dram0;
bcf65fd6 1749}
1750
83ff19ec 1751void PicoMemSetup32x(void)
1752{
1753 unsigned int rs;
bcf65fd6 1754 int i;
83ff19ec 1755
e743be20 1756 Pico32xMem = plat_mmap(0x06000000, sizeof(*Pico32xMem), 0, 0);
83ff19ec 1757 if (Pico32xMem == NULL) {
1758 elprintf(EL_STATUS, "OOM");
1759 return;
1760 }
1761
83ff19ec 1762 get_bios();
acd35d4c 1763
be2c4208 1764 // cartridge area becomes unmapped
1765 // XXX: we take the easy way and don't unmap ROM,
1766 // so that we can avoid handling the RV bit.
1767 // m68k_map_unmap(0x000000, 0x3fffff);
1768
fa8fb754 1769 if (!Pico.m.ncart_in) {
1770 // MD ROM area
1771 rs = sizeof(Pico32xMem->m68k_rom_bank);
1772 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom_bank, 0);
1773 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom_bank, 0);
1774 cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify
1775 cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1);
1776
1777 // 32X ROM (unbanked, XXX: consider mirroring?)
1778 rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK;
1779 if (rs > 0x80000)
1780 rs = 0x80000;
1781 cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1782 cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0);
1783 cpu68k_map_set(m68k_write8_map, 0x880000, 0x880000 + rs - 1, PicoWrite8_cart, 1);
1784 cpu68k_map_set(m68k_write16_map, 0x880000, 0x880000 + rs - 1, PicoWrite16_cart, 1);
602c28ca 1785#ifdef EMU_F68K
fa8fb754 1786 // setup FAME fetchmap
1787 PicoCpuFM68k.Fetch[0] = (unsigned long)Pico32xMem->m68k_rom;
1788 for (rs = 0x88; rs < 0x90; rs++)
1789 PicoCpuFM68k.Fetch[rs] = (unsigned long)Pico.rom - 0x880000;
602c28ca 1790#endif
be2c4208 1791
fa8fb754 1792 // 32X ROM (banked)
1793 bank_switch(0);
1794 cpu68k_map_set(m68k_write8_map, 0x900000, 0x9fffff, PicoWrite8_bank, 1);
1795 cpu68k_map_set(m68k_write16_map, 0x900000, 0x9fffff, PicoWrite16_bank, 1);
1796 }
b78efee2 1797
83ff19ec 1798 // SYS regs
1799 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_32x_on, 1);
1800 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_32x_on, 1);
1801 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_32x_on, 1);
1802 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_32x_on, 1);
1803
bcf65fd6 1804 // SH2 maps: A31,A30,A29,CS1,CS0
1805 // all unmapped by default
e05b81fc 1806 for (i = 0; i < ARRAY_SIZE(sh2_read8_map); i++) {
bcf65fd6 1807 sh2_read8_map[i].addr = MAP_HANDLER(sh2_read8_unmapped);
1808 sh2_read16_map[i].addr = MAP_HANDLER(sh2_read16_unmapped);
e05b81fc 1809 }
1810
1811 for (i = 0; i < ARRAY_SIZE(sh2_write8_map); i++) {
f4bb5d6b 1812 sh2_write8_map[i] = sh2_write8_unmapped;
1813 sh2_write16_map[i] = sh2_write16_unmapped;
bcf65fd6 1814 }
1815
4b315c21 1816 // "purge area"
e05b81fc 1817 for (i = 0x40; i <= 0x5f; i++) {
1818 sh2_write8_map[i >> 1] =
1819 sh2_write16_map[i >> 1] = sh2_write_ignore;
4b315c21 1820 }
1821
bcf65fd6 1822 // CS0
f81107f5 1823 sh2_read8_map[0x00/2].addr = sh2_read8_map[0x20/2].addr = MAP_HANDLER(sh2_read8_cs0);
1824 sh2_read16_map[0x00/2].addr = sh2_read16_map[0x20/2].addr = MAP_HANDLER(sh2_read16_cs0);
e05b81fc 1825 sh2_write8_map[0x00/2] = sh2_write8_map[0x20/2] = sh2_write8_cs0;
1826 sh2_write16_map[0x00/2] = sh2_write16_map[0x20/2] = sh2_write16_cs0;
bcf65fd6 1827 // CS1 - ROM
f81107f5 1828 sh2_read8_map[0x02/2].addr = sh2_read8_map[0x22/2].addr =
1829 sh2_read16_map[0x02/2].addr = sh2_read16_map[0x22/2].addr = MAP_MEMORY(Pico.rom);
1830 sh2_read8_map[0x02/2].mask = sh2_read8_map[0x22/2].mask =
1831 sh2_read16_map[0x02/2].mask = sh2_read16_map[0x22/2].mask = 0x3fffff; // FIXME
bcf65fd6 1832 // CS2 - DRAM - done by Pico32xSwapDRAM()
f81107f5 1833 sh2_read8_map[0x04/2].mask = sh2_read8_map[0x24/2].mask =
1834 sh2_read16_map[0x04/2].mask = sh2_read16_map[0x24/2].mask = 0x01ffff;
bcf65fd6 1835 // CS3 - SDRAM
f81107f5 1836 sh2_read8_map[0x06/2].addr = sh2_read8_map[0x26/2].addr =
1837 sh2_read16_map[0x06/2].addr = sh2_read16_map[0x26/2].addr = MAP_MEMORY(Pico32xMem->sdram);
8a847c12 1838 sh2_write8_map[0x06/2] = sh2_write8_sdram;
1839 sh2_write8_map[0x26/2] = sh2_write8_sdram_wt;
f81107f5 1840 sh2_write16_map[0x06/2] = sh2_write16_map[0x26/2] = sh2_write16_sdram;
1841 sh2_read8_map[0x06/2].mask = sh2_read8_map[0x26/2].mask =
1842 sh2_read16_map[0x06/2].mask = sh2_read16_map[0x26/2].mask = 0x03ffff;
bcf65fd6 1843 // SH2 data array
f81107f5 1844 sh2_read8_map[0xc0/2].addr = MAP_HANDLER(sh2_read8_da);
1845 sh2_read16_map[0xc0/2].addr = MAP_HANDLER(sh2_read16_da);
1846 sh2_write8_map[0xc0/2] = sh2_write8_da;
1847 sh2_write16_map[0xc0/2] = sh2_write16_da;
bcf65fd6 1848 // SH2 IO
f81107f5 1849 sh2_read8_map[0xff/2].addr = MAP_HANDLER(sh2_peripheral_read8);
1850 sh2_read16_map[0xff/2].addr = MAP_HANDLER(sh2_peripheral_read16);
1851 sh2_write8_map[0xff/2] = sh2_peripheral_write8;
1852 sh2_write16_map[0xff/2] = sh2_peripheral_write16;
bcf65fd6 1853
1854 // map DRAM area, both 68k and SH2
1855 Pico32xSwapDRAM(1);
1856
1857 msh2.read8_map = ssh2.read8_map = sh2_read8_map;
1858 msh2.read16_map = ssh2.read16_map = sh2_read16_map;
23686515 1859 msh2.write8_tab = ssh2.write8_tab = (const void **)(void *)sh2_write8_map;
1860 msh2.write16_tab = ssh2.write16_tab = (const void **)(void *)sh2_write16_map;
bcf65fd6 1861
23686515 1862 sh2_drc_mem_setup(&msh2);
1863 sh2_drc_mem_setup(&ssh2);
419973a6 1864
1865 // z80 hack
1866 z80_map_set(z80_write_map, 0x8000, 0xffff, z80_md_bank_write_32x, 1);
be2c4208 1867}
1868
27e26273 1869void Pico32xMemStateLoaded(void)
b4db550e 1870{
1871 bank_switch(Pico32x.regs[4 / 2]);
1872 Pico32xSwapDRAM((Pico32x.vdp_regs[0x0a / 2] & P32XV_FS) ^ P32XV_FS);
b4db550e 1873 memset(Pico32xMem->pwm, 0, sizeof(Pico32xMem->pwm));
27e26273 1874 Pico32x.dirty_pal = 1;
51d86e55 1875
19886062 1876 Pico32x.emu_flags &= ~(P32XF_68KCPOLL | P32XF_68KVPOLL);
1877 memset(&m68k_poll, 0, sizeof(m68k_poll));
1878 msh2.state = 0;
1879 msh2.poll_addr = msh2.poll_cycles = msh2.poll_cnt = 0;
1880 ssh2.state = 0;
1881 ssh2.poll_addr = ssh2.poll_cycles = ssh2.poll_cnt = 0;
1882
b4db550e 1883 sh2_drc_flush_all();
b4db550e 1884}
1885
ed4402a7 1886// vim:shiftwidth=2:ts=2:expandtab