cd sync improvements, part2
[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
69996cb7 12
13// pad delay (for 6 button pads)
531a8f38 14#define PAD_DELAY() { \
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}
69996cb7 18
bf5fbbb4 19// CPUS_RUN
c987bb5c 20#ifndef CPUS_RUN
ae214f1c 21#define CPUS_RUN(m68k_cycles) \
c987bb5c 22 SekRunM68k(m68k_cycles)
bf5fbbb4 23#endif
24
08769494 25// sync m68k to SekCycleAim
26static void SekSyncM68k(void)
ed4402a7 27{
28 int cyc_do;
29 pprof_start(m68k);
19886062 30 pevt_log_m68k_o(EVT_RUN_START);
ed4402a7 31
ae214f1c 32 while ((cyc_do = SekCycleAim - SekCycleCnt) > 0) {
33 SekCycleCnt += cyc_do;
34
35#if defined(EMU_C68K)
36 PicoCpuCM68k.cycles = cyc_do;
37 CycloneRun(&PicoCpuCM68k);
38 SekCycleCnt -= PicoCpuCM68k.cycles;
ed4402a7 39#elif defined(EMU_M68K)
ae214f1c 40 SekCycleCnt += m68k_execute(cyc_do) - cyc_do;
ed4402a7 41#elif defined(EMU_F68K)
ae214f1c 42 SekCycleCnt += fm68k_emulate(cyc_do, 0, 0) - cyc_do;
ed4402a7 43#endif
ae214f1c 44 }
45
46 SekCyclesLeft = 0;
ed4402a7 47
12da51c2 48 SekTrace(0);
19886062 49 pevt_log_m68k_o(EVT_RUN_END);
ed4402a7 50 pprof_end(m68k);
51}
52
08769494 53static inline void SekRunM68k(int cyc)
54{
55 SekCycleAim += cyc;
56 SekSyncM68k();
57}
58
69996cb7 59static int PicoFrameHints(void)
60{
61 struct PicoVideo *pv=&Pico.video;
9761a7d0 62 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
ae214f1c 63 unsigned int cycles;
69996cb7 64 int hint; // Hint counter
65
19886062 66 pevt_log_m68k_o(EVT_FRAME_START);
9761a7d0 67 pv->v_counter = Pico.m.scanline = 0;
538a6098 68
602133e1 69 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
03e4f2a3 70 // draw a frame just after vblank in alternative render mode
71 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
72 PicoFrameFull();
73#ifdef DRAW_FINISH_FUNC
74 DRAW_FINISH_FUNC();
75#endif
76 skip = 1;
77 }
78 else skip=PicoSkipFrame;
79
69996cb7 80 if (Pico.m.pal) {
69996cb7 81 line_sample = 68;
9761a7d0 82 if (pv->reg[1]&8) lines_vis = 240;
69996cb7 83 } else {
69996cb7 84 line_sample = 93;
85 }
86
4b9c5888 87 z80_resetCycles();
4b9c5888 88 PsndDacLine = 0;
7b3f44c6 89 emustatus &= ~1;
69996cb7 90
91 pv->status&=~0x88; // clear V-Int, come out of vblank
92
93 hint=pv->reg[10]; // Load H-Int counter
94 //dprintf("-hint: %i", hint);
95
96 // This is to make active scan longer (needed for Double Dragon 2, mainly)
ae214f1c 97 CPUS_RUN(CYCLES_M68K_ASD);
69996cb7 98
b6d7ac70 99 for (y = 0; y < lines_vis; y++)
69996cb7 100 {
9761a7d0 101 pv->v_counter = Pico.m.scanline = y;
102 if ((pv->reg[12]&6) == 6) { // interlace mode 2
103 pv->v_counter <<= 1;
104 pv->v_counter |= pv->v_counter >> 8;
105 pv->v_counter &= 0xff;
106 }
69996cb7 107
108 // VDP FIFO
109 pv->lwrite_cnt -= 12;
110 if (pv->lwrite_cnt <= 0) {
111 pv->lwrite_cnt=0;
112 Pico.video.status|=0x200;
113 }
114
531a8f38 115 PAD_DELAY();
69996cb7 116
117 // H-Interrupts:
118 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
119 {
120 hint=pv->reg[10]; // Reload H-Int counter
121 pv->pending_ints|=0x10;
122 if (pv->reg[0]&0x10) {
ae214f1c 123 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCyclesDone());
69996cb7 124 SekInterrupt(4);
125 }
126 }
127
128 // decide if we draw this line
b6d7ac70 129 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 130 {
b6d7ac70 131 // find the right moment for frame renderer, when display is no longer blanked
132 if ((pv->reg[1]&0x40) || y > 100) {
133 PicoFrameFull();
fad24893 134#ifdef DRAW_FINISH_FUNC
b6d7ac70 135 DRAW_FINISH_FUNC();
fad24893 136#endif
b6d7ac70 137 skip = 1;
fad24893 138 }
139 }
69996cb7 140
69996cb7 141 // get samples from sound chips
7b3f44c6 142 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 143 {
ae214f1c 144 cycles = SekCyclesDone();
145
be297089 146 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
ae214f1c 147 PicoSyncZ80(cycles);
4b9c5888 148 if (ym2612.dacen && PsndDacLine <= y)
149 PsndDoDAC(y);
ae214f1c 150#ifdef PICO_CD
08769494 151 pcd_sync_s68k(cycles, 0);
ae214f1c 152#endif
a8fd6e37 153#ifdef PICO_32X
ae214f1c 154 p32x_sync_sh2s(cycles);
a8fd6e37 155#endif
7b3f44c6 156 PsndGetSamples(y);
4b9c5888 157 }
69996cb7 158
159 // Run scanline:
160 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
ae214f1c 161 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 162
b0677887 163 if (PicoLineHook) PicoLineHook();
19886062 164 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 165 }
166
03e4f2a3 167 if (!skip)
b6d7ac70 168 {
169 if (DrawScanline < y)
170 PicoDrawSync(y - 1, 0);
171#ifdef DRAW_FINISH_FUNC
03e4f2a3 172 DRAW_FINISH_FUNC();
8ab3e3c1 173#endif
b6d7ac70 174 }
8ab3e3c1 175
947fb5f9 176 // V-int line (224 or 240)
177 Pico.m.scanline = y;
178 pv->v_counter = 0xe0; // bad for 240 mode
179 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
180
69996cb7 181 // VDP FIFO
182 pv->lwrite_cnt=0;
183 Pico.video.status|=0x200;
184
5f9a0d16 185 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
531a8f38 186 PAD_DELAY();
69996cb7 187
188 // Last H-Int:
189 if (--hint < 0)
190 {
191 hint=pv->reg[10]; // Reload H-Int counter
192 pv->pending_ints|=0x10;
ae214f1c 193 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCyclesDone());
69996cb7 194 if (pv->reg[0]&0x10) SekInterrupt(4);
195 }
196
69996cb7 197 pv->status|=0x08; // go into vblank
198 pv->pending_ints|=0x20;
199
200 // the following SekRun is there for several reasons:
201 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
202 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
203 // also delay between last H-int and V-int (Golden Axe 3)
ae214f1c 204 CPUS_RUN(CYCLES_M68K_VINT_LAG);
4b9c5888 205
69996cb7 206 if (pv->reg[1]&0x20) {
ae214f1c 207 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCyclesDone());
69996cb7 208 SekInterrupt(6);
209 }
ae214f1c 210
211 cycles = SekCyclesDone();
be297089 212 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
ae214f1c 213 PicoSyncZ80(cycles);
583ab72c 214 elprintf(EL_INTS, "zint");
69996cb7 215 z80_int();
583ab72c 216 }
69996cb7 217
ae214f1c 218#ifdef PICO_CD
08769494 219 pcd_sync_s68k(cycles, 0);
ae214f1c 220#endif
a8fd6e37 221#ifdef PICO_32X
ae214f1c 222 p32x_sync_sh2s(cycles);
a8fd6e37 223 p32x_start_blank();
224#endif
225
69996cb7 226 // get samples from sound chips
7b3f44c6 227 if (y == 224 && PsndOut)
228 {
229 if (ym2612.dacen && PsndDacLine <= y)
230 PsndDoDAC(y);
231 PsndGetSamples(y);
232 }
69996cb7 233
234 // Run scanline:
235 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
ae214f1c 236 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD);
bf5fbbb4 237
b0677887 238 if (PicoLineHook) PicoLineHook();
19886062 239 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 240
5e128c6d 241 lines = scanlines_total;
242 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
69996cb7 243
b6d7ac70 244 for (y++; y < lines; y++)
69996cb7 245 {
9761a7d0 246 pv->v_counter = Pico.m.scanline = y;
247 if (y >= vcnt_wrap)
248 pv->v_counter -= Pico.m.pal ? 56 : 6;
249 if ((pv->reg[12]&6) == 6)
250 pv->v_counter = (pv->v_counter << 1) | 1;
251 pv->v_counter &= 0xff;
69996cb7 252
531a8f38 253 PAD_DELAY();
69996cb7 254
69996cb7 255 // Run scanline:
256 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
ae214f1c 257 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 258
b0677887 259 if (PicoLineHook) PicoLineHook();
19886062 260 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 261 }
262
ae214f1c 263 // sync cpus
264 cycles = SekCyclesDone();
be297089 265 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
ae214f1c 266 PicoSyncZ80(cycles);
4b9c5888 267 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
268 PsndDoDAC(lines-1);
269
ae214f1c 270#ifdef PICO_CD
08769494 271 pcd_sync_s68k(cycles, 0);
ae214f1c 272#endif
a8fd6e37 273#ifdef PICO_32X
ae214f1c 274 p32x_sync_sh2s(cycles);
a8fd6e37 275#endif
e53704e6 276 timers_cycle();
277
69996cb7 278 return 0;
279}
280
281#undef PAD_DELAY
bf5fbbb4 282#undef CPUS_RUN
69996cb7 283
ae214f1c 284// vim:shiftwidth=2:ts=2:expandtab