new faster sprite priority and sh/hi hadling
[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_S68K_LINE     795
8 #define CYCLES_S68K_ASD      241
9
10 // pad delay (for 6 button pads)
11 #define PAD_DELAY \
12   if (PicoOpt&POPT_6BTN_PAD) { \
13     if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
14     if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
15   }
16
17 // CPUS_RUN
18 #ifndef PICO_CD
19 #define CPUS_RUN(m68k_cycles,s68k_cycles) \
20     SekRunM68k(m68k_cycles);
21 #else
22 #define CPUS_RUN(m68k_cycles,s68k_cycles) \
23 { \
24     if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \
25       SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \
26     } else { \
27       SekRunM68k(m68k_cycles); \
28       if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \
29         SekRunS68k(s68k_cycles); \
30     } \
31 }
32 #endif
33
34 // Accurate but slower frame which does hints
35 static int PicoFrameHints(void)
36 {
37   struct PicoVideo *pv=&Pico.video;
38   int lines, y, lines_vis = 224, line_sample, skip;
39   int hint; // Hint counter
40
41   Pico.m.scanline=0;
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, 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 (y < 224)
121 #endif
122           PicoLine(y);
123       }
124     }
125
126 #ifndef PICO_CD
127     // get samples from sound chips
128     if (y == 32 && PsndOut)
129       emustatus &= ~1;
130     else if ((y == 224 || y == line_sample) && PsndOut)
131     {
132       if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
133         PicoSyncZ80(SekCycleCnt);
134       if (ym2612.dacen && PsndDacLine <= y)
135         PsndDoDAC(y);
136       getSamples(y);
137     }
138 #endif
139
140     // Run scanline:
141     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
142     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
143
144 #ifdef PICO_CD
145     update_chips();
146 #else
147     if (PicoLineHook) PicoLineHook(1);
148 #endif
149   }
150
151 #ifdef DRAW_FINISH_FUNC
152   if (!skip)
153     DRAW_FINISH_FUNC();
154 #endif
155
156   // V-int line (224 or 240)
157   Pico.m.scanline=(short)y;
158
159   // VDP FIFO
160   pv->lwrite_cnt=0;
161   Pico.video.status|=0x200;
162
163   PAD_DELAY
164 #ifdef PICO_CD
165   check_cd_dma();
166 #endif
167
168   // Last H-Int:
169   if (--hint < 0)
170   {
171     hint=pv->reg[10]; // Reload H-Int counter
172     pv->pending_ints|=0x10;
173     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
174     if (pv->reg[0]&0x10) SekInterrupt(4);
175   }
176
177   // V-Interrupt:
178   pv->status|=0x08; // go into vblank
179   pv->pending_ints|=0x20;
180
181   // the following SekRun is there for several reasons:
182   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
183   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
184   // also delay between last H-int and V-int (Golden Axe 3)
185   SekRunM68k(CYCLES_M68K_VINT_LAG);
186
187   if (pv->reg[1]&0x20) {
188     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
189     SekInterrupt(6);
190   }
191   if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) {
192     PicoSyncZ80(SekCycleCnt);
193     elprintf(EL_INTS, "zint");
194     z80_int();
195   }
196
197   // get samples from sound chips
198 #ifndef PICO_CD
199   if (y == 224)
200 #endif
201     if (PsndOut)
202     {
203       if (ym2612.dacen && PsndDacLine <= y)
204         PsndDoDAC(y);
205       getSamples(y);
206     }
207
208   // Run scanline:
209   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
210   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
211     CYCLES_S68K_LINE - CYCLES_S68K_ASD);
212
213 #ifdef PICO_CD
214   update_chips();
215 #else
216   if (PicoLineHook) PicoLineHook(1);
217 #endif
218
219   // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
220   lines = Pico.m.pal ? 312 : 262;
221
222   for (y++;y<lines;y++)
223   {
224     Pico.m.scanline=(short)y;
225
226     PAD_DELAY
227 #ifdef PICO_CD
228     check_cd_dma();
229 #endif
230
231     // Run scanline:
232     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
233     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
234
235 #ifdef PICO_CD
236     update_chips();
237 #else
238     if (PicoLineHook) PicoLineHook(1);
239 #endif
240   }
241
242   // sync z80
243   if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
244     PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for convertor
245   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
246     PsndDoDAC(lines-1);
247
248   return 0;
249 }
250
251 #undef PAD_DELAY
252 #undef CPUS_RUN
253