cc68a136 |
1 | // This is part of Pico Library |
2 | |
3 | // (c) Copyright 2004 Dave, All rights reserved. |
672ad671 |
4 | // (c) Copyright 2007 notaz, All rights reserved. |
cc68a136 |
5 | // Free for non-commercial use. |
6 | |
7 | // For commercial use, separate licencing terms must be obtained. |
8 | |
9 | |
10 | #include "../PicoInt.h" |
11 | #include "../sound/sound.h" |
12 | |
13 | |
76276b0b |
14 | extern unsigned char formatted_bram[4*0x10]; |
cc68a136 |
15 | |
16 | |
17 | int PicoInitMCD(void) |
18 | { |
19 | SekInitS68k(); |
20 | Init_CD_Driver(); |
21 | |
22 | return 0; |
23 | } |
24 | |
25 | |
26 | void PicoExitMCD(void) |
27 | { |
28 | End_CD_Driver(); |
29 | } |
30 | |
31 | int PicoResetMCD(int hard) |
32 | { |
51a902ae |
33 | if (hard) { |
76276b0b |
34 | int fmt_size = sizeof(formatted_bram); |
5c69a605 |
35 | memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram)); |
36 | memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M)); |
37 | memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram)); |
51a902ae |
38 | memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram)); |
76276b0b |
39 | memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size); |
51a902ae |
40 | } |
41 | memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs)); |
4f265db7 |
42 | memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm)); |
5c69a605 |
43 | memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m)); |
51a902ae |
44 | |
d1df8786 |
45 | *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6) |
51a902ae |
46 | Pico_mcd->m.state_flags |= 2; // s68k reset pending |
672ad671 |
47 | Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset |
cc68a136 |
48 | |
cc68a136 |
49 | Reset_CD(); |
5c69a605 |
50 | LC89510_Reset(); |
51a902ae |
51 | gfx_cd_reset(); |
4ff2d527 |
52 | #ifdef _ASM_CD_MEMORY_C |
53 | PicoMemResetCD(1); |
54 | #endif |
cc68a136 |
55 | |
56 | return 0; |
57 | } |
58 | |
59 | static __inline void SekRun(int cyc) |
60 | { |
61 | int cyc_do; |
62 | SekCycleAim+=cyc; |
63 | if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return; |
b837b69b |
64 | #if defined(EMU_C68K) |
65 | PicoCpu.cycles=cyc_do; |
66 | CycloneRun(&PicoCpu); |
67 | SekCycleCnt+=cyc_do-PicoCpu.cycles; |
68 | #elif defined(EMU_M68K) |
cc68a136 |
69 | m68k_set_context(&PicoM68kCPU); |
70 | SekCycleCnt+=m68k_execute(cyc_do); |
71 | #endif |
72 | } |
73 | |
74 | static __inline void SekRunS68k(int cyc) |
75 | { |
76 | int cyc_do; |
77 | SekCycleAimS68k+=cyc; |
78 | if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return; |
b837b69b |
79 | #if defined(EMU_C68K) |
80 | PicoCpuS68k.cycles=cyc_do; |
81 | CycloneRun(&PicoCpuS68k); |
82 | SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles; |
83 | #elif defined(EMU_M68K) |
cc68a136 |
84 | m68k_set_context(&PicoS68kCPU); |
85 | SekCycleCntS68k+=m68k_execute(cyc_do); |
86 | #endif |
87 | } |
88 | |
7336a99a |
89 | #define PS_STEP_M68K ((488<<16)/20) // ~24 |
90 | //#define PS_STEP_S68K 13 |
68cba51e |
91 | |
7336a99a |
92 | #ifndef _ASM_CD_PICO_C |
68cba51e |
93 | static __inline void SekRunPS(int cyc_m68k, int cyc_s68k) |
94 | { |
7336a99a |
95 | int cycn, cycn_s68k, cyc_do; |
96 | int d_cm = 0, d_cs = 0, ex; |
68cba51e |
97 | SekCycleAim+=cyc_m68k; |
98 | SekCycleAimS68k+=cyc_s68k; |
7336a99a |
99 | |
100 | // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k, |
101 | // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline); |
102 | |
103 | /* loop 488 downto 0 in steps of PS_STEP */ |
104 | for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K) |
105 | { |
106 | ex = 0; |
107 | cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16; |
108 | //fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k); |
109 | if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) { |
68cba51e |
110 | #if defined(EMU_C68K) |
7336a99a |
111 | PicoCpu.cycles = cyc_do; |
68cba51e |
112 | CycloneRun(&PicoCpu); |
7336a99a |
113 | SekCycleCnt += cyc_do - PicoCpu.cycles; |
68cba51e |
114 | #elif defined(EMU_M68K) |
115 | m68k_set_context(&PicoM68kCPU); |
7336a99a |
116 | SekCycleCnt += (ex = m68k_execute(cyc_do)); |
68cba51e |
117 | #endif |
7336a99a |
118 | } |
119 | //fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0; |
120 | if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) { |
68cba51e |
121 | #if defined(EMU_C68K) |
7336a99a |
122 | PicoCpuS68k.cycles = cyc_do; |
68cba51e |
123 | CycloneRun(&PicoCpuS68k); |
7336a99a |
124 | SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles; |
68cba51e |
125 | #elif defined(EMU_M68K) |
126 | m68k_set_context(&PicoS68kCPU); |
7336a99a |
127 | SekCycleCntS68k += (ex = m68k_execute(cyc_do)); |
68cba51e |
128 | #endif |
7336a99a |
129 | } |
130 | //fprintf(stderr, "%3i\n", ex); d_cs += ex; |
68cba51e |
131 | } |
7336a99a |
132 | |
133 | //fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs); |
68cba51e |
134 | } |
7336a99a |
135 | #else |
136 | void SekRunPS(int cyc_m68k, int cyc_s68k); |
137 | #endif |
68cba51e |
138 | |
139 | |
bf098bc5 |
140 | static __inline void check_cd_dma(void) |
141 | { |
142 | int ddx; |
143 | |
c459aefd |
144 | if (!(Pico_mcd->scd.Status_CDC & 0x08)) return; |
bf098bc5 |
145 | |
146 | ddx = Pico_mcd->s68k_regs[4] & 7; |
147 | if (ddx < 2) return; // invalid |
c459aefd |
148 | if (ddx < 4) { |
149 | Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port |
150 | return; |
151 | } |
bf098bc5 |
152 | if (ddx == 6) return; // invalid |
153 | |
154 | Update_CDC_TRansfer(ddx); // now go and do the actual transfer |
155 | } |
156 | |
4f265db7 |
157 | static __inline void update_chips(void) |
158 | { |
159 | int counter_timer, int3_set; |
160 | int counter75hz_lim = Pico.m.pal ? 2080 : 2096; |
161 | |
162 | // 75Hz CDC update |
163 | if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) { |
164 | Pico_mcd->m.counter75hz -= counter75hz_lim; |
165 | Check_CD_Command(); |
166 | } |
167 | |
168 | // update timers |
169 | counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708; |
170 | Pico_mcd->m.timer_stopwatch += counter_timer; |
171 | if ((int3_set = Pico_mcd->s68k_regs[0x31])) { |
172 | Pico_mcd->m.timer_int3 -= counter_timer; |
173 | if (Pico_mcd->m.timer_int3 < 0) { |
174 | if (Pico_mcd->s68k_regs[0x33] & (1<<3)) { |
175 | dprintf("s68k: timer irq 3"); |
176 | SekInterruptS68k(3); |
177 | Pico_mcd->m.timer_int3 += int3_set << 16; |
178 | } |
179 | // is this really what happens if irq3 is masked out? |
180 | Pico_mcd->m.timer_int3 &= 0xffffff; |
181 | } |
182 | } |
183 | |
184 | // update gfx chip |
185 | if (Pico_mcd->rot_comp.Reg_58 & 0x8000) |
186 | gfx_cd_update(); |
187 | } |
188 | |
b837b69b |
189 | |
cc68a136 |
190 | static int PicoFrameHintsMCD(void) |
191 | { |
192 | struct PicoVideo *pv=&Pico.video; |
4f265db7 |
193 | int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample; |
cc68a136 |
194 | const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values |
195 | int skip=PicoSkipFrame || (PicoOpt&0x10); |
196 | int hint; // Hint counter |
197 | |
198 | if(Pico.m.pal) { // |
199 | //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488) |
200 | //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228 |
201 | lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well |
202 | line_sample = 68; |
203 | if(pv->reg[1]&8) lines_vis = 240; |
204 | } else { |
205 | //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488 |
206 | //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228 |
207 | lines = 262; |
208 | line_sample = 93; |
209 | } |
210 | |
211 | SekCyclesReset(); |
212 | SekCyclesResetS68k(); |
213 | //z80ExtraCycles = 0; |
214 | |
215 | if(PicoOpt&4) |
216 | z80CycleAim = 0; |
217 | // z80_resetCycles(); |
218 | |
219 | pv->status&=~0x88; // clear V-Int, come out of vblank |
220 | |
221 | hint=pv->reg[10]; // Load H-Int counter |
222 | //dprintf("-hint: %i", hint); |
223 | |
224 | for (y=0;y<lines;y++) |
225 | { |
226 | Pico.m.scanline=(short)y; |
227 | |
228 | // pad delay (for 6 button pads) |
229 | if(PicoOpt&0x20) { |
230 | if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; |
231 | if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; |
232 | } |
233 | |
bf098bc5 |
234 | check_cd_dma(); |
235 | |
cc68a136 |
236 | // H-Interrupts: |
237 | if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe |
238 | { |
239 | //dprintf("rhint:old @ %06x", SekPc); |
240 | hint=pv->reg[10]; // Reload H-Int counter |
241 | pv->pending_ints|=0x10; |
242 | if (pv->reg[0]&0x10) SekInterrupt(4); |
243 | //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt); |
244 | //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86)); |
245 | } |
246 | |
247 | // V-Interrupt: |
248 | if (y == lines_vis) |
249 | { |
250 | //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt); |
251 | pv->status|=0x88; // V-Int happened, go into vblank |
252 | SekRun(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula) |
253 | /*if(Pico.m.z80Run && (PicoOpt&4)) { |
254 | z80CycleAim+=cycles_z80/2; |
255 | total_z80+=z80_run(z80CycleAim-total_z80); |
256 | z80CycleAim-=cycles_z80/2; |
257 | }*/ |
258 | pv->pending_ints|=0x20; |
259 | if(pv->reg[1]&0x20) SekInterrupt(6); |
260 | if(Pico.m.z80Run && (PicoOpt&4)) // ? |
261 | z80_int(); |
262 | //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0)); |
263 | } |
264 | |
265 | // decide if we draw this line |
266 | #if CAN_HANDLE_240_LINES |
267 | if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) ) |
268 | #else |
269 | if(!skip && y<224) |
270 | #endif |
271 | PicoLine(y); |
272 | |
273 | if(PicoOpt&1) |
274 | sound_timers_and_dac(y); |
275 | |
276 | // get samples from sound chips |
7a93adeb |
277 | if (y == 224 && PsndOut) { |
278 | int len = sound_render(0, PsndLen); |
279 | if (PicoWriteSound) PicoWriteSound(len); |
280 | // clear sound buffer |
281 | sound_clear(); |
282 | } |
cc68a136 |
283 | |
284 | // Run scanline: |
285 | //dprintf("m68k starting exec @ %06x", SekPc); |
672ad671 |
286 | if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA(); |
68cba51e |
287 | if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) { |
7336a99a |
288 | SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync" |
68cba51e |
289 | } else { |
290 | SekRun(cycles_68k); |
291 | if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset |
292 | SekRunS68k(cycles_s68k); |
cc68a136 |
293 | } |
294 | |
295 | if((PicoOpt&4) && Pico.m.z80Run) { |
296 | Pico.m.z80Run|=2; |
297 | z80CycleAim+=cycles_z80; |
298 | total_z80+=z80_run(z80CycleAim-total_z80); |
299 | } |
300 | |
4f265db7 |
301 | update_chips(); |
cc68a136 |
302 | } |
303 | |
304 | // draw a frame just after vblank in alternative render mode |
305 | if(!PicoSkipFrame && (PicoOpt&0x10)) |
306 | PicoFrameFull(); |
307 | |
308 | return 0; |
309 | } |
310 | |
311 | |
312 | int PicoFrameMCD(void) |
313 | { |
314 | if(!(PicoOpt&0x10)) |
315 | PicoFrameStart(); |
316 | |
317 | PicoFrameHintsMCD(); |
318 | |
319 | return 0; |
320 | } |
321 | |
322 | |