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 |
12 | extern unsigned char formatted_bram[4*0x10]; |
ae214f1c |
13 | |
14 | static unsigned int m68k_cycle_mult; |
89fa852d |
15 | |
721cd396 |
16 | void (*PicoMCDopenTray)(void) = NULL; |
d687ef50 |
17 | void (*PicoMCDcloseTray)(void) = NULL; |
89fa852d |
18 | |
cc68a136 |
19 | |
2aa27095 |
20 | PICO_INTERNAL void PicoInitMCD(void) |
cc68a136 |
21 | { |
22 | SekInitS68k(); |
23 | Init_CD_Driver(); |
cc68a136 |
24 | } |
25 | |
eff55556 |
26 | PICO_INTERNAL void PicoExitMCD(void) |
cc68a136 |
27 | { |
28 | End_CD_Driver(); |
29 | } |
30 | |
1cb1584b |
31 | PICO_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 | |
41 | PICO_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 |
74 | static __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 |
99 | unsigned 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 */ |
105 | static 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 |
112 | static 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 | |
124 | static 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 |
137 | static 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 |
143 | typedef void (event_cb)(unsigned int now); |
144 | |
145 | /* times are in s68k (12.5MHz) cycles */ |
146 | unsigned int pcd_event_times[PCD_EVENT_COUNT]; |
147 | static unsigned int event_time_next; |
148 | static 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 | |
155 | void 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 |
175 | void 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 |
183 | static 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 | |
08769494 |
222 | int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync) |
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 | |
08769494 |
229 | elprintf(EL_CD, "s68k sync to %u, %u->%u", |
230 | m68k_target, now, s68k_target); |
ae214f1c |
231 | |
232 | if ((Pico_mcd->m.busreq & 3) != 1) { /* busreq/reset */ |
233 | SekCycleCntS68k = SekCycleAimS68k = s68k_target; |
234 | pcd_run_events(m68k_target); |
08769494 |
235 | return 0; |
ae214f1c |
236 | } |
237 | |
238 | while (CYCLES_GT(s68k_target, now)) { |
239 | if (event_time_next && CYCLES_GE(now, event_time_next)) |
240 | pcd_run_events(now); |
241 | |
242 | target = s68k_target; |
243 | if (event_time_next && CYCLES_GT(target, event_time_next)) |
244 | target = event_time_next; |
245 | |
246 | SekRunS68k(target); |
08769494 |
247 | if (m68k_poll_sync && Pico_mcd->m.m68k_poll_cnt == 0) |
248 | break; |
ae214f1c |
249 | } |
08769494 |
250 | |
251 | return s68k_target - now; |
ae214f1c |
252 | #undef now |
c987bb5c |
253 | } |
ae214f1c |
254 | |
08769494 |
255 | static void SekSyncM68k(void); |
256 | |
257 | static void pcd_run_cpus(int m68k_cycles) |
258 | { |
259 | SekCycleAim += m68k_cycles; |
260 | if (Pico_mcd->m.m68k_poll_cnt >= 16 && !SekShouldInterrupt()) { |
261 | int s68k_left = pcd_sync_s68k(SekCycleAim, 1); |
262 | if (s68k_left <= 0) { |
263 | elprintf(EL_CDPOLL, "m68k poll [%02x] %d @%06x", |
264 | Pico_mcd->m.m68k_poll_a, Pico_mcd->m.m68k_poll_cnt, SekPc); |
265 | SekCycleCnt = SekCycleAim; |
266 | return; |
267 | } |
268 | SekCycleCnt = SekCycleAim - (s68k_left * 40220 >> 16); |
269 | } |
270 | |
271 | SekSyncM68k(); |
272 | } |
273 | |
ae214f1c |
274 | #define PICO_CD |
275 | #define CPUS_RUN(m68k_cycles) \ |
08769494 |
276 | pcd_run_cpus(m68k_cycles) |
ae214f1c |
277 | |
efcba75f |
278 | #include "../pico_cmn.c" |
cc68a136 |
279 | |
280 | |
2aa27095 |
281 | PICO_INTERNAL void PicoFrameMCD(void) |
cc68a136 |
282 | { |
602133e1 |
283 | if (!(PicoOpt&POPT_ALT_RENDERER)) |
cc68a136 |
284 | PicoFrameStart(); |
285 | |
ae214f1c |
286 | // ~1.63 for NTSC, ~1.645 for PAL |
287 | if (Pico.m.pal) |
288 | m68k_cycle_mult = ((12500000ull << 16) / (50*312*488)); |
289 | else |
290 | m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1; |
291 | |
bf5fbbb4 |
292 | PicoFrameHints(); |
cc68a136 |
293 | } |
294 | |
ae214f1c |
295 | void pcd_state_loaded(void) |
296 | { |
297 | unsigned int cycles; |
298 | int diff; |
299 | |
300 | pcd_state_loaded_mem(); |
301 | |
302 | // old savestates.. |
303 | cycles = pcd_cycles_m68k_to_s68k(SekCycleAim); |
304 | diff = cycles - SekCycleAimS68k; |
305 | if (diff < -1000 || diff > 1000) { |
306 | SekCycleCntS68k = SekCycleAimS68k = cycles; |
307 | } |
308 | if (pcd_event_times[PCD_EVENT_CDC] == 0) { |
309 | pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_CDC, 12500000/75); |
310 | |
311 | if (Pico_mcd->s68k_regs[0x31]) |
312 | pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3, |
313 | Pico_mcd->s68k_regs[0x31] * 384); |
314 | |
315 | if (Pico_mcd->rot_comp.Reg_58 & 0x8000) { |
316 | Pico_mcd->rot_comp.Reg_58 &= 0x7fff; |
317 | Pico_mcd->rot_comp.Reg_64 = 0; |
318 | if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1) |
319 | SekInterruptS68k(1); |
320 | } |
321 | if (Pico_mcd->scd.Status_CDC & 0x08) |
322 | Update_CDC_TRansfer(Pico_mcd->s68k_regs[4] & 7); |
323 | } |
324 | } |
cc68a136 |
325 | |
ae214f1c |
326 | // vim:shiftwidth=2:ts=2:expandtab |