5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
10 #include "../sound/ym2612.h"
12 extern unsigned char formatted_bram[4*0x10];
14 static unsigned int m68k_cycle_mult;
16 void (*PicoMCDopenTray)(void) = NULL;
17 void (*PicoMCDcloseTray)(void) = NULL;
20 PICO_INTERNAL void PicoInitMCD(void)
26 PICO_INTERNAL void PicoExitMCD(void)
31 PICO_INTERNAL void PicoPowerMCD(void)
33 int fmt_size = sizeof(formatted_bram);
34 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
35 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
36 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
37 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
38 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size,
39 formatted_bram, fmt_size);
40 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
41 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
42 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
46 // cold reset state (tested)
47 Pico_mcd->m.state_flags = PCD_ST_S68K_RST;
48 Pico_mcd->m.busreq = 2; // busreq on, s68k in reset
49 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode, m68k access
50 memset(Pico_mcd->bios + 0x70, 0xff, 4);
53 void pcd_soft_reset(void)
55 // Reset_CD(); // breaks Fahrenheit CD swap
59 #ifdef _ASM_CD_MEMORY_C
60 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
63 pcd_event_schedule_s68k(PCD_EVENT_CDC, 12500000/75);
65 // TODO: test if register state/timers change
68 PICO_INTERNAL int PicoResetMCD(void)
70 // reset button doesn't affect MCD hardware
72 // use SRam.data for RAM cart
73 if (PicoOpt & POPT_EN_MCD_RAMCART) {
74 if (SRam.data == NULL)
75 SRam.data = calloc(1, 0x12000);
77 else if (SRam.data != NULL) {
81 SRam.start = SRam.end = 0; // unused
86 static __inline void SekRunS68k(unsigned int to)
91 if ((cyc_do = SekCycleAimS68k - SekCycleCntS68k) <= 0)
94 if (SekShouldInterrupt())
95 Pico_mcd->m.s68k_poll_a = 0;
97 SekCycleCntS68k += cyc_do;
99 PicoCpuCS68k.cycles = cyc_do;
100 CycloneRun(&PicoCpuCS68k);
101 SekCycleCntS68k -= PicoCpuCS68k.cycles;
102 #elif defined(EMU_M68K)
103 m68k_set_context(&PicoCpuMS68k);
104 SekCycleCntS68k += m68k_execute(cyc_do) - cyc_do;
105 m68k_set_context(&PicoCpuMM68k);
106 #elif defined(EMU_F68K)
107 g_m68kcontext = &PicoCpuFS68k;
108 SekCycleCntS68k += fm68k_emulate(cyc_do, 0, 0) - cyc_do;
109 g_m68kcontext = &PicoCpuFM68k;
114 unsigned int pcd_cycles_m68k_to_s68k(unsigned int c)
116 return (long long)c * m68k_cycle_mult >> 16;
120 static void pcd_cdc_event(unsigned int now)
124 pcd_event_schedule(now, PCD_EVENT_CDC, 12500000/75);
127 static void pcd_int3_timer_event(unsigned int now)
129 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN3) {
130 elprintf(EL_INTS|EL_CD, "s68k: timer irq 3");
134 if (Pico_mcd->s68k_regs[0x31] != 0)
135 pcd_event_schedule(now, PCD_EVENT_TIMER3,
136 Pico_mcd->s68k_regs[0x31] * 384);
139 static void pcd_dma_event(unsigned int now)
141 int ddx = Pico_mcd->s68k_regs[4] & 7;
142 Update_CDC_TRansfer(ddx);
145 typedef void (event_cb)(unsigned int now);
147 /* times are in s68k (12.5MHz) cycles */
148 unsigned int pcd_event_times[PCD_EVENT_COUNT];
149 static unsigned int event_time_next;
150 static event_cb *pcd_event_cbs[PCD_EVENT_COUNT] = {
151 [PCD_EVENT_CDC] = pcd_cdc_event,
152 [PCD_EVENT_TIMER3] = pcd_int3_timer_event,
153 [PCD_EVENT_GFX] = gfx_cd_update,
154 [PCD_EVENT_DMA] = pcd_dma_event,
157 void pcd_event_schedule(unsigned int now, enum pcd_event event, int after)
164 pcd_event_times[event] = 0;
170 elprintf(EL_CD, "cd: new event #%u %u->%u", event, now, when);
171 pcd_event_times[event] = when;
173 if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
174 event_time_next = when;
177 void pcd_event_schedule_s68k(enum pcd_event event, int after)
179 if (SekCyclesLeftS68k > after)
180 SekEndRunS68k(after);
182 pcd_event_schedule(SekCyclesDoneS68k(), event, after);
185 static void pcd_run_events(unsigned int until)
187 int oldest, oldest_diff, time;
191 oldest = -1, oldest_diff = 0x7fffffff;
193 for (i = 0; i < PCD_EVENT_COUNT; i++) {
194 if (pcd_event_times[i]) {
195 diff = pcd_event_times[i] - until;
196 if (diff < oldest_diff) {
203 if (oldest_diff <= 0) {
204 time = pcd_event_times[oldest];
205 pcd_event_times[oldest] = 0;
206 elprintf(EL_CD, "cd: run event #%d %u", oldest, time);
207 pcd_event_cbs[oldest](time);
209 else if (oldest_diff < 0x7fffffff) {
210 event_time_next = pcd_event_times[oldest];
220 elprintf(EL_CD, "cd: next event #%d at %u",
221 oldest, event_time_next);
224 int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync)
226 #define now SekCycleCntS68k
227 unsigned int s68k_target =
228 (unsigned long long)m68k_target * m68k_cycle_mult >> 16;
231 elprintf(EL_CD, "s68k sync to %u, %u->%u",
232 m68k_target, now, s68k_target);
234 if (Pico_mcd->m.busreq != 1) { /* busreq/reset */
235 SekCycleCntS68k = SekCycleAimS68k = s68k_target;
236 pcd_run_events(m68k_target);
240 while (CYCLES_GT(s68k_target, now)) {
241 if (event_time_next && CYCLES_GE(now, event_time_next))
244 target = s68k_target;
245 if (event_time_next && CYCLES_GT(target, event_time_next))
246 target = event_time_next;
249 if (m68k_poll_sync && Pico_mcd->m.m68k_poll_cnt == 0)
253 return s68k_target - now;
257 #define pcd_run_cpus_normal pcd_run_cpus
258 //#define pcd_run_cpus_lockstep pcd_run_cpus
260 static void SekSyncM68k(void);
262 void pcd_run_cpus_normal(int m68k_cycles)
264 SekCycleAim += m68k_cycles;
265 if (SekShouldInterrupt() || Pico_mcd->m.m68k_poll_cnt < 12)
266 Pico_mcd->m.m68k_poll_cnt = 0;
267 else if (Pico_mcd->m.m68k_poll_cnt >= 16) {
268 int s68k_left = pcd_sync_s68k(SekCycleAim, 1);
269 if (s68k_left <= 0) {
270 elprintf(EL_CDPOLL, "m68k poll [%02x] x%d @%06x",
271 Pico_mcd->m.m68k_poll_a, Pico_mcd->m.m68k_poll_cnt, SekPc);
272 SekCycleCnt = SekCycleAim;
275 SekCycleCnt = SekCycleAim - (s68k_left * 40220 >> 16);
281 void pcd_run_cpus_lockstep(int m68k_cycles)
283 unsigned int target = SekCycleAim + m68k_cycles;
287 pcd_sync_s68k(SekCycleAim, 0);
288 } while (CYCLES_GT(target, SekCycleAim));
290 SekCycleAim = target;
294 #define CPUS_RUN(m68k_cycles) \
295 pcd_run_cpus(m68k_cycles)
297 #include "../pico_cmn.c"
300 PICO_INTERNAL void PicoFrameMCD(void)
302 if (!(PicoOpt&POPT_ALT_RENDERER))
305 // ~1.63 for NTSC, ~1.645 for PAL
307 m68k_cycle_mult = ((12500000ull << 16) / (50*312*488));
309 m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1;
314 void pcd_state_loaded(void)
319 pcd_state_loaded_mem();
321 memset(Pico_mcd->pcm_mixbuf, 0, sizeof(Pico_mcd->pcm_mixbuf));
322 Pico_mcd->pcm_mixbuf_dirty = 0;
323 Pico_mcd->pcm_mixpos = 0;
326 cycles = pcd_cycles_m68k_to_s68k(SekCycleAim);
327 diff = cycles - SekCycleAimS68k;
328 if (diff < -1000 || diff > 1000) {
329 SekCycleCntS68k = SekCycleAimS68k = cycles;
331 if (pcd_event_times[PCD_EVENT_CDC] == 0) {
332 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_CDC, 12500000/75);
334 if (Pico_mcd->s68k_regs[0x31])
335 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3,
336 Pico_mcd->s68k_regs[0x31] * 384);
338 if (Pico_mcd->rot_comp.Reg_58 & 0x8000) {
339 Pico_mcd->rot_comp.Reg_58 &= 0x7fff;
340 Pico_mcd->rot_comp.Reg_64 = 0;
341 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1)
344 if (Pico_mcd->scd.Status_CDC & 0x08)
345 Update_CDC_TRansfer(Pico_mcd->s68k_regs[4] & 7);
347 if (Pico_mcd->pcm.update_cycles == 0)
348 Pico_mcd->pcm.update_cycles = cycles;
351 // vim:shiftwidth=2:ts=2:expandtab