2 * common code for base/cd/32x
3 * (C) notaz, 2007-2009,2013
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC
10 #define CYCLES_M68K_VINT_LAG 68
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; \
20 #define CPUS_RUN(m68k_cycles) \
21 SekRunM68k(m68k_cycles)
24 // sync m68k to Pico.t.m68c_aim
25 static void SekSyncM68k(void)
29 pevt_log_m68k_o(EVT_RUN_START);
31 while ((cyc_do = Pico.t.m68c_aim - Pico.t.m68c_cnt) > 0) {
32 Pico.t.m68c_cnt += cyc_do;
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(cyc_do, 0) - cyc_do;
48 pevt_log_m68k_o(EVT_RUN_END);
52 static inline void SekRunM68k(int cyc)
54 Pico.t.m68c_aim += cyc;
55 cyc = Pico.t.m68c_aim - Pico.t.m68c_cnt;
58 Pico.t.m68c_cnt += cyc >> 6; // refresh slowdowns
62 static void do_hint(struct PicoVideo *pv)
64 pv->pending_ints |= 0x10;
65 if (pv->reg[0] & 0x10) {
66 elprintf(EL_INTS, "hint: @ %06x [%u]", SekPc, SekCyclesDone());
71 static void do_timing_hacks_as(struct PicoVideo *pv, int vdp_slots)
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)
79 SekCyclesBurn(CheckDMA());
82 static void do_timing_hacks_vb(void)
85 SekCyclesBurn(CheckDMA());
88 static int PicoFrameHints(void)
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;
96 int hint; // Hint counter
98 pevt_log_m68k_o(EVT_FRAME_START);
100 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (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.
104 #ifdef DRAW_FINISH_FUNC
109 else skip=PicoSkipFrame;
111 Pico.t.m68c_frame_start = SekCyclesDone();
112 pv->v_counter = Pico.m.scanline = 0;
116 // Load H-Int counter
117 hint = (pv->status & PVS_ACTIVE) ? pv->hint_cnt : pv->reg[10];
119 pv->status |= PVS_ACTIVE;
123 pv->v_counter = Pico.m.scanline = y;
124 if ((pv->reg[12]&6) == 6) { // interlace mode 2
126 pv->v_counter |= pv->v_counter >> 8;
127 pv->v_counter &= 0xff;
130 if ((y == 224 && !(pv->reg[1] & 8)) || y == 240)
138 hint = pv->reg[10]; // Reload H-Int counter
142 // decide if we draw this line
143 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
145 // find the right moment for frame renderer, when display is no longer blanked
146 if ((pv->reg[1]&0x40) || y > 100) {
148 #ifdef DRAW_FINISH_FUNC
155 // get samples from sound chips
156 if ((y == 224 || y == line_sample) && PsndOut)
158 cycles = SekCyclesDone();
160 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
163 if (PicoAHW & PAHW_MCD)
164 pcd_sync_s68k(cycles, 0);
167 p32x_sync_sh2s(cycles);
173 Pico.t.m68c_line_start = SekCyclesDone();
174 do_timing_hacks_as(pv, vdp_slots);
175 CPUS_RUN(CYCLES_M68K_LINE);
177 if (PicoLineHook) PicoLineHook();
178 pevt_log_m68k_o(EVT_NEXT_LINE);
181 lines_vis = (pv->reg[1] & 8) ? 240 : 224;
183 pv->status &= ~PVS_ACTIVE;
187 if (Pico.est.DrawScanline < y)
188 PicoDrawSync(y - 1, 0);
189 #ifdef DRAW_FINISH_FUNC
196 Pico.video.status |= SR_EMPT;
198 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
201 // Last H-Int (normally):
204 hint = pv->reg[10]; // Reload H-Int counter
208 pv->status |= SR_VB; // go into vblank
209 pv->pending_ints |= 0x20;
211 // the following SekRun is there for several reasons:
212 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
213 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
214 // also delay between last H-int and V-int (Golden Axe 3)
215 Pico.t.m68c_line_start = SekCyclesDone();
216 do_timing_hacks_vb();
217 CPUS_RUN(CYCLES_M68K_VINT_LAG);
219 if (pv->reg[1] & 0x20) {
220 elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone());
224 cycles = SekCyclesDone();
225 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
227 elprintf(EL_INTS, "zint");
232 if (PicoAHW & PAHW_MCD)
233 pcd_sync_s68k(cycles, 0);
236 p32x_sync_sh2s(cycles);
240 // get samples from sound chips
241 if (y == 224 && PsndOut)
245 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG);
247 if (PicoLineHook) PicoLineHook();
248 pevt_log_m68k_o(EVT_NEXT_LINE);
261 for (y++; y < lines - 1; y++)
263 pv->v_counter = Pico.m.scanline = y;
265 pv->v_counter -= vcnt_adj;
266 if ((pv->reg[12]&6) == 6)
267 pv->v_counter = (pv->v_counter << 1) | 1;
268 pv->v_counter &= 0xff;
272 if ((pv->status & PVS_ACTIVE) && --hint < 0)
274 hint = pv->reg[10]; // Reload H-Int counter
279 Pico.t.m68c_line_start = SekCyclesDone();
280 do_timing_hacks_vb();
281 CPUS_RUN(CYCLES_M68K_LINE);
283 if (PicoLineHook) PicoLineHook();
284 pevt_log_m68k_o(EVT_NEXT_LINE);
287 pv->status &= ~SR_VB;
291 pv->v_counter = 0xff;
296 if ((pv->status & PVS_ACTIVE) && --hint < 0)
298 hint = pv->reg[10]; // Reload H-Int counter
303 Pico.t.m68c_line_start = SekCyclesDone();
304 do_timing_hacks_as(pv, vdp_slots);
305 CPUS_RUN(CYCLES_M68K_LINE);
307 if (PicoLineHook) PicoLineHook();
308 pevt_log_m68k_o(EVT_NEXT_LINE);
311 cycles = SekCyclesDone();
312 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
314 if (PsndOut && ym2612.dacen && PsndDacLine < lines)
315 PsndDoDAC(lines - 1);
316 if (PsndOut && PsndPsgLine < lines)
317 PsndDoPSG(lines - 1);
320 if (PicoAHW & PAHW_MCD)
321 pcd_sync_s68k(cycles, 0);
324 p32x_sync_sh2s(cycles);
336 // vim:shiftwidth=2:ts=2:expandtab