32x: rework scheduling/timing
[picodrive.git] / pico / pico_cmn.c
1 /*
2  * common code for pico.c and cd/pico.c
3  * (C) notaz, 2007-2009
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 #define CYCLES_M68K_ASD      148
12 #define CYCLES_S68K_LINE     795
13 #define CYCLES_S68K_VINT_LAG 111
14 #define CYCLES_S68K_ASD      241
15
16 // pad delay (for 6 button pads)
17 #define PAD_DELAY \
18   if (PicoOpt&POPT_6BTN_PAD) { \
19     if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
20     if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
21   }
22
23 // CPUS_RUN
24 #ifndef CPUS_RUN
25 #define CPUS_RUN(m68k_cycles,s68k_cycles) \
26   SekRunM68k(m68k_cycles)
27 #endif
28
29 static __inline void SekRunM68k(int cyc)
30 {
31   int cyc_do;
32   pprof_start(m68k);
33
34   SekCycleAim+=cyc;
35   if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;
36 #if defined(EMU_CORE_DEBUG)
37   // this means we do run-compare
38   SekCycleCnt+=CM_compareRun(cyc_do, 0);
39 #elif defined(EMU_C68K)
40   PicoCpuCM68k.cycles=cyc_do;
41   CycloneRun(&PicoCpuCM68k);
42   SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
43 #elif defined(EMU_M68K)
44   SekCycleCnt+=m68k_execute(cyc_do);
45 #elif defined(EMU_F68K)
46   SekCycleCnt+=fm68k_emulate(cyc_do+1, 0, 0);
47 #endif
48
49   pprof_end(m68k);
50 }
51
52 static int PicoFrameHints(void)
53 {
54   struct PicoVideo *pv=&Pico.video;
55   int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
56   int hint; // Hint counter
57
58   pv->v_counter = Pico.m.scanline = 0;
59
60   if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
61     // draw a frame just after vblank in alternative render mode
62     // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
63     PicoFrameFull();
64 #ifdef DRAW_FINISH_FUNC
65     DRAW_FINISH_FUNC();
66 #endif
67     skip = 1;
68   }
69   else skip=PicoSkipFrame;
70
71   if (Pico.m.pal) {
72     line_sample = 68;
73     if (pv->reg[1]&8) lines_vis = 240;
74   } else {
75     line_sample = 93;
76   }
77
78   SekCyclesReset();
79   z80_resetCycles();
80 #ifdef PICO_CD
81   SekCyclesResetS68k();
82 #endif
83   PsndDacLine = 0;
84   emustatus &= ~1;
85
86   pv->status&=~0x88; // clear V-Int, come out of vblank
87
88   hint=pv->reg[10]; // Load H-Int counter
89   //dprintf("-hint: %i", hint);
90
91   // This is to make active scan longer (needed for Double Dragon 2, mainly)
92   CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
93
94   for (y = 0; y < lines_vis; y++)
95   {
96     pv->v_counter = Pico.m.scanline = y;
97     if ((pv->reg[12]&6) == 6) { // interlace mode 2
98       pv->v_counter <<= 1;
99       pv->v_counter |= pv->v_counter >> 8;
100       pv->v_counter &= 0xff;
101     }
102
103     // VDP FIFO
104     pv->lwrite_cnt -= 12;
105     if (pv->lwrite_cnt <= 0) {
106       pv->lwrite_cnt=0;
107       Pico.video.status|=0x200;
108     }
109
110     PAD_DELAY
111 #ifdef PICO_CD
112     check_cd_dma();
113 #endif
114
115     // H-Interrupts:
116     if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
117     {
118       hint=pv->reg[10]; // Reload H-Int counter
119       pv->pending_ints|=0x10;
120       if (pv->reg[0]&0x10) {
121         elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
122         SekInterrupt(4);
123       }
124     }
125
126     // decide if we draw this line
127     if (!skip && (PicoOpt & POPT_ALT_RENDERER))
128     {
129       // find the right moment for frame renderer, when display is no longer blanked
130       if ((pv->reg[1]&0x40) || y > 100) {
131         PicoFrameFull();
132 #ifdef DRAW_FINISH_FUNC
133         DRAW_FINISH_FUNC();
134 #endif
135         skip = 1;
136       }
137     }
138
139     // get samples from sound chips
140     if ((y == 224 || y == line_sample) && PsndOut)
141     {
142       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
143         PicoSyncZ80(SekCycleCnt);
144       if (ym2612.dacen && PsndDacLine <= y)
145         PsndDoDAC(y);
146 #ifdef PICO_32X
147       p32x_sync_sh2s(SekCycleCntT + SekCycleCnt);
148 #endif
149       PsndGetSamples(y);
150     }
151
152     // Run scanline:
153     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
154     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
155
156 #ifdef PICO_CD
157     update_chips();
158 #else
159     if (PicoLineHook) PicoLineHook();
160 #endif
161   }
162
163   if (!skip)
164   {
165     if (DrawScanline < y)
166       PicoDrawSync(y - 1, 0);
167 #ifdef DRAW_FINISH_FUNC
168     DRAW_FINISH_FUNC();
169 #endif
170   }
171
172   // V-int line (224 or 240)
173   Pico.m.scanline = y;
174   pv->v_counter = 0xe0; // bad for 240 mode
175   if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
176
177   // VDP FIFO
178   pv->lwrite_cnt=0;
179   Pico.video.status|=0x200;
180
181   memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
182   PAD_DELAY
183 #ifdef PICO_CD
184   check_cd_dma();
185 #endif
186
187   // Last H-Int:
188   if (--hint < 0)
189   {
190     hint=pv->reg[10]; // Reload H-Int counter
191     pv->pending_ints|=0x10;
192     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
193     if (pv->reg[0]&0x10) SekInterrupt(4);
194   }
195
196   pv->status|=0x08; // 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   CPUS_RUN(CYCLES_M68K_VINT_LAG, CYCLES_S68K_VINT_LAG);
204
205   if (pv->reg[1]&0x20) {
206     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
207     SekInterrupt(6);
208   }
209   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
210     PicoSyncZ80(SekCycleCnt);
211     elprintf(EL_INTS, "zint");
212     z80_int();
213   }
214
215 #ifdef PICO_32X
216   p32x_sync_sh2s(SekCycleCntT + SekCycleCnt);
217   p32x_start_blank();
218 #endif
219
220   // get samples from sound chips
221   if (y == 224 && PsndOut)
222   {
223     if (ym2612.dacen && PsndDacLine <= y)
224       PsndDoDAC(y);
225     PsndGetSamples(y);
226   }
227
228   // Run scanline:
229   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
230   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
231     CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
232
233 #ifdef PICO_CD
234   update_chips();
235 #else
236   if (PicoLineHook) PicoLineHook();
237 #endif
238
239   lines = scanlines_total;
240   vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
241
242   for (y++; y < lines; y++)
243   {
244     pv->v_counter = Pico.m.scanline = y;
245     if (y >= vcnt_wrap)
246       pv->v_counter -= Pico.m.pal ? 56 : 6;
247     if ((pv->reg[12]&6) == 6)
248       pv->v_counter = (pv->v_counter << 1) | 1;
249     pv->v_counter &= 0xff;
250
251     PAD_DELAY
252 #ifdef PICO_CD
253     check_cd_dma();
254 #endif
255
256     // Run scanline:
257     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
258     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
259
260 #ifdef PICO_CD
261     update_chips();
262 #else
263     if (PicoLineHook) PicoLineHook();
264 #endif
265   }
266
267   // sync z80
268   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
269     PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
270   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
271     PsndDoDAC(lines-1);
272
273 #ifdef PICO_32X
274   p32x_sync_sh2s(SekCycleCntT + SekCycleCnt);
275 #endif
276   timers_cycle();
277
278   return 0;
279 }
280
281 #undef PAD_DELAY
282 #undef CPUS_RUN
283