32x: move sh2 peripheral emu code to it's own file
[picodrive.git] / pico / 32x / 32x.c
1 /*
2  * PicoDrive
3  * (C) notaz, 2009,2010,2013
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 #include "../pico_int.h"
9 #include "../sound/ym2612.h"
10 #include "../../cpu/sh2/compiler.h"
11
12 struct Pico32x Pico32x;
13 SH2 sh2s[2];
14
15 #define SH2_IDLE_STATES (SH2_STATE_CPOLL|SH2_STATE_VPOLL|SH2_STATE_SLEEP)
16
17 static int REGPARM(2) sh2_irq_cb(SH2 *sh2, int level)
18 {
19   if (sh2->pending_irl > sh2->pending_int_irq) {
20     elprintf(EL_32X, "%csh2 ack/irl %d @ %08x",
21       sh2->is_slave ? 's' : 'm', level, sh2->pc);
22     return 64 + sh2->pending_irl / 2;
23   } else {
24     elprintf(EL_32X, "%csh2 ack/int %d/%d @ %08x",
25       sh2->is_slave ? 's' : 'm', level, sh2->pending_int_vector, sh2->pc);
26     sh2->pending_int_irq = 0; // auto-clear
27     sh2->pending_level = sh2->pending_irl;
28     return sh2->pending_int_vector;
29   }
30 }
31
32 // if !nested_call, must sync CPUs before calling this
33 void p32x_update_irls(SH2 *active_sh2)
34 {
35   int irqs, mlvl = 0, slvl = 0;
36   int m68k_cycles = 0;
37   int mrun, srun;
38
39   if (active_sh2 != NULL)
40     m68k_cycles = sh2_cycles_done_m68k(active_sh2);
41
42   // msh2
43   irqs = (Pico32x.sh2irqs | Pico32x.sh2irqi[0]) & ((Pico32x.sh2irq_mask[0] << 3) | P32XI_VRES);
44   while ((irqs >>= 1))
45     mlvl++;
46   mlvl *= 2;
47
48   // ssh2
49   irqs = (Pico32x.sh2irqs | Pico32x.sh2irqi[1]) & ((Pico32x.sh2irq_mask[1] << 3) | P32XI_VRES);
50   while ((irqs >>= 1))
51     slvl++;
52   slvl *= 2;
53
54   mrun = sh2_irl_irq(&msh2, mlvl, active_sh2 != NULL);
55   if (mrun)
56     p32x_sh2_poll_event(&msh2, SH2_IDLE_STATES, m68k_cycles);
57
58   srun = sh2_irl_irq(&ssh2, slvl, active_sh2 != NULL);
59   if (srun)
60     p32x_sh2_poll_event(&ssh2, SH2_IDLE_STATES, m68k_cycles);
61
62   elprintf(EL_32X, "update_irls: m %d/%d, s %d/%d", mlvl, mrun, slvl, srun);
63 }
64
65 void Pico32xStartup(void)
66 {
67   elprintf(EL_STATUS|EL_32X, "32X startup");
68
69   // TODO: OOM handling
70   PicoAHW |= PAHW_32X;
71   sh2_init(&msh2, 0);
72   msh2.irq_callback = sh2_irq_cb;
73   sh2_init(&ssh2, 1);
74   ssh2.irq_callback = sh2_irq_cb;
75
76   PicoMemSetup32x();
77   p32x_pwm_ctl_changed();
78   p32x_timers_recalc();
79
80   if (!Pico.m.pal)
81     Pico32x.vdp_regs[0] |= P32XV_nPAL;
82
83   PREG8(Pico32xMem->sh2_peri_regs[0], 4) =
84   PREG8(Pico32xMem->sh2_peri_regs[1], 4) = 0x84; // SCI SSR
85
86   rendstatus_old = -1;
87
88   emu_32x_startup();
89 }
90
91 #define HWSWAP(x) (((x) << 16) | ((x) >> 16))
92 void p32x_reset_sh2s(void)
93 {
94   elprintf(EL_32X, "sh2 reset");
95
96   sh2_reset(&msh2);
97   sh2_reset(&ssh2);
98
99   // if we don't have BIOS set, perform it's work here.
100   // MSH2
101   if (p32x_bios_m == NULL) {
102     unsigned int idl_src, idl_dst, idl_size; // initial data load
103     unsigned int vbr;
104
105     // initial data
106     idl_src = HWSWAP(*(unsigned int *)(Pico.rom + 0x3d4)) & ~0xf0000000;
107     idl_dst = HWSWAP(*(unsigned int *)(Pico.rom + 0x3d8)) & ~0xf0000000;
108     idl_size= HWSWAP(*(unsigned int *)(Pico.rom + 0x3dc));
109     if (idl_size > Pico.romsize || idl_src + idl_size > Pico.romsize ||
110         idl_size > 0x40000 || idl_dst + idl_size > 0x40000 || (idl_src & 3) || (idl_dst & 3)) {
111       elprintf(EL_STATUS|EL_ANOMALY, "32x: invalid initial data ptrs: %06x -> %06x, %06x",
112         idl_src, idl_dst, idl_size);
113     }
114     else
115       memcpy(Pico32xMem->sdram + idl_dst, Pico.rom + idl_src, idl_size);
116
117     // GBR/VBR
118     vbr = HWSWAP(*(unsigned int *)(Pico.rom + 0x3e8));
119     sh2_set_gbr(0, 0x20004000);
120     sh2_set_vbr(0, vbr);
121
122     // checksum and M_OK
123     Pico32x.regs[0x28 / 2] = *(unsigned short *)(Pico.rom + 0x18e);
124     // program will set M_OK
125   }
126
127   // SSH2
128   if (p32x_bios_s == NULL) {
129     unsigned int vbr;
130
131     // GBR/VBR
132     vbr = HWSWAP(*(unsigned int *)(Pico.rom + 0x3ec));
133     sh2_set_gbr(1, 0x20004000);
134     sh2_set_vbr(1, vbr);
135     // program will set S_OK
136   }
137
138   msh2.m68krcycles_done = ssh2.m68krcycles_done = SekCyclesDoneT();
139 }
140
141 void Pico32xInit(void)
142 {
143   if (msh2.mult_m68k_to_sh2 == 0 || msh2.mult_sh2_to_m68k == 0)
144     Pico32xSetClocks(PICO_MSH2_HZ, 0);
145   if (ssh2.mult_m68k_to_sh2 == 0 || ssh2.mult_sh2_to_m68k == 0)
146     Pico32xSetClocks(0, PICO_MSH2_HZ);
147 }
148
149 void PicoPower32x(void)
150 {
151   memset(&Pico32x, 0, sizeof(Pico32x));
152
153   Pico32x.regs[0] = P32XS_REN|P32XS_nRES; // verified
154   Pico32x.vdp_regs[0x0a/2] = P32XV_VBLK|P32XV_HBLK|P32XV_PEN;
155   Pico32x.sh2_regs[0] = P32XS2_ADEN;
156 }
157
158 void PicoUnload32x(void)
159 {
160   if (Pico32xMem != NULL)
161     plat_munmap(Pico32xMem, sizeof(*Pico32xMem));
162   Pico32xMem = NULL;
163   sh2_finish(&msh2);
164   sh2_finish(&ssh2);
165
166   PicoAHW &= ~PAHW_32X;
167 }
168
169 void PicoReset32x(void)
170 {
171   if (PicoAHW & PAHW_32X) {
172     Pico32x.sh2irqs |= P32XI_VRES;
173     p32x_update_irls(NULL);
174     p32x_sh2_poll_event(&msh2, SH2_IDLE_STATES, 0);
175     p32x_sh2_poll_event(&ssh2, SH2_IDLE_STATES, 0);
176     p32x_pwm_ctl_changed();
177     p32x_timers_recalc();
178   }
179 }
180
181 static void p32x_start_blank(void)
182 {
183   if (Pico32xDrawMode != PDM32X_OFF && !PicoSkipFrame) {
184     int offs, lines;
185
186     pprof_start(draw);
187
188     offs = 8; lines = 224;
189     if ((Pico.video.reg[1] & 8) && !(PicoOpt & POPT_ALT_RENDERER)) {
190       offs = 0;
191       lines = 240;
192     }
193
194     // XXX: no proper handling of 32col mode..
195     if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0 && // 32x not blanking
196         (Pico.video.reg[12] & 1) && // 40col mode
197         (PicoDrawMask & PDRAW_32X_ON))
198     {
199       int md_bg = Pico.video.reg[7] & 0x3f;
200
201       // we draw full layer (not line-by-line)
202       PicoDraw32xLayer(offs, lines, md_bg);
203     }
204     else if (Pico32xDrawMode != PDM32X_32X_ONLY)
205       PicoDraw32xLayerMdOnly(offs, lines);
206
207     pprof_end(draw);
208   }
209
210   // enter vblank
211   Pico32x.vdp_regs[0x0a/2] |= P32XV_VBLK|P32XV_PEN;
212
213   // FB swap waits until vblank
214   if ((Pico32x.vdp_regs[0x0a/2] ^ Pico32x.pending_fb) & P32XV_FS) {
215     Pico32x.vdp_regs[0x0a/2] &= ~P32XV_FS;
216     Pico32x.vdp_regs[0x0a/2] |= Pico32x.pending_fb;
217     Pico32xSwapDRAM(Pico32x.pending_fb ^ 1);
218   }
219
220   Pico32x.sh2irqs |= P32XI_VINT;
221   p32x_update_irls(NULL);
222   p32x_sh2_poll_event(&msh2, SH2_STATE_VPOLL, 0);
223   p32x_sh2_poll_event(&ssh2, SH2_STATE_VPOLL, 0);
224 }
225
226 // compare cycles, handling overflows
227 // check if a > b
228 #define CYCLES_GT(a, b) \
229   ((int)((a) - (b)) > 0)
230 // check if a >= b
231 #define CYCLES_GE(a, b) \
232   ((int)((a) - (b)) >= 0)
233
234 /* events */
235 static void fillend_event(unsigned int now)
236 {
237   Pico32x.vdp_regs[0x0a/2] &= ~P32XV_nFEN;
238   p32x_sh2_poll_event(&msh2, SH2_STATE_VPOLL, now);
239   p32x_sh2_poll_event(&ssh2, SH2_STATE_VPOLL, now);
240 }
241
242 typedef void (event_cb)(unsigned int now);
243
244 unsigned int event_times[P32X_EVENT_COUNT];
245 static unsigned int event_time_next;
246 static event_cb *event_cbs[] = {
247   [P32X_EVENT_PWM]      = p32x_pwm_irq_event,
248   [P32X_EVENT_FILLEND]  = fillend_event,
249 };
250
251 // schedule event at some time 'after', in m68k clocks
252 void p32x_event_schedule(unsigned int now, enum p32x_event event, int after)
253 {
254   unsigned int when;
255
256   when = (now + after) | 1;
257
258   elprintf(EL_32X, "new event #%u %u->%u", event, now, when);
259   event_times[event] = when;
260
261   if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
262     event_time_next = when;
263 }
264
265 void p32x_event_schedule_sh2(SH2 *sh2, enum p32x_event event, int after)
266 {
267   unsigned int now = sh2_cycles_done_m68k(sh2);
268   int left_to_next;
269
270   p32x_event_schedule(now, event, after);
271
272   left_to_next = (event_time_next - now) * 3;
273   if (sh2_cycles_left(sh2) > left_to_next)
274     sh2_end_run(sh2, left_to_next);
275 }
276
277 static void run_events(unsigned int until)
278 {
279   int oldest, oldest_diff, time;
280   int i, diff;
281
282   while (1) {
283     oldest = -1, oldest_diff = 0x7fffffff;
284
285     for (i = 0; i < P32X_EVENT_COUNT; i++) {
286       if (event_times[i]) {
287         diff = event_times[i] - until;
288         if (diff < oldest_diff) {
289           oldest_diff = diff;
290           oldest = i;
291         }
292       }
293     }
294
295     if (oldest_diff <= 0) {
296       time = event_times[oldest];
297       event_times[oldest] = 0;
298       elprintf(EL_32X, "run event #%d %u", oldest, time);
299       event_cbs[oldest](time);
300     }
301     else if (oldest_diff < 0x7fffffff) {
302       event_time_next = event_times[oldest];
303       break;
304     }
305     else {
306       event_time_next = 0;
307       break;
308     }
309   }
310
311   if (oldest != -1)
312     elprintf(EL_32X, "next event #%d at %u", oldest, event_time_next);
313 }
314
315 static inline void run_sh2(SH2 *sh2, int m68k_cycles)
316 {
317   int cycles, done;
318
319   pevt_log_sh2_o(sh2, EVT_RUN_START);
320   sh2->state |= SH2_STATE_RUN;
321   cycles = C_M68K_TO_SH2(*sh2, m68k_cycles);
322   elprintf(EL_32X, "%csh2 +run %u %d",
323     sh2->is_slave?'s':'m', sh2->m68krcycles_done, cycles);
324
325   done = sh2_execute(sh2, cycles);
326
327   sh2->m68krcycles_done += C_SH2_TO_M68K(*sh2, done);
328   sh2->state &= ~SH2_STATE_RUN;
329   pevt_log_sh2_o(sh2, EVT_RUN_END);
330   elprintf(EL_32X, "%csh2 -run %u %d",
331     sh2->is_slave?'s':'m', sh2->m68krcycles_done, done);
332 }
333
334 // sync other sh2 to this one
335 // note: recursive call
336 void p32x_sync_other_sh2(SH2 *sh2, unsigned int m68k_target)
337 {
338   SH2 *osh2 = &sh2s[sh2->is_slave ^ 1];
339   int left_to_event;
340   int m68k_cycles;
341
342   if (osh2->state & SH2_STATE_RUN)
343     return;
344
345   m68k_cycles = m68k_target - osh2->m68krcycles_done;
346   if (m68k_cycles < 200)
347     return;
348
349   if (osh2->state & SH2_IDLE_STATES) {
350     osh2->m68krcycles_done = m68k_target;
351     return;
352   }
353
354   elprintf(EL_32X, "%csh2 sync to %u %d",
355     osh2->is_slave?'s':'m', m68k_target, m68k_cycles);
356
357   run_sh2(osh2, m68k_cycles);
358
359   // there might be new event to schedule current sh2 to
360   if (event_time_next) {
361     left_to_event = event_time_next - m68k_target;
362     left_to_event *= 3;
363     if (sh2_cycles_left(sh2) > left_to_event) {
364       if (left_to_event < 1)
365         left_to_event = 1;
366       sh2_end_run(sh2, left_to_event);
367     }
368   }
369 }
370
371 #define sync_sh2s_normal p32x_sync_sh2s
372 //#define sync_sh2s_lockstep p32x_sync_sh2s
373
374 /* most timing is in 68k clock */
375 void sync_sh2s_normal(unsigned int m68k_target)
376 {
377   unsigned int now, target, timer_cycles;
378   int cycles;
379
380   elprintf(EL_32X, "sh2 sync to %u", m68k_target);
381
382   if (!(Pico32x.regs[0] & P32XS_nRES)) {
383     msh2.m68krcycles_done = ssh2.m68krcycles_done = m68k_target;
384     return; // rare
385   }
386
387   now = msh2.m68krcycles_done;
388   if (CYCLES_GT(now, ssh2.m68krcycles_done))
389     now = ssh2.m68krcycles_done;
390   timer_cycles = now;
391
392   while (CYCLES_GT(m68k_target, now))
393   {
394     if (event_time_next && CYCLES_GE(now, event_time_next))
395       run_events(now);
396
397     target = m68k_target;
398     if (event_time_next && CYCLES_GT(target, event_time_next))
399       target = event_time_next;
400
401     while (CYCLES_GT(target, now))
402     {
403       elprintf(EL_32X, "sh2 exec to %u %d,%d/%d, flags %x", target,
404         target - msh2.m68krcycles_done, target - ssh2.m68krcycles_done,
405         m68k_target - now, Pico32x.emu_flags);
406
407       if (!(ssh2.state & SH2_IDLE_STATES)) {
408         cycles = target - ssh2.m68krcycles_done;
409         if (cycles > 0) {
410           run_sh2(&ssh2, cycles);
411
412           if (event_time_next && CYCLES_GT(target, event_time_next))
413             target = event_time_next;
414         }
415       }
416
417       if (!(msh2.state & SH2_IDLE_STATES)) {
418         cycles = target - msh2.m68krcycles_done;
419         if (cycles > 0) {
420           run_sh2(&msh2, cycles);
421
422           if (event_time_next && CYCLES_GT(target, event_time_next))
423             target = event_time_next;
424         }
425       }
426
427       now = target;
428       if (!(msh2.state & SH2_IDLE_STATES)) {
429         if (CYCLES_GT(now, msh2.m68krcycles_done))
430           now = msh2.m68krcycles_done;
431       }
432       if (!(ssh2.state & SH2_IDLE_STATES)) {
433         if (CYCLES_GT(now, ssh2.m68krcycles_done))
434           now = ssh2.m68krcycles_done;
435       }
436     }
437
438     p32x_timers_do(now - timer_cycles);
439     timer_cycles = now;
440   }
441
442   // advance idle CPUs
443   if (msh2.state & SH2_IDLE_STATES) {
444     if (CYCLES_GT(m68k_target, msh2.m68krcycles_done))
445       msh2.m68krcycles_done = m68k_target;
446   }
447   if (ssh2.state & SH2_IDLE_STATES) {
448     if (CYCLES_GT(m68k_target, ssh2.m68krcycles_done))
449       ssh2.m68krcycles_done = m68k_target;
450   }
451 }
452
453 #define STEP_68K 24
454
455 void sync_sh2s_lockstep(unsigned int m68k_target)
456 {
457   unsigned int mcycles;
458   
459   mcycles = msh2.m68krcycles_done;
460   if (ssh2.m68krcycles_done < mcycles)
461     mcycles = ssh2.m68krcycles_done;
462
463   while (mcycles < m68k_target) {
464     mcycles += STEP_68K;
465     sync_sh2s_normal(mcycles);
466   }
467 }
468
469 #define CPUS_RUN(m68k_cycles,s68k_cycles) do { \
470   SekRunM68k(m68k_cycles); \
471   if (Pico32x.emu_flags & (P32XF_68KCPOLL|P32XF_68KVPOLL)) \
472     p32x_sync_sh2s(SekCyclesDoneT2()); \
473 } while (0)
474
475 #define PICO_32X
476 #include "../pico_cmn.c"
477
478 void PicoFrame32x(void)
479 {
480   Pico32x.vdp_regs[0x0a/2] &= ~P32XV_VBLK; // get out of vblank
481   if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0) // no forced blanking
482     Pico32x.vdp_regs[0x0a/2] &= ~P32XV_PEN; // no palette access
483
484   p32x_sh2_poll_event(&msh2, SH2_STATE_VPOLL, 0);
485   p32x_sh2_poll_event(&ssh2, SH2_STATE_VPOLL, 0);
486
487   PicoFrameStart();
488   PicoFrameHints();
489   sh2_drc_frame();
490
491   elprintf(EL_32X, "poll: %02x %02x %02x",
492     Pico32x.emu_flags & 3, msh2.state, ssh2.state);
493 }
494
495 // calculate multipliers against 68k clock (7670442)
496 // normally * 3, but effectively slower due to high latencies everywhere
497 // however using something lower breaks MK2 animations
498 void Pico32xSetClocks(int msh2_hz, int ssh2_hz)
499 {
500   float m68k_clk = (float)(OSC_NTSC / 7);
501   if (msh2_hz > 0) {
502     msh2.mult_m68k_to_sh2 = (int)((float)msh2_hz * (1 << CYCLE_MULT_SHIFT) / m68k_clk);
503     msh2.mult_sh2_to_m68k = (int)(m68k_clk * (1 << CYCLE_MULT_SHIFT) / (float)msh2_hz);
504   }
505   if (ssh2_hz > 0) {
506     ssh2.mult_m68k_to_sh2 = (int)((float)ssh2_hz * (1 << CYCLE_MULT_SHIFT) / m68k_clk);
507     ssh2.mult_sh2_to_m68k = (int)(m68k_clk * (1 << CYCLE_MULT_SHIFT) / (float)ssh2_hz);
508   }
509 }
510
511 void Pico32xStateLoaded(int is_early)
512 {
513   if (is_early) {
514     Pico32xMemStateLoaded();
515     return;
516   }
517
518   SekCycleCnt = 0;
519   sh2s[0].m68krcycles_done = sh2s[1].m68krcycles_done = SekCycleCntT;
520   p32x_update_irls(NULL);
521   p32x_pwm_state_loaded();
522   run_events(SekCycleCntT);
523 }
524
525 // vim:shiftwidth=2:ts=2:expandtab