famec: split fm68k_emulate
[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{
84 if (Pico.m.dma_xfers)
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
602133e1 100 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (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 }
109 else skip=PicoSkipFrame;
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
e42a47e2 116 // Load H-Int counter
117 hint = (pv->status & PVS_ACTIVE) ? pv->hint_cnt : pv->reg[10];
69996cb7 118
e42a47e2 119 pv->status |= PVS_ACTIVE;
69996cb7 120
e42a47e2 121 for (y = 0; ; y++)
69996cb7 122 {
9761a7d0 123 pv->v_counter = Pico.m.scanline = y;
124 if ((pv->reg[12]&6) == 6) { // interlace mode 2
125 pv->v_counter <<= 1;
126 pv->v_counter |= pv->v_counter >> 8;
127 pv->v_counter &= 0xff;
128 }
69996cb7 129
e42a47e2 130 if ((y == 224 && !(pv->reg[1] & 8)) || y == 240)
131 break;
132
531a8f38 133 PAD_DELAY();
69996cb7 134
135 // H-Interrupts:
e42a47e2 136 if (--hint < 0)
69996cb7 137 {
e42a47e2 138 hint = pv->reg[10]; // Reload H-Int counter
139 do_hint(pv);
69996cb7 140 }
141
142 // decide if we draw this line
b6d7ac70 143 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 144 {
b6d7ac70 145 // find the right moment for frame renderer, when display is no longer blanked
146 if ((pv->reg[1]&0x40) || y > 100) {
147 PicoFrameFull();
fad24893 148#ifdef DRAW_FINISH_FUNC
b6d7ac70 149 DRAW_FINISH_FUNC();
fad24893 150#endif
b6d7ac70 151 skip = 1;
fad24893 152 }
153 }
69996cb7 154
69996cb7 155 // get samples from sound chips
7b3f44c6 156 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 157 {
ae214f1c 158 cycles = SekCyclesDone();
159
be297089 160 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
ae214f1c 161 PicoSyncZ80(cycles);
ae214f1c 162#ifdef PICO_CD
fa8fb754 163 if (PicoAHW & PAHW_MCD)
164 pcd_sync_s68k(cycles, 0);
ae214f1c 165#endif
a8fd6e37 166#ifdef PICO_32X
ae214f1c 167 p32x_sync_sh2s(cycles);
a8fd6e37 168#endif
7b3f44c6 169 PsndGetSamples(y);
4b9c5888 170 }
69996cb7 171
172 // Run scanline:
0e4bde9b 173 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 174 do_timing_hacks_as(pv, vdp_slots);
ae214f1c 175 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 176
b0677887 177 if (PicoLineHook) PicoLineHook();
19886062 178 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 179 }
180
e42a47e2 181 lines_vis = (pv->reg[1] & 8) ? 240 : 224;
182 if (y == lines_vis)
183 pv->status &= ~PVS_ACTIVE;
184
03e4f2a3 185 if (!skip)
b6d7ac70 186 {
ea38612f 187 if (Pico.est.DrawScanline < y)
b6d7ac70 188 PicoDrawSync(y - 1, 0);
189#ifdef DRAW_FINISH_FUNC
03e4f2a3 190 DRAW_FINISH_FUNC();
8ab3e3c1 191#endif
b6d7ac70 192 }
8ab3e3c1 193
69996cb7 194 // VDP FIFO
e42a47e2 195 pv->lwrite_cnt = 0;
196 Pico.video.status |= SR_EMPT;
69996cb7 197
5f9a0d16 198 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
531a8f38 199 PAD_DELAY();
69996cb7 200
e42a47e2 201 // Last H-Int (normally):
69996cb7 202 if (--hint < 0)
203 {
e42a47e2 204 hint = pv->reg[10]; // Reload H-Int counter
205 do_hint(pv);
69996cb7 206 }
207
0e4bde9b 208 pv->status |= SR_VB | PVS_VB2; // go into vblank
69996cb7 209
210 // the following SekRun is there for several reasons:
211 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
212 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
213 // also delay between last H-int and V-int (Golden Axe 3)
0e4bde9b 214 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 215 do_timing_hacks_vb();
ae214f1c 216 CPUS_RUN(CYCLES_M68K_VINT_LAG);
4b9c5888 217
0e4bde9b 218 pv->status |= SR_F;
22814963 219 pv->pending_ints |= 0x20;
e42a47e2 220 if (pv->reg[1] & 0x20) {
22814963 221 Pico.t.m68c_aim = Pico.t.m68c_cnt + 11; // HACK
222 SekSyncM68k();
e42a47e2 223 elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone());
69996cb7 224 SekInterrupt(6);
225 }
ae214f1c 226
227 cycles = SekCyclesDone();
be297089 228 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
ae214f1c 229 PicoSyncZ80(cycles);
583ab72c 230 elprintf(EL_INTS, "zint");
69996cb7 231 z80_int();
583ab72c 232 }
69996cb7 233
ae214f1c 234#ifdef PICO_CD
fa8fb754 235 if (PicoAHW & PAHW_MCD)
236 pcd_sync_s68k(cycles, 0);
ae214f1c 237#endif
a8fd6e37 238#ifdef PICO_32X
ae214f1c 239 p32x_sync_sh2s(cycles);
a8fd6e37 240 p32x_start_blank();
241#endif
242
69996cb7 243 // get samples from sound chips
7b3f44c6 244 if (y == 224 && PsndOut)
7b3f44c6 245 PsndGetSamples(y);
69996cb7 246
247 // Run scanline:
e42a47e2 248 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG);
bf5fbbb4 249
b0677887 250 if (PicoLineHook) PicoLineHook();
19886062 251 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 252
e42a47e2 253 if (Pico.m.pal) {
254 lines = 313;
255 vcnt_wrap = 0x103;
256 vcnt_adj = 57;
257 }
258 else {
259 lines = 262;
260 vcnt_wrap = 0xEB;
261 vcnt_adj = 6;
262 }
69996cb7 263
e42a47e2 264 for (y++; y < lines - 1; y++)
69996cb7 265 {
9761a7d0 266 pv->v_counter = Pico.m.scanline = y;
267 if (y >= vcnt_wrap)
e42a47e2 268 pv->v_counter -= vcnt_adj;
9761a7d0 269 if ((pv->reg[12]&6) == 6)
270 pv->v_counter = (pv->v_counter << 1) | 1;
271 pv->v_counter &= 0xff;
69996cb7 272
531a8f38 273 PAD_DELAY();
69996cb7 274
e42a47e2 275 if ((pv->status & PVS_ACTIVE) && --hint < 0)
276 {
277 hint = pv->reg[10]; // Reload H-Int counter
278 do_hint(pv);
279 }
280
69996cb7 281 // Run scanline:
0e4bde9b 282 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 283 do_timing_hacks_vb();
ae214f1c 284 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 285
b0677887 286 if (PicoLineHook) PicoLineHook();
19886062 287 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 288 }
289
0e4bde9b 290 pv->status &= ~(SR_VB | PVS_VB2);
291 pv->status |= ((pv->reg[1] >> 3) ^ SR_VB) & SR_VB; // forced blanking
e42a47e2 292
293 // last scanline
294 Pico.m.scanline = y;
295 pv->v_counter = 0xff;
334f00e2 296 pv->lwrite_cnt = 0;
e42a47e2 297
298 PAD_DELAY();
299
300 if ((pv->status & PVS_ACTIVE) && --hint < 0)
301 {
302 hint = pv->reg[10]; // Reload H-Int counter
303 do_hint(pv);
304 }
305
306 // Run scanline:
0e4bde9b 307 Pico.t.m68c_line_start = Pico.t.m68c_aim;
334f00e2 308 do_timing_hacks_as(pv, vdp_slots);
e42a47e2 309 CPUS_RUN(CYCLES_M68K_LINE);
310
311 if (PicoLineHook) PicoLineHook();
312 pevt_log_m68k_o(EVT_NEXT_LINE);
313
ae214f1c 314 // sync cpus
315 cycles = SekCyclesDone();
be297089 316 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
ae214f1c 317 PicoSyncZ80(cycles);
4f2cdbf5 318 if (PsndOut && ym2612.dacen && PsndDacLine < lines)
319 PsndDoDAC(lines - 1);
5d638db0 320 if (PsndOut && PsndPsgLine < lines)
321 PsndDoPSG(lines - 1);
4b9c5888 322
ae214f1c 323#ifdef PICO_CD
fa8fb754 324 if (PicoAHW & PAHW_MCD)
325 pcd_sync_s68k(cycles, 0);
ae214f1c 326#endif
a8fd6e37 327#ifdef PICO_32X
ae214f1c 328 p32x_sync_sh2s(cycles);
a8fd6e37 329#endif
e53704e6 330 timers_cycle();
331
e42a47e2 332 pv->hint_cnt = hint;
333
69996cb7 334 return 0;
335}
336
337#undef PAD_DELAY
bf5fbbb4 338#undef CPUS_RUN
69996cb7 339
ae214f1c 340// vim:shiftwidth=2:ts=2:expandtab