fix a corner case with h-int
[picodrive.git] / pico / pico_cmn.c
CommitLineData
cff531af 1/*
89dbbf2b 2 * common code for base/cd/32x
3 * (C) notaz, 2007-2009,2013
cff531af 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
22814963 10#define CYCLES_M68K_VINT_LAG 112
69996cb7 11
12// pad delay (for 6 button pads)
531a8f38 13#define PAD_DELAY() { \
14 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
15 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
16}
69996cb7 17
bf5fbbb4 18// CPUS_RUN
c987bb5c 19#ifndef CPUS_RUN
ae214f1c 20#define CPUS_RUN(m68k_cycles) \
c987bb5c 21 SekRunM68k(m68k_cycles)
bf5fbbb4 22#endif
23
88fd63ad 24// sync m68k to Pico.t.m68c_aim
08769494 25static void SekSyncM68k(void)
ed4402a7 26{
27 int cyc_do;
28 pprof_start(m68k);
19886062 29 pevt_log_m68k_o(EVT_RUN_START);
ed4402a7 30
88fd63ad 31 while ((cyc_do = Pico.t.m68c_aim - Pico.t.m68c_cnt) > 0) {
32 Pico.t.m68c_cnt += cyc_do;
ae214f1c 33
34#if defined(EMU_C68K)
35 PicoCpuCM68k.cycles = cyc_do;
36 CycloneRun(&PicoCpuCM68k);
88fd63ad 37 Pico.t.m68c_cnt -= PicoCpuCM68k.cycles;
ed4402a7 38#elif defined(EMU_M68K)
88fd63ad 39 Pico.t.m68c_cnt += m68k_execute(cyc_do) - cyc_do;
ed4402a7 40#elif defined(EMU_F68K)
12f23dac 41 Pico.t.m68c_cnt += fm68k_emulate(&PicoCpuFM68k, cyc_do, 0) - cyc_do;
ed4402a7 42#endif
ae214f1c 43 }
44
45 SekCyclesLeft = 0;
ed4402a7 46
12da51c2 47 SekTrace(0);
19886062 48 pevt_log_m68k_o(EVT_RUN_END);
ed4402a7 49 pprof_end(m68k);
50}
51
e9a11abb 52static __inline void SekRunM68k(int cyc)
08769494 53{
88fd63ad 54 Pico.t.m68c_aim += cyc;
55 cyc = Pico.t.m68c_aim - Pico.t.m68c_cnt;
e42a47e2 56 if (cyc <= 0)
57 return;
88fd63ad 58 Pico.t.m68c_cnt += cyc >> 6; // refresh slowdowns
08769494 59 SekSyncM68k();
60}
61
e42a47e2 62static void do_hint(struct PicoVideo *pv)
63{
64 pv->pending_ints |= 0x10;
65 if (pv->reg[0] & 0x10) {
66 elprintf(EL_INTS, "hint: @ %06x [%u]", SekPc, SekCyclesDone());
67 SekInterrupt(4);
68 }
69}
70
334f00e2 71static void do_timing_hacks_as(struct PicoVideo *pv, int vdp_slots)
72{
73 pv->lwrite_cnt += vdp_slots - Pico.m.dma_xfers * 2; // wrong *2
74 if (pv->lwrite_cnt > vdp_slots)
75 pv->lwrite_cnt = vdp_slots;
76 else if (pv->lwrite_cnt < 0)
77 pv->lwrite_cnt = 0;
78 if (Pico.m.dma_xfers)
79 SekCyclesBurn(CheckDMA());
80}
81
82static void do_timing_hacks_vb(void)
83{
35f2b65e 84 if (unlikely(Pico.m.dma_xfers))
334f00e2 85 SekCyclesBurn(CheckDMA());
86}
87
69996cb7 88static int PicoFrameHints(void)
89{
e42a47e2 90 struct PicoVideo *pv = &Pico.video;
91 int line_sample = Pico.m.pal ? 68 : 93;
334f00e2 92 int vdp_slots = (Pico.video.reg[12] & 1) ? 18 : 16;
e42a47e2 93 int lines, y, lines_vis, skip;
94 int vcnt_wrap, vcnt_adj;
ae214f1c 95 unsigned int cycles;
69996cb7 96 int hint; // Hint counter
97
19886062 98 pevt_log_m68k_o(EVT_FRAME_START);
538a6098 99
93f9619e 100 if ((PicoIn.opt&POPT_ALT_RENDERER) && !PicoIn.skipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
03e4f2a3 101 // draw a frame just after vblank in alternative render mode
102 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
103 PicoFrameFull();
104#ifdef DRAW_FINISH_FUNC
105 DRAW_FINISH_FUNC();
106#endif
107 skip = 1;
108 }
93f9619e 109 else skip=PicoIn.skipFrame;
03e4f2a3 110
0e4bde9b 111 Pico.t.m68c_frame_start = Pico.t.m68c_aim;
3162a710 112 pv->v_counter = Pico.m.scanline = 0;
4b9c5888 113 z80_resetCycles();
4f2cdbf5 114 PsndStartFrame();
69996cb7 115
0a0073dc 116 hint = pv->hint_cnt;
e42a47e2 117 pv->status |= PVS_ACTIVE;
69996cb7 118
e42a47e2 119 for (y = 0; ; y++)
69996cb7 120 {
9761a7d0 121 pv->v_counter = Pico.m.scanline = y;
122 if ((pv->reg[12]&6) == 6) { // interlace mode 2
123 pv->v_counter <<= 1;
124 pv->v_counter |= pv->v_counter >> 8;
125 pv->v_counter &= 0xff;
126 }
69996cb7 127
e42a47e2 128 if ((y == 224 && !(pv->reg[1] & 8)) || y == 240)
129 break;
130
531a8f38 131 PAD_DELAY();
69996cb7 132
133 // H-Interrupts:
e42a47e2 134 if (--hint < 0)
69996cb7 135 {
e42a47e2 136 hint = pv->reg[10]; // Reload H-Int counter
137 do_hint(pv);
69996cb7 138 }
139
140 // decide if we draw this line
93f9619e 141 if (!skip && (PicoIn.opt & POPT_ALT_RENDERER))
fad24893 142 {
b6d7ac70 143 // find the right moment for frame renderer, when display is no longer blanked
144 if ((pv->reg[1]&0x40) || y > 100) {
145 PicoFrameFull();
fad24893 146#ifdef DRAW_FINISH_FUNC
b6d7ac70 147 DRAW_FINISH_FUNC();
fad24893 148#endif
b6d7ac70 149 skip = 1;
fad24893 150 }
151 }
69996cb7 152
69996cb7 153 // get samples from sound chips
6311a3ba 154 if ((y == 224 || y == line_sample) && PicoIn.sndOut)
4b9c5888 155 {
ae214f1c 156 cycles = SekCyclesDone();
157
93f9619e 158 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80))
ae214f1c 159 PicoSyncZ80(cycles);
ae214f1c 160#ifdef PICO_CD
93f9619e 161 if (PicoIn.AHW & PAHW_MCD)
fa8fb754 162 pcd_sync_s68k(cycles, 0);
ae214f1c 163#endif
a8fd6e37 164#ifdef PICO_32X
ae214f1c 165 p32x_sync_sh2s(cycles);
a8fd6e37 166#endif
7b3f44c6 167 PsndGetSamples(y);
4b9c5888 168 }
69996cb7 169
170 // Run scanline:
0e4bde9b 171 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 172 do_timing_hacks_as(pv, vdp_slots);
ae214f1c 173 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 174
b0677887 175 if (PicoLineHook) PicoLineHook();
19886062 176 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 177 }
178
e42a47e2 179 lines_vis = (pv->reg[1] & 8) ? 240 : 224;
180 if (y == lines_vis)
181 pv->status &= ~PVS_ACTIVE;
182
03e4f2a3 183 if (!skip)
b6d7ac70 184 {
ea38612f 185 if (Pico.est.DrawScanline < y)
b6d7ac70 186 PicoDrawSync(y - 1, 0);
187#ifdef DRAW_FINISH_FUNC
03e4f2a3 188 DRAW_FINISH_FUNC();
8ab3e3c1 189#endif
b6d7ac70 190 }
8ab3e3c1 191
69996cb7 192 // VDP FIFO
e42a47e2 193 pv->lwrite_cnt = 0;
194 Pico.video.status |= SR_EMPT;
69996cb7 195
93f9619e 196 memcpy(PicoIn.padInt, PicoIn.pad, sizeof(PicoIn.padInt));
531a8f38 197 PAD_DELAY();
69996cb7 198
e42a47e2 199 // Last H-Int (normally):
69996cb7 200 if (--hint < 0)
201 {
e42a47e2 202 hint = pv->reg[10]; // Reload H-Int counter
203 do_hint(pv);
69996cb7 204 }
205
0e4bde9b 206 pv->status |= SR_VB | PVS_VB2; // go into vblank
69996cb7 207
208 // the following SekRun is there for several reasons:
209 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
210 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
211 // also delay between last H-int and V-int (Golden Axe 3)
0e4bde9b 212 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 213 do_timing_hacks_vb();
ae214f1c 214 CPUS_RUN(CYCLES_M68K_VINT_LAG);
4b9c5888 215
0e4bde9b 216 pv->status |= SR_F;
22814963 217 pv->pending_ints |= 0x20;
e42a47e2 218 if (pv->reg[1] & 0x20) {
22814963 219 Pico.t.m68c_aim = Pico.t.m68c_cnt + 11; // HACK
220 SekSyncM68k();
e42a47e2 221 elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone());
69996cb7 222 SekInterrupt(6);
223 }
ae214f1c 224
225 cycles = SekCyclesDone();
93f9619e 226 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) {
ae214f1c 227 PicoSyncZ80(cycles);
583ab72c 228 elprintf(EL_INTS, "zint");
69996cb7 229 z80_int();
583ab72c 230 }
69996cb7 231
ae214f1c 232#ifdef PICO_CD
93f9619e 233 if (PicoIn.AHW & PAHW_MCD)
fa8fb754 234 pcd_sync_s68k(cycles, 0);
ae214f1c 235#endif
a8fd6e37 236#ifdef PICO_32X
ae214f1c 237 p32x_sync_sh2s(cycles);
a8fd6e37 238 p32x_start_blank();
239#endif
240
69996cb7 241 // get samples from sound chips
6311a3ba 242 if (y == 224 && PicoIn.sndOut)
7b3f44c6 243 PsndGetSamples(y);
69996cb7 244
245 // Run scanline:
e42a47e2 246 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG);
bf5fbbb4 247
b0677887 248 if (PicoLineHook) PicoLineHook();
19886062 249 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 250
e42a47e2 251 if (Pico.m.pal) {
252 lines = 313;
253 vcnt_wrap = 0x103;
254 vcnt_adj = 57;
255 }
256 else {
257 lines = 262;
258 vcnt_wrap = 0xEB;
259 vcnt_adj = 6;
260 }
69996cb7 261
e42a47e2 262 for (y++; y < lines - 1; y++)
69996cb7 263 {
9761a7d0 264 pv->v_counter = Pico.m.scanline = y;
265 if (y >= vcnt_wrap)
e42a47e2 266 pv->v_counter -= vcnt_adj;
9761a7d0 267 if ((pv->reg[12]&6) == 6)
268 pv->v_counter = (pv->v_counter << 1) | 1;
269 pv->v_counter &= 0xff;
69996cb7 270
531a8f38 271 PAD_DELAY();
69996cb7 272
35f2b65e 273 if (unlikely(pv->status & PVS_ACTIVE) && --hint < 0)
e42a47e2 274 {
275 hint = pv->reg[10]; // Reload H-Int counter
276 do_hint(pv);
277 }
278
69996cb7 279 // Run scanline:
0e4bde9b 280 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 281 do_timing_hacks_vb();
ae214f1c 282 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 283
b0677887 284 if (PicoLineHook) PicoLineHook();
19886062 285 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 286 }
287
35f2b65e 288 if (unlikely(PicoIn.overclockM68k)) {
289 unsigned int l = PicoIn.overclockM68k * lines / 100;
290 while (l-- > 0) {
291 Pico.t.m68c_cnt -= CYCLES_M68K_LINE;
292 do_timing_hacks_vb();
293 SekSyncM68k();
294 }
295 }
296
0e4bde9b 297 pv->status &= ~(SR_VB | PVS_VB2);
298 pv->status |= ((pv->reg[1] >> 3) ^ SR_VB) & SR_VB; // forced blanking
e42a47e2 299
300 // last scanline
301 Pico.m.scanline = y;
302 pv->v_counter = 0xff;
334f00e2 303 pv->lwrite_cnt = 0;
e42a47e2 304
305 PAD_DELAY();
306
0a0073dc 307 if (unlikely(pv->status & PVS_ACTIVE)) {
308 if (--hint < 0) {
309 hint = pv->reg[10]; // Reload H-Int counter
310 do_hint(pv);
311 }
e42a47e2 312 }
0a0073dc 313 else
314 hint = pv->reg[10];
e42a47e2 315
316 // Run scanline:
0e4bde9b 317 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 318 do_timing_hacks_as(pv, vdp_slots);
e42a47e2 319 CPUS_RUN(CYCLES_M68K_LINE);
320
321 if (PicoLineHook) PicoLineHook();
322 pevt_log_m68k_o(EVT_NEXT_LINE);
323
ae214f1c 324 // sync cpus
325 cycles = SekCyclesDone();
93f9619e 326 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80))
ae214f1c 327 PicoSyncZ80(cycles);
6311a3ba 328 if (PicoIn.sndOut && ym2612.dacen && Pico.snd.dac_line < lines)
4f2cdbf5 329 PsndDoDAC(lines - 1);
6311a3ba 330 if (PicoIn.sndOut && Pico.snd.psg_line < lines)
5d638db0 331 PsndDoPSG(lines - 1);
4b9c5888 332
ae214f1c 333#ifdef PICO_CD
93f9619e 334 if (PicoIn.AHW & PAHW_MCD)
fa8fb754 335 pcd_sync_s68k(cycles, 0);
ae214f1c 336#endif
a8fd6e37 337#ifdef PICO_32X
ae214f1c 338 p32x_sync_sh2s(cycles);
a8fd6e37 339#endif
e53704e6 340 timers_cycle();
341
e42a47e2 342 pv->hint_cnt = hint;
343
69996cb7 344 return 0;
345}
346
347#undef PAD_DELAY
bf5fbbb4 348#undef CPUS_RUN
69996cb7 349
ae214f1c 350// vim:shiftwidth=2:ts=2:expandtab