added timer saving to savestates
[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   PsndDacLine = 0;
67
68   pv->status&=~0x88; // clear V-Int, come out of vblank
69
70   hint=pv->reg[10]; // Load H-Int counter
71   //dprintf("-hint: %i", hint);
72
73   // This is to make active scan longer (needed for Double Dragon 2, mainly)
74   CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
75
76   for (y=0;y<lines_vis;y++)
77   {
78     Pico.m.scanline=(short)y;
79
80     // VDP FIFO
81     pv->lwrite_cnt -= 12;
82     if (pv->lwrite_cnt <= 0) {
83       pv->lwrite_cnt=0;
84       Pico.video.status|=0x200;
85     }
86
87     PAD_DELAY
88 #ifdef PICO_CD
89     check_cd_dma();
90 #endif
91
92     // H-Interrupts:
93     if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
94     {
95       hint=pv->reg[10]; // Reload H-Int counter
96       pv->pending_ints|=0x10;
97       if (pv->reg[0]&0x10) {
98         elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
99         SekInterrupt(4);
100       }
101     }
102
103     // decide if we draw this line
104     if (!skip)
105     {
106       if (PicoOpt&POPT_ALT_RENDERER) {
107         // find the right moment for fast renderer, when display is no longer blanked
108         if ((pv->reg[1]&0x40) || y > 100) {
109           PicoFrameFull();
110 #ifdef DRAW_FINISH_FUNC
111           DRAW_FINISH_FUNC();
112 #endif
113           skip = 1;
114         }
115       }
116       else
117       {
118 #if !CAN_HANDLE_240_LINES
119         if (y < 224)
120 #endif
121           PicoLine(y);
122       }
123     }
124
125 #ifndef PICO_CD
126     // get samples from sound chips
127     if (y == 32 && PsndOut)
128       emustatus &= ~1;
129     else if ((y == 224 || y == line_sample) && PsndOut)
130     {
131       if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
132         PicoSyncZ80(SekCycleCnt);
133       if (ym2612.dacen && PsndDacLine <= y)
134         PsndDoDAC(y);
135       getSamples(y);
136     }
137 #endif
138
139     // Run scanline:
140     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
141     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
142
143 #ifdef PICO_CD
144     update_chips();
145 #else
146     if (PicoLineHook) PicoLineHook(1);
147 #endif
148   }
149
150 #ifdef DRAW_FINISH_FUNC
151   if (!skip)
152     DRAW_FINISH_FUNC();
153 #endif
154
155   // V-int line (224 or 240)
156   Pico.m.scanline=(short)y;
157
158   // VDP FIFO
159   pv->lwrite_cnt=0;
160   Pico.video.status|=0x200;
161
162   PAD_DELAY
163 #ifdef PICO_CD
164   check_cd_dma();
165 #endif
166
167   // Last H-Int:
168   if (--hint < 0)
169   {
170     hint=pv->reg[10]; // Reload H-Int counter
171     pv->pending_ints|=0x10;
172     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
173     if (pv->reg[0]&0x10) SekInterrupt(4);
174   }
175
176   // V-Interrupt:
177   pv->status|=0x08; // go into vblank
178   pv->pending_ints|=0x20;
179
180   // the following SekRun is there for several reasons:
181   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
182   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
183   // also delay between last H-int and V-int (Golden Axe 3)
184   SekRunM68k(CYCLES_M68K_VINT_LAG);
185
186   if (pv->reg[1]&0x20) {
187     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
188     SekInterrupt(6);
189   }
190   if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) {
191     PicoSyncZ80(SekCycleCnt);
192     elprintf(EL_INTS, "zint");
193     z80_int();
194   }
195
196   // get samples from sound chips
197 #ifndef PICO_CD
198   if (y == 224)
199 #endif
200     if (PsndOut)
201     {
202       if (ym2612.dacen && PsndDacLine <= y)
203         PsndDoDAC(y);
204       getSamples(y);
205     }
206
207   // Run scanline:
208   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
209   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
210     CYCLES_S68K_LINE - CYCLES_S68K_ASD);
211
212 #ifdef PICO_CD
213   update_chips();
214 #else
215   if (PicoLineHook) PicoLineHook(1);
216 #endif
217
218   // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
219   lines = Pico.m.pal ? 312 : 262;
220
221   for (y++;y<lines;y++)
222   {
223     Pico.m.scanline=(short)y;
224
225     PAD_DELAY
226 #ifdef PICO_CD
227     check_cd_dma();
228 #endif
229
230     // Run scanline:
231     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
232     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
233
234 #ifdef PICO_CD
235     update_chips();
236 #else
237     if (PicoLineHook) PicoLineHook(1);
238 #endif
239   }
240
241   // sync z80
242   if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
243     PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
244   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
245     PsndDoDAC(lines-1);
246
247   timers_cycle();
248
249   return 0;
250 }
251
252 #undef PAD_DELAY
253 #undef CPUS_RUN
254