cff531af |
1 | /* |
2 | * PicoDrive |
6a98f03e |
3 | * (C) notaz, 2009,2010,2013 |
7bf552b5 |
4 | * (C) irixxxx, 2019-2024 |
cff531af |
5 | * |
6 | * This work is licensed under the terms of MAME license. |
7 | * See COPYING file in the top-level directory. |
8 | */ |
be2c4208 |
9 | #include "../pico_int.h" |
974fdb5b |
10 | #include "../sound/ym2612.h" |
f821bb70 |
11 | #include <cpu/sh2/compiler.h> |
be2c4208 |
12 | |
13 | struct Pico32x Pico32x; |
83ff19ec |
14 | SH2 sh2s[2]; |
be2c4208 |
15 | |
397ccdc6 |
16 | #define SH2_IDLE_STATES (SH2_STATE_CPOLL|SH2_STATE_VPOLL|SH2_STATE_RPOLL|SH2_STATE_SLEEP) |
19886062 |
17 | |
e05b81fc |
18 | static int REGPARM(2) sh2_irq_cb(SH2 *sh2, int level) |
4ea707e1 |
19 | { |
e05b81fc |
20 | if (sh2->pending_irl > sh2->pending_int_irq) { |
f8675e28 |
21 | elprintf_sh2(sh2, EL_32X, "ack/irl %d @ %08x", |
22 | level, sh2_pc(sh2)); |
e05b81fc |
23 | return 64 + sh2->pending_irl / 2; |
24 | } else { |
f8675e28 |
25 | elprintf_sh2(sh2, EL_32X, "ack/int %d/%d @ %08x", |
26 | level, sh2->pending_int_vector, sh2_pc(sh2)); |
e05b81fc |
27 | sh2->pending_int_irq = 0; // auto-clear |
28 | sh2->pending_level = sh2->pending_irl; |
29 | return sh2->pending_int_vector; |
30 | } |
4ea707e1 |
31 | } |
32 | |
c1931173 |
33 | // MUST specify active_sh2 when called from sh2 memhandlers |
d40a5af4 |
34 | void p32x_update_irls(SH2 *active_sh2, unsigned int m68k_cycles) |
4ea707e1 |
35 | { |
36 | int irqs, mlvl = 0, slvl = 0; |
a8fd6e37 |
37 | int mrun, srun; |
4ea707e1 |
38 | |
eec6905e |
39 | if ((Pico32x.regs[0] & (P32XS_nRES|P32XS_ADEN)) != (P32XS_nRES|P32XS_ADEN)) |
40 | return; |
41 | |
19886062 |
42 | if (active_sh2 != NULL) |
43 | m68k_cycles = sh2_cycles_done_m68k(active_sh2); |
44 | |
aaea8e3e |
45 | // find top bit = highest irq number (0 <= irl <= 14/2) by binary search |
46 | |
4ea707e1 |
47 | // msh2 |
83416730 |
48 | irqs = Pico32x.sh2irqi[0]; |
aaea8e3e |
49 | if (irqs >= 0x10) mlvl += 8, irqs >>= 4; |
50 | if (irqs >= 0x04) mlvl += 4, irqs >>= 2; |
51 | if (irqs >= 0x02) mlvl += 2, irqs >>= 1; |
4ea707e1 |
52 | |
53 | // ssh2 |
83416730 |
54 | irqs = Pico32x.sh2irqi[1]; |
aaea8e3e |
55 | if (irqs >= 0x10) slvl += 8, irqs >>= 4; |
56 | if (irqs >= 0x04) slvl += 4, irqs >>= 2; |
57 | if (irqs >= 0x02) slvl += 2, irqs >>= 1; |
4ea707e1 |
58 | |
d40a5af4 |
59 | mrun = sh2_irl_irq(&msh2, mlvl, msh2.state & SH2_STATE_RUN); |
c1931173 |
60 | if (mrun) { |
d8a897a6 |
61 | p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_IDLE_STATES & ~SH2_STATE_SLEEP, m68k_cycles); |
26d58060 |
62 | if (msh2.state & SH2_STATE_RUN) |
63 | sh2_end_run(&msh2, 0); |
c1931173 |
64 | } |
19886062 |
65 | |
d40a5af4 |
66 | srun = sh2_irl_irq(&ssh2, slvl, ssh2.state & SH2_STATE_RUN); |
c1931173 |
67 | if (srun) { |
d8a897a6 |
68 | p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_IDLE_STATES & ~SH2_STATE_SLEEP, m68k_cycles); |
26d58060 |
69 | if (ssh2.state & SH2_STATE_RUN) |
70 | sh2_end_run(&ssh2, 0); |
c1931173 |
71 | } |
19886062 |
72 | |
a8fd6e37 |
73 | elprintf(EL_32X, "update_irls: m %d/%d, s %d/%d", mlvl, mrun, slvl, srun); |
4ea707e1 |
74 | } |
75 | |
9e1fa0a6 |
76 | // the mask register is inconsistent, CMD is supposed to be a mask, |
77 | // while others are actually irq trigger enables? |
78 | // TODO: test on hw.. |
d40a5af4 |
79 | void p32x_trigger_irq(SH2 *sh2, unsigned int m68k_cycles, unsigned int mask) |
9e1fa0a6 |
80 | { |
83416730 |
81 | Pico32x.sh2irqi[0] |= mask & P32XI_VRES; |
82 | Pico32x.sh2irqi[1] |= mask & P32XI_VRES; |
9e1fa0a6 |
83 | Pico32x.sh2irqi[0] |= mask & (Pico32x.sh2irq_mask[0] << 3); |
84 | Pico32x.sh2irqi[1] |= mask & (Pico32x.sh2irq_mask[1] << 3); |
85 | |
86 | p32x_update_irls(sh2, m68k_cycles); |
87 | } |
88 | |
d40a5af4 |
89 | void p32x_update_cmd_irq(SH2 *sh2, unsigned int m68k_cycles) |
9e1fa0a6 |
90 | { |
91 | if ((Pico32x.sh2irq_mask[0] & 2) && (Pico32x.regs[2 / 2] & 1)) |
92 | Pico32x.sh2irqi[0] |= P32XI_CMD; |
93 | else |
94 | Pico32x.sh2irqi[0] &= ~P32XI_CMD; |
95 | |
96 | if ((Pico32x.sh2irq_mask[1] & 2) && (Pico32x.regs[2 / 2] & 2)) |
97 | Pico32x.sh2irqi[1] |= P32XI_CMD; |
98 | else |
99 | Pico32x.sh2irqi[1] &= ~P32XI_CMD; |
100 | |
101 | p32x_update_irls(sh2, m68k_cycles); |
102 | } |
103 | |
be2c4208 |
104 | void Pico32xStartup(void) |
105 | { |
106 | elprintf(EL_STATUS|EL_32X, "32X startup"); |
107 | |
93f9619e |
108 | PicoIn.AHW |= PAHW_32X; |
eec6905e |
109 | // TODO: OOM handling |
110 | if (Pico32xMem == NULL) { |
111 | Pico32xMem = plat_mmap(0x06000000, sizeof(*Pico32xMem), 0, 0); |
112 | if (Pico32xMem == NULL) { |
113 | elprintf(EL_STATUS, "OOM"); |
114 | return; |
115 | } |
116 | memset(Pico32xMem, 0, sizeof(struct Pico32xMem)); |
83ff19ec |
117 | |
eec6905e |
118 | sh2_init(&msh2, 0, &ssh2); |
119 | msh2.irq_callback = sh2_irq_cb; |
120 | sh2_init(&ssh2, 1, &msh2); |
121 | ssh2.irq_callback = sh2_irq_cb; |
122 | } |
fa23e7cc |
123 | |
83ff19ec |
124 | PicoMemSetup32x(); |
045a4c52 |
125 | p32x_pwm_ctl_changed(); |
a8fd6e37 |
126 | p32x_timers_recalc(); |
acd35d4c |
127 | |
fa23e7cc |
128 | Pico32x.regs[0] |= P32XS_ADEN; |
129 | |
fa8fb754 |
130 | Pico32x.sh2_regs[0] = P32XS2_ADEN; |
131 | if (Pico.m.ncart_in) |
fa23e7cc |
132 | Pico32x.sh2_regs[0] |= P32XS2_nCART; |
fa8fb754 |
133 | |
be2c4208 |
134 | if (!Pico.m.pal) |
974fdb5b |
135 | Pico32x.vdp_regs[0] |= P32XV_nPAL; |
eec6905e |
136 | else |
137 | Pico32x.vdp_regs[0] &= ~P32XV_nPAL; |
be2c4208 |
138 | |
2446536b |
139 | rendstatus_old = -1; |
140 | |
3d7abd69 |
141 | Pico32xPrepare(); |
974fdb5b |
142 | emu_32x_startup(); |
be2c4208 |
143 | } |
144 | |
9f7abd68 |
145 | void Pico32xShutdown(void) |
146 | { |
fa23e7cc |
147 | elprintf(EL_STATUS|EL_32X, "32X shutdown"); |
eec6905e |
148 | Pico32x.sh2_regs[0] &= ~P32XS2_ADEN; |
fa23e7cc |
149 | Pico32x.regs[0] &= ~P32XS_ADEN; |
9f7abd68 |
150 | |
151 | rendstatus_old = -1; |
152 | |
153 | PicoIn.AHW &= ~PAHW_32X; |
9961d9fd |
154 | if (PicoIn.AHW & PAHW_MCD) |
155 | PicoMemSetupCD(); |
156 | else |
157 | PicoMemSetup(); |
9f7abd68 |
158 | emu_32x_startup(); |
159 | } |
160 | |
83ff19ec |
161 | void p32x_reset_sh2s(void) |
162 | { |
163 | elprintf(EL_32X, "sh2 reset"); |
164 | |
165 | sh2_reset(&msh2); |
166 | sh2_reset(&ssh2); |
cd0ace28 |
167 | sh2_peripheral_reset(&msh2); |
168 | sh2_peripheral_reset(&ssh2); |
83ff19ec |
169 | |
170 | // if we don't have BIOS set, perform it's work here. |
171 | // MSH2 |
172 | if (p32x_bios_m == NULL) { |
83ff19ec |
173 | sh2_set_gbr(0, 0x20004000); |
83ff19ec |
174 | |
805fbe6f |
175 | if (!Pico.m.ncart_in) { // copy IDL from cartridge |
61c4e511 |
176 | unsigned int idl_src, idl_dst, idl_size; // initial data load |
177 | unsigned int vbr; |
61c4e511 |
178 | // initial data |
91ea9406 |
179 | idl_src = CPU_BE2(*(u32 *)(Pico.rom + 0x3d4)) & ~0xf0000000; |
180 | idl_dst = CPU_BE2(*(u32 *)(Pico.rom + 0x3d8)) & ~0xf0000000; |
181 | idl_size= CPU_BE2(*(u32 *)(Pico.rom + 0x3dc)); |
439cf7f8 |
182 | // copy in guest memory space |
183 | idl_src += 0x2000000; |
184 | idl_dst += 0x6000000; |
185 | while (idl_size >= 4) { |
186 | p32x_sh2_write32(idl_dst, p32x_sh2_read32(idl_src, &msh2), &msh2); |
187 | idl_src += 4, idl_dst += 4, idl_size -= 4; |
61c4e511 |
188 | } |
61c4e511 |
189 | |
190 | // VBR |
91ea9406 |
191 | vbr = CPU_BE2(*(u32 *)(Pico.rom + 0x3e8)); |
61c4e511 |
192 | sh2_set_vbr(0, vbr); |
193 | |
194 | // checksum and M_OK |
91ea9406 |
195 | Pico32x.regs[0x28 / 2] = *(u16 *)(Pico.rom + 0x18e); |
61c4e511 |
196 | } |
83ff19ec |
197 | // program will set M_OK |
198 | } |
199 | |
200 | // SSH2 |
201 | if (p32x_bios_s == NULL) { |
202 | unsigned int vbr; |
203 | |
204 | // GBR/VBR |
91ea9406 |
205 | vbr = CPU_BE2(*(u32 *)(Pico.rom + 0x3ec)); |
83ff19ec |
206 | sh2_set_gbr(1, 0x20004000); |
207 | sh2_set_vbr(1, vbr); |
208 | // program will set S_OK |
209 | } |
ed4402a7 |
210 | |
ae214f1c |
211 | msh2.m68krcycles_done = ssh2.m68krcycles_done = SekCyclesDone(); |
83ff19ec |
212 | } |
213 | |
be2c4208 |
214 | void Pico32xInit(void) |
215 | { |
974fdb5b |
216 | } |
217 | |
218 | void PicoPower32x(void) |
219 | { |
220 | memset(&Pico32x, 0, sizeof(Pico32x)); |
5e49c3a8 |
221 | |
83ff19ec |
222 | Pico32x.regs[0] = P32XS_REN|P32XS_nRES; // verified |
c9f94534 |
223 | Pico32x.regs[0x10/2] = 0xffff; |
4a1fb183 |
224 | Pico32x.vdp_regs[0x0a/2] = P32XV_VBLK|P32XV_PEN; |
be2c4208 |
225 | } |
226 | |
5e49c3a8 |
227 | void PicoUnload32x(void) |
228 | { |
9961d9fd |
229 | if (PicoIn.AHW & PAHW_32X) |
230 | Pico32xShutdown(); |
9f7abd68 |
231 | |
eec6905e |
232 | sh2_finish(&msh2); |
233 | sh2_finish(&ssh2); |
234 | |
5e49c3a8 |
235 | if (Pico32xMem != NULL) |
b081408f |
236 | plat_munmap(Pico32xMem, sizeof(*Pico32xMem)); |
5e49c3a8 |
237 | Pico32xMem = NULL; |
5e49c3a8 |
238 | } |
239 | |
be2c4208 |
240 | void PicoReset32x(void) |
241 | { |
93f9619e |
242 | if (PicoIn.AHW & PAHW_32X) { |
ae214f1c |
243 | p32x_trigger_irq(NULL, SekCyclesDone(), P32XI_VRES); |
d8a897a6 |
244 | p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_IDLE_STATES, SekCyclesDone()); |
245 | p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_IDLE_STATES, SekCyclesDone()); |
045a4c52 |
246 | p32x_pwm_ctl_changed(); |
a8fd6e37 |
247 | p32x_timers_recalc(); |
83ff19ec |
248 | } |
be2c4208 |
249 | } |
250 | |
2322260c |
251 | static void Pico32xRenderSync(int lines) |
974fdb5b |
252 | { |
93f9619e |
253 | if (Pico32xDrawMode != PDM32X_OFF && !PicoIn.skipFrame) { |
2322260c |
254 | int offs; |
5aec752d |
255 | |
256 | pprof_start(draw); |
257 | |
2322260c |
258 | offs = 8; |
259 | if (Pico.video.reg[1] & 8) |
7a961c19 |
260 | offs = 0; |
7a961c19 |
261 | |
5a681086 |
262 | if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0 && // 32x not blanking |
e0bcb7a9 |
263 | (!(Pico.video.debug_p & PVD_KILL_32X))) |
5a681086 |
264 | { |
265 | int md_bg = Pico.video.reg[7] & 0x3f; |
5a681086 |
266 | |
2322260c |
267 | // we draw lines up to the sync point (not line-by-line) |
268 | PicoDraw32xLayer(offs, lines-Pico32x.sync_line, md_bg); |
5a681086 |
269 | } |
52e4a905 |
270 | else if (Pico32xDrawMode == PDM32X_BOTH) |
2322260c |
271 | PicoDraw32xLayerMdOnly(offs, lines-Pico32x.sync_line); |
5aec752d |
272 | |
273 | pprof_end(draw); |
5a681086 |
274 | } |
fdaf9d10 |
275 | } |
5a681086 |
276 | |
2322260c |
277 | void Pico32xDrawSync(SH2 *sh2) |
278 | { |
45457406 |
279 | // the fast renderer isn't operating on a line-by-line base |
280 | if (sh2 && !(PicoIn.opt & POPT_ALT_RENDERER)) { |
2322260c |
281 | unsigned int cycle = (sh2 ? sh2_cycles_done_m68k(sh2) : SekCyclesDone()); |
282 | int line = ((cycle - Pico.t.m68c_frame_start) * (long long)((1LL<<32)/488.5)) >> 32; |
283 | |
284 | if (Pico32x.sync_line < line && line < (Pico.video.reg[1] & 8 ? 240 : 224)) { |
285 | // make sure the MD image is also sync'ed to this line for merging |
286 | PicoDrawSync(line, 0, 0); |
287 | |
45457406 |
288 | // pfff... need to save and restore some persistent data for MD renderer |
2322260c |
289 | void *dest = Pico.est.DrawLineDest; |
290 | int incr = Pico.est.DrawLineDestIncr; |
291 | Pico32xRenderSync(line); |
292 | Pico.est.DrawLineDest = dest; |
293 | Pico.est.DrawLineDestIncr = incr; |
294 | } |
295 | |
296 | // remember line we sync'ed to |
297 | Pico32x.sync_line = line; |
298 | } |
299 | } |
300 | |
301 | static void p32x_render_frame(void) |
302 | { |
303 | if (Pico32xDrawMode != PDM32X_OFF && !PicoIn.skipFrame) { |
304 | int lines; |
305 | |
306 | pprof_start(draw); |
307 | |
308 | lines = 224; |
309 | if (Pico.video.reg[1] & 8) |
310 | lines = 240; |
311 | |
312 | Pico32xRenderSync(lines); |
313 | } |
314 | } |
315 | |
fdaf9d10 |
316 | static void p32x_start_blank(void) |
317 | { |
974fdb5b |
318 | // enter vblank |
319 | Pico32x.vdp_regs[0x0a/2] |= P32XV_VBLK|P32XV_PEN; |
320 | |
4ea707e1 |
321 | // FB swap waits until vblank |
974fdb5b |
322 | if ((Pico32x.vdp_regs[0x0a/2] ^ Pico32x.pending_fb) & P32XV_FS) { |
eec6905e |
323 | Pico32x.vdp_regs[0x0a/2] ^= P32XV_FS; |
324 | Pico32xSwapDRAM(Pico32x.pending_fb ^ P32XV_FS); |
974fdb5b |
325 | } |
4ea707e1 |
326 | |
7b02a2c3 |
327 | p32x_trigger_irq(NULL, Pico.t.m68c_aim, P32XI_VINT); |
d8a897a6 |
328 | p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_STATE_VPOLL, Pico.t.m68c_aim); |
329 | p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_STATE_VPOLL, Pico.t.m68c_aim); |
974fdb5b |
330 | } |
331 | |
fdaf9d10 |
332 | static void p32x_end_blank(void) |
333 | { |
334 | // end vblank |
335 | Pico32x.vdp_regs[0x0a/2] &= ~P32XV_VBLK; // get out of vblank |
336 | if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0) // no forced blanking |
337 | Pico32x.vdp_regs[0x0a/2] &= ~P32XV_PEN; // no palette access |
7263343d |
338 | if (!(Pico32x.sh2_regs[0] & 0x80)) { |
339 | // NB must precede VInt per hw manual, min 4 SH-2 cycles to pass Mars Check |
3167aa9a |
340 | Pico32x.hint_counter = (int)(-1.5*0x10); |
d8a897a6 |
341 | p32x_schedule_hint(NULL, Pico.t.m68c_aim); |
7263343d |
342 | } |
c9d5f41b |
343 | |
d8a897a6 |
344 | p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_STATE_VPOLL, Pico.t.m68c_aim); |
345 | p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_STATE_VPOLL, Pico.t.m68c_aim); |
fdaf9d10 |
346 | } |
347 | |
d40a5af4 |
348 | void p32x_schedule_hint(SH2 *sh2, unsigned int m68k_cycles) |
5ac99d9a |
349 | { |
350 | // rather rough, 32x hint is useless in practice |
351 | int after; |
5ac99d9a |
352 | if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 4)) |
353 | return; // nobody cares |
fdaf9d10 |
354 | if (!(Pico32x.sh2_regs[0] & 0x80) && (Pico.video.status & PVS_VB2)) |
5ac99d9a |
355 | return; |
356 | |
7263343d |
357 | Pico32x.hint_counter += (Pico32x.sh2_regs[4 / 2] + 1) * (int)(488.5*0x10); |
358 | after = Pico32x.hint_counter >> 4; |
359 | Pico32x.hint_counter &= 0xf; |
5ac99d9a |
360 | if (sh2 != NULL) |
361 | p32x_event_schedule_sh2(sh2, P32X_EVENT_HINT, after); |
362 | else |
363 | p32x_event_schedule(m68k_cycles, P32X_EVENT_HINT, after); |
364 | } |
365 | |
a8fd6e37 |
366 | /* events */ |
a8fd6e37 |
367 | static void fillend_event(unsigned int now) |
368 | { |
369 | Pico32x.vdp_regs[0x0a/2] &= ~P32XV_nFEN; |
d8a897a6 |
370 | p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_STATE_VPOLL, now); |
371 | p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_STATE_VPOLL, now); |
a8fd6e37 |
372 | } |
373 | |
5ac99d9a |
374 | static void hint_event(unsigned int now) |
375 | { |
9e1fa0a6 |
376 | p32x_trigger_irq(NULL, now, P32XI_HINT); |
5ac99d9a |
377 | p32x_schedule_hint(NULL, now); |
378 | } |
379 | |
a8fd6e37 |
380 | typedef void (event_cb)(unsigned int now); |
381 | |
ae214f1c |
382 | /* times are in m68k (7.6MHz) cycles */ |
383 | unsigned int p32x_event_times[P32X_EVENT_COUNT]; |
a8fd6e37 |
384 | static unsigned int event_time_next; |
ae214f1c |
385 | static event_cb *p32x_event_cbs[P32X_EVENT_COUNT] = { |
24aab4da |
386 | p32x_pwm_irq_event, // P32X_EVENT_PWM |
387 | fillend_event, // P32X_EVENT_FILLEND |
388 | hint_event, // P32X_EVENT_HINT |
a8fd6e37 |
389 | }; |
390 | |
19886062 |
391 | // schedule event at some time 'after', in m68k clocks |
392 | void p32x_event_schedule(unsigned int now, enum p32x_event event, int after) |
a8fd6e37 |
393 | { |
19886062 |
394 | unsigned int when; |
395 | |
396 | when = (now + after) | 1; |
a8fd6e37 |
397 | |
ae214f1c |
398 | elprintf(EL_32X, "32x: new event #%u %u->%u", event, now, when); |
399 | p32x_event_times[event] = when; |
a8fd6e37 |
400 | |
19886062 |
401 | if (event_time_next == 0 || CYCLES_GT(event_time_next, when)) |
a8fd6e37 |
402 | event_time_next = when; |
403 | } |
404 | |
19886062 |
405 | void p32x_event_schedule_sh2(SH2 *sh2, enum p32x_event event, int after) |
406 | { |
407 | unsigned int now = sh2_cycles_done_m68k(sh2); |
408 | int left_to_next; |
409 | |
410 | p32x_event_schedule(now, event, after); |
411 | |
2fa02d5a |
412 | left_to_next = C_M68K_TO_SH2(sh2, (int)(event_time_next - now)); |
413 | if (sh2_cycles_left(sh2) > left_to_next) { |
414 | if (left_to_next < 1) |
6bb230c7 |
415 | left_to_next = 0; |
2fa02d5a |
416 | sh2_end_run(sh2, left_to_next); |
417 | } |
19886062 |
418 | } |
419 | |
ae214f1c |
420 | static void p32x_run_events(unsigned int until) |
a8fd6e37 |
421 | { |
422 | int oldest, oldest_diff, time; |
423 | int i, diff; |
424 | |
425 | while (1) { |
426 | oldest = -1, oldest_diff = 0x7fffffff; |
427 | |
428 | for (i = 0; i < P32X_EVENT_COUNT; i++) { |
ae214f1c |
429 | if (p32x_event_times[i]) { |
430 | diff = p32x_event_times[i] - until; |
a8fd6e37 |
431 | if (diff < oldest_diff) { |
432 | oldest_diff = diff; |
433 | oldest = i; |
434 | } |
435 | } |
436 | } |
437 | |
438 | if (oldest_diff <= 0) { |
ae214f1c |
439 | time = p32x_event_times[oldest]; |
440 | p32x_event_times[oldest] = 0; |
441 | elprintf(EL_32X, "32x: run event #%d %u", oldest, time); |
442 | p32x_event_cbs[oldest](time); |
a8fd6e37 |
443 | } |
444 | else if (oldest_diff < 0x7fffffff) { |
ae214f1c |
445 | event_time_next = p32x_event_times[oldest]; |
a8fd6e37 |
446 | break; |
447 | } |
448 | else { |
449 | event_time_next = 0; |
450 | break; |
451 | } |
452 | } |
453 | |
454 | if (oldest != -1) |
ae214f1c |
455 | elprintf(EL_32X, "32x: next event #%d at %u", |
456 | oldest, event_time_next); |
a8fd6e37 |
457 | } |
458 | |
d40a5af4 |
459 | static void run_sh2(SH2 *sh2, unsigned int m68k_cycles) |
19886062 |
460 | { |
d40a5af4 |
461 | unsigned int cycles, done; |
19886062 |
462 | |
463 | pevt_log_sh2_o(sh2, EVT_RUN_START); |
464 | sh2->state |= SH2_STATE_RUN; |
2fa02d5a |
465 | cycles = C_M68K_TO_SH2(sh2, m68k_cycles); |
f8675e28 |
466 | elprintf_sh2(sh2, EL_32X, "+run %u %d @%08x", |
467 | sh2->m68krcycles_done, cycles, sh2->pc); |
19886062 |
468 | |
2eb21331 |
469 | done = sh2_execute(sh2, cycles); |
19886062 |
470 | |
2fa02d5a |
471 | sh2->m68krcycles_done += C_SH2_TO_M68K(sh2, done); |
19886062 |
472 | sh2->state &= ~SH2_STATE_RUN; |
473 | pevt_log_sh2_o(sh2, EVT_RUN_END); |
f8675e28 |
474 | elprintf_sh2(sh2, EL_32X, "-run %u %d", |
475 | sh2->m68krcycles_done, done); |
19886062 |
476 | } |
477 | |
478 | // sync other sh2 to this one |
479 | // note: recursive call |
480 | void p32x_sync_other_sh2(SH2 *sh2, unsigned int m68k_target) |
481 | { |
f81107f5 |
482 | SH2 *osh2 = sh2->other_sh2; |
19886062 |
483 | int left_to_event; |
484 | int m68k_cycles; |
485 | |
486 | if (osh2->state & SH2_STATE_RUN) |
487 | return; |
488 | |
489 | m68k_cycles = m68k_target - osh2->m68krcycles_done; |
490 | if (m68k_cycles < 200) |
491 | return; |
492 | |
493 | if (osh2->state & SH2_IDLE_STATES) { |
494 | osh2->m68krcycles_done = m68k_target; |
495 | return; |
496 | } |
497 | |
f8675e28 |
498 | elprintf_sh2(osh2, EL_32X, "sync to %u %d", |
499 | m68k_target, m68k_cycles); |
19886062 |
500 | |
501 | run_sh2(osh2, m68k_cycles); |
502 | |
503 | // there might be new event to schedule current sh2 to |
504 | if (event_time_next) { |
2fa02d5a |
505 | left_to_event = C_M68K_TO_SH2(sh2, (int)(event_time_next - m68k_target)); |
19886062 |
506 | if (sh2_cycles_left(sh2) > left_to_event) { |
507 | if (left_to_event < 1) |
6bb230c7 |
508 | left_to_event = 0; |
19886062 |
509 | sh2_end_run(sh2, left_to_event); |
510 | } |
511 | } |
512 | } |
a8fd6e37 |
513 | |
6c2041fe |
514 | #define STEP_LS 24 |
86c16afd |
515 | #define STEP_N 528 // at least one line (488) |
6c2041fe |
516 | |
ed4402a7 |
517 | #define sync_sh2s_normal p32x_sync_sh2s |
518 | //#define sync_sh2s_lockstep p32x_sync_sh2s |
974fdb5b |
519 | |
a8fd6e37 |
520 | /* most timing is in 68k clock */ |
ed4402a7 |
521 | void sync_sh2s_normal(unsigned int m68k_target) |
522 | { |
86c16afd |
523 | unsigned int now, target, next, timer_cycles; |
19886062 |
524 | int cycles; |
ed4402a7 |
525 | |
a8fd6e37 |
526 | elprintf(EL_32X, "sh2 sync to %u", m68k_target); |
ed4402a7 |
527 | |
9961d9fd |
528 | if ((Pico32x.regs[0] & (P32XS_nRES|P32XS_ADEN)) != (P32XS_nRES|P32XS_ADEN)) { |
27e26273 |
529 | msh2.m68krcycles_done = ssh2.m68krcycles_done = m68k_target; |
ed4402a7 |
530 | return; // rare |
27e26273 |
531 | } |
ed4402a7 |
532 | |
a8fd6e37 |
533 | now = msh2.m68krcycles_done; |
534 | if (CYCLES_GT(now, ssh2.m68krcycles_done)) |
535 | now = ssh2.m68krcycles_done; |
536 | timer_cycles = now; |
537 | |
2fa02d5a |
538 | pprof_start(m68k); |
a8fd6e37 |
539 | while (CYCLES_GT(m68k_target, now)) |
ed4402a7 |
540 | { |
a8fd6e37 |
541 | if (event_time_next && CYCLES_GE(now, event_time_next)) |
ae214f1c |
542 | p32x_run_events(now); |
ed4402a7 |
543 | |
a8fd6e37 |
544 | target = m68k_target; |
545 | if (event_time_next && CYCLES_GT(target, event_time_next)) |
546 | target = event_time_next; |
a8fd6e37 |
547 | while (CYCLES_GT(target, now)) |
548 | { |
86c16afd |
549 | next = target; |
550 | if (CYCLES_GT(target, now + STEP_N)) |
551 | next = now + STEP_N; |
552 | elprintf(EL_32X, "sh2 exec to %u %d,%d/%d, flags %x", next, |
553 | next - msh2.m68krcycles_done, next - ssh2.m68krcycles_done, |
a8fd6e37 |
554 | m68k_target - now, Pico32x.emu_flags); |
ed4402a7 |
555 | |
2fa02d5a |
556 | pprof_start(ssh2); |
19886062 |
557 | if (!(ssh2.state & SH2_IDLE_STATES)) { |
86c16afd |
558 | cycles = next - ssh2.m68krcycles_done; |
a8fd6e37 |
559 | if (cycles > 0) { |
f53e166c |
560 | run_sh2(&ssh2, cycles > 20U ? cycles : 20U); |
a8fd6e37 |
561 | |
562 | if (event_time_next && CYCLES_GT(target, event_time_next)) |
563 | target = event_time_next; |
86c16afd |
564 | if (CYCLES_GT(next, target)) |
565 | next = target; |
a8fd6e37 |
566 | } |
ed4402a7 |
567 | } |
2fa02d5a |
568 | pprof_end(ssh2); |
ed4402a7 |
569 | |
2fa02d5a |
570 | pprof_start(msh2); |
19886062 |
571 | if (!(msh2.state & SH2_IDLE_STATES)) { |
86c16afd |
572 | cycles = next - msh2.m68krcycles_done; |
a8fd6e37 |
573 | if (cycles > 0) { |
f53e166c |
574 | run_sh2(&msh2, cycles > 20U ? cycles : 20U); |
a8fd6e37 |
575 | |
576 | if (event_time_next && CYCLES_GT(target, event_time_next)) |
577 | target = event_time_next; |
86c16afd |
578 | if (CYCLES_GT(next, target)) |
579 | next = target; |
a8fd6e37 |
580 | } |
ed4402a7 |
581 | } |
2fa02d5a |
582 | pprof_end(msh2); |
a8fd6e37 |
583 | |
86c16afd |
584 | now = next; |
2eb21331 |
585 | if (CYCLES_GT(now, msh2.m68krcycles_done)) { |
586 | if (!(msh2.state & SH2_IDLE_STATES)) |
19886062 |
587 | now = msh2.m68krcycles_done; |
588 | } |
2eb21331 |
589 | if (CYCLES_GT(now, ssh2.m68krcycles_done)) { |
590 | if (!(ssh2.state & SH2_IDLE_STATES)) |
19886062 |
591 | now = ssh2.m68krcycles_done; |
592 | } |
e7ee5010 |
593 | if (CYCLES_GT(now, timer_cycles+STEP_N)) { |
74cc7aeb |
594 | if (msh2.state & SH2_TIMER_RUN) |
595 | p32x_timer_do(&msh2, now - timer_cycles); |
596 | if (ssh2.state & SH2_TIMER_RUN) |
597 | p32x_timer_do(&ssh2, now - timer_cycles); |
86c16afd |
598 | timer_cycles = now; |
599 | } |
ed4402a7 |
600 | } |
a8fd6e37 |
601 | |
74cc7aeb |
602 | if (msh2.state & SH2_TIMER_RUN) |
603 | p32x_timer_do(&msh2, now - timer_cycles); |
604 | if (ssh2.state & SH2_TIMER_RUN) |
605 | p32x_timer_do(&ssh2, now - timer_cycles); |
a8fd6e37 |
606 | timer_cycles = now; |
ed4402a7 |
607 | } |
2fa02d5a |
608 | pprof_end_sub(m68k); |
19886062 |
609 | |
610 | // advance idle CPUs |
611 | if (msh2.state & SH2_IDLE_STATES) { |
612 | if (CYCLES_GT(m68k_target, msh2.m68krcycles_done)) |
613 | msh2.m68krcycles_done = m68k_target; |
614 | } |
615 | if (ssh2.state & SH2_IDLE_STATES) { |
616 | if (CYCLES_GT(m68k_target, ssh2.m68krcycles_done)) |
617 | ssh2.m68krcycles_done = m68k_target; |
618 | } |
31fbc691 |
619 | |
620 | // everyone is in sync now |
621 | Pico32x.comm_dirty = 0; |
236990cf |
622 | } |
acd35d4c |
623 | |
ed4402a7 |
624 | void sync_sh2s_lockstep(unsigned int m68k_target) |
625 | { |
626 | unsigned int mcycles; |
627 | |
628 | mcycles = msh2.m68krcycles_done; |
4af2edc3 |
629 | if (CYCLES_GT(mcycles, ssh2.m68krcycles_done)) |
ed4402a7 |
630 | mcycles = ssh2.m68krcycles_done; |
631 | |
4af2edc3 |
632 | while (CYCLES_GT(m68k_target, mcycles)) { |
6c2041fe |
633 | mcycles += STEP_LS; |
ed4402a7 |
634 | sync_sh2s_normal(mcycles); |
635 | } |
87accdf7 |
636 | } |
637 | |
ae214f1c |
638 | #define CPUS_RUN(m68k_cycles) do { \ |
93f9619e |
639 | if (PicoIn.AHW & PAHW_MCD) \ |
fa8fb754 |
640 | pcd_run_cpus(m68k_cycles); \ |
641 | else \ |
642 | SekRunM68k(m68k_cycles); \ |
643 | \ |
ae214f1c |
644 | if ((Pico32x.emu_flags & P32XF_Z80_32X_IO) && Pico.m.z80Run \ |
93f9619e |
645 | && !Pico.m.z80_reset && (PicoIn.opt & POPT_EN_Z80)) \ |
ae214f1c |
646 | PicoSyncZ80(SekCyclesDone()); \ |
19886062 |
647 | if (Pico32x.emu_flags & (P32XF_68KCPOLL|P32XF_68KVPOLL)) \ |
ae214f1c |
648 | p32x_sync_sh2s(SekCyclesDone()); \ |
ed4402a7 |
649 | } while (0) |
87accdf7 |
650 | |
ed4402a7 |
651 | #define PICO_32X |
fa8fb754 |
652 | #define PICO_CD |
974fdb5b |
653 | #include "../pico_cmn.c" |
654 | |
655 | void PicoFrame32x(void) |
656 | { |
93f9619e |
657 | if (PicoIn.AHW & PAHW_MCD) |
a6523294 |
658 | pcd_prepare_frame(); |
659 | |
974fdb5b |
660 | PicoFrameStart(); |
2322260c |
661 | Pico32x.sync_line = 0; |
f9ed9446 |
662 | if (Pico32xDrawMode != PDM32X_BOTH) |
663 | Pico.est.rendstatus |= PDRAW_SYNC_NEEDED; |
974fdb5b |
664 | PicoFrameHints(); |
51d86e55 |
665 | |
19886062 |
666 | elprintf(EL_32X, "poll: %02x %02x %02x", |
667 | Pico32x.emu_flags & 3, msh2.state, ssh2.state); |
974fdb5b |
668 | } |
db1d3564 |
669 | |
ed4402a7 |
670 | // calculate multipliers against 68k clock (7670442) |
671 | // normally * 3, but effectively slower due to high latencies everywhere |
672 | // however using something lower breaks MK2 animations |
673 | void Pico32xSetClocks(int msh2_hz, int ssh2_hz) |
674 | { |
675 | float m68k_clk = (float)(OSC_NTSC / 7); |
676 | if (msh2_hz > 0) { |
677 | msh2.mult_m68k_to_sh2 = (int)((float)msh2_hz * (1 << CYCLE_MULT_SHIFT) / m68k_clk); |
678 | msh2.mult_sh2_to_m68k = (int)(m68k_clk * (1 << CYCLE_MULT_SHIFT) / (float)msh2_hz); |
679 | } |
680 | if (ssh2_hz > 0) { |
681 | ssh2.mult_m68k_to_sh2 = (int)((float)ssh2_hz * (1 << CYCLE_MULT_SHIFT) / m68k_clk); |
682 | ssh2.mult_sh2_to_m68k = (int)(m68k_clk * (1 << CYCLE_MULT_SHIFT) / (float)ssh2_hz); |
683 | } |
684 | } |
685 | |
27e26273 |
686 | void Pico32xStateLoaded(int is_early) |
687 | { |
688 | if (is_early) { |
689 | Pico32xMemStateLoaded(); |
690 | return; |
691 | } |
692 | |
7263343d |
693 | if (CYCLES_GE(sh2s[0].m68krcycles_done - Pico.t.m68c_aim, 500) || |
694 | CYCLES_GE(sh2s[1].m68krcycles_done - Pico.t.m68c_aim, 500)) |
96baa875 |
695 | sh2s[0].m68krcycles_done = sh2s[1].m68krcycles_done = SekCyclesDone(); |
ae214f1c |
696 | p32x_update_irls(NULL, SekCyclesDone()); |
b1a65866 |
697 | p32x_timers_recalc(); |
df63f1a6 |
698 | p32x_pwm_state_loaded(); |
ae214f1c |
699 | p32x_run_events(SekCyclesDone()); |
fa23e7cc |
700 | |
701 | // TODO wakeup CPUs for now. poll detection stuff must go to the save state! |
702 | p32x_m68k_poll_event(0, -1); |
703 | p32x_sh2_poll_event(msh2.poll_addr, &msh2, SH2_IDLE_STATES, msh2.m68krcycles_done); |
704 | p32x_sh2_poll_event(ssh2.poll_addr, &ssh2, SH2_IDLE_STATES, ssh2.m68krcycles_done); |
27e26273 |
705 | } |
706 | |
3d7abd69 |
707 | void Pico32xPrepare(void) |
708 | { |
071a9e59 |
709 | // fallback in case it was missing in saved config |
710 | if (msh2.mult_m68k_to_sh2 == 0 || msh2.mult_sh2_to_m68k == 0) |
711 | Pico32xSetClocks(PICO_MSH2_HZ, 0); |
712 | if (ssh2.mult_m68k_to_sh2 == 0 || ssh2.mult_sh2_to_m68k == 0) |
713 | Pico32xSetClocks(0, PICO_SSH2_HZ); |
7263343d |
714 | |
3d7abd69 |
715 | sh2_execute_prepare(&msh2, PicoIn.opt & POPT_EN_DRC); |
716 | sh2_execute_prepare(&ssh2, PicoIn.opt & POPT_EN_DRC); |
717 | } |
718 | |
ed4402a7 |
719 | // vim:shiftwidth=2:ts=2:expandtab |