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