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