various bugfixes
[picodrive.git] / Pico / PicoFrameHints.c
CommitLineData
69996cb7 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
bf5fbbb4 6#define CYCLES_S68K_LINE 795
7#define CYCLES_S68K_ASD 241
69996cb7 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 { \
5f20bb80 20 int cnt; \
69996cb7 21 if (Pico.m.z80Run & 2) z80CycleAim += z80_cycles; \
22 else { \
5f20bb80 23 cnt = SekCyclesDone() - z80startCycle; \
69996cb7 24 cnt = (cnt>>1)-(cnt>>5); \
9a8ffeee 25 if (cnt < 0 || cnt > (z80_cycles)) cnt = z80_cycles; \
69996cb7 26 Pico.m.z80Run |= 2; \
27 z80CycleAim+=cnt; \
28 } \
5f20bb80 29 cnt=z80CycleAim-total_z80; \
30 if (cnt > 0) total_z80+=z80_run(cnt); \
69996cb7 31 } \
32}
33
bf5fbbb4 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
69996cb7 53// Accurate but slower frame which does hints
54static int PicoFrameHints(void)
55{
56 struct PicoVideo *pv=&Pico.video;
5f20bb80 57 int lines,y,lines_vis = 224,total_z80 = 0,z80CycleAim = 0,line_sample;
69996cb7 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();
bf5fbbb4 73#ifdef PICO_CD
74 SekCyclesResetS68k();
75#endif
69996cb7 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)
5f20bb80 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);
69996cb7 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
bf5fbbb4 99#ifdef PICO_CD
100 check_cd_dma();
101#endif
69996cb7 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
bf5fbbb4 125#ifndef PICO_CD
69996cb7 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);
bf5fbbb4 131#endif
69996cb7 132
133 // Run scanline:
134 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 135 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE);
136
137#ifdef PICO_CD
138 update_chips();
139#endif
69996cb7 140 }
141
8ab3e3c1 142#ifdef DRAW_FINISH_FUNC
143 DRAW_FINISH_FUNC();
144#endif
145
69996cb7 146 // V-int line (224 or 240)
147 Pico.m.scanline=(short)y;
148
149 // VDP FIFO
150 pv->lwrite_cnt=0;
151 Pico.video.status|=0x200;
152
153 PAD_DELAY
bf5fbbb4 154#ifdef PICO_CD
155 check_cd_dma();
156#endif
69996cb7 157
158 // Last H-Int:
159 if (--hint < 0)
160 {
161 hint=pv->reg[10]; // Reload H-Int counter
162 pv->pending_ints|=0x10;
163 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
164 if (pv->reg[0]&0x10) SekInterrupt(4);
165 }
166
167 // V-Interrupt:
168 pv->status|=0x08; // go into vblank
169 pv->pending_ints|=0x20;
170
171 // the following SekRun is there for several reasons:
172 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
173 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
174 // also delay between last H-int and V-int (Golden Axe 3)
bf5fbbb4 175 SekRunM68k(CYCLES_M68K_VINT_LAG);
69996cb7 176 if (pv->reg[1]&0x20) {
177 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
178 SekInterrupt(6);
179 }
180 if (Pico.m.z80Run && (PicoOpt&4))
181 z80_int();
182
183 if (PicoOpt&1)
184 sound_timers_and_dac(y);
185
186 // get samples from sound chips
bf5fbbb4 187#ifndef PICO_CD
188 if (y == 224)
189#endif
190 if (PsndOut)
191 getSamples(y);
69996cb7 192
193 // Run scanline:
194 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 195 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
196 CYCLES_Z80_LINE - CYCLES_Z80_ASD, CYCLES_S68K_LINE - CYCLES_S68K_ASD);
197
198#ifdef PICO_CD
199 update_chips();
200#endif
69996cb7 201
202 // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
203 lines = Pico.m.pal ? 312 : 262;
204
205 for (y++;y<lines;y++)
206 {
207 Pico.m.scanline=(short)y;
208
209 PAD_DELAY
bf5fbbb4 210#ifdef PICO_CD
211 check_cd_dma();
212#endif
69996cb7 213
214 if(PicoOpt&1)
215 sound_timers_and_dac(y);
216
217 // Run scanline:
218 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 219 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE);
220
221#ifdef PICO_CD
222 update_chips();
223#endif
69996cb7 224 }
225
226 // draw a frame just after vblank in alternative render mode
227 if (!PicoSkipFrame && (PicoOpt&0x10))
228 PicoFrameFull();
229
230 return 0;
231}
232
233#undef PAD_DELAY
234#undef Z80_RUN
bf5fbbb4 235#undef CPUS_RUN
69996cb7 236