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