c14040a604b33837c3adcbdb683f39551ec19316
[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;
58   int skip=PicoSkipFrame || (PicoOpt&0x10);
59   int hint; // Hint counter
60
61   if (Pico.m.pal) {
62     //cycles_68k = (int) ((double) OSC_PAL  /  7 / 50 / 312 + 0.4); // should compile to a constant (488)
63     //cycles_z80 = (int) ((double) OSC_PAL  / 15 / 50 / 312 + 0.4); // 228
64     line_sample = 68;
65     if(pv->reg[1]&8) lines_vis = 240;
66   } else {
67     //cycles_68k = (int) ((double) OSC_NTSC /  7 / 60 / 262 + 0.4); // 488
68     //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
69     line_sample = 93;
70   }
71
72   SekCyclesReset();
73 #ifdef PICO_CD
74   SekCyclesResetS68k();
75 #endif
76
77   pv->status&=~0x88; // clear V-Int, come out of vblank
78
79   hint=pv->reg[10]; // Load H-Int counter
80   //dprintf("-hint: %i", hint);
81
82   // This is to make active scan longer (needed for Double Dragon 2, mainly)
83   // also trying to adjust for z80 overclock here (due to int line cycle counts)
84   z80CycleAim = Pico.m.pal ? -40 : 7;
85   CPUS_RUN(CYCLES_M68K_ASD, 0, CYCLES_S68K_ASD);
86
87   for (y=0;y<lines_vis;y++)
88   {
89     Pico.m.scanline=(short)y;
90
91     // VDP FIFO
92     pv->lwrite_cnt -= 12;
93     if (pv->lwrite_cnt <= 0) {
94       pv->lwrite_cnt=0;
95       Pico.video.status|=0x200;
96     }
97
98     PAD_DELAY
99 #ifdef PICO_CD
100     check_cd_dma();
101 #endif
102
103     // H-Interrupts:
104     if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
105     {
106       hint=pv->reg[10]; // Reload H-Int counter
107       pv->pending_ints|=0x10;
108       if (pv->reg[0]&0x10) {
109         elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
110         SekInterrupt(4);
111       }
112     }
113
114     // decide if we draw this line
115 #if CAN_HANDLE_240_LINES
116     if(!skip && ((!(pv->reg[1]&8) && y<224) || (pv->reg[1]&8)) )
117 #else
118     if(!skip && y<224)
119 #endif
120       PicoLine(y);
121
122     if(PicoOpt&1)
123       sound_timers_and_dac(y);
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       getSamples(y);
131 #endif
132
133     // Run scanline:
134     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
135     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE);
136
137 #ifdef PICO_CD
138     update_chips();
139 #endif
140   }
141
142   // V-int line (224 or 240)
143   Pico.m.scanline=(short)y;
144
145   // VDP FIFO
146   pv->lwrite_cnt=0;
147   Pico.video.status|=0x200;
148
149   PAD_DELAY
150 #ifdef PICO_CD
151   check_cd_dma();
152 #endif
153
154   // Last H-Int:
155   if (--hint < 0)
156   {
157     hint=pv->reg[10]; // Reload H-Int counter
158     pv->pending_ints|=0x10;
159     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
160     if (pv->reg[0]&0x10) SekInterrupt(4);
161   }
162
163   // V-Interrupt:
164   pv->status|=0x08; // go into vblank
165   pv->pending_ints|=0x20;
166
167   // the following SekRun is there for several reasons:
168   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
169   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
170   // also delay between last H-int and V-int (Golden Axe 3)
171   SekRunM68k(CYCLES_M68K_VINT_LAG);
172   if (pv->reg[1]&0x20) {
173     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
174     SekInterrupt(6);
175   }
176   if (Pico.m.z80Run && (PicoOpt&4))
177     z80_int();
178
179   if (PicoOpt&1)
180     sound_timers_and_dac(y);
181
182   // get samples from sound chips
183 #ifndef PICO_CD
184   if (y == 224)
185 #endif
186     if (PsndOut)
187       getSamples(y);
188
189   // Run scanline:
190   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
191   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
192     CYCLES_Z80_LINE - CYCLES_Z80_ASD, CYCLES_S68K_LINE - CYCLES_S68K_ASD);
193
194 #ifdef PICO_CD
195     update_chips();
196 #endif
197
198   // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
199   lines = Pico.m.pal ? 312 : 262;
200
201   for (y++;y<lines;y++)
202   {
203     Pico.m.scanline=(short)y;
204
205     PAD_DELAY
206 #ifdef PICO_CD
207     check_cd_dma();
208 #endif
209
210     if(PicoOpt&1)
211       sound_timers_and_dac(y);
212
213     // Run scanline:
214     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
215     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE);
216
217 #ifdef PICO_CD
218     update_chips();
219 #endif
220   }
221
222   // draw a frame just after vblank in alternative render mode
223   if (!PicoSkipFrame && (PicoOpt&0x10))
224     PicoFrameFull();
225
226   return 0;
227 }
228
229 #undef PAD_DELAY
230 #undef Z80_RUN
231 #undef CPUS_RUN
232