f7d58be5f51185033410c649aa512950a4c03a5c
[picodrive.git] / pico / 32x / 32x.c
1 /*
2  * PicoDrive
3  * (C) notaz, 2009,2010,2013
4  * (C) irixxxx, 2019-2024
5  *
6  * This work is licensed under the terms of MAME license.
7  * See COPYING file in the top-level directory.
8  */
9 #include "../pico_int.h"
10 #include "../sound/ym2612.h"
11 #include <cpu/sh2/compiler.h>
12
13 struct Pico32x Pico32x;
14 SH2 sh2s[2];
15
16 #define SH2_IDLE_STATES (SH2_STATE_CPOLL|SH2_STATE_VPOLL|SH2_STATE_RPOLL|SH2_STATE_SLEEP)
17
18 static int REGPARM(2) sh2_irq_cb(SH2 *sh2, int level)
19 {
20   if (sh2->pending_irl > sh2->pending_int_irq) {
21     elprintf_sh2(sh2, EL_32X, "ack/irl %d @ %08x",
22       level, sh2_pc(sh2));
23     return 64 + sh2->pending_irl / 2;
24   } else {
25     elprintf_sh2(sh2, EL_32X, "ack/int %d/%d @ %08x",
26       level, sh2->pending_int_vector, sh2_pc(sh2));
27     sh2->pending_int_irq = 0; // auto-clear
28     sh2->pending_level = sh2->pending_irl;
29     return sh2->pending_int_vector;
30   }
31 }
32
33 // MUST specify active_sh2 when called from sh2 memhandlers
34 void p32x_update_irls(SH2 *active_sh2, unsigned int m68k_cycles)
35 {
36   int irqs, mlvl = 0, slvl = 0;
37   int mrun, srun;
38
39   if ((Pico32x.regs[0] & (P32XS_nRES|P32XS_ADEN)) != (P32XS_nRES|P32XS_ADEN))
40     return;
41
42   if (active_sh2 != NULL)
43     m68k_cycles = sh2_cycles_done_m68k(active_sh2);
44
45   // find top bit = highest irq number (0 <= irl <= 14/2) by binary search
46
47   // msh2
48   irqs = Pico32x.sh2irqi[0];
49   if (irqs >= 0x10)     mlvl += 8, irqs >>= 4;
50   if (irqs >= 0x04)     mlvl += 4, irqs >>= 2;
51   if (irqs >= 0x02)     mlvl += 2, irqs >>= 1;
52
53   // ssh2
54   irqs = Pico32x.sh2irqi[1];
55   if (irqs >= 0x10)     slvl += 8, irqs >>= 4;
56   if (irqs >= 0x04)     slvl += 4, irqs >>= 2;
57   if (irqs >= 0x02)     slvl += 2, irqs >>= 1;
58
59   mrun = sh2_irl_irq(&msh2, mlvl, msh2.state & SH2_STATE_RUN);
60   if (mrun) {
61     p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_IDLE_STATES & ~SH2_STATE_SLEEP, m68k_cycles);
62     if (msh2.state & SH2_STATE_RUN)
63       sh2_end_run(&msh2, 0);
64   }
65
66   srun = sh2_irl_irq(&ssh2, slvl, ssh2.state & SH2_STATE_RUN);
67   if (srun) {
68     p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_IDLE_STATES & ~SH2_STATE_SLEEP, m68k_cycles);
69     if (ssh2.state & SH2_STATE_RUN)
70       sh2_end_run(&ssh2, 0);
71   }
72
73   elprintf(EL_32X, "update_irls: m %d/%d, s %d/%d", mlvl, mrun, slvl, srun);
74 }
75
76 // the mask register is inconsistent, CMD is supposed to be a mask,
77 // while others are actually irq trigger enables?
78 // TODO: test on hw..
79 void p32x_trigger_irq(SH2 *sh2, unsigned int m68k_cycles, unsigned int mask)
80 {
81   Pico32x.sh2irqi[0] |= mask & P32XI_VRES;
82   Pico32x.sh2irqi[1] |= mask & P32XI_VRES;
83   Pico32x.sh2irqi[0] |= mask & (Pico32x.sh2irq_mask[0] << 3);
84   Pico32x.sh2irqi[1] |= mask & (Pico32x.sh2irq_mask[1] << 3);
85
86   p32x_update_irls(sh2, m68k_cycles);
87 }
88
89 void p32x_update_cmd_irq(SH2 *sh2, unsigned int m68k_cycles)
90 {
91   if ((Pico32x.sh2irq_mask[0] & 2) && (Pico32x.regs[2 / 2] & 1))
92     Pico32x.sh2irqi[0] |= P32XI_CMD;
93   else
94     Pico32x.sh2irqi[0] &= ~P32XI_CMD;
95
96   if ((Pico32x.sh2irq_mask[1] & 2) && (Pico32x.regs[2 / 2] & 2))
97     Pico32x.sh2irqi[1] |= P32XI_CMD;
98   else
99     Pico32x.sh2irqi[1] &= ~P32XI_CMD;
100
101   p32x_update_irls(sh2, m68k_cycles);
102 }
103
104 void Pico32xStartup(void)
105 {
106   elprintf(EL_STATUS|EL_32X, "32X startup");
107
108   PicoIn.AHW |= PAHW_32X;
109   // TODO: OOM handling
110   if (Pico32xMem == NULL) {
111     Pico32xMem = plat_mmap(0x06000000, sizeof(*Pico32xMem), 0, 0);
112     if (Pico32xMem == NULL) {
113       elprintf(EL_STATUS, "OOM");
114       return;
115     }
116     memset(Pico32xMem, 0, sizeof(struct Pico32xMem));
117
118     sh2_init(&msh2, 0, &ssh2);
119     msh2.irq_callback = sh2_irq_cb;
120     sh2_init(&ssh2, 1, &msh2);
121     ssh2.irq_callback = sh2_irq_cb;
122   }
123
124   PicoMemSetup32x();
125   p32x_pwm_ctl_changed();
126   p32x_timers_recalc();
127
128   Pico32x.regs[0] |= P32XS_ADEN;
129
130   Pico32x.sh2_regs[0] = P32XS2_ADEN;
131   if (Pico.m.ncart_in)
132     Pico32x.sh2_regs[0] |= P32XS2_nCART;
133
134   if (!Pico.m.pal)
135     Pico32x.vdp_regs[0] |= P32XV_nPAL;
136   else
137     Pico32x.vdp_regs[0] &= ~P32XV_nPAL;
138
139   rendstatus_old = -1;
140
141   Pico32xPrepare();
142   emu_32x_startup();
143 }
144
145 void Pico32xShutdown(void)
146 {
147   elprintf(EL_STATUS|EL_32X, "32X shutdown");
148   Pico32x.sh2_regs[0] &= ~P32XS2_ADEN;
149   Pico32x.regs[0] &= ~P32XS_ADEN;
150
151   rendstatus_old = -1;
152
153   PicoIn.AHW &= ~PAHW_32X;
154   if (PicoIn.AHW & PAHW_MCD)
155     PicoMemSetupCD();
156   else
157     PicoMemSetup();
158   emu_32x_startup();
159 }
160
161 void p32x_reset_sh2s(void)
162 {
163   elprintf(EL_32X, "sh2 reset");
164
165   sh2_reset(&msh2);
166   sh2_reset(&ssh2);
167   sh2_peripheral_reset(&msh2);
168   sh2_peripheral_reset(&ssh2);
169
170   // if we don't have BIOS set, perform it's work here.
171   // MSH2
172   if (p32x_bios_m == NULL) {
173     sh2_set_gbr(0, 0x20004000);
174
175     if (!Pico.m.ncart_in) { // copy IDL from cartridge
176       unsigned int idl_src, idl_dst, idl_size; // initial data load
177       unsigned int vbr;
178       // initial data
179       idl_src = CPU_BE2(*(u32 *)(Pico.rom + 0x3d4)) & ~0xf0000000;
180       idl_dst = CPU_BE2(*(u32 *)(Pico.rom + 0x3d8)) & ~0xf0000000;
181       idl_size= CPU_BE2(*(u32 *)(Pico.rom + 0x3dc));
182       // copy in guest memory space
183       idl_src += 0x2000000;
184       idl_dst += 0x6000000;
185       while (idl_size >= 4) {
186         p32x_sh2_write32(idl_dst, p32x_sh2_read32(idl_src, &msh2), &msh2);
187         idl_src += 4, idl_dst += 4, idl_size -= 4;
188       }
189
190       // VBR
191       vbr = CPU_BE2(*(u32 *)(Pico.rom + 0x3e8));
192       sh2_set_vbr(0, vbr);
193
194       // checksum and M_OK
195       Pico32x.regs[0x28 / 2] = *(u16 *)(Pico.rom + 0x18e);
196     }
197     // program will set M_OK
198   }
199
200   // SSH2
201   if (p32x_bios_s == NULL) {
202     unsigned int vbr;
203
204     // GBR/VBR
205     vbr = CPU_BE2(*(u32 *)(Pico.rom + 0x3ec));
206     sh2_set_gbr(1, 0x20004000);
207     sh2_set_vbr(1, vbr);
208     // program will set S_OK
209   }
210
211   msh2.m68krcycles_done = ssh2.m68krcycles_done = SekCyclesDone();
212 }
213
214 void Pico32xInit(void)
215 {
216 }
217
218 void PicoPower32x(void)
219 {
220   memset(&Pico32x, 0, sizeof(Pico32x));
221
222   Pico32x.regs[0] = P32XS_REN|P32XS_nRES; // verified
223   Pico32x.regs[0x10/2] = 0xffff;
224   Pico32x.vdp_regs[0x0a/2] = P32XV_VBLK|P32XV_PEN;
225 }
226
227 void PicoUnload32x(void)
228 {
229   if (PicoIn.AHW & PAHW_32X)
230     Pico32xShutdown();
231
232   sh2_finish(&msh2);
233   sh2_finish(&ssh2);
234
235   if (Pico32xMem != NULL)
236     plat_munmap(Pico32xMem, sizeof(*Pico32xMem));
237   Pico32xMem = NULL;
238 }
239
240 void PicoReset32x(void)
241 {
242   if (PicoIn.AHW & PAHW_32X) {
243     p32x_trigger_irq(NULL, SekCyclesDone(), P32XI_VRES);
244     p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_IDLE_STATES, SekCyclesDone());
245     p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_IDLE_STATES, SekCyclesDone());
246     p32x_pwm_ctl_changed();
247     p32x_timers_recalc();
248   }
249 }
250
251 static void Pico32xRenderSync(int lines)
252 {
253   if (Pico32xDrawMode != PDM32X_OFF && !PicoIn.skipFrame) {
254     int offs;
255
256     pprof_start(draw);
257
258     offs = 8;
259     if (Pico.video.reg[1] & 8)
260       offs = 0;
261
262     if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0 && // 32x not blanking
263         (!(Pico.video.debug_p & PVD_KILL_32X)))
264     {
265       int md_bg = Pico.video.reg[7] & 0x3f;
266
267       // we draw lines up to the sync point (not line-by-line)
268       PicoDraw32xLayer(offs, lines-Pico32x.sync_line, md_bg);
269     }
270     else if (Pico32xDrawMode == PDM32X_BOTH)
271       PicoDraw32xLayerMdOnly(offs, lines-Pico32x.sync_line);
272
273     pprof_end(draw);
274   }
275 }
276
277 void Pico32xDrawSync(SH2 *sh2)
278 {
279   // the fast renderer isn't operating on a line-by-line base
280   if (sh2 && !(PicoIn.opt & POPT_ALT_RENDERER)) {
281     unsigned int cycle = (sh2 ? sh2_cycles_done_m68k(sh2) : SekCyclesDone());
282     int line = ((cycle - Pico.t.m68c_frame_start) * (long long)((1LL<<32)/488.5)) >> 32;
283
284     if (Pico32x.sync_line < line && line < (Pico.video.reg[1] & 8 ? 240 : 224)) {
285       // make sure the MD image is also sync'ed to this line for merging
286       PicoDrawSync(line, 0, 0);
287
288       // pfff... need to save and restore some persistent data for MD renderer
289       void *dest = Pico.est.DrawLineDest;
290       int incr = Pico.est.DrawLineDestIncr;
291       Pico32xRenderSync(line);
292       Pico.est.DrawLineDest = dest;
293       Pico.est.DrawLineDestIncr = incr;
294     }
295
296     // remember line we sync'ed to
297     Pico32x.sync_line = line;
298   }
299 }
300
301 static void p32x_render_frame(void)
302 {
303   if (Pico32xDrawMode != PDM32X_OFF && !PicoIn.skipFrame) {
304     int lines;
305
306     pprof_start(draw);
307
308     lines = 224;
309     if (Pico.video.reg[1] & 8)
310       lines = 240;
311
312     Pico32xRenderSync(lines);
313   }
314 }
315
316 static void p32x_start_blank(void)
317 {
318   // enter vblank
319   Pico32x.vdp_regs[0x0a/2] |= P32XV_VBLK|P32XV_PEN;
320
321   // FB swap waits until vblank
322   if ((Pico32x.vdp_regs[0x0a/2] ^ Pico32x.pending_fb) & P32XV_FS) {
323     Pico32x.vdp_regs[0x0a/2] ^= P32XV_FS;
324     Pico32xSwapDRAM(Pico32x.pending_fb ^ P32XV_FS);
325   }
326
327   p32x_trigger_irq(NULL, Pico.t.m68c_aim, P32XI_VINT);
328   p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_STATE_VPOLL, Pico.t.m68c_aim);
329   p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_STATE_VPOLL, Pico.t.m68c_aim);
330 }
331
332 static void p32x_end_blank(void)
333 {
334   // end vblank
335   Pico32x.vdp_regs[0x0a/2] &= ~P32XV_VBLK; // get out of vblank
336   if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0) // no forced blanking
337     Pico32x.vdp_regs[0x0a/2] &= ~P32XV_PEN; // no palette access
338   if (!(Pico32x.sh2_regs[0] & 0x80)) {
339     // NB must precede VInt per hw manual, min 4 SH-2 cycles to pass Mars Check
340     Pico32x.hint_counter = (int)(-1.5*0x10);
341     p32x_schedule_hint(NULL, Pico.t.m68c_aim);
342   }
343
344   p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_STATE_VPOLL, Pico.t.m68c_aim);
345   p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_STATE_VPOLL, Pico.t.m68c_aim);
346 }
347
348 void p32x_schedule_hint(SH2 *sh2, unsigned int m68k_cycles)
349 {
350   // rather rough, 32x hint is useless in practice
351   int after;
352   if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 4))
353     return; // nobody cares
354   if (!(Pico32x.sh2_regs[0] & 0x80) && (Pico.video.status & PVS_VB2))
355     return;
356
357   Pico32x.hint_counter += (Pico32x.sh2_regs[4 / 2] + 1) * (int)(488.5*0x10);
358   after = Pico32x.hint_counter >> 4;
359   Pico32x.hint_counter &= 0xf;
360   if (sh2 != NULL)
361     p32x_event_schedule_sh2(sh2, P32X_EVENT_HINT, after);
362   else
363     p32x_event_schedule(m68k_cycles, P32X_EVENT_HINT, after);
364 }
365
366 /* events */
367 static void fillend_event(unsigned int now)
368 {
369   Pico32x.vdp_regs[0x0a/2] &= ~P32XV_nFEN;
370   p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_STATE_VPOLL, now);
371   p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_STATE_VPOLL, now);
372 }
373
374 static void hint_event(unsigned int now)
375 {
376   p32x_trigger_irq(NULL, now, P32XI_HINT);
377   p32x_schedule_hint(NULL, now);
378 }
379
380 typedef void (event_cb)(unsigned int now);
381
382 /* times are in m68k (7.6MHz) cycles */
383 unsigned int p32x_event_times[P32X_EVENT_COUNT];
384 static unsigned int event_time_next;
385 static event_cb *p32x_event_cbs[P32X_EVENT_COUNT] = {
386   p32x_pwm_irq_event, // P32X_EVENT_PWM
387   fillend_event,      // P32X_EVENT_FILLEND
388   hint_event,         // P32X_EVENT_HINT
389 };
390
391 // schedule event at some time 'after', in m68k clocks
392 void p32x_event_schedule(unsigned int now, enum p32x_event event, int after)
393 {
394   unsigned int when;
395
396   when = (now + after) | 1;
397
398   elprintf(EL_32X, "32x: new event #%u %u->%u", event, now, when);
399   p32x_event_times[event] = when;
400
401   if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
402     event_time_next = when;
403 }
404
405 void p32x_event_schedule_sh2(SH2 *sh2, enum p32x_event event, int after)
406 {
407   unsigned int now = sh2_cycles_done_m68k(sh2);
408   int left_to_next;
409
410   p32x_event_schedule(now, event, after);
411
412   left_to_next = C_M68K_TO_SH2(sh2, (int)(event_time_next - now));
413   if (sh2_cycles_left(sh2) > left_to_next) {
414     if (left_to_next < 1)
415       left_to_next = 0;
416     sh2_end_run(sh2, left_to_next);
417   }
418 }
419
420 static void p32x_run_events(unsigned int until)
421 {
422   int oldest, oldest_diff, time;
423   int i, diff;
424
425   while (1) {
426     oldest = -1, oldest_diff = 0x7fffffff;
427
428     for (i = 0; i < P32X_EVENT_COUNT; i++) {
429       if (p32x_event_times[i]) {
430         diff = p32x_event_times[i] - until;
431         if (diff < oldest_diff) {
432           oldest_diff = diff;
433           oldest = i;
434         }
435       }
436     }
437
438     if (oldest_diff <= 0) {
439       time = p32x_event_times[oldest];
440       p32x_event_times[oldest] = 0;
441       elprintf(EL_32X, "32x: run event #%d %u", oldest, time);
442       p32x_event_cbs[oldest](time);
443     }
444     else if (oldest_diff < 0x7fffffff) {
445       event_time_next = p32x_event_times[oldest];
446       break;
447     }
448     else {
449       event_time_next = 0;
450       break;
451     }
452   }
453
454   if (oldest != -1)
455     elprintf(EL_32X, "32x: next event #%d at %u",
456       oldest, event_time_next);
457 }
458
459 static void run_sh2(SH2 *sh2, unsigned int m68k_cycles)
460 {
461   unsigned int cycles, done;
462
463   pevt_log_sh2_o(sh2, EVT_RUN_START);
464   sh2->state |= SH2_STATE_RUN;
465   cycles = C_M68K_TO_SH2(sh2, m68k_cycles);
466   elprintf_sh2(sh2, EL_32X, "+run %u %d @%08x",
467     sh2->m68krcycles_done, cycles, sh2->pc);
468
469   done = sh2_execute(sh2, cycles);
470
471   sh2->m68krcycles_done += C_SH2_TO_M68K(sh2, done);
472   sh2->state &= ~SH2_STATE_RUN;
473   pevt_log_sh2_o(sh2, EVT_RUN_END);
474   elprintf_sh2(sh2, EL_32X, "-run %u %d",
475     sh2->m68krcycles_done, done);
476 }
477
478 // sync other sh2 to this one
479 // note: recursive call
480 void p32x_sync_other_sh2(SH2 *sh2, unsigned int m68k_target)
481 {
482   SH2 *osh2 = sh2->other_sh2;
483   int left_to_event;
484   int m68k_cycles;
485
486   if (osh2->state & SH2_STATE_RUN)
487     return;
488
489   m68k_cycles = m68k_target - osh2->m68krcycles_done;
490   if (m68k_cycles < 200)
491     return;
492
493   if (osh2->state & SH2_IDLE_STATES) {
494     osh2->m68krcycles_done = m68k_target;
495     return;
496   }
497
498   elprintf_sh2(osh2, EL_32X, "sync to %u %d",
499     m68k_target, m68k_cycles);
500
501   run_sh2(osh2, m68k_cycles);
502
503   // there might be new event to schedule current sh2 to
504   if (event_time_next) {
505     left_to_event = C_M68K_TO_SH2(sh2, (int)(event_time_next - m68k_target));
506     if (sh2_cycles_left(sh2) > left_to_event) {
507       if (left_to_event < 1)
508         left_to_event = 0;
509       sh2_end_run(sh2, left_to_event);
510     }
511   }
512 }
513
514 #define STEP_LS 24
515 #define STEP_N 528 // at least one line (488)
516
517 #define sync_sh2s_normal p32x_sync_sh2s
518 //#define sync_sh2s_lockstep p32x_sync_sh2s
519
520 /* most timing is in 68k clock */
521 void sync_sh2s_normal(unsigned int m68k_target)
522 {
523   unsigned int now, target, next, timer_cycles;
524   int cycles;
525
526   elprintf(EL_32X, "sh2 sync to %u", m68k_target);
527
528   if ((Pico32x.regs[0] & (P32XS_nRES|P32XS_ADEN)) != (P32XS_nRES|P32XS_ADEN)) {
529     msh2.m68krcycles_done = ssh2.m68krcycles_done = m68k_target;
530     return; // rare
531   }
532
533   now = msh2.m68krcycles_done;
534   if (CYCLES_GT(now, ssh2.m68krcycles_done))
535     now = ssh2.m68krcycles_done;
536   timer_cycles = now;
537
538   pprof_start(m68k);
539   while (CYCLES_GT(m68k_target, now))
540   {
541     if (event_time_next && CYCLES_GE(now, event_time_next))
542       p32x_run_events(now);
543
544     target = m68k_target;
545     if (event_time_next && CYCLES_GT(target, event_time_next))
546       target = event_time_next;
547     while (CYCLES_GT(target, now))
548     {
549       next = target;
550       if (CYCLES_GT(target, now + STEP_N))
551         next = now + STEP_N;
552       elprintf(EL_32X, "sh2 exec to %u %d,%d/%d, flags %x", next,
553         next - msh2.m68krcycles_done, next - ssh2.m68krcycles_done,
554         m68k_target - now, Pico32x.emu_flags);
555
556       pprof_start(ssh2);
557       if (!(ssh2.state & SH2_IDLE_STATES)) {
558         cycles = next - ssh2.m68krcycles_done;
559         if (cycles > 0) {
560           run_sh2(&ssh2, cycles > 20U ? cycles : 20U);
561
562           if (event_time_next && CYCLES_GT(target, event_time_next))
563             target = event_time_next;
564           if (CYCLES_GT(next, target))
565             next = target;
566         }
567       }
568       pprof_end(ssh2);
569
570       pprof_start(msh2);
571       if (!(msh2.state & SH2_IDLE_STATES)) {
572         cycles = next - msh2.m68krcycles_done;
573         if (cycles > 0) {
574           run_sh2(&msh2, cycles > 20U ? cycles : 20U);
575
576           if (event_time_next && CYCLES_GT(target, event_time_next))
577             target = event_time_next;
578           if (CYCLES_GT(next, target))
579             next = target;
580         }
581       }
582       pprof_end(msh2);
583
584       now = next;
585       if (CYCLES_GT(now, msh2.m68krcycles_done)) {
586         if (!(msh2.state & SH2_IDLE_STATES))
587           now = msh2.m68krcycles_done;
588       }
589       if (CYCLES_GT(now, ssh2.m68krcycles_done)) {
590         if (!(ssh2.state & SH2_IDLE_STATES)) 
591           now = ssh2.m68krcycles_done;
592       }
593       if (CYCLES_GT(now, timer_cycles+STEP_N)) {
594         if  (msh2.state & SH2_TIMER_RUN)
595           p32x_timer_do(&msh2, now - timer_cycles);
596         if  (ssh2.state & SH2_TIMER_RUN)
597           p32x_timer_do(&ssh2, now - timer_cycles);
598         timer_cycles = now;
599       }
600     }
601
602     if  (msh2.state & SH2_TIMER_RUN)
603       p32x_timer_do(&msh2, now - timer_cycles);
604     if  (ssh2.state & SH2_TIMER_RUN)
605       p32x_timer_do(&ssh2, now - timer_cycles);
606     timer_cycles = now;
607   }
608   pprof_end_sub(m68k);
609
610   // advance idle CPUs
611   if (msh2.state & SH2_IDLE_STATES) {
612     if (CYCLES_GT(m68k_target, msh2.m68krcycles_done))
613       msh2.m68krcycles_done = m68k_target;
614   }
615   if (ssh2.state & SH2_IDLE_STATES) {
616     if (CYCLES_GT(m68k_target, ssh2.m68krcycles_done))
617       ssh2.m68krcycles_done = m68k_target;
618   }
619
620   // everyone is in sync now
621   Pico32x.comm_dirty = 0;
622 }
623
624 void sync_sh2s_lockstep(unsigned int m68k_target)
625 {
626   unsigned int mcycles;
627   
628   mcycles = msh2.m68krcycles_done;
629   if (CYCLES_GT(mcycles, ssh2.m68krcycles_done))
630     mcycles = ssh2.m68krcycles_done;
631
632   while (CYCLES_GT(m68k_target, mcycles)) {
633     mcycles += STEP_LS;
634     sync_sh2s_normal(mcycles);
635   }
636 }
637
638 #define CPUS_RUN(m68k_cycles) do { \
639   if (PicoIn.AHW & PAHW_MCD) \
640     pcd_run_cpus(m68k_cycles); \
641   else \
642     SekRunM68k(m68k_cycles); \
643   \
644   if ((Pico32x.emu_flags & P32XF_Z80_32X_IO) && Pico.m.z80Run \
645       && !Pico.m.z80_reset && (PicoIn.opt & POPT_EN_Z80)) \
646     PicoSyncZ80(SekCyclesDone()); \
647   if (Pico32x.emu_flags & (P32XF_68KCPOLL|P32XF_68KVPOLL)) \
648     p32x_sync_sh2s(SekCyclesDone()); \
649 } while (0)
650
651 #define PICO_32X
652 #define PICO_CD
653 #include "../pico_cmn.c"
654
655 void PicoFrame32x(void)
656 {
657   if (PicoIn.AHW & PAHW_MCD)
658     pcd_prepare_frame();
659
660   PicoFrameStart();
661   Pico32x.sync_line = 0;
662   if (Pico32xDrawMode != PDM32X_BOTH)
663     Pico.est.rendstatus |= PDRAW_SYNC_NEEDED;
664   PicoFrameHints();
665
666   elprintf(EL_32X, "poll: %02x %02x %02x",
667     Pico32x.emu_flags & 3, msh2.state, ssh2.state);
668 }
669
670 // calculate multipliers against 68k clock (7670442)
671 // normally * 3, but effectively slower due to high latencies everywhere
672 // however using something lower breaks MK2 animations
673 void Pico32xSetClocks(int msh2_hz, int ssh2_hz)
674 {
675   float m68k_clk = (float)(OSC_NTSC / 7);
676   if (msh2_hz > 0) {
677     msh2.mult_m68k_to_sh2 = (int)((float)msh2_hz * (1 << CYCLE_MULT_SHIFT) / m68k_clk);
678     msh2.mult_sh2_to_m68k = (int)(m68k_clk * (1 << CYCLE_MULT_SHIFT) / (float)msh2_hz);
679   }
680   if (ssh2_hz > 0) {
681     ssh2.mult_m68k_to_sh2 = (int)((float)ssh2_hz * (1 << CYCLE_MULT_SHIFT) / m68k_clk);
682     ssh2.mult_sh2_to_m68k = (int)(m68k_clk * (1 << CYCLE_MULT_SHIFT) / (float)ssh2_hz);
683   }
684 }
685
686 void Pico32xStateLoaded(int is_early)
687 {
688   if (is_early) {
689     Pico32xMemStateLoaded();
690     return;
691   }
692
693   if (CYCLES_GE(sh2s[0].m68krcycles_done - Pico.t.m68c_aim, 500) ||
694       CYCLES_GE(sh2s[1].m68krcycles_done - Pico.t.m68c_aim, 500))
695     sh2s[0].m68krcycles_done = sh2s[1].m68krcycles_done = SekCyclesDone();
696   p32x_update_irls(NULL, SekCyclesDone());
697   p32x_timers_recalc();
698   p32x_pwm_state_loaded();
699   p32x_run_events(SekCyclesDone());
700
701   // TODO wakeup CPUs for now. poll detection stuff must go to the save state!
702   p32x_m68k_poll_event(0, -1);
703   p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_IDLE_STATES, msh2.m68krcycles_done);
704   p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_IDLE_STATES, ssh2.m68krcycles_done);
705 }
706
707 void Pico32xPrepare(void)
708 {
709   // fallback in case it was missing in saved config
710   if (msh2.mult_m68k_to_sh2 == 0 || msh2.mult_sh2_to_m68k == 0)
711     Pico32xSetClocks(PICO_MSH2_HZ, 0);
712   if (ssh2.mult_m68k_to_sh2 == 0 || ssh2.mult_sh2_to_m68k == 0)
713     Pico32xSetClocks(0, PICO_SSH2_HZ);
714
715   sh2_execute_prepare(&msh2, PicoIn.opt & POPT_EN_DRC);
716   sh2_execute_prepare(&ssh2, PicoIn.opt & POPT_EN_DRC);
717 }
718
719 // vim:shiftwidth=2:ts=2:expandtab