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));
44 // cold reset state (tested)
45 Pico_mcd->m.state_flags = PCD_ST_S68K_RST;
46 Pico_mcd->m.busreq = 2; // busreq on, s68k in reset
47 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode, m68k access
48 memset(Pico_mcd->bios + 0x70, 0xff, 4);
51 PICO_INTERNAL int PicoResetMCD(void)
57 #ifdef _ASM_CD_MEMORY_C
58 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
61 // use SRam.data for RAM cart
62 if (PicoOpt & POPT_EN_MCD_RAMCART) {
63 if (SRam.data == NULL)
64 SRam.data = calloc(1, 0x12000);
66 else if (SRam.data != NULL) {
70 SRam.start = SRam.end = 0; // unused
72 pcd_event_schedule(0, PCD_EVENT_CDC, 12500000/75);
77 static __inline void SekRunS68k(unsigned int to)
82 if ((cyc_do = SekCycleAimS68k - SekCycleCntS68k) <= 0)
85 SekCycleCntS68k += cyc_do;
87 PicoCpuCS68k.cycles = cyc_do;
88 CycloneRun(&PicoCpuCS68k);
89 SekCycleCntS68k -= PicoCpuCS68k.cycles;
90 #elif defined(EMU_M68K)
91 m68k_set_context(&PicoCpuMS68k);
92 SekCycleCntS68k += m68k_execute(cyc_do) - cyc_do;
93 m68k_set_context(&PicoCpuMM68k);
94 #elif defined(EMU_F68K)
95 g_m68kcontext = &PicoCpuFS68k;
96 SekCycleCntS68k += fm68k_emulate(cyc_do, 0, 0) - cyc_do;
97 g_m68kcontext = &PicoCpuFM68k;
102 unsigned int pcd_cycles_m68k_to_s68k(unsigned int c)
104 return (long long)c * m68k_cycle_mult >> 16;
108 static void pcd_cdc_event(unsigned int now)
112 pcd_event_schedule(now, PCD_EVENT_CDC, 12500000/75);
115 static void pcd_int3_timer_event(unsigned int now)
117 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN3) {
118 elprintf(EL_INTS|EL_CD, "s68k: timer irq 3");
122 if (Pico_mcd->s68k_regs[0x31] != 0)
123 pcd_event_schedule(now, PCD_EVENT_TIMER3,
124 Pico_mcd->s68k_regs[0x31] * 384);
127 static void pcd_gfx_event(unsigned int now)
130 if (Pico_mcd->rot_comp.Reg_58 & 0x8000) {
131 Pico_mcd->rot_comp.Reg_58 &= 0x7fff;
132 Pico_mcd->rot_comp.Reg_64 = 0;
133 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1) {
134 elprintf(EL_INTS |EL_CD, "s68k: gfx_cd irq 1");
140 static void pcd_dma_event(unsigned int now)
142 int ddx = Pico_mcd->s68k_regs[4] & 7;
143 Update_CDC_TRansfer(ddx);
146 typedef void (event_cb)(unsigned int now);
148 /* times are in s68k (12.5MHz) cycles */
149 unsigned int pcd_event_times[PCD_EVENT_COUNT];
150 static unsigned int event_time_next;
151 static event_cb *pcd_event_cbs[PCD_EVENT_COUNT] = {
152 [PCD_EVENT_CDC] = pcd_cdc_event,
153 [PCD_EVENT_TIMER3] = pcd_int3_timer_event,
154 [PCD_EVENT_GFX] = pcd_gfx_event,
155 [PCD_EVENT_DMA] = pcd_dma_event,
158 void pcd_event_schedule(unsigned int now, enum pcd_event event, int after)
165 pcd_event_times[event] = 0;
171 elprintf(EL_CD, "cd: new event #%u %u->%u", event, now, when);
172 pcd_event_times[event] = when;
174 if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
175 event_time_next = when;
178 void pcd_event_schedule_s68k(enum pcd_event event, int after)
180 if (SekCyclesLeftS68k > after)
181 SekEndRunS68k(after);
183 pcd_event_schedule(SekCyclesDoneS68k(), event, after);
186 static void pcd_run_events(unsigned int until)
188 int oldest, oldest_diff, time;
192 oldest = -1, oldest_diff = 0x7fffffff;
194 for (i = 0; i < PCD_EVENT_COUNT; i++) {
195 if (pcd_event_times[i]) {
196 diff = pcd_event_times[i] - until;
197 if (diff < oldest_diff) {
204 if (oldest_diff <= 0) {
205 time = pcd_event_times[oldest];
206 pcd_event_times[oldest] = 0;
207 elprintf(EL_CD, "cd: run event #%d %u", oldest, time);
208 pcd_event_cbs[oldest](time);
210 else if (oldest_diff < 0x7fffffff) {
211 event_time_next = pcd_event_times[oldest];
221 elprintf(EL_CD, "cd: next event #%d at %u",
222 oldest, event_time_next);
225 int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync)
227 #define now SekCycleCntS68k
228 unsigned int s68k_target =
229 (unsigned long long)m68k_target * m68k_cycle_mult >> 16;
232 elprintf(EL_CD, "s68k sync to %u, %u->%u",
233 m68k_target, now, s68k_target);
235 if (Pico_mcd->m.busreq != 1) { /* busreq/reset */
236 SekCycleCntS68k = SekCycleAimS68k = s68k_target;
237 pcd_run_events(m68k_target);
241 while (CYCLES_GT(s68k_target, now)) {
242 if (event_time_next && CYCLES_GE(now, event_time_next))
245 target = s68k_target;
246 if (event_time_next && CYCLES_GT(target, event_time_next))
247 target = event_time_next;
250 if (m68k_poll_sync && Pico_mcd->m.m68k_poll_cnt == 0)
254 return s68k_target - now;
258 #define pcd_run_cpus_normal pcd_run_cpus
259 //#define pcd_run_cpus_lockstep pcd_run_cpus
261 static void SekSyncM68k(void);
263 static inline void pcd_run_cpus_normal(int m68k_cycles)
265 SekCycleAim += m68k_cycles;
266 if (SekShouldInterrupt())
267 Pico_mcd->m.m68k_poll_cnt = 0;
268 else if (Pico_mcd->m.m68k_poll_cnt >= 16) {
269 int s68k_left = pcd_sync_s68k(SekCycleAim, 1);
270 if (s68k_left <= 0) {
271 elprintf(EL_CDPOLL, "m68k poll [%02x] x%d @%06x",
272 Pico_mcd->m.m68k_poll_a, Pico_mcd->m.m68k_poll_cnt, SekPc);
273 SekCycleCnt = SekCycleAim;
276 SekCycleCnt = SekCycleAim - (s68k_left * 40220 >> 16);
282 static inline void pcd_run_cpus_lockstep(int m68k_cycles)
284 unsigned int target = SekCycleAim + m68k_cycles;
288 pcd_sync_s68k(SekCycleAim, 0);
289 } while (CYCLES_GT(target, SekCycleAim));
291 SekCycleAim = target;
295 #define CPUS_RUN(m68k_cycles) \
296 pcd_run_cpus(m68k_cycles)
298 #include "../pico_cmn.c"
301 PICO_INTERNAL void PicoFrameMCD(void)
303 if (!(PicoOpt&POPT_ALT_RENDERER))
306 // ~1.63 for NTSC, ~1.645 for PAL
308 m68k_cycle_mult = ((12500000ull << 16) / (50*312*488));
310 m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1;
315 void pcd_state_loaded(void)
320 pcd_state_loaded_mem();
323 cycles = pcd_cycles_m68k_to_s68k(SekCycleAim);
324 diff = cycles - SekCycleAimS68k;
325 if (diff < -1000 || diff > 1000) {
326 SekCycleCntS68k = SekCycleAimS68k = cycles;
328 if (pcd_event_times[PCD_EVENT_CDC] == 0) {
329 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_CDC, 12500000/75);
331 if (Pico_mcd->s68k_regs[0x31])
332 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3,
333 Pico_mcd->s68k_regs[0x31] * 384);
335 if (Pico_mcd->rot_comp.Reg_58 & 0x8000) {
336 Pico_mcd->rot_comp.Reg_58 &= 0x7fff;
337 Pico_mcd->rot_comp.Reg_64 = 0;
338 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1)
341 if (Pico_mcd->scd.Status_CDC & 0x08)
342 Update_CDC_TRansfer(Pico_mcd->s68k_regs[4] & 7);
346 // vim:shiftwidth=2:ts=2:expandtab