4 * (C) irixxxx, 2019-2024
6 * This work is licensed under the terms of MAME license.
7 * See COPYING file in the top-level directory.
10 #include "../pico_int.h"
11 #include "../sound/ym2612.h"
14 extern unsigned char formatted_bram[4*0x10];
16 static unsigned int mcd_m68k_cycle_mult;
17 static unsigned int mcd_s68k_cycle_mult;
18 static unsigned int mcd_m68k_cycle_base;
19 static unsigned int mcd_s68k_cycle_base;
23 PICO_INTERNAL void PicoCreateMCD(unsigned char *bios_data, int bios_size)
26 Pico_mcd = plat_mmap(0x05000000, sizeof(mcd_state), 0, 0);
27 if (Pico_mcd == NULL) {
28 elprintf(EL_STATUS, "OOM");
32 memset(Pico_mcd, 0, sizeof(mcd_state));
34 if (bios_data && bios_size > 0) {
35 if (bios_size > sizeof(Pico_mcd->bios))
36 bios_size = sizeof(Pico_mcd->bios);
37 memcpy(Pico_mcd->bios, bios_data, bios_size);
41 PICO_INTERNAL void PicoInitMCD(void)
46 PICO_INTERNAL void PicoExitMCD(void)
50 plat_munmap(Pico_mcd, sizeof(mcd_state));
55 PICO_INTERNAL void PicoPowerMCD(void)
60 SekCycleCntS68k = SekCycleAimS68k = 0;
62 fmt_size = sizeof(formatted_bram);
63 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
64 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
65 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
66 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
67 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size,
68 formatted_bram, fmt_size);
69 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
70 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
71 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
76 // cold reset state (tested)
77 Pico_mcd->m.state_flags = PCD_ST_S68K_RST;
78 Pico_mcd->m.busreq = 2; // busreq on, s68k in reset
79 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode, m68k access
80 if (Pico.romsize == 0) // no HINT vector from gate array for MSU
81 memset(Pico_mcd->bios + 0x70, 0xff, 4);
82 pcd_event_schedule_s68k(PCD_EVENT_CDC, 12500000/75);
88 void pcd_soft_reset(void)
90 elprintf(EL_CD, "cd: soft reset");
92 Pico_mcd->m.s68k_pend_ints = 0;
95 #ifdef _ASM_CD_MEMORY_C
96 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
99 memset(&Pico_mcd->s68k_regs[0x38], 0, 9);
100 Pico_mcd->s68k_regs[0x38+9] = 0x0f; // default checksum
102 pcd_event_schedule_s68k(PCD_EVENT_CDC, 12500000/75);
104 // TODO: test if register state/timers change
107 PICO_INTERNAL int PicoResetMCD(void)
109 // reset button doesn't affect MCD hardware
111 // use Pico.sv.data for RAM cart
112 if (Pico.romsize == 0) {
113 if (PicoIn.opt & POPT_EN_MCD_RAMCART) {
114 if (Pico.sv.data == NULL)
115 Pico.sv.data = calloc(1, 0x12000);
117 else if (Pico.sv.data != NULL) {
121 Pico.sv.start = Pico.sv.end = 0; // unused
127 static void SekRunS68k(unsigned int to)
131 SekCycleAimS68k = to;
132 if ((cyc_do = SekCycleAimS68k - SekCycleCntS68k) <= 0)
136 SekCycleCntS68k += cyc_do;
137 #if defined(EMU_C68K)
138 PicoCpuCS68k.cycles = cyc_do;
139 CycloneRun(&PicoCpuCS68k);
140 SekCycleCntS68k -= PicoCpuCS68k.cycles;
141 #elif defined(EMU_M68K)
142 m68k_set_context(&PicoCpuMS68k);
143 SekCycleCntS68k += m68k_execute(cyc_do) - cyc_do;
144 m68k_set_context(&PicoCpuMM68k);
145 #elif defined(EMU_F68K)
146 SekCycleCntS68k += fm68k_emulate(&PicoCpuFS68k, cyc_do, 0) - cyc_do;
148 SekCyclesLeftS68k = 0;
152 void PicoMCDPrepare(void)
154 // 12500000/(osc/7), ~1.63 for NTSC, ~1.645 for PAL
155 #define DIV_ROUND(x,y) ((x)+(y)/2) / (y) // round to nearest, x/y+0.5 -> (x+y/2)/y
156 unsigned int osc = (Pico.m.pal ? OSC_PAL : OSC_NTSC);
157 mcd_m68k_cycle_mult = DIV_ROUND(7 * 12500000ull << 16, osc);
158 mcd_s68k_cycle_mult = DIV_ROUND(1ull * osc << 16, 7 * 12500000);
161 unsigned int pcd_cycles_m68k_to_s68k(unsigned int c)
163 return (long long)c * mcd_m68k_cycle_mult >> 16;
167 static void pcd_cdc_event(unsigned int now)
169 int audio = Pico_mcd->s68k_regs[0x36] & 0x1;
174 // main 68k cycles since frame start
175 int cycles = 1LL*(now-mcd_s68k_cycle_base) * mcd_s68k_cycle_mult >> 16;
176 // samples@rate since frame start
177 int samples = 1LL * cycles_68k_to_z80(cycles) * Pico.snd.clkz_mult >> 20;
178 // samples@44100Hz since frame start
179 samples = samples * Pico.snd.cdda_mult >> 16;
180 if (samples < 2352/4) // save offset to 1st used sample for state saving
181 Pico_mcd->m.cdda_lba_offset = 2352/4 - samples;
183 /* if audio just turned on, store start offset for sound */
184 audio &= !(Pico_mcd->s68k_regs[0x36] & 0x1);
186 Pico_mcd->m.cdda_lba_offset = 0; // starting with full lba
187 Pico_mcd->cdda_frame_offs = samples;
190 /* check if a new CDD command has been processed */
191 if (!(Pico_mcd->s68k_regs[0x4b] & 0xf0))
193 /* reset CDD command wait flag */
194 Pico_mcd->s68k_regs[0x4b] = 0xf0;
197 if ((Pico_mcd->s68k_regs[0x33] & PCDS_IEN4) && (Pico_mcd->s68k_regs[0x37] & 4)) {
198 elprintf(EL_INTS|EL_CD, "s68k: cdd irq 4");
204 pcd_event_schedule(now, PCD_EVENT_CDC, 12500000/75);
207 static void pcd_int3_timer_event(unsigned int now)
209 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN3) {
210 elprintf(EL_INTS|EL_CD, "s68k: timer irq 3");
214 if (Pico_mcd->s68k_regs[0x31] != 0)
215 pcd_event_schedule(now, PCD_EVENT_TIMER3,
216 (Pico_mcd->s68k_regs[0x31]+1) * 384);
219 static void pcd_dma_event(unsigned int now)
224 typedef void (event_cb)(unsigned int now);
226 /* times are in s68k (12.5MHz) cycles */
227 unsigned int pcd_event_times[PCD_EVENT_COUNT];
228 static unsigned int event_time_next;
229 static event_cb *pcd_event_cbs[PCD_EVENT_COUNT] = {
230 pcd_cdc_event, // PCD_EVENT_CDC
231 pcd_int3_timer_event, // PCD_EVENT_TIMER3
232 gfx_update, // PCD_EVENT_GFX
233 pcd_dma_event, // PCD_EVENT_DMA
236 void pcd_event_schedule(unsigned int now, enum pcd_event event, int after)
240 if ((now|after) == 0) {
242 pcd_event_times[event] = 0;
249 elprintf(EL_CD, "cd: new event #%u %u->%u", event, now, when);
250 pcd_event_times[event] = when;
252 if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
253 event_time_next = when;
256 void pcd_event_schedule_s68k(enum pcd_event event, int after)
258 SekEndRunS68k(after);
260 pcd_event_schedule(SekCyclesDoneS68k(), event, after);
263 static void pcd_run_events(unsigned int until)
265 int oldest, oldest_diff, time;
269 oldest = -1, oldest_diff = 0x7fffffff;
271 for (i = 0; i < PCD_EVENT_COUNT; i++) {
272 if (pcd_event_times[i]) {
273 diff = pcd_event_times[i] - until;
274 if (diff < oldest_diff) {
281 if (oldest_diff <= 0) {
282 time = pcd_event_times[oldest];
283 pcd_event_times[oldest] = 0;
284 elprintf(EL_CD, "cd: run event #%d %u", oldest, time);
285 pcd_event_cbs[oldest](time);
287 else if (oldest_diff < 0x7fffffff) {
288 event_time_next = pcd_event_times[oldest];
298 elprintf(EL_CD, "cd: next event #%d at %u",
299 oldest, event_time_next);
302 void pcd_irq_s68k(int irq, int state)
305 SekInterruptS68k(irq);
306 Pico_mcd->m.state_flags &= ~PCD_ST_S68K_POLL;
307 Pico_mcd->m.s68k_poll_cnt = 0;
309 SekInterruptClearS68k(irq);
312 int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync)
314 #define now SekCycleCntS68k
315 unsigned int s68k_target;
318 target = m68k_target - mcd_m68k_cycle_base;
319 s68k_target = mcd_s68k_cycle_base +
320 ((unsigned long long)target * mcd_m68k_cycle_mult >> 16);
322 elprintf(EL_CD, "s68k sync to %u, %u->%u",
323 m68k_target, now, s68k_target);
325 if (Pico_mcd->m.busreq != 1) { /* busreq/reset */
326 SekCycleCntS68k = SekCycleAimS68k = s68k_target;
327 pcd_run_events(s68k_target);
331 while (CYCLES_GT(s68k_target, now)) {
332 if (event_time_next && CYCLES_GE(now, event_time_next))
335 target = s68k_target;
336 if (event_time_next && CYCLES_GT(target, event_time_next))
337 target = event_time_next;
339 if (Pico_mcd->m.state_flags & (PCD_ST_S68K_POLL|PCD_ST_S68K_SLEEP))
340 SekCycleCntS68k = SekCycleAimS68k = target;
344 if (m68k_poll_sync && Pico_mcd->m.m68k_poll_cnt == 0)
348 return s68k_target - now;
352 #define pcd_run_cpus_normal pcd_run_cpus
353 //#define pcd_run_cpus_lockstep pcd_run_cpus
355 static void SekAimM68k(int cyc, int mult);
356 static int SekSyncM68k(int once);
358 void pcd_run_cpus_normal(int m68k_cycles)
360 SekAimM68k(m68k_cycles, 0x108);
362 while (CYCLES_GT(Pico.t.m68c_aim, Pico.t.m68c_cnt)) {
363 if (SekShouldInterrupt()) {
364 Pico_mcd->m.state_flags &= ~PCD_ST_M68K_POLL;
365 Pico_mcd->m.m68k_poll_cnt = 0;
368 #ifdef USE_POLL_DETECT
369 if (Pico_mcd->m.state_flags & PCD_ST_M68K_POLL) {
371 // main CPU is polling, (wake and) run sub only
372 if (Pico_mcd->m.state_flags & (PCD_ST_S68K_POLL|PCD_ST_S68K_SLEEP)) {
373 Pico_mcd->m.state_flags &= ~(PCD_ST_S68K_POLL|PCD_ST_S68K_SLEEP);
374 Pico_mcd->m.s68k_poll_cnt = 0;
376 s68k_left = pcd_sync_s68k(Pico.t.m68c_aim, 1);
378 Pico.t.m68c_cnt = Pico.t.m68c_aim;
380 Pico.t.m68c_cnt -= ((long long)s68k_left * mcd_s68k_cycle_mult >> 16);
381 if (Pico_mcd->m.state_flags & (PCD_ST_S68K_POLL|PCD_ST_S68K_SLEEP)) {
382 // slave has stopped, wake master to avoid lockups
383 Pico_mcd->m.state_flags &= ~PCD_ST_M68K_POLL;
384 Pico_mcd->m.m68k_poll_cnt = 0;
387 elprintf(EL_CDPOLL, "m68k poll [%02x] x%d @%06x",
388 Pico_mcd->m.m68k_poll_a, Pico_mcd->m.m68k_poll_cnt, SekPc);
393 // make sure sub doesn't get too far out of sync with main
394 if (!(Pico_mcd->m.state_flags & (PCD_ST_S68K_POLL|PCD_ST_S68K_SLEEP)) &&
395 pcd_cycles_m68k_to_s68k(Pico.t.m68c_aim - mcd_m68k_cycle_base) >
396 5000 + SekCycleAimS68k - mcd_s68k_cycle_base)
397 pcd_sync_s68k(Pico.t.m68c_cnt, 0);
399 if (Pico_mcd->m.state_flags & PCD_ST_S68K_SYNC) {
400 Pico_mcd->m.state_flags &= ~PCD_ST_S68K_SYNC;
401 pcd_sync_s68k(Pico.t.m68c_cnt, 0);
406 void pcd_run_cpus_lockstep(int m68k_cycles)
408 unsigned int target = Pico.t.m68c_aim + m68k_cycles;
410 while (CYCLES_GT(target, Pico.t.m68c_aim)) {
411 int cycles = target - Pico.t.m68c_aim;
412 if (cycles > 8) cycles = 8;
413 SekAimM68k(cycles, 0x108);
415 pcd_sync_s68k(Pico.t.m68c_cnt, 0);
420 #define CPUS_RUN(m68k_cycles) \
421 pcd_run_cpus(m68k_cycles)
423 #include "../pico_cmn.c"
426 void pcd_prepare_frame(void)
428 // need this because we can't have direct mapping between
429 // master<->slave cycle counters because of overflows
430 mcd_m68k_cycle_base = Pico.t.m68c_aim;
431 mcd_s68k_cycle_base = SekCycleAimS68k;
434 PICO_INTERNAL void PicoFrameMCD(void)
442 void pcd_state_loaded(void)
446 pcd_state_loaded_mem();
448 memset(Pico_mcd->pcm_mixbuf, 0, sizeof(Pico_mcd->pcm_mixbuf));
449 Pico_mcd->pcm_mixbuf_dirty = 0;
450 Pico_mcd->pcm_mixpos = 0;
451 Pico_mcd->pcm_regs_dirty = 1;
454 cycles = pcd_cycles_m68k_to_s68k(Pico.t.m68c_aim);
455 if (CYCLES_GE(cycles - SekCycleAimS68k, 12500000/60)) {
456 SekCycleCntS68k = SekCycleAimS68k = cycles;
458 if (pcd_event_times[PCD_EVENT_CDC] == 0) {
459 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_CDC, 12500000/75);
461 if (Pico_mcd->s68k_regs[0x31])
462 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3,
463 (Pico_mcd->s68k_regs[0x31]+1) * 384);
466 if (CYCLES_GE(cycles - Pico_mcd->pcm.update_cycles, 12500000/50))
467 Pico_mcd->pcm.update_cycles = cycles;
469 if (Pico_mcd->m.need_sync) {
470 Pico_mcd->m.state_flags |= PCD_ST_S68K_SYNC;
471 Pico_mcd->m.need_sync = 0;
476 pcd_run_events(SekCycleCntS68k);
482 // vim:shiftwidth=2:ts=2:expandtab