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