cd sync improvements
[picodrive.git] / pico / cd / pico.c
CommitLineData
cff531af 1/*
2 * PicoDrive
ae214f1c 3 * (C) notaz, 2007,2013
cff531af 4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
cc68a136 8
efcba75f 9#include "../pico_int.h"
43e6eaad 10#include "../sound/ym2612.h"
cc68a136 11
76276b0b 12extern unsigned char formatted_bram[4*0x10];
ae214f1c 13
14static unsigned int m68k_cycle_mult;
89fa852d 15
721cd396 16void (*PicoMCDopenTray)(void) = NULL;
d687ef50 17void (*PicoMCDcloseTray)(void) = NULL;
89fa852d 18
cc68a136 19
2aa27095 20PICO_INTERNAL void PicoInitMCD(void)
cc68a136 21{
22 SekInitS68k();
23 Init_CD_Driver();
cc68a136 24}
25
eff55556 26PICO_INTERNAL void PicoExitMCD(void)
cc68a136 27{
28 End_CD_Driver();
29}
30
1cb1584b 31PICO_INTERNAL void PicoPowerMCD(void)
32{
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, formatted_bram, fmt_size);
39}
40
41PICO_INTERNAL int PicoResetMCD(void)
cc68a136 42{
51a902ae 43 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
4f265db7 44 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
5c69a605 45 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
51a902ae 46
895d1512 47 memset(Pico_mcd->bios + 0x70, 0xff, 4); // reset hint vector (simplest way to implement reg6)
bc3c13d3 48 Pico_mcd->m.state_flags = 0;
672ad671 49 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
cc68a136 50
cc68a136 51 Reset_CD();
5c69a605 52 LC89510_Reset();
51a902ae 53 gfx_cd_reset();
3aa1e148 54#ifdef _ASM_CD_MEMORY_C
00bd648e 55 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
4ff2d527 56#endif
cc68a136 57
6cadc2da 58 // use SRam.data for RAM cart
af37bca8 59 if (PicoOpt & POPT_EN_MCD_RAMCART) {
d6114368 60 if (SRam.data == NULL)
61 SRam.data = calloc(1, 0x12000);
62 }
63 else if (SRam.data != NULL) {
64 free(SRam.data);
65 SRam.data = NULL;
66 }
b542be46 67 SRam.start = SRam.end = 0; // unused
6cadc2da 68
ae214f1c 69 pcd_event_schedule(0, PCD_EVENT_CDC, 12500000/75);
70
cc68a136 71 return 0;
72}
73
ae214f1c 74static __inline void SekRunS68k(unsigned int to)
cc68a136 75{
76 int cyc_do;
ae214f1c 77
78 SekCycleAimS68k = to;
79 if ((cyc_do = SekCycleAimS68k - SekCycleCntS68k) <= 0)
80 return;
81
82 SekCycleCntS68k += cyc_do;
83#if defined(EMU_C68K)
84 PicoCpuCS68k.cycles = cyc_do;
3aa1e148 85 CycloneRun(&PicoCpuCS68k);
ae214f1c 86 SekCycleCntS68k -= PicoCpuCS68k.cycles;
b837b69b 87#elif defined(EMU_M68K)
3aa1e148 88 m68k_set_context(&PicoCpuMS68k);
ae214f1c 89 SekCycleCntS68k += m68k_execute(cyc_do) - cyc_do;
ed4402a7 90 m68k_set_context(&PicoCpuMM68k);
3aa1e148 91#elif defined(EMU_F68K)
ae214f1c 92 g_m68kcontext = &PicoCpuFS68k;
93 SekCycleCntS68k += fm68k_emulate(cyc_do, 0, 0) - cyc_do;
94 g_m68kcontext = &PicoCpuFM68k;
cc68a136 95#endif
96}
97
68cba51e 98
ae214f1c 99unsigned int pcd_cycles_m68k_to_s68k(unsigned int c)
8022f53d 100{
ae214f1c 101 return (long long)c * m68k_cycle_mult >> 16;
8022f53d 102}
ae214f1c 103
104/* events */
105static void pcd_cdc_event(unsigned int now)
68cba51e 106{
ae214f1c 107 // 75Hz CDC update
108 Check_CD_Command();
109 pcd_event_schedule(now, PCD_EVENT_CDC, 12500000/75);
110}
7336a99a 111
ae214f1c 112static void pcd_int3_timer_event(unsigned int now)
113{
114 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN3) {
115 elprintf(EL_INTS|EL_CD, "s68k: timer irq 3");
116 SekInterruptS68k(3);
117 }
7336a99a 118
ae214f1c 119 if (Pico_mcd->s68k_regs[0x31] != 0)
120 pcd_event_schedule(now, PCD_EVENT_TIMER3,
121 Pico_mcd->s68k_regs[0x31] * 384);
122}
123
124static void pcd_gfx_event(unsigned int now)
125{
126 // update gfx chip
127 if (Pico_mcd->rot_comp.Reg_58 & 0x8000) {
128 Pico_mcd->rot_comp.Reg_58 &= 0x7fff;
129 Pico_mcd->rot_comp.Reg_64 = 0;
130 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1) {
131 elprintf(EL_INTS |EL_CD, "s68k: gfx_cd irq 1");
132 SekInterruptS68k(1);
7336a99a 133 }
68cba51e 134 }
68cba51e 135}
136
ae214f1c 137static void pcd_dma_event(unsigned int now)
138{
139 int ddx = Pico_mcd->s68k_regs[4] & 7;
140 Update_CDC_TRansfer(ddx);
141}
68cba51e 142
ae214f1c 143typedef void (event_cb)(unsigned int now);
144
145/* times are in s68k (12.5MHz) cycles */
146unsigned int pcd_event_times[PCD_EVENT_COUNT];
147static unsigned int event_time_next;
148static event_cb *pcd_event_cbs[PCD_EVENT_COUNT] = {
149 [PCD_EVENT_CDC] = pcd_cdc_event,
150 [PCD_EVENT_TIMER3] = pcd_int3_timer_event,
151 [PCD_EVENT_GFX] = pcd_gfx_event,
152 [PCD_EVENT_DMA] = pcd_dma_event,
153};
154
155void pcd_event_schedule(unsigned int now, enum pcd_event event, int after)
bf098bc5 156{
ae214f1c 157 unsigned int when;
158
159 when = now + after;
160 if (when == 0) {
161 // event cancelled
162 pcd_event_times[event] = 0;
163 return;
164 }
bf098bc5 165
ae214f1c 166 when |= 1;
bf098bc5 167
ae214f1c 168 elprintf(EL_CD, "cd: new event #%u %u->%u", event, now, when);
169 pcd_event_times[event] = when;
bf098bc5 170
ae214f1c 171 if (event_time_next == 0 || CYCLES_GT(event_time_next, when))
172 event_time_next = when;
bf098bc5 173}
174
ae214f1c 175void pcd_event_schedule_s68k(enum pcd_event event, int after)
4f265db7 176{
ae214f1c 177 if (SekCyclesLeftS68k > after)
178 SekEndRunS68k(after);
4f265db7 179
ae214f1c 180 pcd_event_schedule(SekCyclesDoneS68k(), event, after);
181}
4f265db7 182
ae214f1c 183static void pcd_run_events(unsigned int until)
184{
185 int oldest, oldest_diff, time;
186 int i, diff;
187
188 while (1) {
189 oldest = -1, oldest_diff = 0x7fffffff;
190
191 for (i = 0; i < PCD_EVENT_COUNT; i++) {
192 if (pcd_event_times[i]) {
193 diff = pcd_event_times[i] - until;
194 if (diff < oldest_diff) {
195 oldest_diff = diff;
196 oldest = i;
197 }
198 }
199 }
200
201 if (oldest_diff <= 0) {
202 time = pcd_event_times[oldest];
203 pcd_event_times[oldest] = 0;
204 elprintf(EL_CD, "cd: run event #%d %u", oldest, time);
205 pcd_event_cbs[oldest](time);
206 }
207 else if (oldest_diff < 0x7fffffff) {
208 event_time_next = pcd_event_times[oldest];
209 break;
210 }
211 else {
212 event_time_next = 0;
213 break;
214 }
215 }
4f265db7 216
ae214f1c 217 if (oldest != -1)
218 elprintf(EL_CD, "cd: next event #%d at %u",
219 oldest, event_time_next);
4f265db7 220}
221
bc3c13d3 222void pcd_sync_s68k(unsigned int m68k_target)
ae214f1c 223{
224 #define now SekCycleCntS68k
225 unsigned int s68k_target =
226 (unsigned long long)m68k_target * m68k_cycle_mult >> 16;
227 unsigned int target;
b837b69b 228
ae214f1c 229 elprintf(EL_CD, "s68k sync to %u/%u", m68k_target, s68k_target);
230
231 if ((Pico_mcd->m.busreq & 3) != 1) { /* busreq/reset */
232 SekCycleCntS68k = SekCycleAimS68k = s68k_target;
233 pcd_run_events(m68k_target);
234 return;
235 }
236
237 while (CYCLES_GT(s68k_target, now)) {
238 if (event_time_next && CYCLES_GE(now, event_time_next))
239 pcd_run_events(now);
240
241 target = s68k_target;
242 if (event_time_next && CYCLES_GT(target, event_time_next))
243 target = event_time_next;
244
245 SekRunS68k(target);
246 }
247 #undef now
c987bb5c 248}
ae214f1c 249
250#define PICO_CD
251#define CPUS_RUN(m68k_cycles) \
252 SekRunM68k(m68k_cycles)
253
efcba75f 254#include "../pico_cmn.c"
cc68a136 255
256
2aa27095 257PICO_INTERNAL void PicoFrameMCD(void)
cc68a136 258{
602133e1 259 if (!(PicoOpt&POPT_ALT_RENDERER))
cc68a136 260 PicoFrameStart();
261
ae214f1c 262 // ~1.63 for NTSC, ~1.645 for PAL
263 if (Pico.m.pal)
264 m68k_cycle_mult = ((12500000ull << 16) / (50*312*488));
265 else
266 m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1;
267
bf5fbbb4 268 PicoFrameHints();
cc68a136 269}
270
ae214f1c 271void pcd_state_loaded(void)
272{
273 unsigned int cycles;
274 int diff;
275
276 pcd_state_loaded_mem();
277
278 // old savestates..
279 cycles = pcd_cycles_m68k_to_s68k(SekCycleAim);
280 diff = cycles - SekCycleAimS68k;
281 if (diff < -1000 || diff > 1000) {
282 SekCycleCntS68k = SekCycleAimS68k = cycles;
283 }
284 if (pcd_event_times[PCD_EVENT_CDC] == 0) {
285 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_CDC, 12500000/75);
286
287 if (Pico_mcd->s68k_regs[0x31])
288 pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3,
289 Pico_mcd->s68k_regs[0x31] * 384);
290
291 if (Pico_mcd->rot_comp.Reg_58 & 0x8000) {
292 Pico_mcd->rot_comp.Reg_58 &= 0x7fff;
293 Pico_mcd->rot_comp.Reg_64 = 0;
294 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1)
295 SekInterruptS68k(1);
296 }
297 if (Pico_mcd->scd.Status_CDC & 0x08)
298 Update_CDC_TRansfer(Pico_mcd->s68k_regs[4] & 7);
299 }
300}
cc68a136 301
ae214f1c 302// vim:shiftwidth=2:ts=2:expandtab