| 1 | /* |
| 2 | * common code for base/cd/32x |
| 3 | * (C) notaz, 2007-2009,2013 |
| 4 | * |
| 5 | * This work is licensed under the terms of MAME license. |
| 6 | * See COPYING file in the top-level directory. |
| 7 | */ |
| 8 | |
| 9 | #define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC |
| 10 | #define CYCLES_M68K_VINT_LAG 112 |
| 11 | |
| 12 | // pad delay (for 6 button pads) |
| 13 | #define PAD_DELAY() { \ |
| 14 | if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \ |
| 15 | if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \ |
| 16 | } |
| 17 | |
| 18 | // CPUS_RUN |
| 19 | #ifndef CPUS_RUN |
| 20 | #define CPUS_RUN(m68k_cycles) \ |
| 21 | SekRunM68k(m68k_cycles) |
| 22 | #endif |
| 23 | |
| 24 | // sync m68k to Pico.t.m68c_aim |
| 25 | static void SekSyncM68k(void) |
| 26 | { |
| 27 | int cyc_do; |
| 28 | pprof_start(m68k); |
| 29 | pevt_log_m68k_o(EVT_RUN_START); |
| 30 | |
| 31 | while ((cyc_do = Pico.t.m68c_aim - Pico.t.m68c_cnt) > 0) { |
| 32 | Pico.t.m68c_cnt += cyc_do; |
| 33 | |
| 34 | #if defined(EMU_C68K) |
| 35 | PicoCpuCM68k.cycles = cyc_do; |
| 36 | CycloneRun(&PicoCpuCM68k); |
| 37 | Pico.t.m68c_cnt -= PicoCpuCM68k.cycles; |
| 38 | #elif defined(EMU_M68K) |
| 39 | Pico.t.m68c_cnt += m68k_execute(cyc_do) - cyc_do; |
| 40 | #elif defined(EMU_F68K) |
| 41 | Pico.t.m68c_cnt += fm68k_emulate(&PicoCpuFM68k, cyc_do, 0) - cyc_do; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | SekCyclesLeft = 0; |
| 46 | |
| 47 | SekTrace(0); |
| 48 | pevt_log_m68k_o(EVT_RUN_END); |
| 49 | pprof_end(m68k); |
| 50 | } |
| 51 | |
| 52 | static __inline void SekRunM68k(int cyc) |
| 53 | { |
| 54 | Pico.t.m68c_aim += cyc; |
| 55 | cyc = Pico.t.m68c_aim - Pico.t.m68c_cnt; |
| 56 | if (cyc <= 0) |
| 57 | return; |
| 58 | Pico.t.m68c_cnt += cyc >> 6; // refresh slowdowns |
| 59 | SekSyncM68k(); |
| 60 | } |
| 61 | |
| 62 | static void do_hint(struct PicoVideo *pv) |
| 63 | { |
| 64 | pv->pending_ints |= 0x10; |
| 65 | if (pv->reg[0] & 0x10) { |
| 66 | elprintf(EL_INTS, "hint: @ %06x [%u]", SekPc, SekCyclesDone()); |
| 67 | SekInterrupt(4); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void do_timing_hacks_as(struct PicoVideo *pv, int vdp_slots) |
| 72 | { |
| 73 | pv->lwrite_cnt += vdp_slots - Pico.m.dma_xfers * 2; // wrong *2 |
| 74 | if (pv->lwrite_cnt > vdp_slots) |
| 75 | pv->lwrite_cnt = vdp_slots; |
| 76 | else if (pv->lwrite_cnt < 0) |
| 77 | pv->lwrite_cnt = 0; |
| 78 | if (Pico.m.dma_xfers) |
| 79 | SekCyclesBurn(CheckDMA()); |
| 80 | } |
| 81 | |
| 82 | static void do_timing_hacks_vb(void) |
| 83 | { |
| 84 | if (unlikely(Pico.m.dma_xfers)) |
| 85 | SekCyclesBurn(CheckDMA()); |
| 86 | } |
| 87 | |
| 88 | static int PicoFrameHints(void) |
| 89 | { |
| 90 | struct PicoVideo *pv = &Pico.video; |
| 91 | int line_sample = Pico.m.pal ? 68 : 93; |
| 92 | int vdp_slots = (Pico.video.reg[12] & 1) ? 18 : 16; |
| 93 | int lines, y, lines_vis, skip; |
| 94 | int vcnt_wrap, vcnt_adj; |
| 95 | unsigned int cycles; |
| 96 | int hint; // Hint counter |
| 97 | |
| 98 | pevt_log_m68k_o(EVT_FRAME_START); |
| 99 | |
| 100 | if ((PicoIn.opt&POPT_ALT_RENDERER) && !PicoIn.skipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled |
| 101 | // draw a frame just after vblank in alternative render mode |
| 102 | // yes, this will cause 1 frame lag, but this is inaccurate mode anyway. |
| 103 | PicoFrameFull(); |
| 104 | #ifdef DRAW_FINISH_FUNC |
| 105 | DRAW_FINISH_FUNC(); |
| 106 | #endif |
| 107 | skip = 1; |
| 108 | } |
| 109 | else skip=PicoIn.skipFrame; |
| 110 | |
| 111 | Pico.t.m68c_frame_start = Pico.t.m68c_aim; |
| 112 | pv->v_counter = Pico.m.scanline = 0; |
| 113 | z80_resetCycles(); |
| 114 | PsndStartFrame(); |
| 115 | |
| 116 | // Load H-Int counter |
| 117 | hint = (pv->status & PVS_ACTIVE) ? pv->hint_cnt : pv->reg[10]; |
| 118 | |
| 119 | pv->status |= PVS_ACTIVE; |
| 120 | |
| 121 | for (y = 0; ; y++) |
| 122 | { |
| 123 | pv->v_counter = Pico.m.scanline = y; |
| 124 | if ((pv->reg[12]&6) == 6) { // interlace mode 2 |
| 125 | pv->v_counter <<= 1; |
| 126 | pv->v_counter |= pv->v_counter >> 8; |
| 127 | pv->v_counter &= 0xff; |
| 128 | } |
| 129 | |
| 130 | if ((y == 224 && !(pv->reg[1] & 8)) || y == 240) |
| 131 | break; |
| 132 | |
| 133 | PAD_DELAY(); |
| 134 | |
| 135 | // H-Interrupts: |
| 136 | if (--hint < 0) |
| 137 | { |
| 138 | hint = pv->reg[10]; // Reload H-Int counter |
| 139 | do_hint(pv); |
| 140 | } |
| 141 | |
| 142 | // decide if we draw this line |
| 143 | if (!skip && (PicoIn.opt & POPT_ALT_RENDERER)) |
| 144 | { |
| 145 | // find the right moment for frame renderer, when display is no longer blanked |
| 146 | if ((pv->reg[1]&0x40) || y > 100) { |
| 147 | PicoFrameFull(); |
| 148 | #ifdef DRAW_FINISH_FUNC |
| 149 | DRAW_FINISH_FUNC(); |
| 150 | #endif |
| 151 | skip = 1; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // get samples from sound chips |
| 156 | if ((y == 224 || y == line_sample) && PicoIn.sndOut) |
| 157 | { |
| 158 | cycles = SekCyclesDone(); |
| 159 | |
| 160 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) |
| 161 | PicoSyncZ80(cycles); |
| 162 | #ifdef PICO_CD |
| 163 | if (PicoIn.AHW & PAHW_MCD) |
| 164 | pcd_sync_s68k(cycles, 0); |
| 165 | #endif |
| 166 | #ifdef PICO_32X |
| 167 | p32x_sync_sh2s(cycles); |
| 168 | #endif |
| 169 | PsndGetSamples(y); |
| 170 | } |
| 171 | |
| 172 | // Run scanline: |
| 173 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
| 174 | do_timing_hacks_as(pv, vdp_slots); |
| 175 | CPUS_RUN(CYCLES_M68K_LINE); |
| 176 | |
| 177 | if (PicoLineHook) PicoLineHook(); |
| 178 | pevt_log_m68k_o(EVT_NEXT_LINE); |
| 179 | } |
| 180 | |
| 181 | lines_vis = (pv->reg[1] & 8) ? 240 : 224; |
| 182 | if (y == lines_vis) |
| 183 | pv->status &= ~PVS_ACTIVE; |
| 184 | |
| 185 | if (!skip) |
| 186 | { |
| 187 | if (Pico.est.DrawScanline < y) |
| 188 | PicoDrawSync(y - 1, 0); |
| 189 | #ifdef DRAW_FINISH_FUNC |
| 190 | DRAW_FINISH_FUNC(); |
| 191 | #endif |
| 192 | } |
| 193 | |
| 194 | // VDP FIFO |
| 195 | pv->lwrite_cnt = 0; |
| 196 | Pico.video.status |= SR_EMPT; |
| 197 | |
| 198 | memcpy(PicoIn.padInt, PicoIn.pad, sizeof(PicoIn.padInt)); |
| 199 | PAD_DELAY(); |
| 200 | |
| 201 | // Last H-Int (normally): |
| 202 | if (--hint < 0) |
| 203 | { |
| 204 | hint = pv->reg[10]; // Reload H-Int counter |
| 205 | do_hint(pv); |
| 206 | } |
| 207 | |
| 208 | pv->status |= SR_VB | PVS_VB2; // go into vblank |
| 209 | |
| 210 | // the following SekRun is there for several reasons: |
| 211 | // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga) |
| 212 | // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants) |
| 213 | // also delay between last H-int and V-int (Golden Axe 3) |
| 214 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
| 215 | do_timing_hacks_vb(); |
| 216 | CPUS_RUN(CYCLES_M68K_VINT_LAG); |
| 217 | |
| 218 | pv->status |= SR_F; |
| 219 | pv->pending_ints |= 0x20; |
| 220 | if (pv->reg[1] & 0x20) { |
| 221 | Pico.t.m68c_aim = Pico.t.m68c_cnt + 11; // HACK |
| 222 | SekSyncM68k(); |
| 223 | elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone()); |
| 224 | SekInterrupt(6); |
| 225 | } |
| 226 | |
| 227 | cycles = SekCyclesDone(); |
| 228 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) { |
| 229 | PicoSyncZ80(cycles); |
| 230 | elprintf(EL_INTS, "zint"); |
| 231 | z80_int(); |
| 232 | } |
| 233 | |
| 234 | #ifdef PICO_CD |
| 235 | if (PicoIn.AHW & PAHW_MCD) |
| 236 | pcd_sync_s68k(cycles, 0); |
| 237 | #endif |
| 238 | #ifdef PICO_32X |
| 239 | p32x_sync_sh2s(cycles); |
| 240 | p32x_start_blank(); |
| 241 | #endif |
| 242 | |
| 243 | // get samples from sound chips |
| 244 | if (y == 224 && PicoIn.sndOut) |
| 245 | PsndGetSamples(y); |
| 246 | |
| 247 | // Run scanline: |
| 248 | CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG); |
| 249 | |
| 250 | if (PicoLineHook) PicoLineHook(); |
| 251 | pevt_log_m68k_o(EVT_NEXT_LINE); |
| 252 | |
| 253 | if (Pico.m.pal) { |
| 254 | lines = 313; |
| 255 | vcnt_wrap = 0x103; |
| 256 | vcnt_adj = 57; |
| 257 | } |
| 258 | else { |
| 259 | lines = 262; |
| 260 | vcnt_wrap = 0xEB; |
| 261 | vcnt_adj = 6; |
| 262 | } |
| 263 | |
| 264 | for (y++; y < lines - 1; y++) |
| 265 | { |
| 266 | pv->v_counter = Pico.m.scanline = y; |
| 267 | if (y >= vcnt_wrap) |
| 268 | pv->v_counter -= vcnt_adj; |
| 269 | if ((pv->reg[12]&6) == 6) |
| 270 | pv->v_counter = (pv->v_counter << 1) | 1; |
| 271 | pv->v_counter &= 0xff; |
| 272 | |
| 273 | PAD_DELAY(); |
| 274 | |
| 275 | if (unlikely(pv->status & PVS_ACTIVE) && --hint < 0) |
| 276 | { |
| 277 | hint = pv->reg[10]; // Reload H-Int counter |
| 278 | do_hint(pv); |
| 279 | } |
| 280 | |
| 281 | // Run scanline: |
| 282 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
| 283 | do_timing_hacks_vb(); |
| 284 | CPUS_RUN(CYCLES_M68K_LINE); |
| 285 | |
| 286 | if (PicoLineHook) PicoLineHook(); |
| 287 | pevt_log_m68k_o(EVT_NEXT_LINE); |
| 288 | } |
| 289 | |
| 290 | if (unlikely(PicoIn.overclockM68k)) { |
| 291 | unsigned int l = PicoIn.overclockM68k * lines / 100; |
| 292 | while (l-- > 0) { |
| 293 | Pico.t.m68c_cnt -= CYCLES_M68K_LINE; |
| 294 | do_timing_hacks_vb(); |
| 295 | SekSyncM68k(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | pv->status &= ~(SR_VB | PVS_VB2); |
| 300 | pv->status |= ((pv->reg[1] >> 3) ^ SR_VB) & SR_VB; // forced blanking |
| 301 | |
| 302 | // last scanline |
| 303 | Pico.m.scanline = y; |
| 304 | pv->v_counter = 0xff; |
| 305 | pv->lwrite_cnt = 0; |
| 306 | |
| 307 | PAD_DELAY(); |
| 308 | |
| 309 | if ((pv->status & PVS_ACTIVE) && --hint < 0) |
| 310 | { |
| 311 | hint = pv->reg[10]; // Reload H-Int counter |
| 312 | do_hint(pv); |
| 313 | } |
| 314 | |
| 315 | // Run scanline: |
| 316 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
| 317 | do_timing_hacks_as(pv, vdp_slots); |
| 318 | CPUS_RUN(CYCLES_M68K_LINE); |
| 319 | |
| 320 | if (PicoLineHook) PicoLineHook(); |
| 321 | pevt_log_m68k_o(EVT_NEXT_LINE); |
| 322 | |
| 323 | // sync cpus |
| 324 | cycles = SekCyclesDone(); |
| 325 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) |
| 326 | PicoSyncZ80(cycles); |
| 327 | if (PicoIn.sndOut && ym2612.dacen && Pico.snd.dac_line < lines) |
| 328 | PsndDoDAC(lines - 1); |
| 329 | if (PicoIn.sndOut && Pico.snd.psg_line < lines) |
| 330 | PsndDoPSG(lines - 1); |
| 331 | |
| 332 | #ifdef PICO_CD |
| 333 | if (PicoIn.AHW & PAHW_MCD) |
| 334 | pcd_sync_s68k(cycles, 0); |
| 335 | #endif |
| 336 | #ifdef PICO_32X |
| 337 | p32x_sync_sh2s(cycles); |
| 338 | #endif |
| 339 | timers_cycle(); |
| 340 | |
| 341 | pv->hint_cnt = hint; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | #undef PAD_DELAY |
| 347 | #undef CPUS_RUN |
| 348 | |
| 349 | // vim:shiftwidth=2:ts=2:expandtab |