adjust z80 timing a bit
[picodrive.git] / pico / pico_cmn.c
... / ...
CommitLineData
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 68
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 SekCycleAim
25static 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 = SekCycleAim - SekCycleCnt) > 0) {
32 SekCycleCnt += cyc_do;
33
34#if defined(EMU_C68K)
35 PicoCpuCM68k.cycles = cyc_do;
36 CycloneRun(&PicoCpuCM68k);
37 SekCycleCnt -= PicoCpuCM68k.cycles;
38#elif defined(EMU_M68K)
39 SekCycleCnt += m68k_execute(cyc_do) - cyc_do;
40#elif defined(EMU_F68K)
41 SekCycleCnt += fm68k_emulate(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
52static inline void SekRunM68k(int cyc)
53{
54 SekCycleAim += cyc;
55 cyc = SekCycleAim - SekCycleCnt;
56 if (cyc <= 0)
57 return;
58 SekCycleCnt += cyc >> 6; // refresh slowdowns
59 SekSyncM68k();
60}
61
62static 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
71static int PicoFrameHints(void)
72{
73 struct PicoVideo *pv = &Pico.video;
74 int line_sample = Pico.m.pal ? 68 : 93;
75 int lines, y, lines_vis, skip;
76 int vcnt_wrap, vcnt_adj;
77 unsigned int cycles;
78 int hint; // Hint counter
79
80 pevt_log_m68k_o(EVT_FRAME_START);
81
82 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
83 // draw a frame just after vblank in alternative render mode
84 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
85 PicoFrameFull();
86#ifdef DRAW_FINISH_FUNC
87 DRAW_FINISH_FUNC();
88#endif
89 skip = 1;
90 }
91 else skip=PicoSkipFrame;
92
93 timing.m68c_frame_start = SekCyclesDone();
94 pv->v_counter = Pico.m.scanline = 0;
95 z80_resetCycles();
96 PsndStartFrame();
97
98 // Load H-Int counter
99 hint = (pv->status & PVS_ACTIVE) ? pv->hint_cnt : pv->reg[10];
100
101 pv->status |= PVS_ACTIVE;
102
103 for (y = 0; ; y++)
104 {
105 pv->v_counter = Pico.m.scanline = y;
106 if ((pv->reg[12]&6) == 6) { // interlace mode 2
107 pv->v_counter <<= 1;
108 pv->v_counter |= pv->v_counter >> 8;
109 pv->v_counter &= 0xff;
110 }
111
112 if ((y == 224 && !(pv->reg[1] & 8)) || y == 240)
113 break;
114
115 // VDP FIFO
116 pv->lwrite_cnt -= 12;
117 if (pv->lwrite_cnt <= 0) {
118 pv->lwrite_cnt = 0;
119 Pico.video.status |= SR_EMPT;
120 }
121
122 PAD_DELAY();
123
124 // H-Interrupts:
125 if (--hint < 0)
126 {
127 hint = pv->reg[10]; // Reload H-Int counter
128 do_hint(pv);
129 }
130
131 // decide if we draw this line
132 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
133 {
134 // find the right moment for frame renderer, when display is no longer blanked
135 if ((pv->reg[1]&0x40) || y > 100) {
136 PicoFrameFull();
137#ifdef DRAW_FINISH_FUNC
138 DRAW_FINISH_FUNC();
139#endif
140 skip = 1;
141 }
142 }
143
144 // get samples from sound chips
145 if ((y == 224 || y == line_sample) && PsndOut)
146 {
147 cycles = SekCyclesDone();
148
149 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
150 PicoSyncZ80(cycles);
151#ifdef PICO_CD
152 if (PicoAHW & PAHW_MCD)
153 pcd_sync_s68k(cycles, 0);
154#endif
155#ifdef PICO_32X
156 p32x_sync_sh2s(cycles);
157#endif
158 PsndGetSamples(y);
159 }
160
161 // Run scanline:
162 line_base_cycles = SekCyclesDone();
163 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
164 CPUS_RUN(CYCLES_M68K_LINE);
165
166 if (PicoLineHook) PicoLineHook();
167 pevt_log_m68k_o(EVT_NEXT_LINE);
168 }
169
170 lines_vis = (pv->reg[1] & 8) ? 240 : 224;
171 if (y == lines_vis)
172 pv->status &= ~PVS_ACTIVE;
173
174 if (!skip)
175 {
176 if (Pico.est.DrawScanline < y)
177 PicoDrawSync(y - 1, 0);
178#ifdef DRAW_FINISH_FUNC
179 DRAW_FINISH_FUNC();
180#endif
181 }
182
183 // VDP FIFO
184 pv->lwrite_cnt = 0;
185 Pico.video.status |= SR_EMPT;
186
187 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
188 PAD_DELAY();
189
190 // Last H-Int (normally):
191 if (--hint < 0)
192 {
193 hint = pv->reg[10]; // Reload H-Int counter
194 do_hint(pv);
195 }
196
197 pv->status |= SR_VB; // go into vblank
198 pv->pending_ints |= 0x20;
199
200 // the following SekRun is there for several reasons:
201 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
202 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
203 // also delay between last H-int and V-int (Golden Axe 3)
204 line_base_cycles = SekCyclesDone();
205 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
206 CPUS_RUN(CYCLES_M68K_VINT_LAG);
207
208 if (pv->reg[1] & 0x20) {
209 elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone());
210 SekInterrupt(6);
211 }
212
213 cycles = SekCyclesDone();
214 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
215 PicoSyncZ80(cycles);
216 elprintf(EL_INTS, "zint");
217 z80_int();
218 }
219
220#ifdef PICO_CD
221 if (PicoAHW & PAHW_MCD)
222 pcd_sync_s68k(cycles, 0);
223#endif
224#ifdef PICO_32X
225 p32x_sync_sh2s(cycles);
226 p32x_start_blank();
227#endif
228
229 // get samples from sound chips
230 if (y == 224 && PsndOut)
231 PsndGetSamples(y);
232
233 // Run scanline:
234 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG);
235
236 if (PicoLineHook) PicoLineHook();
237 pevt_log_m68k_o(EVT_NEXT_LINE);
238
239 if (Pico.m.pal) {
240 lines = 313;
241 vcnt_wrap = 0x103;
242 vcnt_adj = 57;
243 }
244 else {
245 lines = 262;
246 vcnt_wrap = 0xEB;
247 vcnt_adj = 6;
248 }
249
250 for (y++; y < lines - 1; y++)
251 {
252 pv->v_counter = Pico.m.scanline = y;
253 if (y >= vcnt_wrap)
254 pv->v_counter -= vcnt_adj;
255 if ((pv->reg[12]&6) == 6)
256 pv->v_counter = (pv->v_counter << 1) | 1;
257 pv->v_counter &= 0xff;
258
259 PAD_DELAY();
260
261 if ((pv->status & PVS_ACTIVE) && --hint < 0)
262 {
263 hint = pv->reg[10]; // Reload H-Int counter
264 do_hint(pv);
265 }
266
267 // Run scanline:
268 line_base_cycles = SekCyclesDone();
269 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
270 CPUS_RUN(CYCLES_M68K_LINE);
271
272 if (PicoLineHook) PicoLineHook();
273 pevt_log_m68k_o(EVT_NEXT_LINE);
274 }
275
276 pv->status &= ~SR_VB;
277
278 // last scanline
279 Pico.m.scanline = y;
280 pv->v_counter = 0xff;
281
282 PAD_DELAY();
283
284 if ((pv->status & PVS_ACTIVE) && --hint < 0)
285 {
286 hint = pv->reg[10]; // Reload H-Int counter
287 do_hint(pv);
288 }
289
290 // Run scanline:
291 line_base_cycles = SekCyclesDone();
292 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
293 CPUS_RUN(CYCLES_M68K_LINE);
294
295 if (PicoLineHook) PicoLineHook();
296 pevt_log_m68k_o(EVT_NEXT_LINE);
297
298 // sync cpus
299 cycles = SekCyclesDone();
300 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
301 PicoSyncZ80(cycles);
302 if (PsndOut && ym2612.dacen && PsndDacLine < lines)
303 PsndDoDAC(lines - 1);
304 if (PsndOut && PsndPsgLine < lines)
305 PsndDoPSG(lines - 1);
306
307#ifdef PICO_CD
308 if (PicoAHW & PAHW_MCD)
309 pcd_sync_s68k(cycles, 0);
310#endif
311#ifdef PICO_32X
312 p32x_sync_sh2s(cycles);
313#endif
314 timers_cycle();
315
316 pv->hint_cnt = hint;
317
318 return 0;
319}
320
321#undef PAD_DELAY
322#undef CPUS_RUN
323
324// vim:shiftwidth=2:ts=2:expandtab