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