| 1 | /* |
| 2 | * PicoDrive |
| 3 | * (C) notaz, 2009,2010,2013 |
| 4 | * |
| 5 | * This work is licensed under the terms of MAME license. |
| 6 | * See COPYING file in the top-level directory. |
| 7 | * |
| 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 |
| 12 | * a15106 ........ F....SDR UE...... .....SDR 4006 // Full 68S Dma Rv fUll[fb] Empt[fb] |
| 13 | * a15108 (32bit DREQ src) 4008 |
| 14 | * a1510c (32bit DREQ dst) 400c |
| 15 | * a15110 llllllll llllll00 4010 // DREQ Len |
| 16 | * a15112 (16bit FIFO reg) 4012 |
| 17 | * a15114 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 |
| 23 | * a15120 (16 bytes comm) 2020 |
| 24 | * a15130 (PWM) 2030 |
| 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 |
| 35 | * cart 2000000-23fffff 6-15 |
| 36 | * dram/fb 4000000-401ffff 5-12 1-3 |
| 37 | * fb ovr 4020000-403ffff |
| 38 | * sdram 6000000-603ffff 12 2 (cycles) |
| 39 | * d.a. c0000000-? |
| 40 | */ |
| 41 | #include "../pico_int.h" |
| 42 | #include "../memory.h" |
| 43 | #include "../../cpu/sh2/compiler.h" |
| 44 | |
| 45 | static const char str_mars[] = "MARS"; |
| 46 | |
| 47 | void *p32x_bios_g, *p32x_bios_m, *p32x_bios_s; |
| 48 | struct Pico32xMem *Pico32xMem; |
| 49 | |
| 50 | static void bank_switch(int b); |
| 51 | |
| 52 | // addressing byte in 16bit reg |
| 53 | #define REG8IN16(ptr, offs) ((u8 *)ptr)[(offs) ^ 1] |
| 54 | |
| 55 | // poll detection |
| 56 | #define POLL_THRESHOLD 3 |
| 57 | |
| 58 | static struct { |
| 59 | u32 addr, cycles; |
| 60 | int cnt; |
| 61 | } m68k_poll; |
| 62 | |
| 63 | static int m68k_poll_detect(u32 a, u32 cycles, u32 flags) |
| 64 | { |
| 65 | int ret = 0; |
| 66 | |
| 67 | if (a - 2 <= m68k_poll.addr && m68k_poll.addr <= a + 2 |
| 68 | && cycles - m68k_poll.cycles <= 64 && !SekNotPolling) |
| 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); |
| 74 | ret = 1; |
| 75 | } |
| 76 | Pico32x.emu_flags |= flags; |
| 77 | } |
| 78 | } |
| 79 | else { |
| 80 | m68k_poll.cnt = 0; |
| 81 | m68k_poll.addr = a; |
| 82 | SekNotPolling = 0; |
| 83 | } |
| 84 | m68k_poll.cycles = cycles; |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | void 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 | |
| 100 | static void sh2_poll_detect(SH2 *sh2, u32 a, u32 flags, int maxcnt) |
| 101 | { |
| 102 | int cycles_left = sh2_cycles_left(sh2); |
| 103 | |
| 104 | if (a == sh2->poll_addr && sh2->poll_cycles - cycles_left <= 10) { |
| 105 | if (sh2->poll_cnt++ > maxcnt) { |
| 106 | if (!(sh2->state & flags)) |
| 107 | elprintf_sh2(sh2, EL_32X, "state: %02x->%02x", |
| 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 | } |
| 116 | else |
| 117 | sh2->poll_cnt = 0; |
| 118 | sh2->poll_addr = a; |
| 119 | sh2->poll_cycles = cycles_left; |
| 120 | } |
| 121 | |
| 122 | void p32x_sh2_poll_event(SH2 *sh2, u32 flags, u32 m68k_cycles) |
| 123 | { |
| 124 | if (sh2->state & flags) { |
| 125 | elprintf_sh2(sh2, EL_32X, "state: %02x->%02x", sh2->state, |
| 126 | sh2->state & ~flags); |
| 127 | |
| 128 | if (sh2->m68krcycles_done < m68k_cycles) |
| 129 | sh2->m68krcycles_done = m68k_cycles; |
| 130 | |
| 131 | pevt_log_sh2_o(sh2, EVT_POLL_END); |
| 132 | } |
| 133 | |
| 134 | sh2->state &= ~flags; |
| 135 | sh2->poll_addr = sh2->poll_cycles = sh2->poll_cnt = 0; |
| 136 | } |
| 137 | |
| 138 | static void sh2s_sync_on_read(SH2 *sh2) |
| 139 | { |
| 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); |
| 147 | } |
| 148 | |
| 149 | // SH2 faking |
| 150 | //#define FAKE_SH2 |
| 151 | #ifdef FAKE_SH2 |
| 152 | static int p32x_csum_faked; |
| 153 | static const u16 comm_fakevals[] = { |
| 154 | 0x4d5f, 0x4f4b, // M_OK |
| 155 | 0x535f, 0x4f4b, // S_OK |
| 156 | 0x4D41, 0x5346, // MASF - Brutal Unleashed |
| 157 | 0x5331, 0x4d31, // Darxide |
| 158 | 0x5332, 0x4d32, |
| 159 | 0x5333, 0x4d33, |
| 160 | 0x0000, 0x0000, // eq for doom |
| 161 | 0x0002, // Mortal Kombat |
| 162 | // 0, // pad |
| 163 | }; |
| 164 | |
| 165 | static 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 |
| 177 | |
| 178 | // ------------------------------------------------------------------ |
| 179 | // 68k regs |
| 180 | |
| 181 | static u32 p32x_reg_read16(u32 a) |
| 182 | { |
| 183 | a &= 0x3e; |
| 184 | |
| 185 | #if 0 |
| 186 | if ((a & 0x30) == 0x20) |
| 187 | return sh2_comm_faker(a); |
| 188 | #else |
| 189 | if ((a & 0x30) == 0x20) { |
| 190 | unsigned int cycles = SekCyclesDone(); |
| 191 | int comreg = 1 << (a & 0x0f) / 2; |
| 192 | |
| 193 | if (cycles - msh2.m68krcycles_done > 244 |
| 194 | || (Pico32x.comm_dirty_68k & comreg)) |
| 195 | p32x_sync_sh2s(cycles); |
| 196 | |
| 197 | if (Pico32x.comm_dirty_sh2 & comreg) |
| 198 | Pico32x.comm_dirty_sh2 &= ~comreg; |
| 199 | else if (m68k_poll_detect(a, cycles, P32XF_68KCPOLL)) { |
| 200 | SekSetStop(1); |
| 201 | SekEndRun(16); |
| 202 | } |
| 203 | goto out; |
| 204 | } |
| 205 | #endif |
| 206 | |
| 207 | if (a == 2) { // INTM, INTS |
| 208 | unsigned int cycles = SekCyclesDone(); |
| 209 | if (cycles - msh2.m68krcycles_done > 64) |
| 210 | p32x_sync_sh2s(cycles); |
| 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | if ((a & 0x30) == 0x30) |
| 215 | return p32x_pwm_read16(a, NULL, SekCyclesDone()); |
| 216 | |
| 217 | out: |
| 218 | return Pico32x.regs[a / 2]; |
| 219 | } |
| 220 | |
| 221 | static 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) { |
| 238 | p32x_sync_sh2s(SekCyclesDone()); |
| 239 | p32x_dreq0_trigger(); |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | elprintf(EL_32X|EL_ANOMALY, "DREQ FIFO overflow!"); |
| 244 | } |
| 245 | |
| 246 | // writable bits tested |
| 247 | static void p32x_reg_write8(u32 a, u32 d) |
| 248 | { |
| 249 | u16 *r = Pico32x.regs; |
| 250 | a &= 0x3f; |
| 251 | |
| 252 | // for things like bset on comm port |
| 253 | m68k_poll.cnt = 0; |
| 254 | |
| 255 | switch (a) { |
| 256 | case 0x00: // adapter ctl: FM writable |
| 257 | REG8IN16(r, 0x00) = d & 0x80; |
| 258 | return; |
| 259 | case 0x01: // adapter ctl: RES and ADEN writable |
| 260 | if ((d ^ r[0]) & d & P32XS_nRES) |
| 261 | p32x_reset_sh2s(); |
| 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 |
| 266 | return; |
| 267 | case 0x03: // irq ctl |
| 268 | if ((d ^ r[0x02 / 2]) & 3) { |
| 269 | int cycles = SekCyclesDone(); |
| 270 | p32x_sync_sh2s(cycles); |
| 271 | r[0x02 / 2] = d & 3; |
| 272 | p32x_update_cmd_irq(NULL, cycles); |
| 273 | } |
| 274 | return; |
| 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; |
| 281 | bank_switch(d); |
| 282 | } |
| 283 | return; |
| 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; |
| 336 | return; |
| 337 | case 0x1b: // TV |
| 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; |
| 349 | d = r[0x30 / 2]; |
| 350 | goto pwm_write; |
| 351 | case 0x32: // PWM cycle |
| 352 | REG8IN16(r, a) = d & 0x0f; |
| 353 | d = r[0x32 / 2]; |
| 354 | goto pwm_write; |
| 355 | case 0x33: |
| 356 | REG8IN16(r, a) = d; |
| 357 | d = r[0x32 / 2]; |
| 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: |
| 369 | d = (REG8IN16(r, a ^ 1) << 8) | (d & 0xff); |
| 370 | REG8IN16(r, a ^ 1) = 0; |
| 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: |
| 380 | p32x_pwm_write16(a & ~1, d, NULL, SekCyclesDone()); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | if ((a & 0x30) == 0x20) { |
| 385 | int cycles = SekCyclesDone(); |
| 386 | int comreg; |
| 387 | |
| 388 | if (REG8IN16(r, a) == d) |
| 389 | return; |
| 390 | |
| 391 | comreg = 1 << (a & 0x0f) / 2; |
| 392 | if (Pico32x.comm_dirty_68k & comreg) |
| 393 | p32x_sync_sh2s(cycles); |
| 394 | |
| 395 | REG8IN16(r, a) = d; |
| 396 | p32x_sh2_poll_event(&sh2s[0], SH2_STATE_CPOLL, cycles); |
| 397 | p32x_sh2_poll_event(&sh2s[1], SH2_STATE_CPOLL, cycles); |
| 398 | Pico32x.comm_dirty_68k |= comreg; |
| 399 | |
| 400 | if (cycles - (int)msh2.m68krcycles_done > 120) |
| 401 | p32x_sync_sh2s(cycles); |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static void p32x_reg_write16(u32 a, u32 d) |
| 407 | { |
| 408 | u16 *r = Pico32x.regs; |
| 409 | a &= 0x3e; |
| 410 | |
| 411 | // for things like bset on comm port |
| 412 | m68k_poll.cnt = 0; |
| 413 | |
| 414 | switch (a) { |
| 415 | case 0x00: // adapter ctl |
| 416 | if ((d ^ r[0]) & d & P32XS_nRES) |
| 417 | p32x_reset_sh2s(); |
| 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; |
| 432 | return; |
| 433 | case 0x10: // DREQ len |
| 434 | r[a / 2] = d & ~3; |
| 435 | return; |
| 436 | case 0x12: // FIFO reg |
| 437 | dreq0_write(r, d); |
| 438 | return; |
| 439 | case 0x1a: // TV + mystery bit |
| 440 | r[a / 2] = d & 0x0101; |
| 441 | return; |
| 442 | case 0x30: // PWM control |
| 443 | d = (r[a / 2] & ~0x0f) | (d & 0x0f); |
| 444 | r[a / 2] = d; |
| 445 | p32x_pwm_write16(a, d, NULL, SekCyclesDone()); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | // comm port |
| 450 | if ((a & 0x30) == 0x20) { |
| 451 | int cycles = SekCyclesDone(); |
| 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 | |
| 461 | r[a / 2] = d; |
| 462 | p32x_sh2_poll_event(&sh2s[0], SH2_STATE_CPOLL, cycles); |
| 463 | p32x_sh2_poll_event(&sh2s[1], SH2_STATE_CPOLL, cycles); |
| 464 | Pico32x.comm_dirty_68k |= comreg; |
| 465 | |
| 466 | if (cycles - (int)msh2.m68krcycles_done > 120) |
| 467 | p32x_sync_sh2s(cycles); |
| 468 | return; |
| 469 | } |
| 470 | // PWM |
| 471 | else if ((a & 0x30) == 0x30) { |
| 472 | p32x_pwm_write16(a, d, NULL, SekCyclesDone()); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | p32x_reg_write8(a + 1, d); |
| 477 | } |
| 478 | |
| 479 | // ------------------------------------------------------------------ |
| 480 | // VDP regs |
| 481 | static u32 p32x_vdp_read16(u32 a) |
| 482 | { |
| 483 | u32 d; |
| 484 | a &= 0x0e; |
| 485 | |
| 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; |
| 499 | } |
| 500 | |
| 501 | static void p32x_vdp_write8(u32 a, u32 d) |
| 502 | { |
| 503 | u16 *r = Pico32x.vdp_regs; |
| 504 | a &= 0x0f; |
| 505 | |
| 506 | // TODO: verify what's writeable |
| 507 | switch (a) { |
| 508 | case 0x01: |
| 509 | // priority inversion is handled in palette |
| 510 | if ((r[0] ^ d) & P32XV_PRI) |
| 511 | Pico32x.dirty_pal = 1; |
| 512 | r[0] = (r[0] & P32XV_nPAL) | (d & 0xff); |
| 513 | break; |
| 514 | case 0x03: // shift (for pp mode) |
| 515 | r[2 / 2] = d & 1; |
| 516 | break; |
| 517 | case 0x05: // fill len |
| 518 | r[4 / 2] = d & 0xff; |
| 519 | break; |
| 520 | case 0x0b: |
| 521 | d &= 1; |
| 522 | Pico32x.pending_fb = d; |
| 523 | // if we are blanking and FS bit is changing |
| 524 | if (((r[0x0a/2] & P32XV_VBLK) || (r[0] & P32XV_Mx) == 0) && ((r[0x0a/2] ^ d) & P32XV_FS)) { |
| 525 | r[0x0a/2] ^= P32XV_FS; |
| 526 | Pico32xSwapDRAM(d ^ 1); |
| 527 | elprintf(EL_32X, "VDP FS: %d", r[0x0a/2] & P32XV_FS); |
| 528 | } |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static void p32x_vdp_write16(u32 a, u32 d, SH2 *sh2) |
| 534 | { |
| 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]; |
| 542 | int len = Pico32x.vdp_regs[4 / 2] + 1; |
| 543 | int len1 = len; |
| 544 | a = Pico32x.vdp_regs[6 / 2]; |
| 545 | while (len1--) { |
| 546 | dram[a] = d; |
| 547 | a = (a & 0xff00) | ((a + 1) & 0xff); |
| 548 | } |
| 549 | Pico32x.vdp_regs[0x06 / 2] = a; |
| 550 | Pico32x.vdp_regs[0x08 / 2] = d; |
| 551 | if (sh2 != NULL && len > 4) { |
| 552 | Pico32x.vdp_regs[0x0a / 2] |= P32XV_nFEN; |
| 553 | // supposedly takes 3 bus/6 sh2 cycles? or 3 sh2 cycles? |
| 554 | p32x_event_schedule_sh2(sh2, P32X_EVENT_FILLEND, 3 + len); |
| 555 | } |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | p32x_vdp_write8(a | 1, d); |
| 560 | } |
| 561 | |
| 562 | // ------------------------------------------------------------------ |
| 563 | // SH2 regs |
| 564 | |
| 565 | static u32 p32x_sh2reg_read16(u32 a, SH2 *sh2) |
| 566 | { |
| 567 | u16 *r = Pico32x.regs; |
| 568 | a &= 0x3e; |
| 569 | |
| 570 | switch (a) { |
| 571 | case 0x00: // adapter/irq ctl |
| 572 | return (r[0] & P32XS_FM) | Pico32x.sh2_regs[0] |
| 573 | | Pico32x.sh2irq_mask[sh2->is_slave]; |
| 574 | case 0x04: // H count (often as comm too) |
| 575 | sh2_poll_detect(sh2, a, SH2_STATE_CPOLL, 3); |
| 576 | sh2s_sync_on_read(sh2); |
| 577 | return Pico32x.sh2_regs[4 / 2]; |
| 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: |
| 584 | case 0x10: // DREQ len |
| 585 | return r[a / 2]; |
| 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; // ? |
| 600 | } |
| 601 | |
| 602 | // comm port |
| 603 | if ((a & 0x30) == 0x20) { |
| 604 | int comreg = 1 << (a & 0x0f) / 2; |
| 605 | if (Pico32x.comm_dirty_68k & comreg) |
| 606 | Pico32x.comm_dirty_68k &= ~comreg; |
| 607 | else |
| 608 | sh2_poll_detect(sh2, a, SH2_STATE_CPOLL, 3); |
| 609 | sh2s_sync_on_read(sh2); |
| 610 | return r[a / 2]; |
| 611 | } |
| 612 | if ((a & 0x30) == 0x30) |
| 613 | return p32x_pwm_read16(a, sh2, sh2_cycles_done_m68k(sh2)); |
| 614 | |
| 615 | elprintf_sh2(sh2, EL_32X|EL_ANOMALY, |
| 616 | "unhandled sysreg r16 [%02x] @%08x", a, sh2_pc(sh2)); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static void p32x_sh2reg_write8(u32 a, u32 d, SH2 *sh2) |
| 621 | { |
| 622 | u16 *r = Pico32x.regs; |
| 623 | u32 old; |
| 624 | |
| 625 | a &= 0x3f; |
| 626 | sh2->poll_addr = 0; |
| 627 | |
| 628 | switch (a) { |
| 629 | case 0x00: // FM |
| 630 | r[0] &= ~P32XS_FM; |
| 631 | r[0] |= (d << 8) & P32XS_FM; |
| 632 | return; |
| 633 | case 0x01: // HEN/irq masks |
| 634 | old = Pico32x.sh2irq_mask[sh2->is_slave]; |
| 635 | if ((d ^ old) & 1) |
| 636 | p32x_pwm_sync_to_sh2(sh2); |
| 637 | |
| 638 | Pico32x.sh2irq_mask[sh2->is_slave] = d & 0x0f; |
| 639 | Pico32x.sh2_regs[0] &= ~0x80; |
| 640 | Pico32x.sh2_regs[0] |= d & 0x80; |
| 641 | |
| 642 | if ((d ^ old) & 1) |
| 643 | p32x_pwm_schedule_sh2(sh2); |
| 644 | if ((old ^ d) & 2) |
| 645 | p32x_update_cmd_irq(sh2, 0); |
| 646 | if ((old ^ d) & 4) |
| 647 | p32x_schedule_hint(sh2, 0); |
| 648 | return; |
| 649 | case 0x04: // ignored? |
| 650 | return; |
| 651 | case 0x05: // H count |
| 652 | d &= 0xff; |
| 653 | if (Pico32x.sh2_regs[4 / 2] != d) { |
| 654 | Pico32x.sh2_regs[4 / 2] = d; |
| 655 | p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL, |
| 656 | sh2_cycles_done_m68k(sh2)); |
| 657 | sh2_end_run(sh2, 4); |
| 658 | } |
| 659 | return; |
| 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; |
| 699 | } |
| 700 | |
| 701 | if ((a & 0x30) == 0x20) { |
| 702 | int comreg; |
| 703 | if (REG8IN16(r, a) == d) |
| 704 | return; |
| 705 | |
| 706 | REG8IN16(r, a) = d; |
| 707 | p32x_m68k_poll_event(P32XF_68KCPOLL); |
| 708 | p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL, |
| 709 | sh2_cycles_done_m68k(sh2)); |
| 710 | comreg = 1 << (a & 0x0f) / 2; |
| 711 | Pico32x.comm_dirty_sh2 |= comreg; |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | elprintf(EL_32X|EL_ANOMALY, |
| 716 | "unhandled sysreg w8 [%02x] %02x @%08x", a, d, sh2_pc(sh2)); |
| 717 | } |
| 718 | |
| 719 | static void p32x_sh2reg_write16(u32 a, u32 d, SH2 *sh2) |
| 720 | { |
| 721 | a &= 0x3e; |
| 722 | |
| 723 | sh2->poll_addr = 0; |
| 724 | |
| 725 | // comm |
| 726 | if ((a & 0x30) == 0x20) { |
| 727 | int comreg; |
| 728 | if (Pico32x.regs[a / 2] == d) |
| 729 | return; |
| 730 | |
| 731 | Pico32x.regs[a / 2] = d; |
| 732 | p32x_m68k_poll_event(P32XF_68KCPOLL); |
| 733 | p32x_sh2_poll_event(sh2->other_sh2, SH2_STATE_CPOLL, |
| 734 | sh2_cycles_done_m68k(sh2)); |
| 735 | comreg = 1 << (a & 0x0f) / 2; |
| 736 | Pico32x.comm_dirty_sh2 |= comreg; |
| 737 | return; |
| 738 | } |
| 739 | // PWM |
| 740 | else if ((a & 0x30) == 0x30) { |
| 741 | p32x_pwm_write16(a, d, sh2, sh2_cycles_done_m68k(sh2)); |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | switch (a) { |
| 746 | case 0: // FM |
| 747 | Pico32x.regs[0] &= ~P32XS_FM; |
| 748 | Pico32x.regs[0] |= d & P32XS_FM; |
| 749 | break; |
| 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; |
| 763 | case 0x1c: |
| 764 | p32x_pwm_sync_to_sh2(sh2); |
| 765 | Pico32x.sh2irqi[sh2->is_slave] &= ~P32XI_PWM; |
| 766 | p32x_pwm_schedule_sh2(sh2); |
| 767 | goto irls; |
| 768 | } |
| 769 | |
| 770 | p32x_sh2reg_write8(a | 1, d, sh2); |
| 771 | return; |
| 772 | |
| 773 | irls: |
| 774 | p32x_update_irls(sh2, 0); |
| 775 | } |
| 776 | |
| 777 | // ------------------------------------------------------------------ |
| 778 | // 32x 68k handlers |
| 779 | |
| 780 | // after ADEN |
| 781 | static u32 PicoRead8_32x_on(u32 a) |
| 782 | { |
| 783 | u32 d = 0; |
| 784 | if ((a & 0xffc0) == 0x5100) { // a15100 |
| 785 | d = p32x_reg_read16(a); |
| 786 | goto out_16to8; |
| 787 | } |
| 788 | |
| 789 | if ((a & 0xfc00) != 0x5000) { |
| 790 | if (PicoAHW & PAHW_MCD) |
| 791 | return PicoRead8_mcd_io(a); |
| 792 | else |
| 793 | return PicoRead8_io(a); |
| 794 | } |
| 795 | |
| 796 | if ((a & 0xfff0) == 0x5180) { // a15180 |
| 797 | d = p32x_vdp_read16(a); |
| 798 | goto out_16to8; |
| 799 | } |
| 800 | |
| 801 | if ((a & 0xfe00) == 0x5200) { // a15200 |
| 802 | d = Pico32xMem->pal[(a & 0x1ff) / 2]; |
| 803 | goto out_16to8; |
| 804 | } |
| 805 | |
| 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 | |
| 814 | out_16to8: |
| 815 | if (a & 1) |
| 816 | d &= 0xff; |
| 817 | else |
| 818 | d >>= 8; |
| 819 | |
| 820 | out: |
| 821 | elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc); |
| 822 | return d; |
| 823 | } |
| 824 | |
| 825 | static u32 PicoRead16_32x_on(u32 a) |
| 826 | { |
| 827 | u32 d = 0; |
| 828 | if ((a & 0xffc0) == 0x5100) { // a15100 |
| 829 | d = p32x_reg_read16(a); |
| 830 | goto out; |
| 831 | } |
| 832 | |
| 833 | if ((a & 0xfc00) != 0x5000) { |
| 834 | if (PicoAHW & PAHW_MCD) |
| 835 | return PicoRead16_mcd_io(a); |
| 836 | else |
| 837 | return PicoRead16_io(a); |
| 838 | } |
| 839 | |
| 840 | if ((a & 0xfff0) == 0x5180) { // a15180 |
| 841 | d = p32x_vdp_read16(a); |
| 842 | goto out; |
| 843 | } |
| 844 | |
| 845 | if ((a & 0xfe00) == 0x5200) { // a15200 |
| 846 | d = Pico32xMem->pal[(a & 0x1ff) / 2]; |
| 847 | goto out; |
| 848 | } |
| 849 | |
| 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 | |
| 858 | out: |
| 859 | elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc); |
| 860 | return d; |
| 861 | } |
| 862 | |
| 863 | static void PicoWrite8_32x_on(u32 a, u32 d) |
| 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 | |
| 873 | if ((a & 0xfc00) != 0x5000) { |
| 874 | if (PicoAHW & PAHW_MCD) |
| 875 | PicoWrite8_mcd_io(a, d); |
| 876 | else |
| 877 | PicoWrite8_io(a, d); |
| 878 | if (a == 0xa130f1) |
| 879 | bank_switch(Pico32x.regs[4 / 2]); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (!(Pico32x.regs[0] & P32XS_FM)) { |
| 884 | if ((a & 0xfff0) == 0x5180) { // a15180 |
| 885 | p32x_vdp_write8(a, d); |
| 886 | return; |
| 887 | } |
| 888 | |
| 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 | } |
| 896 | } |
| 897 | |
| 898 | elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
| 899 | } |
| 900 | |
| 901 | static void PicoWrite16_32x_on(u32 a, u32 d) |
| 902 | { |
| 903 | if ((a & 0xfc00) == 0x5000) |
| 904 | elprintf(EL_32X, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
| 905 | |
| 906 | if ((a & 0xffc0) == 0x5100) { // a15100 |
| 907 | p32x_reg_write16(a, d); |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | if ((a & 0xfc00) != 0x5000) { |
| 912 | if (PicoAHW & PAHW_MCD) |
| 913 | PicoWrite16_mcd_io(a, d); |
| 914 | else |
| 915 | PicoWrite16_io(a, d); |
| 916 | if (a == 0xa130f0) |
| 917 | bank_switch(Pico32x.regs[4 / 2]); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | if (!(Pico32x.regs[0] & P32XS_FM)) { |
| 922 | if ((a & 0xfff0) == 0x5180) { // a15180 |
| 923 | p32x_vdp_write16(a, d, NULL); // FIXME? |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | if ((a & 0xfe00) == 0x5200) { // a15200 |
| 928 | Pico32xMem->pal[(a & 0x1ff) / 2] = d; |
| 929 | Pico32x.dirty_pal = 1; |
| 930 | return; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
| 935 | } |
| 936 | |
| 937 | // before ADEN |
| 938 | u32 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 | |
| 955 | out: |
| 956 | elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc); |
| 957 | return d; |
| 958 | } |
| 959 | |
| 960 | u32 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 | |
| 976 | out: |
| 977 | elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc); |
| 978 | return d; |
| 979 | } |
| 980 | |
| 981 | void 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 | |
| 1009 | void 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 | |
| 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 | |
| 1042 | static void m68k_write8_dram0_ow(u32 a, u32 d) |
| 1043 | { |
| 1044 | sh2_write8_dramN(0); |
| 1045 | } |
| 1046 | |
| 1047 | static void m68k_write8_dram1_ow(u32 a, u32 d) |
| 1048 | { |
| 1049 | sh2_write8_dramN(1); |
| 1050 | } |
| 1051 | |
| 1052 | #define sh2_write16_dramN(n) \ |
| 1053 | u16 *pd = &Pico32xMem->dram[n][(a & 0x1ffff) / 2]; \ |
| 1054 | if (!(a & 0x20000)) { \ |
| 1055 | *pd = d; \ |
| 1056 | return; \ |
| 1057 | } \ |
| 1058 | /* overwrite */ \ |
| 1059 | if (!(d & 0xff00)) d |= *pd & 0xff00; \ |
| 1060 | if (!(d & 0x00ff)) d |= *pd & 0x00ff; \ |
| 1061 | *pd = d; |
| 1062 | |
| 1063 | static void m68k_write16_dram0_ow(u32 a, u32 d) |
| 1064 | { |
| 1065 | sh2_write16_dramN(0); |
| 1066 | } |
| 1067 | |
| 1068 | static void m68k_write16_dram1_ow(u32 a, u32 d) |
| 1069 | { |
| 1070 | sh2_write16_dramN(1); |
| 1071 | } |
| 1072 | |
| 1073 | // ----------------------------------------------------------------- |
| 1074 | |
| 1075 | // hint vector is writeable |
| 1076 | static void PicoWrite8_hint(u32 a, u32 d) |
| 1077 | { |
| 1078 | if ((a & 0xfffc) == 0x0070) { |
| 1079 | Pico32xMem->m68k_rom[a ^ 1] = d; |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", |
| 1084 | a, d & 0xff, SekPc); |
| 1085 | } |
| 1086 | |
| 1087 | static 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 | |
| 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 |
| 1099 | static 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 | |
| 1107 | static 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 |
| 1116 | static u32 PicoRead8_bank(u32 a) |
| 1117 | { |
| 1118 | a = (Pico32x.regs[4 / 2] << 20) | (a & 0xfffff); |
| 1119 | return m68k_read8(a); |
| 1120 | } |
| 1121 | |
| 1122 | static u32 PicoRead16_bank(u32 a) |
| 1123 | { |
| 1124 | a = (Pico32x.regs[4 / 2] << 20) | (a & 0xfffff); |
| 1125 | return m68k_read16(a); |
| 1126 | } |
| 1127 | |
| 1128 | static 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 | |
| 1138 | static 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 | |
| 1148 | static 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); |
| 1152 | } |
| 1153 | |
| 1154 | static void bank_switch(int b) |
| 1155 | { |
| 1156 | unsigned int rs, bank; |
| 1157 | |
| 1158 | if (Pico.m.ncart_in) |
| 1159 | return; |
| 1160 | |
| 1161 | bank = b << 20; |
| 1162 | if ((Pico.m.sram_reg & SRR_MAPPED) && bank == Pico.sv.start) { |
| 1163 | bank_map_handler(); |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | if (bank >= Pico.romsize) { |
| 1168 | elprintf(EL_32X|EL_ANOMALY, "missing bank @ %06x", bank); |
| 1169 | bank_map_handler(); |
| 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); |
| 1182 | } |
| 1183 | |
| 1184 | // ----------------------------------------------------------------- |
| 1185 | // SH2 |
| 1186 | // ----------------------------------------------------------------- |
| 1187 | |
| 1188 | // read8 |
| 1189 | static u32 sh2_read8_unmapped(u32 a, SH2 *sh2) |
| 1190 | { |
| 1191 | elprintf_sh2(sh2, EL_32X, "unmapped r8 [%08x] %02x @%06x", |
| 1192 | a, 0, sh2_pc(sh2)); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static u32 sh2_read8_cs0(u32 a, SH2 *sh2) |
| 1197 | { |
| 1198 | u32 d = 0; |
| 1199 | |
| 1200 | sh2_burn_cycles(sh2, 1*2); |
| 1201 | |
| 1202 | // 0x3ffc0 is veridied |
| 1203 | if ((a & 0x3ffc0) == 0x4000) { |
| 1204 | d = p32x_sh2reg_read16(a, sh2); |
| 1205 | goto out_16to8; |
| 1206 | } |
| 1207 | |
| 1208 | if ((a & 0x3fff0) == 0x4100) { |
| 1209 | d = p32x_vdp_read16(a); |
| 1210 | sh2_poll_detect(sh2, a, SH2_STATE_VPOLL, 7); |
| 1211 | goto out_16to8; |
| 1212 | } |
| 1213 | |
| 1214 | // TODO: mirroring? |
| 1215 | if (!sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_m)) |
| 1216 | return Pico32xMem->sh2_rom_m.b[a ^ 1]; |
| 1217 | if (sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_s)) |
| 1218 | return Pico32xMem->sh2_rom_s.b[a ^ 1]; |
| 1219 | |
| 1220 | if ((a & 0x3fe00) == 0x4200) { |
| 1221 | d = Pico32xMem->pal[(a & 0x1ff) / 2]; |
| 1222 | goto out_16to8; |
| 1223 | } |
| 1224 | |
| 1225 | return sh2_read8_unmapped(a, sh2); |
| 1226 | |
| 1227 | out_16to8: |
| 1228 | if (a & 1) |
| 1229 | d &= 0xff; |
| 1230 | else |
| 1231 | d >>= 8; |
| 1232 | |
| 1233 | elprintf_sh2(sh2, EL_32X, "r8 [%08x] %02x @%06x", |
| 1234 | a, d, sh2_pc(sh2)); |
| 1235 | return d; |
| 1236 | } |
| 1237 | |
| 1238 | static u32 sh2_read8_da(u32 a, SH2 *sh2) |
| 1239 | { |
| 1240 | return sh2->data_array[(a & 0xfff) ^ 1]; |
| 1241 | } |
| 1242 | |
| 1243 | // read16 |
| 1244 | static u32 sh2_read16_unmapped(u32 a, SH2 *sh2) |
| 1245 | { |
| 1246 | elprintf_sh2(sh2, EL_32X, "unmapped r16 [%08x] %04x @%06x", |
| 1247 | a, 0, sh2_pc(sh2)); |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static u32 sh2_read16_cs0(u32 a, SH2 *sh2) |
| 1252 | { |
| 1253 | u32 d = 0; |
| 1254 | |
| 1255 | sh2_burn_cycles(sh2, 1*2); |
| 1256 | |
| 1257 | if ((a & 0x3ffc0) == 0x4000) { |
| 1258 | d = p32x_sh2reg_read16(a, sh2); |
| 1259 | if (!(EL_LOGMASK & EL_PWM) && (a & 0x30) == 0x30) // hide PWM |
| 1260 | return d; |
| 1261 | goto out; |
| 1262 | } |
| 1263 | |
| 1264 | if ((a & 0x3fff0) == 0x4100) { |
| 1265 | d = p32x_vdp_read16(a); |
| 1266 | sh2_poll_detect(sh2, a, SH2_STATE_VPOLL, 7); |
| 1267 | goto out; |
| 1268 | } |
| 1269 | |
| 1270 | if (!sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_m)) |
| 1271 | return Pico32xMem->sh2_rom_m.w[a / 2]; |
| 1272 | if (sh2->is_slave && a < sizeof(Pico32xMem->sh2_rom_s)) |
| 1273 | return Pico32xMem->sh2_rom_s.w[a / 2]; |
| 1274 | |
| 1275 | if ((a & 0x3fe00) == 0x4200) { |
| 1276 | d = Pico32xMem->pal[(a & 0x1ff) / 2]; |
| 1277 | goto out; |
| 1278 | } |
| 1279 | |
| 1280 | return sh2_read16_unmapped(a, sh2); |
| 1281 | |
| 1282 | out: |
| 1283 | elprintf_sh2(sh2, EL_32X, "r16 [%08x] %04x @%06x", |
| 1284 | a, d, sh2_pc(sh2)); |
| 1285 | return d; |
| 1286 | } |
| 1287 | |
| 1288 | static u32 sh2_read16_da(u32 a, SH2 *sh2) |
| 1289 | { |
| 1290 | return ((u16 *)sh2->data_array)[(a & 0xfff) / 2]; |
| 1291 | } |
| 1292 | |
| 1293 | // writes |
| 1294 | static void REGPARM(3) sh2_write_ignore(u32 a, u32 d, SH2 *sh2) |
| 1295 | { |
| 1296 | } |
| 1297 | |
| 1298 | // write8 |
| 1299 | static void REGPARM(3) sh2_write8_unmapped(u32 a, u32 d, SH2 *sh2) |
| 1300 | { |
| 1301 | elprintf_sh2(sh2, EL_32X, "unmapped w8 [%08x] %02x @%06x", |
| 1302 | a, d & 0xff, sh2_pc(sh2)); |
| 1303 | } |
| 1304 | |
| 1305 | static void REGPARM(3) sh2_write8_cs0(u32 a, u32 d, SH2 *sh2) |
| 1306 | { |
| 1307 | elprintf_sh2(sh2, EL_32X, "w8 [%08x] %02x @%06x", |
| 1308 | a, d & 0xff, sh2_pc(sh2)); |
| 1309 | |
| 1310 | if (Pico32x.regs[0] & P32XS_FM) { |
| 1311 | if ((a & 0x3fff0) == 0x4100) { |
| 1312 | sh2->poll_addr = 0; |
| 1313 | p32x_vdp_write8(a, d); |
| 1314 | return; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | if ((a & 0x3ffc0) == 0x4000) { |
| 1319 | p32x_sh2reg_write8(a, d, sh2); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | sh2_write8_unmapped(a, d, sh2); |
| 1324 | } |
| 1325 | |
| 1326 | static void REGPARM(3) sh2_write8_dram0(u32 a, u32 d, SH2 *sh2) |
| 1327 | { |
| 1328 | sh2_write8_dramN(0); |
| 1329 | } |
| 1330 | |
| 1331 | static void REGPARM(3) sh2_write8_dram1(u32 a, u32 d, SH2 *sh2) |
| 1332 | { |
| 1333 | sh2_write8_dramN(1); |
| 1334 | } |
| 1335 | |
| 1336 | static void REGPARM(3) sh2_write8_sdram(u32 a, u32 d, SH2 *sh2) |
| 1337 | { |
| 1338 | u32 a1 = a & 0x3ffff; |
| 1339 | #ifdef DRC_SH2 |
| 1340 | int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT]; |
| 1341 | if (t) |
| 1342 | sh2_drc_wcheck_ram(a, t, sh2->is_slave); |
| 1343 | #endif |
| 1344 | Pico32xMem->sdram[a1 ^ 1] = d; |
| 1345 | } |
| 1346 | |
| 1347 | static void REGPARM(3) sh2_write8_sdram_wt(u32 a, u32 d, SH2 *sh2) |
| 1348 | { |
| 1349 | // xmen sync hack.. |
| 1350 | if (a < 0x26000200) |
| 1351 | sh2_end_run(sh2, 32); |
| 1352 | |
| 1353 | sh2_write8_sdram(a, d, sh2); |
| 1354 | } |
| 1355 | |
| 1356 | static void REGPARM(3) sh2_write8_da(u32 a, u32 d, SH2 *sh2) |
| 1357 | { |
| 1358 | u32 a1 = a & 0xfff; |
| 1359 | #ifdef DRC_SH2 |
| 1360 | int id = sh2->is_slave; |
| 1361 | int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT]; |
| 1362 | if (t) |
| 1363 | sh2_drc_wcheck_da(a, t, id); |
| 1364 | #endif |
| 1365 | sh2->data_array[a1 ^ 1] = d; |
| 1366 | } |
| 1367 | |
| 1368 | // write16 |
| 1369 | static void REGPARM(3) sh2_write16_unmapped(u32 a, u32 d, SH2 *sh2) |
| 1370 | { |
| 1371 | elprintf_sh2(sh2, EL_32X, "unmapped w16 [%08x] %04x @%06x", |
| 1372 | a, d & 0xffff, sh2_pc(sh2)); |
| 1373 | } |
| 1374 | |
| 1375 | static void REGPARM(3) sh2_write16_cs0(u32 a, u32 d, SH2 *sh2) |
| 1376 | { |
| 1377 | if (((EL_LOGMASK & EL_PWM) || (a & 0x30) != 0x30)) // hide PWM |
| 1378 | elprintf_sh2(sh2, EL_32X, "w16 [%08x] %04x @%06x", |
| 1379 | a, d & 0xffff, sh2_pc(sh2)); |
| 1380 | |
| 1381 | if (Pico32x.regs[0] & P32XS_FM) { |
| 1382 | if ((a & 0x3fff0) == 0x4100) { |
| 1383 | sh2->poll_addr = 0; |
| 1384 | p32x_vdp_write16(a, d, sh2); |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| 1388 | if ((a & 0x3fe00) == 0x4200) { |
| 1389 | Pico32xMem->pal[(a & 0x1ff) / 2] = d; |
| 1390 | Pico32x.dirty_pal = 1; |
| 1391 | return; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if ((a & 0x3ffc0) == 0x4000) { |
| 1396 | p32x_sh2reg_write16(a, d, sh2); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | sh2_write16_unmapped(a, d, sh2); |
| 1401 | } |
| 1402 | |
| 1403 | static void REGPARM(3) sh2_write16_dram0(u32 a, u32 d, SH2 *sh2) |
| 1404 | { |
| 1405 | sh2_write16_dramN(0); |
| 1406 | } |
| 1407 | |
| 1408 | static void REGPARM(3) sh2_write16_dram1(u32 a, u32 d, SH2 *sh2) |
| 1409 | { |
| 1410 | sh2_write16_dramN(1); |
| 1411 | } |
| 1412 | |
| 1413 | static void REGPARM(3) sh2_write16_sdram(u32 a, u32 d, SH2 *sh2) |
| 1414 | { |
| 1415 | u32 a1 = a & 0x3ffff; |
| 1416 | #ifdef DRC_SH2 |
| 1417 | int t = Pico32xMem->drcblk_ram[a1 >> SH2_DRCBLK_RAM_SHIFT]; |
| 1418 | if (t) |
| 1419 | sh2_drc_wcheck_ram(a, t, sh2->is_slave); |
| 1420 | #endif |
| 1421 | ((u16 *)Pico32xMem->sdram)[a1 / 2] = d; |
| 1422 | } |
| 1423 | |
| 1424 | static void REGPARM(3) sh2_write16_da(u32 a, u32 d, SH2 *sh2) |
| 1425 | { |
| 1426 | u32 a1 = a & 0xfff; |
| 1427 | #ifdef DRC_SH2 |
| 1428 | int id = sh2->is_slave; |
| 1429 | int t = Pico32xMem->drcblk_da[id][a1 >> SH2_DRCBLK_DA_SHIFT]; |
| 1430 | if (t) |
| 1431 | sh2_drc_wcheck_da(a, t, id); |
| 1432 | #endif |
| 1433 | ((u16 *)sh2->data_array)[a1 / 2] = d; |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | typedef u32 (sh2_read_handler)(u32 a, SH2 *sh2); |
| 1438 | typedef void REGPARM(3) (sh2_write_handler)(u32 a, u32 d, SH2 *sh2); |
| 1439 | |
| 1440 | #define SH2MAP_ADDR2OFFS_R(a) \ |
| 1441 | ((u32)(a) >> SH2_READ_SHIFT) |
| 1442 | |
| 1443 | #define SH2MAP_ADDR2OFFS_W(a) \ |
| 1444 | ((u32)(a) >> SH2_WRITE_SHIFT) |
| 1445 | |
| 1446 | u32 REGPARM(2) p32x_sh2_read8(u32 a, SH2 *sh2) |
| 1447 | { |
| 1448 | const sh2_memmap *sh2_map = sh2->read8_map; |
| 1449 | uptr p; |
| 1450 | |
| 1451 | sh2_map += SH2MAP_ADDR2OFFS_R(a); |
| 1452 | p = sh2_map->addr; |
| 1453 | if (map_flag_set(p)) |
| 1454 | return ((sh2_read_handler *)(p << 1))(a, sh2); |
| 1455 | else |
| 1456 | return *(u8 *)((p << 1) + ((a & sh2_map->mask) ^ 1)); |
| 1457 | } |
| 1458 | |
| 1459 | u32 REGPARM(2) p32x_sh2_read16(u32 a, SH2 *sh2) |
| 1460 | { |
| 1461 | const sh2_memmap *sh2_map = sh2->read16_map; |
| 1462 | uptr p; |
| 1463 | |
| 1464 | sh2_map += SH2MAP_ADDR2OFFS_R(a); |
| 1465 | p = sh2_map->addr; |
| 1466 | if (map_flag_set(p)) |
| 1467 | return ((sh2_read_handler *)(p << 1))(a, sh2); |
| 1468 | else |
| 1469 | return *(u16 *)((p << 1) + ((a & sh2_map->mask) & ~1)); |
| 1470 | } |
| 1471 | |
| 1472 | u32 REGPARM(2) p32x_sh2_read32(u32 a, SH2 *sh2) |
| 1473 | { |
| 1474 | const sh2_memmap *sh2_map = sh2->read16_map; |
| 1475 | sh2_read_handler *handler; |
| 1476 | u32 offs; |
| 1477 | uptr p; |
| 1478 | |
| 1479 | offs = SH2MAP_ADDR2OFFS_R(a); |
| 1480 | sh2_map += offs; |
| 1481 | p = sh2_map->addr; |
| 1482 | if (!map_flag_set(p)) { |
| 1483 | // XXX: maybe 32bit access instead with ror? |
| 1484 | u16 *pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1)); |
| 1485 | return (pd[0] << 16) | pd[1]; |
| 1486 | } |
| 1487 | |
| 1488 | if (offs == SH2MAP_ADDR2OFFS_R(0xffffc000)) |
| 1489 | return sh2_peripheral_read32(a, sh2); |
| 1490 | |
| 1491 | handler = (sh2_read_handler *)(p << 1); |
| 1492 | return (handler(a, sh2) << 16) | handler(a + 2, sh2); |
| 1493 | } |
| 1494 | |
| 1495 | void REGPARM(3) p32x_sh2_write8(u32 a, u32 d, SH2 *sh2) |
| 1496 | { |
| 1497 | const void **sh2_wmap = sh2->write8_tab; |
| 1498 | sh2_write_handler *wh; |
| 1499 | |
| 1500 | wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)]; |
| 1501 | wh(a, d, sh2); |
| 1502 | } |
| 1503 | |
| 1504 | void REGPARM(3) p32x_sh2_write16(u32 a, u32 d, SH2 *sh2) |
| 1505 | { |
| 1506 | const void **sh2_wmap = sh2->write16_tab; |
| 1507 | sh2_write_handler *wh; |
| 1508 | |
| 1509 | wh = sh2_wmap[SH2MAP_ADDR2OFFS_W(a)]; |
| 1510 | wh(a, d, sh2); |
| 1511 | } |
| 1512 | |
| 1513 | void REGPARM(3) p32x_sh2_write32(u32 a, u32 d, SH2 *sh2) |
| 1514 | { |
| 1515 | const void **sh2_wmap = sh2->write16_tab; |
| 1516 | sh2_write_handler *wh; |
| 1517 | u32 offs; |
| 1518 | |
| 1519 | offs = SH2MAP_ADDR2OFFS_W(a); |
| 1520 | |
| 1521 | if (offs == SH2MAP_ADDR2OFFS_W(0xffffc000)) { |
| 1522 | sh2_peripheral_write32(a, d, sh2); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | wh = sh2_wmap[offs]; |
| 1527 | wh(a, d >> 16, sh2); |
| 1528 | wh(a + 2, d, sh2); |
| 1529 | } |
| 1530 | |
| 1531 | // ----------------------------------------------------------------- |
| 1532 | |
| 1533 | static void z80_md_bank_write_32x(unsigned int a, unsigned char d) |
| 1534 | { |
| 1535 | unsigned int addr68k; |
| 1536 | |
| 1537 | addr68k = Pico.m.z80_bank68k << 15; |
| 1538 | addr68k += a & 0x7fff; |
| 1539 | if ((addr68k & 0xfff000) == 0xa15000) |
| 1540 | Pico32x.emu_flags |= P32XF_Z80_32X_IO; |
| 1541 | |
| 1542 | elprintf(EL_Z80BNK, "z80->68k w8 [%06x] %02x", addr68k, d); |
| 1543 | m68k_write8(addr68k, d); |
| 1544 | } |
| 1545 | |
| 1546 | // ----------------------------------------------------------------- |
| 1547 | |
| 1548 | static const u16 msh2_code[] = { |
| 1549 | // trap instructions |
| 1550 | 0xaffe, // 200 bra <self> |
| 1551 | 0x0009, // 202 nop |
| 1552 | // have to wait a bit until m68k initial program finishes clearing stuff |
| 1553 | // to avoid races with game SH2 code, like in Tempo |
| 1554 | 0xd406, // 204 mov.l @(_m_ok,pc), r4 |
| 1555 | 0xc400, // 206 mov.b @(h'0,gbr),r0 |
| 1556 | 0xc801, // 208 tst #1, r0 |
| 1557 | 0x8b0f, // 20a bf cd_start |
| 1558 | 0xd105, // 20c mov.l @(_cnt,pc), r1 |
| 1559 | 0xd206, // 20e mov.l @(_start,pc), r2 |
| 1560 | 0x71ff, // 210 add #-1, r1 |
| 1561 | 0x4115, // 212 cmp/pl r1 |
| 1562 | 0x89fc, // 214 bt -2 |
| 1563 | 0x6043, // 216 mov r4, r0 |
| 1564 | 0xc208, // 218 mov.l r0, @(h'20,gbr) |
| 1565 | 0x6822, // 21a mov.l @r2, r8 |
| 1566 | 0x482b, // 21c jmp @r8 |
| 1567 | 0x0009, // 21e nop |
| 1568 | ('M'<<8)|'_', ('O'<<8)|'K', // 220 _m_ok |
| 1569 | 0x0001, 0x0000, // 224 _cnt |
| 1570 | 0x2200, 0x03e0, // master start pointer in ROM |
| 1571 | // cd_start: |
| 1572 | 0xd20d, // 22c mov.l @(__cd_,pc), r2 |
| 1573 | 0xc608, // 22e mov.l @(h'20,gbr), r0 |
| 1574 | 0x3200, // 230 cmp/eq r0, r2 |
| 1575 | 0x8bfc, // 232 bf #-2 |
| 1576 | 0xe000, // 234 mov #0, r0 |
| 1577 | 0xcf80, // 236 or.b #0x80,@(r0,gbr) |
| 1578 | 0xd80b, // 238 mov.l @(_start_cd,pc), r8 // 24000018 |
| 1579 | 0xd30c, // 23a mov.l @(_max_len,pc), r3 |
| 1580 | 0x5b84, // 23c mov.l @(h'10,r8), r11 // master vbr |
| 1581 | 0x5a82, // 23e mov.l @(8,r8), r10 // entry |
| 1582 | 0x5081, // 240 mov.l @(4,r8), r0 // len |
| 1583 | 0x5980, // 242 mov.l @(0,r8), r9 // dst |
| 1584 | 0x3036, // 244 cmp/hi r3,r0 |
| 1585 | 0x8b00, // 246 bf #1 |
| 1586 | 0x6033, // 248 mov r3,r0 |
| 1587 | 0x7820, // 24a add #0x20, r8 |
| 1588 | // ipl_copy: |
| 1589 | 0x6286, // 24c mov.l @r8+, r2 |
| 1590 | 0x2922, // 24e mov.l r2, @r9 |
| 1591 | 0x7904, // 250 add #4, r9 |
| 1592 | 0x70fc, // 252 add #-4, r0 |
| 1593 | 0x8800, // 254 cmp/eq #0, r0 |
| 1594 | 0x8bf9, // 256 bf #-5 |
| 1595 | // |
| 1596 | 0x4b2e, // 258 ldc r11, vbr |
| 1597 | 0x6043, // 25a mov r4, r0 // M_OK |
| 1598 | 0xc208, // 25c mov.l r0, @(h'20,gbr) |
| 1599 | 0x4a2b, // 25e jmp @r10 |
| 1600 | 0x0009, // 260 nop |
| 1601 | 0x0009, // 262 nop // pad |
| 1602 | ('_'<<8)|'C', ('D'<<8)|'_', // 264 __cd_ |
| 1603 | 0x2400, 0x0018, // 268 _start_cd |
| 1604 | 0x0001, 0xffe0, // 26c _max_len |
| 1605 | }; |
| 1606 | |
| 1607 | static const u16 ssh2_code[] = { |
| 1608 | 0xaffe, // 200 bra <self> |
| 1609 | 0x0009, // 202 nop |
| 1610 | // code to wait for master, in case authentic master BIOS is used |
| 1611 | 0xd106, // 204 mov.l @(_m_ok,pc), r1 |
| 1612 | 0xd208, // 206 mov.l @(_start,pc), r2 |
| 1613 | 0xc608, // 208 mov.l @(h'20,gbr), r0 |
| 1614 | 0x3100, // 20a cmp/eq r0, r1 |
| 1615 | 0x8bfc, // 20c bf #-2 |
| 1616 | 0xc400, // 20e mov.b @(h'0,gbr),r0 |
| 1617 | 0xc801, // 210 tst #1, r0 |
| 1618 | 0xd004, // 212 mov.l @(_s_ok,pc), r0 |
| 1619 | 0x8b0a, // 214 bf cd_start |
| 1620 | 0xc209, // 216 mov.l r0, @(h'24,gbr) |
| 1621 | 0x6822, // 218 mov.l @r2, r8 |
| 1622 | 0x482b, // 21a jmp @r8 |
| 1623 | 0x0009, // 21c nop |
| 1624 | 0x0009, // 21e nop |
| 1625 | ('M'<<8)|'_', ('O'<<8)|'K', // 220 |
| 1626 | ('S'<<8)|'_', ('O'<<8)|'K', // 224 |
| 1627 | 0x2200, 0x03e4, // slave start pointer in ROM |
| 1628 | // cd_start: |
| 1629 | 0xd803, // 22c mov.l @(_start_cd,pc), r8 // 24000018 |
| 1630 | 0x5b85, // 22e mov.l @(h'14,r8), r11 // slave vbr |
| 1631 | 0x5a83, // 230 mov.l @(h'0c,r8), r10 // entry |
| 1632 | 0x4b2e, // 232 ldc r11, vbr |
| 1633 | 0xc209, // 234 mov.l r0, @(h'24,gbr) // write S_OK |
| 1634 | 0x4a2b, // 236 jmp @r10 |
| 1635 | 0x0009, // 238 nop |
| 1636 | 0x0009, // 23a nop |
| 1637 | 0x2400, 0x0018, // 23c _start_cd |
| 1638 | }; |
| 1639 | |
| 1640 | #define HWSWAP(x) (((u16)(x) << 16) | ((x) >> 16)) |
| 1641 | static void get_bios(void) |
| 1642 | { |
| 1643 | u16 *ps; |
| 1644 | u32 *pl; |
| 1645 | int i; |
| 1646 | |
| 1647 | // M68K ROM |
| 1648 | if (p32x_bios_g != NULL) { |
| 1649 | elprintf(EL_STATUS|EL_32X, "32x: using supplied 68k BIOS"); |
| 1650 | Byteswap(Pico32xMem->m68k_rom, p32x_bios_g, sizeof(Pico32xMem->m68k_rom)); |
| 1651 | } |
| 1652 | else { |
| 1653 | // generate 68k ROM |
| 1654 | ps = (u16 *)Pico32xMem->m68k_rom; |
| 1655 | pl = (u32 *)ps; |
| 1656 | for (i = 1; i < 0xc0/4; i++) |
| 1657 | pl[i] = HWSWAP(0x880200 + (i - 1) * 6); |
| 1658 | |
| 1659 | // fill with nops |
| 1660 | for (i = 0xc0/2; i < 0x100/2; i++) |
| 1661 | ps[i] = 0x4e71; |
| 1662 | |
| 1663 | #if 0 |
| 1664 | ps[0xc0/2] = 0x46fc; |
| 1665 | ps[0xc2/2] = 0x2700; // move #0x2700,sr |
| 1666 | ps[0xfe/2] = 0x60fe; // jump to self |
| 1667 | #else |
| 1668 | ps[0xfe/2] = 0x4e75; // rts |
| 1669 | #endif |
| 1670 | } |
| 1671 | // fill remaining m68k_rom page with game ROM |
| 1672 | memcpy(Pico32xMem->m68k_rom_bank + sizeof(Pico32xMem->m68k_rom), |
| 1673 | Pico.rom + sizeof(Pico32xMem->m68k_rom), |
| 1674 | sizeof(Pico32xMem->m68k_rom_bank) - sizeof(Pico32xMem->m68k_rom)); |
| 1675 | |
| 1676 | // MSH2 |
| 1677 | if (p32x_bios_m != NULL) { |
| 1678 | elprintf(EL_STATUS|EL_32X, "32x: using supplied master SH2 BIOS"); |
| 1679 | Byteswap(&Pico32xMem->sh2_rom_m, p32x_bios_m, sizeof(Pico32xMem->sh2_rom_m)); |
| 1680 | } |
| 1681 | else { |
| 1682 | pl = (u32 *)&Pico32xMem->sh2_rom_m; |
| 1683 | |
| 1684 | // fill exception vector table to our trap address |
| 1685 | for (i = 0; i < 128; i++) |
| 1686 | pl[i] = HWSWAP(0x200); |
| 1687 | |
| 1688 | // start |
| 1689 | pl[0] = pl[2] = HWSWAP(0x204); |
| 1690 | // reset SP |
| 1691 | pl[1] = pl[3] = HWSWAP(0x6040000); |
| 1692 | |
| 1693 | // startup code |
| 1694 | memcpy(&Pico32xMem->sh2_rom_m.b[0x200], msh2_code, sizeof(msh2_code)); |
| 1695 | } |
| 1696 | |
| 1697 | // SSH2 |
| 1698 | if (p32x_bios_s != NULL) { |
| 1699 | elprintf(EL_STATUS|EL_32X, "32x: using supplied slave SH2 BIOS"); |
| 1700 | Byteswap(&Pico32xMem->sh2_rom_s, p32x_bios_s, sizeof(Pico32xMem->sh2_rom_s)); |
| 1701 | } |
| 1702 | else { |
| 1703 | pl = (u32 *)&Pico32xMem->sh2_rom_s; |
| 1704 | |
| 1705 | // fill exception vector table to our trap address |
| 1706 | for (i = 0; i < 128; i++) |
| 1707 | pl[i] = HWSWAP(0x200); |
| 1708 | |
| 1709 | // start |
| 1710 | pl[0] = pl[2] = HWSWAP(0x204); |
| 1711 | // reset SP |
| 1712 | pl[1] = pl[3] = HWSWAP(0x603f800); |
| 1713 | |
| 1714 | // startup code |
| 1715 | memcpy(&Pico32xMem->sh2_rom_s.b[0x200], ssh2_code, sizeof(ssh2_code)); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | #define MAP_MEMORY(m) ((uptr)(m) >> 1) |
| 1720 | #define MAP_HANDLER(h) ( ((uptr)(h) >> 1) | ((uptr)1 << (sizeof(uptr) * 8 - 1)) ) |
| 1721 | |
| 1722 | static sh2_memmap sh2_read8_map[0x80], sh2_read16_map[0x80]; |
| 1723 | // for writes we are using handlers only |
| 1724 | static sh2_write_handler *sh2_write8_map[0x80], *sh2_write16_map[0x80]; |
| 1725 | |
| 1726 | void Pico32xSwapDRAM(int b) |
| 1727 | { |
| 1728 | cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0); |
| 1729 | cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0); |
| 1730 | cpu68k_map_set(m68k_read8_map, 0x860000, 0x87ffff, Pico32xMem->dram[b], 0); |
| 1731 | cpu68k_map_set(m68k_read16_map, 0x860000, 0x87ffff, Pico32xMem->dram[b], 0); |
| 1732 | cpu68k_map_set(m68k_write8_map, 0x840000, 0x87ffff, |
| 1733 | b ? m68k_write8_dram1_ow : m68k_write8_dram0_ow, 1); |
| 1734 | cpu68k_map_set(m68k_write16_map, 0x840000, 0x87ffff, |
| 1735 | b ? m68k_write16_dram1_ow : m68k_write16_dram0_ow, 1); |
| 1736 | |
| 1737 | // SH2 |
| 1738 | sh2_read8_map[0x04/2].addr = sh2_read8_map[0x24/2].addr = |
| 1739 | sh2_read16_map[0x04/2].addr = sh2_read16_map[0x24/2].addr = MAP_MEMORY(Pico32xMem->dram[b]); |
| 1740 | |
| 1741 | sh2_write8_map[0x04/2] = sh2_write8_map[0x24/2] = b ? sh2_write8_dram1 : sh2_write8_dram0; |
| 1742 | sh2_write16_map[0x04/2] = sh2_write16_map[0x24/2] = b ? sh2_write16_dram1 : sh2_write16_dram0; |
| 1743 | } |
| 1744 | |
| 1745 | void PicoMemSetup32x(void) |
| 1746 | { |
| 1747 | unsigned int rs; |
| 1748 | int i; |
| 1749 | |
| 1750 | Pico32xMem = plat_mmap(0x06000000, sizeof(*Pico32xMem), 0, 0); |
| 1751 | if (Pico32xMem == NULL) { |
| 1752 | elprintf(EL_STATUS, "OOM"); |
| 1753 | return; |
| 1754 | } |
| 1755 | |
| 1756 | get_bios(); |
| 1757 | |
| 1758 | // cartridge area becomes unmapped |
| 1759 | // XXX: we take the easy way and don't unmap ROM, |
| 1760 | // so that we can avoid handling the RV bit. |
| 1761 | // m68k_map_unmap(0x000000, 0x3fffff); |
| 1762 | |
| 1763 | if (!Pico.m.ncart_in) { |
| 1764 | // MD ROM area |
| 1765 | rs = sizeof(Pico32xMem->m68k_rom_bank); |
| 1766 | cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom_bank, 0); |
| 1767 | cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom_bank, 0); |
| 1768 | cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify |
| 1769 | cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1); |
| 1770 | |
| 1771 | // 32X ROM (unbanked, XXX: consider mirroring?) |
| 1772 | rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
| 1773 | if (rs > 0x80000) |
| 1774 | rs = 0x80000; |
| 1775 | cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0); |
| 1776 | cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs - 1, Pico.rom, 0); |
| 1777 | cpu68k_map_set(m68k_write8_map, 0x880000, 0x880000 + rs - 1, PicoWrite8_cart, 1); |
| 1778 | cpu68k_map_set(m68k_write16_map, 0x880000, 0x880000 + rs - 1, PicoWrite16_cart, 1); |
| 1779 | #ifdef EMU_F68K |
| 1780 | // setup FAME fetchmap |
| 1781 | PicoCpuFM68k.Fetch[0] = (unsigned long)Pico32xMem->m68k_rom; |
| 1782 | for (rs = 0x88; rs < 0x90; rs++) |
| 1783 | PicoCpuFM68k.Fetch[rs] = (unsigned long)Pico.rom - 0x880000; |
| 1784 | #endif |
| 1785 | |
| 1786 | // 32X ROM (banked) |
| 1787 | bank_switch(0); |
| 1788 | cpu68k_map_set(m68k_write8_map, 0x900000, 0x9fffff, PicoWrite8_bank, 1); |
| 1789 | cpu68k_map_set(m68k_write16_map, 0x900000, 0x9fffff, PicoWrite16_bank, 1); |
| 1790 | } |
| 1791 | |
| 1792 | // SYS regs |
| 1793 | cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_32x_on, 1); |
| 1794 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_32x_on, 1); |
| 1795 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_32x_on, 1); |
| 1796 | cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_32x_on, 1); |
| 1797 | |
| 1798 | // SH2 maps: A31,A30,A29,CS1,CS0 |
| 1799 | // all unmapped by default |
| 1800 | for (i = 0; i < ARRAY_SIZE(sh2_read8_map); i++) { |
| 1801 | sh2_read8_map[i].addr = MAP_HANDLER(sh2_read8_unmapped); |
| 1802 | sh2_read16_map[i].addr = MAP_HANDLER(sh2_read16_unmapped); |
| 1803 | } |
| 1804 | |
| 1805 | for (i = 0; i < ARRAY_SIZE(sh2_write8_map); i++) { |
| 1806 | sh2_write8_map[i] = sh2_write8_unmapped; |
| 1807 | sh2_write16_map[i] = sh2_write16_unmapped; |
| 1808 | } |
| 1809 | |
| 1810 | // "purge area" |
| 1811 | for (i = 0x40; i <= 0x5f; i++) { |
| 1812 | sh2_write8_map[i >> 1] = |
| 1813 | sh2_write16_map[i >> 1] = sh2_write_ignore; |
| 1814 | } |
| 1815 | |
| 1816 | // CS0 |
| 1817 | sh2_read8_map[0x00/2].addr = sh2_read8_map[0x20/2].addr = MAP_HANDLER(sh2_read8_cs0); |
| 1818 | sh2_read16_map[0x00/2].addr = sh2_read16_map[0x20/2].addr = MAP_HANDLER(sh2_read16_cs0); |
| 1819 | sh2_write8_map[0x00/2] = sh2_write8_map[0x20/2] = sh2_write8_cs0; |
| 1820 | sh2_write16_map[0x00/2] = sh2_write16_map[0x20/2] = sh2_write16_cs0; |
| 1821 | // CS1 - ROM |
| 1822 | sh2_read8_map[0x02/2].addr = sh2_read8_map[0x22/2].addr = |
| 1823 | sh2_read16_map[0x02/2].addr = sh2_read16_map[0x22/2].addr = MAP_MEMORY(Pico.rom); |
| 1824 | sh2_read8_map[0x02/2].mask = sh2_read8_map[0x22/2].mask = |
| 1825 | sh2_read16_map[0x02/2].mask = sh2_read16_map[0x22/2].mask = 0x3fffff; // FIXME |
| 1826 | // CS2 - DRAM - done by Pico32xSwapDRAM() |
| 1827 | sh2_read8_map[0x04/2].mask = sh2_read8_map[0x24/2].mask = |
| 1828 | sh2_read16_map[0x04/2].mask = sh2_read16_map[0x24/2].mask = 0x01ffff; |
| 1829 | // CS3 - SDRAM |
| 1830 | sh2_read8_map[0x06/2].addr = sh2_read8_map[0x26/2].addr = |
| 1831 | sh2_read16_map[0x06/2].addr = sh2_read16_map[0x26/2].addr = MAP_MEMORY(Pico32xMem->sdram); |
| 1832 | sh2_write8_map[0x06/2] = sh2_write8_sdram; |
| 1833 | sh2_write8_map[0x26/2] = sh2_write8_sdram_wt; |
| 1834 | sh2_write16_map[0x06/2] = sh2_write16_map[0x26/2] = sh2_write16_sdram; |
| 1835 | sh2_read8_map[0x06/2].mask = sh2_read8_map[0x26/2].mask = |
| 1836 | sh2_read16_map[0x06/2].mask = sh2_read16_map[0x26/2].mask = 0x03ffff; |
| 1837 | // SH2 data array |
| 1838 | sh2_read8_map[0xc0/2].addr = MAP_HANDLER(sh2_read8_da); |
| 1839 | sh2_read16_map[0xc0/2].addr = MAP_HANDLER(sh2_read16_da); |
| 1840 | sh2_write8_map[0xc0/2] = sh2_write8_da; |
| 1841 | sh2_write16_map[0xc0/2] = sh2_write16_da; |
| 1842 | // SH2 IO |
| 1843 | sh2_read8_map[0xff/2].addr = MAP_HANDLER(sh2_peripheral_read8); |
| 1844 | sh2_read16_map[0xff/2].addr = MAP_HANDLER(sh2_peripheral_read16); |
| 1845 | sh2_write8_map[0xff/2] = sh2_peripheral_write8; |
| 1846 | sh2_write16_map[0xff/2] = sh2_peripheral_write16; |
| 1847 | |
| 1848 | // map DRAM area, both 68k and SH2 |
| 1849 | Pico32xSwapDRAM(1); |
| 1850 | |
| 1851 | msh2.read8_map = ssh2.read8_map = sh2_read8_map; |
| 1852 | msh2.read16_map = ssh2.read16_map = sh2_read16_map; |
| 1853 | msh2.write8_tab = ssh2.write8_tab = (const void **)(void *)sh2_write8_map; |
| 1854 | msh2.write16_tab = ssh2.write16_tab = (const void **)(void *)sh2_write16_map; |
| 1855 | |
| 1856 | sh2_drc_mem_setup(&msh2); |
| 1857 | sh2_drc_mem_setup(&ssh2); |
| 1858 | |
| 1859 | // z80 hack |
| 1860 | z80_map_set(z80_write_map, 0x8000, 0xffff, z80_md_bank_write_32x, 1); |
| 1861 | } |
| 1862 | |
| 1863 | void Pico32xMemStateLoaded(void) |
| 1864 | { |
| 1865 | bank_switch(Pico32x.regs[4 / 2]); |
| 1866 | Pico32xSwapDRAM((Pico32x.vdp_regs[0x0a / 2] & P32XV_FS) ^ P32XV_FS); |
| 1867 | memset(Pico32xMem->pwm, 0, sizeof(Pico32xMem->pwm)); |
| 1868 | Pico32x.dirty_pal = 1; |
| 1869 | |
| 1870 | Pico32x.emu_flags &= ~(P32XF_68KCPOLL | P32XF_68KVPOLL); |
| 1871 | memset(&m68k_poll, 0, sizeof(m68k_poll)); |
| 1872 | msh2.state = 0; |
| 1873 | msh2.poll_addr = msh2.poll_cycles = msh2.poll_cnt = 0; |
| 1874 | ssh2.state = 0; |
| 1875 | ssh2.poll_addr = ssh2.poll_cycles = ssh2.poll_cnt = 0; |
| 1876 | |
| 1877 | sh2_drc_flush_all(); |
| 1878 | } |
| 1879 | |
| 1880 | // vim:shiftwidth=2:ts=2:expandtab |