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