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