bin_to_cso_mp3 improved
[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
bf5fbbb4 7#define CYCLES_S68K_LINE 795
8#define CYCLES_S68K_ASD 241
69996cb7 9
10// pad delay (for 6 button pads)
11#define PAD_DELAY \
602133e1 12 if (PicoOpt&POPT_6BTN_PAD) { \
69996cb7 13 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
14 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
15 }
16
bf5fbbb4 17// CPUS_RUN
18#ifndef PICO_CD
538a6098 19#define CPUS_RUN(m68k_cycles,s68k_cycles) \
4b9c5888 20 SekRunM68k(m68k_cycles);
bf5fbbb4 21#else
538a6098 22#define CPUS_RUN(m68k_cycles,s68k_cycles) \
bf5fbbb4 23{ \
602133e1 24 if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \
bf5fbbb4 25 SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \
26 } else { \
27 SekRunM68k(m68k_cycles); \
28 if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \
29 SekRunS68k(s68k_cycles); \
30 } \
bf5fbbb4 31}
32#endif
33
69996cb7 34// Accurate but slower frame which does hints
35static int PicoFrameHints(void)
36{
37 struct PicoVideo *pv=&Pico.video;
9761a7d0 38 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
69996cb7 39 int hint; // Hint counter
40
9761a7d0 41 pv->v_counter = Pico.m.scanline = 0;
538a6098 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;
9761a7d0 56 if (pv->reg[1]&8) lines_vis = 240;
69996cb7 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 PsndDacLine = 0;
69996cb7 67
68 pv->status&=~0x88; // clear V-Int, come out of vblank
69
70 hint=pv->reg[10]; // Load H-Int counter
71 //dprintf("-hint: %i", hint);
72
73 // This is to make active scan longer (needed for Double Dragon 2, mainly)
538a6098 74 CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
69996cb7 75
b6d7ac70 76 for (y = 0; y < lines_vis; y++)
69996cb7 77 {
9761a7d0 78 pv->v_counter = Pico.m.scanline = y;
79 if ((pv->reg[12]&6) == 6) { // interlace mode 2
80 pv->v_counter <<= 1;
81 pv->v_counter |= pv->v_counter >> 8;
82 pv->v_counter &= 0xff;
83 }
69996cb7 84
85 // VDP FIFO
86 pv->lwrite_cnt -= 12;
87 if (pv->lwrite_cnt <= 0) {
88 pv->lwrite_cnt=0;
89 Pico.video.status|=0x200;
90 }
91
92 PAD_DELAY
bf5fbbb4 93#ifdef PICO_CD
94 check_cd_dma();
95#endif
69996cb7 96
97 // H-Interrupts:
98 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
99 {
100 hint=pv->reg[10]; // Reload H-Int counter
101 pv->pending_ints|=0x10;
102 if (pv->reg[0]&0x10) {
103 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
104 SekInterrupt(4);
105 }
106 }
107
108 // decide if we draw this line
b6d7ac70 109 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 110 {
b6d7ac70 111 // find the right moment for frame renderer, when display is no longer blanked
112 if ((pv->reg[1]&0x40) || y > 100) {
113 PicoFrameFull();
fad24893 114#ifdef DRAW_FINISH_FUNC
b6d7ac70 115 DRAW_FINISH_FUNC();
fad24893 116#endif
b6d7ac70 117 skip = 1;
fad24893 118 }
119 }
69996cb7 120
bf5fbbb4 121#ifndef PICO_CD
69996cb7 122 // get samples from sound chips
fad24893 123 if (y == 32 && PsndOut)
69996cb7 124 emustatus &= ~1;
fad24893 125 else if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 126 {
127 if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
128 PicoSyncZ80(SekCycleCnt);
129 if (ym2612.dacen && PsndDacLine <= y)
130 PsndDoDAC(y);
69996cb7 131 getSamples(y);
4b9c5888 132 }
bf5fbbb4 133#endif
69996cb7 134
135 // Run scanline:
136 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 137 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 138
139#ifdef PICO_CD
140 update_chips();
017512f2 141#else
b0677887 142 if (PicoLineHook) PicoLineHook();
bf5fbbb4 143#endif
69996cb7 144 }
145
03e4f2a3 146 if (!skip)
b6d7ac70 147 {
148 if (DrawScanline < y)
149 PicoDrawSync(y - 1, 0);
150#ifdef DRAW_FINISH_FUNC
03e4f2a3 151 DRAW_FINISH_FUNC();
8ab3e3c1 152#endif
b6d7ac70 153 }
8ab3e3c1 154
947fb5f9 155 // V-int line (224 or 240)
156 Pico.m.scanline = y;
157 pv->v_counter = 0xe0; // bad for 240 mode
158 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
159
69996cb7 160 // VDP FIFO
161 pv->lwrite_cnt=0;
162 Pico.video.status|=0x200;
163
164 PAD_DELAY
bf5fbbb4 165#ifdef PICO_CD
166 check_cd_dma();
167#endif
69996cb7 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
69996cb7 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,
538a6098 211 CYCLES_S68K_LINE - CYCLES_S68K_ASD);
bf5fbbb4 212
213#ifdef PICO_CD
017512f2 214 update_chips();
215#else
b0677887 216 if (PicoLineHook) PicoLineHook();
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;
9761a7d0 221 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens
69996cb7 222
b6d7ac70 223 for (y++; y < lines; y++)
69996cb7 224 {
9761a7d0 225 pv->v_counter = Pico.m.scanline = y;
226 if (y >= vcnt_wrap)
227 pv->v_counter -= Pico.m.pal ? 56 : 6;
228 if ((pv->reg[12]&6) == 6)
229 pv->v_counter = (pv->v_counter << 1) | 1;
230 pv->v_counter &= 0xff;
69996cb7 231
232 PAD_DELAY
bf5fbbb4 233#ifdef PICO_CD
234 check_cd_dma();
235#endif
69996cb7 236
69996cb7 237 // Run scanline:
238 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 239 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 240
241#ifdef PICO_CD
242 update_chips();
017512f2 243#else
b0677887 244 if (PicoLineHook) PicoLineHook();
bf5fbbb4 245#endif
69996cb7 246 }
247
4b9c5888 248 // sync z80
249 if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
e53704e6 250 PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
4b9c5888 251 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
252 PsndDoDAC(lines-1);
253
e53704e6 254 timers_cycle();
255
69996cb7 256 return 0;
257}
258
259#undef PAD_DELAY
bf5fbbb4 260#undef CPUS_RUN
69996cb7 261