don't crash on bad timing
[picodrive.git] / pico / pico_cmn.c
CommitLineData
cff531af 1/*
2 * common code for pico.c and cd/pico.c
3 * (C) notaz, 2007-2009
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
8b99ab90 8
69996cb7 9#define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC
10#define CYCLES_M68K_VINT_LAG 68
11#define CYCLES_M68K_ASD 148
bf5fbbb4 12#define CYCLES_S68K_LINE 795
236990cf 13#define CYCLES_S68K_VINT_LAG 111
bf5fbbb4 14#define CYCLES_S68K_ASD 241
69996cb7 15
16// pad delay (for 6 button pads)
17#define PAD_DELAY \
602133e1 18 if (PicoOpt&POPT_6BTN_PAD) { \
69996cb7 19 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
20 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
21 }
22
bf5fbbb4 23// CPUS_RUN
c987bb5c 24#ifndef CPUS_RUN
538a6098 25#define CPUS_RUN(m68k_cycles,s68k_cycles) \
c987bb5c 26 SekRunM68k(m68k_cycles)
bf5fbbb4 27#endif
28
69996cb7 29static int PicoFrameHints(void)
30{
31 struct PicoVideo *pv=&Pico.video;
9761a7d0 32 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
69996cb7 33 int hint; // Hint counter
34
9761a7d0 35 pv->v_counter = Pico.m.scanline = 0;
538a6098 36
602133e1 37 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
03e4f2a3 38 // draw a frame just after vblank in alternative render mode
39 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
40 PicoFrameFull();
41#ifdef DRAW_FINISH_FUNC
42 DRAW_FINISH_FUNC();
43#endif
44 skip = 1;
45 }
46 else skip=PicoSkipFrame;
47
69996cb7 48 if (Pico.m.pal) {
69996cb7 49 line_sample = 68;
9761a7d0 50 if (pv->reg[1]&8) lines_vis = 240;
69996cb7 51 } else {
69996cb7 52 line_sample = 93;
53 }
54
55 SekCyclesReset();
4b9c5888 56 z80_resetCycles();
bf5fbbb4 57#ifdef PICO_CD
58 SekCyclesResetS68k();
59#endif
4b9c5888 60 PsndDacLine = 0;
7b3f44c6 61 emustatus &= ~1;
69996cb7 62
63 pv->status&=~0x88; // clear V-Int, come out of vblank
64
65 hint=pv->reg[10]; // Load H-Int counter
66 //dprintf("-hint: %i", hint);
67
68 // This is to make active scan longer (needed for Double Dragon 2, mainly)
538a6098 69 CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
69996cb7 70
b6d7ac70 71 for (y = 0; y < lines_vis; y++)
69996cb7 72 {
9761a7d0 73 pv->v_counter = Pico.m.scanline = y;
74 if ((pv->reg[12]&6) == 6) { // interlace mode 2
75 pv->v_counter <<= 1;
76 pv->v_counter |= pv->v_counter >> 8;
77 pv->v_counter &= 0xff;
78 }
69996cb7 79
80 // VDP FIFO
81 pv->lwrite_cnt -= 12;
82 if (pv->lwrite_cnt <= 0) {
83 pv->lwrite_cnt=0;
84 Pico.video.status|=0x200;
85 }
86
87 PAD_DELAY
bf5fbbb4 88#ifdef PICO_CD
89 check_cd_dma();
90#endif
db1d3564 91#ifdef PICO_32X
1d7a28a7 92 p32x_timers_do(1);
db1d3564 93#endif
69996cb7 94
95 // H-Interrupts:
96 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
97 {
98 hint=pv->reg[10]; // Reload H-Int counter
99 pv->pending_ints|=0x10;
100 if (pv->reg[0]&0x10) {
101 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
102 SekInterrupt(4);
103 }
104 }
105
106 // decide if we draw this line
b6d7ac70 107 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 108 {
b6d7ac70 109 // find the right moment for frame renderer, when display is no longer blanked
110 if ((pv->reg[1]&0x40) || y > 100) {
111 PicoFrameFull();
fad24893 112#ifdef DRAW_FINISH_FUNC
b6d7ac70 113 DRAW_FINISH_FUNC();
fad24893 114#endif
b6d7ac70 115 skip = 1;
fad24893 116 }
117 }
69996cb7 118
69996cb7 119 // get samples from sound chips
7b3f44c6 120 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 121 {
be297089 122 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
4b9c5888 123 PicoSyncZ80(SekCycleCnt);
124 if (ym2612.dacen && PsndDacLine <= y)
125 PsndDoDAC(y);
7b3f44c6 126 PsndGetSamples(y);
4b9c5888 127 }
69996cb7 128
129 // Run scanline:
130 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 131 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 132
133#ifdef PICO_CD
134 update_chips();
017512f2 135#else
b0677887 136 if (PicoLineHook) PicoLineHook();
bf5fbbb4 137#endif
69996cb7 138 }
139
03e4f2a3 140 if (!skip)
b6d7ac70 141 {
142 if (DrawScanline < y)
143 PicoDrawSync(y - 1, 0);
144#ifdef DRAW_FINISH_FUNC
03e4f2a3 145 DRAW_FINISH_FUNC();
8ab3e3c1 146#endif
b6d7ac70 147 }
8ab3e3c1 148
947fb5f9 149 // V-int line (224 or 240)
150 Pico.m.scanline = y;
151 pv->v_counter = 0xe0; // bad for 240 mode
152 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
153
69996cb7 154 // VDP FIFO
155 pv->lwrite_cnt=0;
156 Pico.video.status|=0x200;
157
5f9a0d16 158 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
69996cb7 159 PAD_DELAY
bf5fbbb4 160#ifdef PICO_CD
161 check_cd_dma();
162#endif
db1d3564 163#ifdef PICO_32X
1d7a28a7 164 p32x_timers_do(1);
db1d3564 165#endif
69996cb7 166
167 // Last H-Int:
168 if (--hint < 0)
169 {
170 hint=pv->reg[10]; // Reload H-Int counter
171 pv->pending_ints|=0x10;
172 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
173 if (pv->reg[0]&0x10) SekInterrupt(4);
174 }
175
69996cb7 176 pv->status|=0x08; // go into vblank
177 pv->pending_ints|=0x20;
178
974fdb5b 179#ifdef PICO_32X
180 p32x_start_blank();
181#endif
182
69996cb7 183 // the following SekRun is there for several reasons:
184 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
185 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
186 // also delay between last H-int and V-int (Golden Axe 3)
236990cf 187 CPUS_RUN(CYCLES_M68K_VINT_LAG, CYCLES_S68K_VINT_LAG);
4b9c5888 188
69996cb7 189 if (pv->reg[1]&0x20) {
190 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
191 SekInterrupt(6);
192 }
be297089 193 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
4b9c5888 194 PicoSyncZ80(SekCycleCnt);
583ab72c 195 elprintf(EL_INTS, "zint");
69996cb7 196 z80_int();
583ab72c 197 }
69996cb7 198
69996cb7 199 // get samples from sound chips
7b3f44c6 200 if (y == 224 && PsndOut)
201 {
202 if (ym2612.dacen && PsndDacLine <= y)
203 PsndDoDAC(y);
204 PsndGetSamples(y);
205 }
69996cb7 206
207 // Run scanline:
208 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 209 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
236990cf 210 CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
bf5fbbb4 211
212#ifdef PICO_CD
017512f2 213 update_chips();
214#else
b0677887 215 if (PicoLineHook) PicoLineHook();
bf5fbbb4 216#endif
69996cb7 217
5e128c6d 218 lines = scanlines_total;
219 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
69996cb7 220
b6d7ac70 221 for (y++; y < lines; y++)
69996cb7 222 {
9761a7d0 223 pv->v_counter = Pico.m.scanline = y;
224 if (y >= vcnt_wrap)
225 pv->v_counter -= Pico.m.pal ? 56 : 6;
226 if ((pv->reg[12]&6) == 6)
227 pv->v_counter = (pv->v_counter << 1) | 1;
228 pv->v_counter &= 0xff;
69996cb7 229
230 PAD_DELAY
bf5fbbb4 231#ifdef PICO_CD
232 check_cd_dma();
233#endif
db1d3564 234#ifdef PICO_32X
1d7a28a7 235 p32x_timers_do(1);
db1d3564 236#endif
69996cb7 237
69996cb7 238 // Run scanline:
239 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 240 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 241
242#ifdef PICO_CD
243 update_chips();
017512f2 244#else
b0677887 245 if (PicoLineHook) PicoLineHook();
bf5fbbb4 246#endif
69996cb7 247 }
248
4b9c5888 249 // sync z80
be297089 250 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
e53704e6 251 PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
4b9c5888 252 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
253 PsndDoDAC(lines-1);
254
e53704e6 255 timers_cycle();
256
69996cb7 257 return 0;
258}
259
260#undef PAD_DELAY
bf5fbbb4 261#undef CPUS_RUN
69996cb7 262