32x: map dram correctly on 68k side
[picodrive.git] / pico / 32x / 32x.c
CommitLineData
cff531af 1/*
2 * PicoDrive
6a98f03e 3 * (C) notaz, 2009,2010,2013
cff531af 4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
be2c4208 8#include "../pico_int.h"
974fdb5b 9#include "../sound/ym2612.h"
51d86e55 10#include "../../cpu/sh2/compiler.h"
be2c4208 11
12struct Pico32x Pico32x;
83ff19ec 13SH2 sh2s[2];
be2c4208 14
19886062 15#define SH2_IDLE_STATES (SH2_STATE_CPOLL|SH2_STATE_VPOLL|SH2_STATE_SLEEP)
16
e05b81fc 17static int REGPARM(2) sh2_irq_cb(SH2 *sh2, int level)
4ea707e1 18{
e05b81fc 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 }
4ea707e1 30}
31
a8fd6e37 32// if !nested_call, must sync CPUs before calling this
19886062 33void p32x_update_irls(SH2 *active_sh2)
4ea707e1 34{
35 int irqs, mlvl = 0, slvl = 0;
19886062 36 int m68k_cycles = 0;
a8fd6e37 37 int mrun, srun;
4ea707e1 38
19886062 39 if (active_sh2 != NULL)
40 m68k_cycles = sh2_cycles_done_m68k(active_sh2);
41
4ea707e1 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
19886062 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
a8fd6e37 62 elprintf(EL_32X, "update_irls: m %d/%d, s %d/%d", mlvl, mrun, slvl, srun);
4ea707e1 63}
64
be2c4208 65void Pico32xStartup(void)
66{
67 elprintf(EL_STATUS|EL_32X, "32X startup");
68
679af8a3 69 // TODO: OOM handling
be2c4208 70 PicoAHW |= PAHW_32X;
b78efee2 71 sh2_init(&msh2, 0);
4ea707e1 72 msh2.irq_callback = sh2_irq_cb;
b78efee2 73 sh2_init(&ssh2, 1);
4ea707e1 74 ssh2.irq_callback = sh2_irq_cb;
83ff19ec 75
76 PicoMemSetup32x();
045a4c52 77 p32x_pwm_ctl_changed();
a8fd6e37 78 p32x_timers_recalc();
acd35d4c 79
be2c4208 80 if (!Pico.m.pal)
974fdb5b 81 Pico32x.vdp_regs[0] |= P32XV_nPAL;
be2c4208 82
1d7a28a7 83 PREG8(Pico32xMem->sh2_peri_regs[0], 4) =
84 PREG8(Pico32xMem->sh2_peri_regs[1], 4) = 0x84; // SCI SSR
85
2446536b 86 rendstatus_old = -1;
87
974fdb5b 88 emu_32x_startup();
be2c4208 89}
90
83ff19ec 91#define HWSWAP(x) (((x) << 16) | ((x) >> 16))
92void 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 }
ed4402a7 137
138 msh2.m68krcycles_done = ssh2.m68krcycles_done = SekCyclesDoneT();
83ff19ec 139}
140
be2c4208 141void Pico32xInit(void)
142{
ed4402a7 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);
974fdb5b 147}
148
149void PicoPower32x(void)
150{
151 memset(&Pico32x, 0, sizeof(Pico32x));
5e49c3a8 152
83ff19ec 153 Pico32x.regs[0] = P32XS_REN|P32XS_nRES; // verified
974fdb5b 154 Pico32x.vdp_regs[0x0a/2] = P32XV_VBLK|P32XV_HBLK|P32XV_PEN;
87accdf7 155 Pico32x.sh2_regs[0] = P32XS2_ADEN;
be2c4208 156}
157
5e49c3a8 158void PicoUnload32x(void)
159{
160 if (Pico32xMem != NULL)
b081408f 161 plat_munmap(Pico32xMem, sizeof(*Pico32xMem));
5e49c3a8 162 Pico32xMem = NULL;
e898de13 163 sh2_finish(&msh2);
164 sh2_finish(&ssh2);
5e49c3a8 165
166 PicoAHW &= ~PAHW_32X;
167}
168
be2c4208 169void PicoReset32x(void)
170{
83ff19ec 171 if (PicoAHW & PAHW_32X) {
172 Pico32x.sh2irqs |= P32XI_VRES;
19886062 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);
045a4c52 176 p32x_pwm_ctl_changed();
a8fd6e37 177 p32x_timers_recalc();
83ff19ec 178 }
be2c4208 179}
180
974fdb5b 181static void p32x_start_blank(void)
182{
7a961c19 183 if (Pico32xDrawMode != PDM32X_OFF && !PicoSkipFrame) {
5aec752d 184 int offs, lines;
185
186 pprof_start(draw);
187
188 offs = 8; lines = 224;
7a961c19 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..
5a681086 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;
5a681086 200
201 // we draw full layer (not line-by-line)
202 PicoDraw32xLayer(offs, lines, md_bg);
203 }
7a961c19 204 else if (Pico32xDrawMode != PDM32X_32X_ONLY)
205 PicoDraw32xLayerMdOnly(offs, lines);
5aec752d 206
207 pprof_end(draw);
5a681086 208 }
209
974fdb5b 210 // enter vblank
211 Pico32x.vdp_regs[0x0a/2] |= P32XV_VBLK|P32XV_PEN;
212
4ea707e1 213 // FB swap waits until vblank
974fdb5b 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 }
4ea707e1 219
97d3f47f 220 Pico32x.sh2irqs |= P32XI_VINT;
19886062 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);
974fdb5b 224}
225
19886062 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
a8fd6e37 234/* events */
a8fd6e37 235static void fillend_event(unsigned int now)
236{
237 Pico32x.vdp_regs[0x0a/2] &= ~P32XV_nFEN;
19886062 238 p32x_sh2_poll_event(&msh2, SH2_STATE_VPOLL, now);
239 p32x_sh2_poll_event(&ssh2, SH2_STATE_VPOLL, now);
a8fd6e37 240}
241
242typedef void (event_cb)(unsigned int now);
243
6a98f03e 244unsigned int event_times[P32X_EVENT_COUNT];
a8fd6e37 245static unsigned int event_time_next;
246static event_cb *event_cbs[] = {
df63f1a6 247 [P32X_EVENT_PWM] = p32x_pwm_irq_event,
a8fd6e37 248 [P32X_EVENT_FILLEND] = fillend_event,
249};
250
19886062 251// schedule event at some time 'after', in m68k clocks
252void p32x_event_schedule(unsigned int now, enum p32x_event event, int after)
a8fd6e37 253{
19886062 254 unsigned int when;
255
256 when = (now + after) | 1;
a8fd6e37 257
258 elprintf(EL_32X, "new event #%u %u->%u", event, now, when);
259 event_times[event] = when;
260
19886062 261 if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
a8fd6e37 262 event_time_next = when;
263}
264
19886062 265void 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
a8fd6e37 277static 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
19886062 315static 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
336void 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}
a8fd6e37 370
ed4402a7 371#define sync_sh2s_normal p32x_sync_sh2s
372//#define sync_sh2s_lockstep p32x_sync_sh2s
974fdb5b 373
a8fd6e37 374/* most timing is in 68k clock */
ed4402a7 375void sync_sh2s_normal(unsigned int m68k_target)
376{
a8fd6e37 377 unsigned int now, target, timer_cycles;
19886062 378 int cycles;
ed4402a7 379
a8fd6e37 380 elprintf(EL_32X, "sh2 sync to %u", m68k_target);
ed4402a7 381
27e26273 382 if (!(Pico32x.regs[0] & P32XS_nRES)) {
383 msh2.m68krcycles_done = ssh2.m68krcycles_done = m68k_target;
ed4402a7 384 return; // rare
27e26273 385 }
ed4402a7 386
a8fd6e37 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))
ed4402a7 393 {
a8fd6e37 394 if (event_time_next && CYCLES_GE(now, event_time_next))
395 run_events(now);
ed4402a7 396
a8fd6e37 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);
ed4402a7 406
19886062 407 if (!(ssh2.state & SH2_IDLE_STATES)) {
a8fd6e37 408 cycles = target - ssh2.m68krcycles_done;
409 if (cycles > 0) {
19886062 410 run_sh2(&ssh2, cycles);
a8fd6e37 411
412 if (event_time_next && CYCLES_GT(target, event_time_next))
413 target = event_time_next;
414 }
ed4402a7 415 }
416
19886062 417 if (!(msh2.state & SH2_IDLE_STATES)) {
a8fd6e37 418 cycles = target - msh2.m68krcycles_done;
419 if (cycles > 0) {
19886062 420 run_sh2(&msh2, cycles);
a8fd6e37 421
422 if (event_time_next && CYCLES_GT(target, event_time_next))
423 target = event_time_next;
424 }
ed4402a7 425 }
a8fd6e37 426
19886062 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 }
ed4402a7 436 }
a8fd6e37 437
045a4c52 438 p32x_timers_do(now - timer_cycles);
a8fd6e37 439 timer_cycles = now;
ed4402a7 440 }
19886062 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 }
236990cf 451}
acd35d4c 452
c987bb5c 453#define STEP_68K 24
ed4402a7 454
455void 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 }
87accdf7 467}
468
ed4402a7 469#define CPUS_RUN(m68k_cycles,s68k_cycles) do { \
470 SekRunM68k(m68k_cycles); \
19886062 471 if (Pico32x.emu_flags & (P32XF_68KCPOLL|P32XF_68KVPOLL)) \
472 p32x_sync_sh2s(SekCyclesDoneT2()); \
ed4402a7 473} while (0)
87accdf7 474
ed4402a7 475#define PICO_32X
974fdb5b 476#include "../pico_cmn.c"
477
478void PicoFrame32x(void)
479{
4ea707e1 480 Pico32x.vdp_regs[0x0a/2] &= ~P32XV_VBLK; // get out of vblank
db1d3564 481 if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0) // no forced blanking
482 Pico32x.vdp_regs[0x0a/2] &= ~P32XV_PEN; // no palette access
4ea707e1 483
19886062 484 p32x_sh2_poll_event(&msh2, SH2_STATE_VPOLL, 0);
485 p32x_sh2_poll_event(&ssh2, SH2_STATE_VPOLL, 0);
974fdb5b 486
487 PicoFrameStart();
488 PicoFrameHints();
51d86e55 489 sh2_drc_frame();
490
19886062 491 elprintf(EL_32X, "poll: %02x %02x %02x",
492 Pico32x.emu_flags & 3, msh2.state, ssh2.state);
974fdb5b 493}
db1d3564 494
ed4402a7 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
498void 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
27e26273 511void Pico32xStateLoaded(int is_early)
512{
513 if (is_early) {
514 Pico32xMemStateLoaded();
515 return;
516 }
517
19886062 518 SekCycleCnt = 0;
27e26273 519 sh2s[0].m68krcycles_done = sh2s[1].m68krcycles_done = SekCycleCntT;
19886062 520 p32x_update_irls(NULL);
df63f1a6 521 p32x_pwm_state_loaded();
27e26273 522 run_events(SekCycleCntT);
523}
524
ed4402a7 525// vim:shiftwidth=2:ts=2:expandtab