cff531af |
1 | /* |
89dbbf2b |
2 | * common code for base/cd/32x |
3 | * (C) notaz, 2007-2009,2013 |
7bf552b5 |
4 | * (C) irixxxx, 2020-2024 |
cff531af |
5 | * |
6 | * This work is licensed under the terms of MAME license. |
7 | * See COPYING file in the top-level directory. |
8 | */ |
8b99ab90 |
9 | |
69996cb7 |
10 | #define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC |
22814963 |
11 | #define CYCLES_M68K_VINT_LAG 112 |
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 | |
88fd63ad |
25 | // sync m68k to Pico.t.m68c_aim |
43e14010 |
26 | static void SekExecM68k(int cyc_do) |
ed4402a7 |
27 | { |
43e14010 |
28 | Pico.t.m68c_cnt += cyc_do; |
ae214f1c |
29 | |
30 | #if defined(EMU_C68K) |
43e14010 |
31 | PicoCpuCM68k.cycles = cyc_do; |
32 | CycloneRun(&PicoCpuCM68k); |
33 | Pico.t.m68c_cnt -= PicoCpuCM68k.cycles; |
ed4402a7 |
34 | #elif defined(EMU_M68K) |
43e14010 |
35 | Pico.t.m68c_cnt += m68k_execute(cyc_do) - cyc_do; |
ed4402a7 |
36 | #elif defined(EMU_F68K) |
43e14010 |
37 | Pico.t.m68c_cnt += fm68k_emulate(&PicoCpuFM68k, cyc_do, 0) - cyc_do; |
ed4402a7 |
38 | #endif |
8d67848d |
39 | SekCyclesLeft = 0; |
43e14010 |
40 | } |
41 | |
8eeb3426 |
42 | static int SekSyncM68k(int once) |
43e14010 |
43 | { |
44 | int cyc_do; |
134092fe |
45 | |
43e14010 |
46 | pprof_start(m68k); |
47 | pevt_log_m68k_o(EVT_RUN_START); |
48 | |
134092fe |
49 | while ((cyc_do = Pico.t.m68c_aim - Pico.t.m68c_cnt) > 0) { |
50 | // the Z80 CPU is stealing some bus cycles from the 68K main CPU when |
133006a9 |
51 | // accessing the main bus. Account for these by shortening the time |
134092fe |
52 | // the 68K CPU runs. |
9581731a |
53 | int z80_buscyc = Pico.t.z80_buscycles >> (4+(~Pico.m.scanline & 1)); |
3c049017 |
54 | // NB the Z80 isn't fast enough to steal more than half the bandwidth. |
4e3657f8 |
55 | // the fastest would be POP cc which takes 10+~3.3*2 z-cyc (~35 cyc) for a |
56 | // 16 bit value, but 68k is only blocked for ~16 cyc for the 2 bus cycles. |
3c049017 |
57 | if (z80_buscyc > cyc_do/2) |
58 | z80_buscyc = cyc_do/2; |
59 | SekExecM68k(cyc_do - z80_buscyc); |
134092fe |
60 | Pico.t.m68c_cnt += z80_buscyc; |
5d4a7fb9 |
61 | Pico.t.z80_buscycles -= z80_buscyc<<4; |
8eeb3426 |
62 | if (once) break; |
134092fe |
63 | } |
ae214f1c |
64 | |
12da51c2 |
65 | SekTrace(0); |
19886062 |
66 | pevt_log_m68k_o(EVT_RUN_END); |
ed4402a7 |
67 | pprof_end(m68k); |
8eeb3426 |
68 | |
69 | return Pico.t.m68c_aim > Pico.t.m68c_cnt; |
ed4402a7 |
70 | } |
71 | |
7263343d |
72 | static __inline void SekAimM68k(int cyc, int mult) |
08769494 |
73 | { |
7263343d |
74 | // refresh slowdown, for cart: 2 cycles every 128 - make this 1 every 64, |
75 | // for RAM: seems to be 0-3 every 128. Carts usually run from the cart |
76 | // area, but MCD games only use RAM, hence a different multiplier is needed. |
133006a9 |
77 | // NB must be quite accurate, so handle fractions as well (c/f OutRunners) |
7263343d |
78 | int delay = (Pico.t.refresh_delay += cyc*mult) >> 14; |
79 | Pico.t.m68c_cnt += delay; |
80 | Pico.t.refresh_delay -= delay << 14; |
88fd63ad |
81 | Pico.t.m68c_aim += cyc; |
7263343d |
82 | } |
134092fe |
83 | |
7263343d |
84 | static __inline void SekRunM68k(int cyc) |
85 | { |
71f9c68f |
86 | // TODO 0x100 would be 2 cycles/128, moreover far too sensitive |
274dd51a |
87 | SekAimM68k(cyc, 0x108); // OutRunners, testpico, VDPFIFOTesting |
8eeb3426 |
88 | SekSyncM68k(0); |
08769494 |
89 | } |
90 | |
fdaf9d10 |
91 | static void SyncCPUs(unsigned int cycles) |
92 | { |
93 | // sync cpus |
94 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) |
95 | PicoSyncZ80(cycles); |
96 | |
97 | #ifdef PICO_CD |
98 | if (PicoIn.AHW & PAHW_MCD) |
99 | pcd_sync_s68k(cycles, 0); |
100 | #endif |
101 | #ifdef PICO_32X |
102 | p32x_sync_sh2s(cycles); |
103 | #endif |
104 | } |
105 | |
e42a47e2 |
106 | static void do_hint(struct PicoVideo *pv) |
107 | { |
108 | pv->pending_ints |= 0x10; |
109 | if (pv->reg[0] & 0x10) { |
110 | elprintf(EL_INTS, "hint: @ %06x [%u]", SekPc, SekCyclesDone()); |
ba2b97dc |
111 | if (SekIrqLevel < pv->hint_irq) |
112 | SekInterrupt(pv->hint_irq); |
e42a47e2 |
113 | } |
114 | } |
115 | |
17bd69ad |
116 | static void do_timing_hacks_end(struct PicoVideo *pv) |
334f00e2 |
117 | { |
3b68e510 |
118 | PicoVideoFIFOSync(CYCLES_M68K_LINE); |
134092fe |
119 | |
120 | // need rather tight Z80 sync for emulation of main bus cycle stealing |
7263343d |
121 | if (Pico.m.scanline&1) |
133006a9 |
122 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) |
123 | PicoSyncZ80(Pico.t.m68c_aim); |
334f00e2 |
124 | } |
125 | |
17bd69ad |
126 | static void do_timing_hacks_start(struct PicoVideo *pv) |
334f00e2 |
127 | { |
134092fe |
128 | int cycles = PicoVideoFIFOHint(); |
129 | |
130 | SekCyclesBurn(cycles); // prolong cpu HOLD if necessary |
131 | // XXX how to handle Z80 bus cycle stealing during DMA correctly? |
5d4a7fb9 |
132 | if ((Pico.t.z80_buscycles -= cycles<<4) < 0) |
134092fe |
133 | Pico.t.z80_buscycles = 0; |
46b4c1d3 |
134 | Pico.t.m68c_aim += Pico.m.scanline&1; // add 1 every 2 lines for 488.5 cycles |
334f00e2 |
135 | } |
136 | |
69996cb7 |
137 | static int PicoFrameHints(void) |
138 | { |
e42a47e2 |
139 | struct PicoVideo *pv = &Pico.video; |
e42a47e2 |
140 | int lines, y, lines_vis, skip; |
69996cb7 |
141 | int hint; // Hint counter |
142 | |
19886062 |
143 | pevt_log_m68k_o(EVT_FRAME_START); |
538a6098 |
144 | |
fdaf9d10 |
145 | skip = PicoIn.skipFrame; |
03e4f2a3 |
146 | |
0e4bde9b |
147 | Pico.t.m68c_frame_start = Pico.t.m68c_aim; |
4f2cdbf5 |
148 | PsndStartFrame(); |
69996cb7 |
149 | |
0a0073dc |
150 | hint = pv->hint_cnt; |
fdaf9d10 |
151 | |
152 | // === active display === |
e42a47e2 |
153 | pv->status |= PVS_ACTIVE; |
69996cb7 |
154 | |
46b4c1d3 |
155 | for (y = 0; y < 240; y++) |
69996cb7 |
156 | { |
46b4c1d3 |
157 | if (y == 224 && !(pv->reg[1] & 8)) |
2c4675a9 |
158 | break; |
159 | |
46b4c1d3 |
160 | Pico.m.scanline = y; |
161 | pv->v_counter = PicoVideoGetV(y, 0); |
162 | |
531a8f38 |
163 | PAD_DELAY(); |
69996cb7 |
164 | |
165 | // H-Interrupts: |
e42a47e2 |
166 | if (--hint < 0) |
69996cb7 |
167 | { |
e42a47e2 |
168 | hint = pv->reg[10]; // Reload H-Int counter |
169 | do_hint(pv); |
69996cb7 |
170 | } |
171 | |
172 | // decide if we draw this line |
46b4c1d3 |
173 | if (unlikely(PicoIn.opt & POPT_ALT_RENDERER) && !skip) |
fad24893 |
174 | { |
b6d7ac70 |
175 | // find the right moment for frame renderer, when display is no longer blanked |
176 | if ((pv->reg[1]&0x40) || y > 100) { |
f9ed9446 |
177 | if (Pico.est.rendstatus & PDRAW_SYNC_NEEDED) |
178 | PicoFrameFull(); |
fad24893 |
179 | #ifdef DRAW_FINISH_FUNC |
b6d7ac70 |
180 | DRAW_FINISH_FUNC(); |
fad24893 |
181 | #endif |
f9ed9446 |
182 | Pico.est.rendstatus &= ~PDRAW_SYNC_NEEDED; |
b6d7ac70 |
183 | skip = 1; |
fad24893 |
184 | } |
185 | } |
69996cb7 |
186 | |
69996cb7 |
187 | // Run scanline: |
0e4bde9b |
188 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
17bd69ad |
189 | do_timing_hacks_start(pv); |
ae214f1c |
190 | CPUS_RUN(CYCLES_M68K_LINE); |
17bd69ad |
191 | do_timing_hacks_end(pv); |
bf5fbbb4 |
192 | |
b0677887 |
193 | if (PicoLineHook) PicoLineHook(); |
19886062 |
194 | pevt_log_m68k_o(EVT_NEXT_LINE); |
69996cb7 |
195 | } |
196 | |
fdaf9d10 |
197 | SyncCPUs(Pico.t.m68c_aim); |
198 | |
03e4f2a3 |
199 | if (!skip) |
b6d7ac70 |
200 | { |
ea38612f |
201 | if (Pico.est.DrawScanline < y) |
f9ed9446 |
202 | PicoVideoSync(-1); |
b6d7ac70 |
203 | #ifdef DRAW_FINISH_FUNC |
03e4f2a3 |
204 | DRAW_FINISH_FUNC(); |
8ab3e3c1 |
205 | #endif |
f9ed9446 |
206 | Pico.est.rendstatus &= ~PDRAW_SYNC_NEEDED; |
b6d7ac70 |
207 | } |
fdaf9d10 |
208 | #ifdef PICO_32X |
209 | p32x_render_frame(); |
210 | #endif |
8ab3e3c1 |
211 | |
c9d5f41b |
212 | // === VBLANK, 1st line === |
2c4675a9 |
213 | lines_vis = (pv->reg[1] & 8) ? 240 : 224; |
214 | if (y == lines_vis) |
215 | pv->status &= ~PVS_ACTIVE; |
46b4c1d3 |
216 | Pico.m.scanline = y; |
217 | pv->v_counter = PicoVideoGetV(y, 0); |
c9d5f41b |
218 | |
93f9619e |
219 | memcpy(PicoIn.padInt, PicoIn.pad, sizeof(PicoIn.padInt)); |
531a8f38 |
220 | PAD_DELAY(); |
69996cb7 |
221 | |
e42a47e2 |
222 | // Last H-Int (normally): |
69996cb7 |
223 | if (--hint < 0) |
224 | { |
e42a47e2 |
225 | hint = pv->reg[10]; // Reload H-Int counter |
226 | do_hint(pv); |
69996cb7 |
227 | } |
228 | |
0e4bde9b |
229 | pv->status |= SR_VB | PVS_VB2; // go into vblank |
c9d5f41b |
230 | #ifdef PICO_32X |
231 | p32x_start_blank(); |
232 | #endif |
69996cb7 |
233 | |
234 | // the following SekRun is there for several reasons: |
235 | // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga) |
236 | // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants) |
237 | // also delay between last H-int and V-int (Golden Axe 3) |
0e4bde9b |
238 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
099f68c2 |
239 | PicoVideoFIFOMode(pv->reg[1]&0x40, pv->reg[12]&1); |
17bd69ad |
240 | do_timing_hacks_start(pv); |
ae214f1c |
241 | CPUS_RUN(CYCLES_M68K_VINT_LAG); |
4b9c5888 |
242 | |
c9d5f41b |
243 | SyncCPUs(Pico.t.m68c_aim); |
244 | |
06ad4337 |
245 | // slot 0x00 |
0e4bde9b |
246 | pv->status |= SR_F; |
22814963 |
247 | pv->pending_ints |= 0x20; |
c9d5f41b |
248 | |
e42a47e2 |
249 | if (pv->reg[1] & 0x20) { |
4e3657f8 |
250 | // as per https://gendev.spritesmind.net/forum/viewtopic.php?t=2202, IRQ |
251 | // is usually sampled after operand reading, so the next instruction will |
252 | // be executed before the IRQ is taken. |
3c049017 |
253 | if (Pico.t.m68c_cnt - Pico.t.m68c_aim < 40) // CPU blocked? |
4e3657f8 |
254 | SekExecM68k(4); |
e42a47e2 |
255 | elprintf(EL_INTS, "vint: @ %06x [%u]", SekPc, SekCyclesDone()); |
4e3657f8 |
256 | // TODO: IRQ usually sampled after operand reading, so insn can't turn it |
257 | // off? single exception is MOVE.L which samples IRQ after the 1st write? |
69996cb7 |
258 | SekInterrupt(6); |
259 | } |
ae214f1c |
260 | |
06ad4337 |
261 | // slot 0x01 |
262 | if (pv->reg[12] & 2) |
263 | pv->status ^= SR_ODD; // change odd bit in interlace modes |
264 | else |
265 | pv->status &= ~SR_ODD; // never set in non-interlace modes |
266 | |
d97d056c |
267 | // assert Z80 interrupt for one scanline even in busrq hold (Teddy Blues) |
268 | if (/*Pico.m.z80Run &&*/ !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) { |
583ab72c |
269 | elprintf(EL_INTS, "zint"); |
d97d056c |
270 | z80_int_assert(1); |
583ab72c |
271 | } |
69996cb7 |
272 | |
69996cb7 |
273 | // Run scanline: |
e42a47e2 |
274 | CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG); |
17bd69ad |
275 | do_timing_hacks_end(pv); |
bf5fbbb4 |
276 | |
b0677887 |
277 | if (PicoLineHook) PicoLineHook(); |
19886062 |
278 | pevt_log_m68k_o(EVT_NEXT_LINE); |
69996cb7 |
279 | |
d97d056c |
280 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoIn.opt&POPT_EN_Z80)) |
281 | PicoSyncZ80(Pico.t.m68c_aim); |
282 | z80_int_assert(0); |
283 | |
fdaf9d10 |
284 | // === VBLANK === |
70216221 |
285 | lines = Pico.m.pal ? 313 : 262; |
e42a47e2 |
286 | for (y++; y < lines - 1; y++) |
69996cb7 |
287 | { |
70216221 |
288 | Pico.m.scanline = y; |
46b4c1d3 |
289 | pv->v_counter = PicoVideoGetV(y, 1); |
69996cb7 |
290 | |
531a8f38 |
291 | PAD_DELAY(); |
69996cb7 |
292 | |
35f2b65e |
293 | if (unlikely(pv->status & PVS_ACTIVE) && --hint < 0) |
e42a47e2 |
294 | { |
295 | hint = pv->reg[10]; // Reload H-Int counter |
296 | do_hint(pv); |
297 | } |
298 | |
69996cb7 |
299 | // Run scanline: |
0e4bde9b |
300 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
17bd69ad |
301 | do_timing_hacks_start(pv); |
ae214f1c |
302 | CPUS_RUN(CYCLES_M68K_LINE); |
17bd69ad |
303 | do_timing_hacks_end(pv); |
bf5fbbb4 |
304 | |
b0677887 |
305 | if (PicoLineHook) PicoLineHook(); |
19886062 |
306 | pevt_log_m68k_o(EVT_NEXT_LINE); |
69996cb7 |
307 | } |
308 | |
35f2b65e |
309 | if (unlikely(PicoIn.overclockM68k)) { |
310 | unsigned int l = PicoIn.overclockM68k * lines / 100; |
311 | while (l-- > 0) { |
312 | Pico.t.m68c_cnt -= CYCLES_M68K_LINE; |
17bd69ad |
313 | do_timing_hacks_start(pv); |
8eeb3426 |
314 | SekSyncM68k(0); |
17bd69ad |
315 | do_timing_hacks_end(pv); |
35f2b65e |
316 | } |
317 | } |
318 | |
fdaf9d10 |
319 | // === VBLANK last line === |
0e4bde9b |
320 | pv->status &= ~(SR_VB | PVS_VB2); |
321 | pv->status |= ((pv->reg[1] >> 3) ^ SR_VB) & SR_VB; // forced blanking |
e42a47e2 |
322 | |
323 | // last scanline |
8ac9ab7f |
324 | Pico.m.scanline = y++; |
e42a47e2 |
325 | pv->v_counter = 0xff; |
326 | |
327 | PAD_DELAY(); |
328 | |
0a0073dc |
329 | if (unlikely(pv->status & PVS_ACTIVE)) { |
330 | if (--hint < 0) { |
331 | hint = pv->reg[10]; // Reload H-Int counter |
332 | do_hint(pv); |
333 | } |
e42a47e2 |
334 | } |
0a0073dc |
335 | else |
336 | hint = pv->reg[10]; |
e42a47e2 |
337 | |
338 | // Run scanline: |
0e4bde9b |
339 | Pico.t.m68c_line_start = Pico.t.m68c_aim; |
099f68c2 |
340 | PicoVideoFIFOMode(pv->reg[1]&0x40, pv->reg[12]&1); |
17bd69ad |
341 | do_timing_hacks_start(pv); |
e42a47e2 |
342 | CPUS_RUN(CYCLES_M68K_LINE); |
17bd69ad |
343 | do_timing_hacks_end(pv); |
e42a47e2 |
344 | |
345 | if (PicoLineHook) PicoLineHook(); |
346 | pevt_log_m68k_o(EVT_NEXT_LINE); |
347 | |
fdaf9d10 |
348 | SyncCPUs(Pico.t.m68c_aim); |
a8fd6e37 |
349 | #ifdef PICO_32X |
fdaf9d10 |
350 | p32x_end_blank(); |
a8fd6e37 |
351 | #endif |
8ac9ab7f |
352 | |
353 | // get samples from sound chips |
9f1d5acd |
354 | PsndGetSamples(y); |
8ac9ab7f |
355 | |
a0ecb299 |
356 | timers_cycle(cycles_68k_to_z80(Pico.t.m68c_aim - Pico.t.m68c_frame_start)); |
274dd51a |
357 | z80_resetCycles(); |
e53704e6 |
358 | |
e42a47e2 |
359 | pv->hint_cnt = hint; |
360 | |
69996cb7 |
361 | return 0; |
362 | } |
363 | |
364 | #undef PAD_DELAY |
bf5fbbb4 |
365 | #undef CPUS_RUN |
69996cb7 |
366 | |
ae214f1c |
367 | // vim:shiftwidth=2:ts=2:expandtab |