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