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