32x: start reworking sheduling
[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 #ifdef PICO_32X
115     p32x_timers_do(1);
116 #endif
117
118     // H-Interrupts:
119     if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
120     {
121       hint=pv->reg[10]; // Reload H-Int counter
122       pv->pending_ints|=0x10;
123       if (pv->reg[0]&0x10) {
124         elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
125         SekInterrupt(4);
126       }
127     }
128
129     // decide if we draw this line
130     if (!skip && (PicoOpt & POPT_ALT_RENDERER))
131     {
132       // find the right moment for frame renderer, when display is no longer blanked
133       if ((pv->reg[1]&0x40) || y > 100) {
134         PicoFrameFull();
135 #ifdef DRAW_FINISH_FUNC
136         DRAW_FINISH_FUNC();
137 #endif
138         skip = 1;
139       }
140     }
141
142     // get samples from sound chips
143     if ((y == 224 || y == line_sample) && PsndOut)
144     {
145       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
146         PicoSyncZ80(SekCycleCnt);
147       if (ym2612.dacen && PsndDacLine <= y)
148         PsndDoDAC(y);
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 #ifdef PICO_32X
187   p32x_timers_do(1);
188 #endif
189
190   // Last H-Int:
191   if (--hint < 0)
192   {
193     hint=pv->reg[10]; // Reload H-Int counter
194     pv->pending_ints|=0x10;
195     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
196     if (pv->reg[0]&0x10) SekInterrupt(4);
197   }
198
199   pv->status|=0x08; // go into vblank
200   pv->pending_ints|=0x20;
201
202 #ifdef PICO_32X
203   p32x_start_blank();
204 #endif
205
206   // the following SekRun is there for several reasons:
207   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
208   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
209   // also delay between last H-int and V-int (Golden Axe 3)
210   CPUS_RUN(CYCLES_M68K_VINT_LAG, CYCLES_S68K_VINT_LAG);
211
212   if (pv->reg[1]&0x20) {
213     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
214     SekInterrupt(6);
215   }
216   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
217     PicoSyncZ80(SekCycleCnt);
218     elprintf(EL_INTS, "zint");
219     z80_int();
220   }
221
222   // get samples from sound chips
223   if (y == 224 && PsndOut)
224   {
225     if (ym2612.dacen && PsndDacLine <= y)
226       PsndDoDAC(y);
227     PsndGetSamples(y);
228   }
229
230   // Run scanline:
231   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
232   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
233     CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
234
235 #ifdef PICO_CD
236   update_chips();
237 #else
238   if (PicoLineHook) PicoLineHook();
239 #endif
240
241   lines = scanlines_total;
242   vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
243
244   for (y++; y < lines; y++)
245   {
246     pv->v_counter = Pico.m.scanline = y;
247     if (y >= vcnt_wrap)
248       pv->v_counter -= Pico.m.pal ? 56 : 6;
249     if ((pv->reg[12]&6) == 6)
250       pv->v_counter = (pv->v_counter << 1) | 1;
251     pv->v_counter &= 0xff;
252
253     PAD_DELAY
254 #ifdef PICO_CD
255     check_cd_dma();
256 #endif
257 #ifdef PICO_32X
258     p32x_timers_do(1);
259 #endif
260
261     // Run scanline:
262     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
263     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
264
265 #ifdef PICO_CD
266     update_chips();
267 #else
268     if (PicoLineHook) PicoLineHook();
269 #endif
270   }
271
272   // sync z80
273   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
274     PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
275   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
276     PsndDoDAC(lines-1);
277
278   timers_cycle();
279
280   return 0;
281 }
282
283 #undef PAD_DELAY
284 #undef CPUS_RUN
285