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)); |
4fb43555 |
38 | memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, |
39 | formatted_bram, fmt_size); |
51a902ae |
40 | memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs)); |
4f265db7 |
41 | memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm)); |
5c69a605 |
42 | memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m)); |
51a902ae |
43 | |
d0132772 |
44 | Reset_CD(); |
45 | |
4fb43555 |
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 |
4fb43555 |
50 | memset(Pico_mcd->bios + 0x70, 0xff, 4); |
51 | } |
cc68a136 |
52 | |
d0132772 |
53 | void pcd_soft_reset(void) |
4fb43555 |
54 | { |
d0132772 |
55 | // Reset_CD(); // breaks Fahrenheit CD swap |
56 | |
5c69a605 |
57 | LC89510_Reset(); |
51a902ae |
58 | gfx_cd_reset(); |
3aa1e148 |
59 | #ifdef _ASM_CD_MEMORY_C |
00bd648e |
60 | //PicoMemResetCDdecode(1); // don't have to call this in 2M mode |
4ff2d527 |
61 | #endif |
cc68a136 |
62 | |
d0132772 |
63 | pcd_event_schedule_s68k(PCD_EVENT_CDC, 12500000/75); |
64 | |
65 | // TODO: test if register state/timers change |
66 | } |
67 | |
68 | PICO_INTERNAL int PicoResetMCD(void) |
69 | { |
70 | // reset button doesn't affect MCD hardware |
71 | |
6cadc2da |
72 | // use SRam.data for RAM cart |
af37bca8 |
73 | if (PicoOpt & POPT_EN_MCD_RAMCART) { |
d6114368 |
74 | if (SRam.data == NULL) |
75 | SRam.data = calloc(1, 0x12000); |
76 | } |
77 | else if (SRam.data != NULL) { |
78 | free(SRam.data); |
79 | SRam.data = NULL; |
80 | } |
b542be46 |
81 | SRam.start = SRam.end = 0; // unused |
6cadc2da |
82 | |
cc68a136 |
83 | return 0; |
84 | } |
85 | |
ae214f1c |
86 | static __inline void SekRunS68k(unsigned int to) |
cc68a136 |
87 | { |
88 | int cyc_do; |
ae214f1c |
89 | |
90 | SekCycleAimS68k = to; |
91 | if ((cyc_do = SekCycleAimS68k - SekCycleCntS68k) <= 0) |
92 | return; |
93 | |
30e8aac4 |
94 | if (SekShouldInterrupt()) |
95 | Pico_mcd->m.s68k_poll_a = 0; |
96 | |
ae214f1c |
97 | SekCycleCntS68k += cyc_do; |
98 | #if defined(EMU_C68K) |
99 | PicoCpuCS68k.cycles = cyc_do; |
3aa1e148 |
100 | CycloneRun(&PicoCpuCS68k); |
ae214f1c |
101 | SekCycleCntS68k -= PicoCpuCS68k.cycles; |
b837b69b |
102 | #elif defined(EMU_M68K) |
3aa1e148 |
103 | m68k_set_context(&PicoCpuMS68k); |
ae214f1c |
104 | SekCycleCntS68k += m68k_execute(cyc_do) - cyc_do; |
ed4402a7 |
105 | m68k_set_context(&PicoCpuMM68k); |
3aa1e148 |
106 | #elif defined(EMU_F68K) |
ae214f1c |
107 | g_m68kcontext = &PicoCpuFS68k; |
108 | SekCycleCntS68k += fm68k_emulate(cyc_do, 0, 0) - cyc_do; |
109 | g_m68kcontext = &PicoCpuFM68k; |
cc68a136 |
110 | #endif |
111 | } |
112 | |
68cba51e |
113 | |
ae214f1c |
114 | unsigned int pcd_cycles_m68k_to_s68k(unsigned int c) |
8022f53d |
115 | { |
ae214f1c |
116 | return (long long)c * m68k_cycle_mult >> 16; |
8022f53d |
117 | } |
ae214f1c |
118 | |
119 | /* events */ |
120 | static void pcd_cdc_event(unsigned int now) |
68cba51e |
121 | { |
ae214f1c |
122 | // 75Hz CDC update |
123 | Check_CD_Command(); |
124 | pcd_event_schedule(now, PCD_EVENT_CDC, 12500000/75); |
125 | } |
7336a99a |
126 | |
ae214f1c |
127 | static void pcd_int3_timer_event(unsigned int now) |
128 | { |
129 | if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN3) { |
130 | elprintf(EL_INTS|EL_CD, "s68k: timer irq 3"); |
131 | SekInterruptS68k(3); |
132 | } |
7336a99a |
133 | |
ae214f1c |
134 | if (Pico_mcd->s68k_regs[0x31] != 0) |
135 | pcd_event_schedule(now, PCD_EVENT_TIMER3, |
136 | Pico_mcd->s68k_regs[0x31] * 384); |
137 | } |
138 | |
ae214f1c |
139 | static void pcd_dma_event(unsigned int now) |
140 | { |
141 | int ddx = Pico_mcd->s68k_regs[4] & 7; |
142 | Update_CDC_TRansfer(ddx); |
143 | } |
68cba51e |
144 | |
ae214f1c |
145 | typedef void (event_cb)(unsigned int now); |
146 | |
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, |
87650acd |
153 | [PCD_EVENT_GFX] = gfx_cd_update, |
ae214f1c |
154 | [PCD_EVENT_DMA] = pcd_dma_event, |
155 | }; |
156 | |
157 | void pcd_event_schedule(unsigned int now, enum pcd_event event, int after) |
bf098bc5 |
158 | { |
ae214f1c |
159 | unsigned int when; |
160 | |
161 | when = now + after; |
162 | if (when == 0) { |
163 | // event cancelled |
164 | pcd_event_times[event] = 0; |
165 | return; |
166 | } |
bf098bc5 |
167 | |
ae214f1c |
168 | when |= 1; |
bf098bc5 |
169 | |
ae214f1c |
170 | elprintf(EL_CD, "cd: new event #%u %u->%u", event, now, when); |
171 | pcd_event_times[event] = when; |
bf098bc5 |
172 | |
ae214f1c |
173 | if (event_time_next == 0 || CYCLES_GT(event_time_next, when)) |
174 | event_time_next = when; |
bf098bc5 |
175 | } |
176 | |
ae214f1c |
177 | void pcd_event_schedule_s68k(enum pcd_event event, int after) |
4f265db7 |
178 | { |
ae214f1c |
179 | if (SekCyclesLeftS68k > after) |
180 | SekEndRunS68k(after); |
4f265db7 |
181 | |
ae214f1c |
182 | pcd_event_schedule(SekCyclesDoneS68k(), event, after); |
183 | } |
4f265db7 |
184 | |
ae214f1c |
185 | static void pcd_run_events(unsigned int until) |
186 | { |
187 | int oldest, oldest_diff, time; |
188 | int i, diff; |
189 | |
190 | while (1) { |
191 | oldest = -1, oldest_diff = 0x7fffffff; |
192 | |
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) { |
197 | oldest_diff = diff; |
198 | oldest = i; |
199 | } |
200 | } |
201 | } |
202 | |
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); |
208 | } |
209 | else if (oldest_diff < 0x7fffffff) { |
210 | event_time_next = pcd_event_times[oldest]; |
211 | break; |
212 | } |
213 | else { |
214 | event_time_next = 0; |
215 | break; |
216 | } |
217 | } |
4f265db7 |
218 | |
ae214f1c |
219 | if (oldest != -1) |
220 | elprintf(EL_CD, "cd: next event #%d at %u", |
221 | oldest, event_time_next); |
4f265db7 |
222 | } |
223 | |
08769494 |
224 | int pcd_sync_s68k(unsigned int m68k_target, int m68k_poll_sync) |
ae214f1c |
225 | { |
226 | #define now SekCycleCntS68k |
227 | unsigned int s68k_target = |
228 | (unsigned long long)m68k_target * m68k_cycle_mult >> 16; |
229 | unsigned int target; |
b837b69b |
230 | |
08769494 |
231 | elprintf(EL_CD, "s68k sync to %u, %u->%u", |
232 | m68k_target, now, s68k_target); |
ae214f1c |
233 | |
4fb43555 |
234 | if (Pico_mcd->m.busreq != 1) { /* busreq/reset */ |
ae214f1c |
235 | SekCycleCntS68k = SekCycleAimS68k = s68k_target; |
236 | pcd_run_events(m68k_target); |
08769494 |
237 | return 0; |
ae214f1c |
238 | } |
239 | |
240 | while (CYCLES_GT(s68k_target, now)) { |
241 | if (event_time_next && CYCLES_GE(now, event_time_next)) |
242 | pcd_run_events(now); |
243 | |
244 | target = s68k_target; |
245 | if (event_time_next && CYCLES_GT(target, event_time_next)) |
246 | target = event_time_next; |
247 | |
248 | SekRunS68k(target); |
08769494 |
249 | if (m68k_poll_sync && Pico_mcd->m.m68k_poll_cnt == 0) |
250 | break; |
ae214f1c |
251 | } |
08769494 |
252 | |
253 | return s68k_target - now; |
ae214f1c |
254 | #undef now |
c987bb5c |
255 | } |
ae214f1c |
256 | |
ba6e8bfd |
257 | #define pcd_run_cpus_normal pcd_run_cpus |
258 | //#define pcd_run_cpus_lockstep pcd_run_cpus |
259 | |
08769494 |
260 | static void SekSyncM68k(void); |
261 | |
fa8fb754 |
262 | void pcd_run_cpus_normal(int m68k_cycles) |
08769494 |
263 | { |
264 | SekCycleAim += m68k_cycles; |
30e8aac4 |
265 | if (SekShouldInterrupt() || Pico_mcd->m.m68k_poll_cnt < 12) |
cc5ffc3c |
266 | Pico_mcd->m.m68k_poll_cnt = 0; |
267 | else if (Pico_mcd->m.m68k_poll_cnt >= 16) { |
08769494 |
268 | int s68k_left = pcd_sync_s68k(SekCycleAim, 1); |
269 | if (s68k_left <= 0) { |
ba6e8bfd |
270 | elprintf(EL_CDPOLL, "m68k poll [%02x] x%d @%06x", |
08769494 |
271 | Pico_mcd->m.m68k_poll_a, Pico_mcd->m.m68k_poll_cnt, SekPc); |
272 | SekCycleCnt = SekCycleAim; |
273 | return; |
274 | } |
275 | SekCycleCnt = SekCycleAim - (s68k_left * 40220 >> 16); |
276 | } |
277 | |
278 | SekSyncM68k(); |
279 | } |
280 | |
fa8fb754 |
281 | void pcd_run_cpus_lockstep(int m68k_cycles) |
ba6e8bfd |
282 | { |
283 | unsigned int target = SekCycleAim + m68k_cycles; |
284 | do { |
285 | SekCycleAim += 8; |
286 | SekSyncM68k(); |
287 | pcd_sync_s68k(SekCycleAim, 0); |
288 | } while (CYCLES_GT(target, SekCycleAim)); |
cc5ffc3c |
289 | |
290 | SekCycleAim = target; |
ba6e8bfd |
291 | } |
292 | |
ae214f1c |
293 | #define PICO_CD |
294 | #define CPUS_RUN(m68k_cycles) \ |
08769494 |
295 | pcd_run_cpus(m68k_cycles) |
ae214f1c |
296 | |
efcba75f |
297 | #include "../pico_cmn.c" |
cc68a136 |
298 | |
299 | |
2aa27095 |
300 | PICO_INTERNAL void PicoFrameMCD(void) |
cc68a136 |
301 | { |
602133e1 |
302 | if (!(PicoOpt&POPT_ALT_RENDERER)) |
cc68a136 |
303 | PicoFrameStart(); |
304 | |
ae214f1c |
305 | // ~1.63 for NTSC, ~1.645 for PAL |
306 | if (Pico.m.pal) |
307 | m68k_cycle_mult = ((12500000ull << 16) / (50*312*488)); |
308 | else |
309 | m68k_cycle_mult = ((12500000ull << 16) / (60*262*488)) + 1; |
310 | |
bf5fbbb4 |
311 | PicoFrameHints(); |
cc68a136 |
312 | } |
313 | |
ae214f1c |
314 | void pcd_state_loaded(void) |
315 | { |
316 | unsigned int cycles; |
317 | int diff; |
318 | |
319 | pcd_state_loaded_mem(); |
320 | |
321 | // old savestates.. |
322 | cycles = pcd_cycles_m68k_to_s68k(SekCycleAim); |
323 | diff = cycles - SekCycleAimS68k; |
324 | if (diff < -1000 || diff > 1000) { |
325 | SekCycleCntS68k = SekCycleAimS68k = cycles; |
326 | } |
327 | if (pcd_event_times[PCD_EVENT_CDC] == 0) { |
328 | pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_CDC, 12500000/75); |
329 | |
330 | if (Pico_mcd->s68k_regs[0x31]) |
331 | pcd_event_schedule(SekCycleAimS68k, PCD_EVENT_TIMER3, |
332 | Pico_mcd->s68k_regs[0x31] * 384); |
333 | |
334 | if (Pico_mcd->rot_comp.Reg_58 & 0x8000) { |
335 | Pico_mcd->rot_comp.Reg_58 &= 0x7fff; |
336 | Pico_mcd->rot_comp.Reg_64 = 0; |
337 | if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1) |
338 | SekInterruptS68k(1); |
339 | } |
340 | if (Pico_mcd->scd.Status_CDC & 0x08) |
341 | Update_CDC_TRansfer(Pico_mcd->s68k_regs[4] & 7); |
342 | } |
343 | } |
cc68a136 |
344 | |
ae214f1c |
345 | // vim:shiftwidth=2:ts=2:expandtab |