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