2 * common code for base/cd/32x
3 * (C) notaz, 2007-2009,2013
4 * (C) irixxxx, 2020-2024
6 * This work is licensed under the terms of MAME license.
7 * See COPYING file in the top-level directory.
10 #define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC
11 #define CYCLES_M68K_VINT_LAG 112
13 // pad delay (for 6 button pads)
14 #define PAD_DELAY() { \
15 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
16 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
21 #define CPUS_RUN(m68k_cycles) \
22 SekRunM68k(m68k_cycles)
25 // sync m68k to Pico.t.m68c_aim
26 static void SekExecM68k(int cyc_do)
28 Pico.t.m68c_cnt += cyc_do;
31 PicoCpuCM68k.cycles = cyc_do;
32 CycloneRun(&PicoCpuCM68k);
33 Pico.t.m68c_cnt -= PicoCpuCM68k.cycles;
34 #elif defined(EMU_M68K)
35 Pico.t.m68c_cnt += m68k_execute(cyc_do) - cyc_do;
36 #elif defined(EMU_F68K)
37 Pico.t.m68c_cnt += fm68k_emulate(&PicoCpuFM68k, cyc_do, 0) - cyc_do;
42 static int SekSyncM68k(int once)
47 pevt_log_m68k_o(EVT_RUN_START);
49 while ((cyc_do = Pico.t.m68c_aim - Pico.t.m68c_cnt) > 0) {
50 // the Z80 CPU is stealing some bus cycles from the 68K main CPU when
51 // accessing the main bus. Account for these by shortening the time
53 int z80_buscyc = Pico.t.z80_buscycles >> (~Pico.m.scanline & 1);
54 if (z80_buscyc <= cyc_do)
55 SekExecM68k(cyc_do - z80_buscyc);
58 Pico.t.m68c_cnt += z80_buscyc;
59 Pico.t.z80_buscycles -= z80_buscyc;
64 pevt_log_m68k_o(EVT_RUN_END);
67 return Pico.t.m68c_aim > Pico.t.m68c_cnt;
70 static __inline void SekAimM68k(int cyc, int mult)
72 // refresh slowdown, for cart: 2 cycles every 128 - make this 1 every 64,
73 // for RAM: seems to be 0-3 every 128. Carts usually run from the cart
74 // area, but MCD games only use RAM, hence a different multiplier is needed.
75 // NB must be quite accurate, so handle fractions as well (c/f OutRunners)
76 int delay = (Pico.t.refresh_delay += cyc*mult) >> 14;
77 Pico.t.m68c_cnt += delay;
78 Pico.t.refresh_delay -= delay << 14;
79 Pico.t.m68c_aim += cyc;
82 static __inline void SekRunM68k(int cyc)
84 // TODO 0x100 would be 2 cycles/128, moreover far too sensitive
85 SekAimM68k(cyc, 0x108); // OutRunners, testpico, VDPFIFOTesting
89 static void SyncCPUs(unsigned int cycles)
92 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80))
96 if (PicoIn.AHW & PAHW_MCD)
97 pcd_sync_s68k(cycles, 0);
100 p32x_sync_sh2s(cycles);
104 static void do_hint(struct PicoVideo *pv)
106 pv->pending_ints |= 0x10;
107 if (pv->reg[0] & 0x10) {
108 elprintf(EL_INTS, "hint: @ %06x [%u]", SekPc, SekCyclesDone());
109 if (SekIrqLevel < pv->hint_irq)
110 SekInterrupt(pv->hint_irq);
114 static void do_timing_hacks_end(struct PicoVideo *pv)
116 PicoVideoFIFOSync(CYCLES_M68K_LINE);
118 // need rather tight Z80 sync for emulation of main bus cycle stealing
119 if (Pico.m.scanline&1)
120 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80))
121 PicoSyncZ80(Pico.t.m68c_aim);
124 static void do_timing_hacks_start(struct PicoVideo *pv)
126 int cycles = PicoVideoFIFOHint();
128 SekCyclesBurn(cycles); // prolong cpu HOLD if necessary
129 // XXX how to handle Z80 bus cycle stealing during DMA correctly?
130 if ((Pico.t.z80_buscycles -= cycles) < 0)
131 Pico.t.z80_buscycles = 0;
132 Pico.t.m68c_aim += Pico.m.scanline&1; // add 1 every 2 lines for 488.5 cycles
135 static int PicoFrameHints(void)
137 struct PicoVideo *pv = &Pico.video;
138 int lines, y, lines_vis, skip;
139 int hint; // Hint counter
141 pevt_log_m68k_o(EVT_FRAME_START);
143 skip = PicoIn.skipFrame;
145 Pico.t.m68c_frame_start = Pico.t.m68c_aim;
150 // === active display ===
151 pv->status |= PVS_ACTIVE;
153 for (y = 0; y < 240; y++)
155 if (y == 224 && !(pv->reg[1] & 8))
159 pv->v_counter = PicoVideoGetV(y, 0);
166 hint = pv->reg[10]; // Reload H-Int counter
170 // decide if we draw this line
171 if (unlikely(PicoIn.opt & POPT_ALT_RENDERER) && !skip)
173 // find the right moment for frame renderer, when display is no longer blanked
174 if ((pv->reg[1]&0x40) || y > 100) {
175 if (Pico.est.rendstatus & PDRAW_SYNC_NEEDED)
177 #ifdef DRAW_FINISH_FUNC
180 Pico.est.rendstatus &= ~PDRAW_SYNC_NEEDED;
186 Pico.t.m68c_line_start = Pico.t.m68c_aim;
187 do_timing_hacks_start(pv);
188 CPUS_RUN(CYCLES_M68K_LINE);
189 do_timing_hacks_end(pv);
191 if (PicoLineHook) PicoLineHook();
192 pevt_log_m68k_o(EVT_NEXT_LINE);
195 SyncCPUs(Pico.t.m68c_aim);
199 if (Pico.est.DrawScanline < y)
201 #ifdef DRAW_FINISH_FUNC
204 Pico.est.rendstatus &= ~PDRAW_SYNC_NEEDED;
210 // === VBLANK, 1st line ===
211 lines_vis = (pv->reg[1] & 8) ? 240 : 224;
213 pv->status &= ~PVS_ACTIVE;
215 pv->v_counter = PicoVideoGetV(y, 0);
217 memcpy(PicoIn.padInt, PicoIn.pad, sizeof(PicoIn.padInt));
220 // Last H-Int (normally):
223 hint = pv->reg[10]; // Reload H-Int counter
227 pv->status |= SR_VB | PVS_VB2; // go into vblank
232 // the following SekRun is there for several reasons:
233 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
234 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
235 // also delay between last H-int and V-int (Golden Axe 3)
236 Pico.t.m68c_line_start = Pico.t.m68c_aim;
237 PicoVideoFIFOMode(pv->reg[1]&0x40, pv->reg[12]&1);
238 do_timing_hacks_start(pv);
239 CPUS_RUN(CYCLES_M68K_VINT_LAG);
241 SyncCPUs(Pico.t.m68c_aim);
244 pv->pending_ints |= 0x20;
246 if (pv->reg[1] & 0x20) {
247 if (Pico.t.m68c_cnt - Pico.t.m68c_aim < 60) // CPU blocked?
248 SekExecM68k(11); // HACK
249 elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone());
253 // assert Z80 interrupt for one scanline even in busrq hold (Teddy Blues)
254 if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) {
255 elprintf(EL_INTS, "zint");
260 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG);
261 do_timing_hacks_end(pv);
263 if (PicoLineHook) PicoLineHook();
264 pevt_log_m68k_o(EVT_NEXT_LINE);
266 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80))
267 PicoSyncZ80(Pico.t.m68c_aim);
271 lines = Pico.m.pal ? 313 : 262;
272 for (y++; y < lines - 1; y++)
275 pv->v_counter = PicoVideoGetV(y, 1);
279 if (unlikely(pv->status & PVS_ACTIVE) && --hint < 0)
281 hint = pv->reg[10]; // Reload H-Int counter
286 Pico.t.m68c_line_start = Pico.t.m68c_aim;
287 do_timing_hacks_start(pv);
288 CPUS_RUN(CYCLES_M68K_LINE);
289 do_timing_hacks_end(pv);
291 if (PicoLineHook) PicoLineHook();
292 pevt_log_m68k_o(EVT_NEXT_LINE);
295 if (unlikely(PicoIn.overclockM68k)) {
296 unsigned int l = PicoIn.overclockM68k * lines / 100;
298 Pico.t.m68c_cnt -= CYCLES_M68K_LINE;
299 do_timing_hacks_start(pv);
301 do_timing_hacks_end(pv);
305 // === VBLANK last line ===
306 pv->status &= ~(SR_VB | PVS_VB2);
307 pv->status |= ((pv->reg[1] >> 3) ^ SR_VB) & SR_VB; // forced blanking
310 Pico.m.scanline = y++;
311 pv->v_counter = 0xff;
315 if (unlikely(pv->status & PVS_ACTIVE)) {
317 hint = pv->reg[10]; // Reload H-Int counter
325 Pico.t.m68c_line_start = Pico.t.m68c_aim;
326 PicoVideoFIFOMode(pv->reg[1]&0x40, pv->reg[12]&1);
327 do_timing_hacks_start(pv);
328 CPUS_RUN(CYCLES_M68K_LINE);
329 do_timing_hacks_end(pv);
331 if (PicoLineHook) PicoLineHook();
332 pevt_log_m68k_o(EVT_NEXT_LINE);
334 SyncCPUs(Pico.t.m68c_aim);
339 // get samples from sound chips
342 timers_cycle(cycles_68k_to_z80(Pico.t.m68c_aim - Pico.t.m68c_frame_start));
353 // vim:shiftwidth=2:ts=2:expandtab