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 | { |
fa1e5e29 |
33 | memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram)); |
34 | memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M)); |
35 | memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram)); |
51a902ae |
36 | if (hard) { |
76276b0b |
37 | int fmt_size = sizeof(formatted_bram); |
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)); |
51a902ae |
43 | |
d1df8786 |
44 | *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6) |
51a902ae |
45 | Pico_mcd->m.state_flags |= 2; // s68k reset pending |
672ad671 |
46 | Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset |
51a902ae |
47 | Pico_mcd->m.counter75hz = 0; |
cc68a136 |
48 | |
49 | LC89510_Reset(); |
50 | Reset_CD(); |
51a902ae |
51 | gfx_cd_reset(); |
cc68a136 |
52 | |
53 | return 0; |
54 | } |
55 | |
56 | static __inline void SekRun(int cyc) |
57 | { |
58 | int cyc_do; |
59 | SekCycleAim+=cyc; |
60 | if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return; |
b837b69b |
61 | #if defined(EMU_C68K) |
62 | PicoCpu.cycles=cyc_do; |
63 | CycloneRun(&PicoCpu); |
64 | SekCycleCnt+=cyc_do-PicoCpu.cycles; |
65 | #elif defined(EMU_M68K) |
cc68a136 |
66 | m68k_set_context(&PicoM68kCPU); |
67 | SekCycleCnt+=m68k_execute(cyc_do); |
68 | #endif |
69 | } |
70 | |
71 | static __inline void SekRunS68k(int cyc) |
72 | { |
73 | int cyc_do; |
74 | SekCycleAimS68k+=cyc; |
75 | if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return; |
b837b69b |
76 | #if defined(EMU_C68K) |
77 | PicoCpuS68k.cycles=cyc_do; |
78 | CycloneRun(&PicoCpuS68k); |
79 | SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles; |
80 | #elif defined(EMU_M68K) |
cc68a136 |
81 | m68k_set_context(&PicoS68kCPU); |
82 | SekCycleCntS68k+=m68k_execute(cyc_do); |
83 | #endif |
84 | } |
85 | |
bf098bc5 |
86 | static __inline void check_cd_dma(void) |
87 | { |
88 | int ddx; |
89 | |
c459aefd |
90 | if (!(Pico_mcd->scd.Status_CDC & 0x08)) return; |
bf098bc5 |
91 | |
92 | ddx = Pico_mcd->s68k_regs[4] & 7; |
93 | if (ddx < 2) return; // invalid |
c459aefd |
94 | if (ddx < 4) { |
95 | Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port |
96 | return; |
97 | } |
bf098bc5 |
98 | if (ddx == 6) return; // invalid |
99 | |
100 | Update_CDC_TRansfer(ddx); // now go and do the actual transfer |
101 | } |
102 | |
4f265db7 |
103 | static __inline void update_chips(void) |
104 | { |
105 | int counter_timer, int3_set; |
106 | int counter75hz_lim = Pico.m.pal ? 2080 : 2096; |
107 | |
108 | // 75Hz CDC update |
109 | if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) { |
110 | Pico_mcd->m.counter75hz -= counter75hz_lim; |
111 | Check_CD_Command(); |
112 | } |
113 | |
114 | // update timers |
115 | counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708; |
116 | Pico_mcd->m.timer_stopwatch += counter_timer; |
117 | if ((int3_set = Pico_mcd->s68k_regs[0x31])) { |
118 | Pico_mcd->m.timer_int3 -= counter_timer; |
119 | if (Pico_mcd->m.timer_int3 < 0) { |
120 | if (Pico_mcd->s68k_regs[0x33] & (1<<3)) { |
121 | dprintf("s68k: timer irq 3"); |
122 | SekInterruptS68k(3); |
123 | Pico_mcd->m.timer_int3 += int3_set << 16; |
124 | } |
125 | // is this really what happens if irq3 is masked out? |
126 | Pico_mcd->m.timer_int3 &= 0xffffff; |
127 | } |
128 | } |
129 | |
130 | // update gfx chip |
131 | if (Pico_mcd->rot_comp.Reg_58 & 0x8000) |
132 | gfx_cd_update(); |
133 | } |
134 | |
b837b69b |
135 | |
cc68a136 |
136 | static int PicoFrameHintsMCD(void) |
137 | { |
138 | struct PicoVideo *pv=&Pico.video; |
4f265db7 |
139 | int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample; |
cc68a136 |
140 | const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values |
141 | int skip=PicoSkipFrame || (PicoOpt&0x10); |
142 | int hint; // Hint counter |
143 | |
144 | if(Pico.m.pal) { // |
145 | //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488) |
146 | //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228 |
147 | lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well |
148 | line_sample = 68; |
149 | if(pv->reg[1]&8) lines_vis = 240; |
150 | } else { |
151 | //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488 |
152 | //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228 |
153 | lines = 262; |
154 | line_sample = 93; |
155 | } |
156 | |
157 | SekCyclesReset(); |
158 | SekCyclesResetS68k(); |
159 | //z80ExtraCycles = 0; |
160 | |
161 | if(PicoOpt&4) |
162 | z80CycleAim = 0; |
163 | // z80_resetCycles(); |
164 | |
165 | pv->status&=~0x88; // clear V-Int, come out of vblank |
166 | |
167 | hint=pv->reg[10]; // Load H-Int counter |
168 | //dprintf("-hint: %i", hint); |
169 | |
170 | for (y=0;y<lines;y++) |
171 | { |
172 | Pico.m.scanline=(short)y; |
173 | |
174 | // pad delay (for 6 button pads) |
175 | if(PicoOpt&0x20) { |
176 | if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; |
177 | if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; |
178 | } |
179 | |
bf098bc5 |
180 | check_cd_dma(); |
181 | |
cc68a136 |
182 | // H-Interrupts: |
183 | if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe |
184 | { |
185 | //dprintf("rhint:old @ %06x", SekPc); |
186 | hint=pv->reg[10]; // Reload H-Int counter |
187 | pv->pending_ints|=0x10; |
188 | if (pv->reg[0]&0x10) SekInterrupt(4); |
189 | //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt); |
190 | //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86)); |
191 | } |
192 | |
193 | // V-Interrupt: |
194 | if (y == lines_vis) |
195 | { |
196 | //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt); |
197 | pv->status|=0x88; // V-Int happened, go into vblank |
198 | 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) |
199 | /*if(Pico.m.z80Run && (PicoOpt&4)) { |
200 | z80CycleAim+=cycles_z80/2; |
201 | total_z80+=z80_run(z80CycleAim-total_z80); |
202 | z80CycleAim-=cycles_z80/2; |
203 | }*/ |
204 | pv->pending_ints|=0x20; |
205 | if(pv->reg[1]&0x20) SekInterrupt(6); |
206 | if(Pico.m.z80Run && (PicoOpt&4)) // ? |
207 | z80_int(); |
208 | //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0)); |
209 | } |
210 | |
211 | // decide if we draw this line |
212 | #if CAN_HANDLE_240_LINES |
213 | if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) ) |
214 | #else |
215 | if(!skip && y<224) |
216 | #endif |
217 | PicoLine(y); |
218 | |
219 | if(PicoOpt&1) |
220 | sound_timers_and_dac(y); |
221 | |
222 | // get samples from sound chips |
7a93adeb |
223 | if (y == 224 && PsndOut) { |
224 | int len = sound_render(0, PsndLen); |
225 | if (PicoWriteSound) PicoWriteSound(len); |
226 | // clear sound buffer |
227 | sound_clear(); |
228 | } |
cc68a136 |
229 | |
230 | // Run scanline: |
231 | //dprintf("m68k starting exec @ %06x", SekPc); |
672ad671 |
232 | if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA(); |
cc68a136 |
233 | SekRun(cycles_68k); |
c459aefd |
234 | if ((Pico_mcd->m.busreq&3) == 1) { // no busreq/no reset |
cc68a136 |
235 | #if 0 |
236 | int i; |
237 | FILE *f = fopen("prg_ram.bin", "wb"); |
238 | for (i = 0; i < 0x80000; i+=2) |
239 | { |
240 | int tmp = Pico_mcd->prg_ram[i]; |
241 | Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1]; |
242 | Pico_mcd->prg_ram[i+1] = tmp; |
243 | } |
244 | fwrite(Pico_mcd->prg_ram, 1, 0x80000, f); |
245 | fclose(f); |
246 | exit(1); |
247 | #endif |
248 | //dprintf("s68k starting exec @ %06x", SekPcS68k); |
249 | SekRunS68k(cycles_s68k); |
250 | } |
251 | |
252 | if((PicoOpt&4) && Pico.m.z80Run) { |
253 | Pico.m.z80Run|=2; |
254 | z80CycleAim+=cycles_z80; |
255 | total_z80+=z80_run(z80CycleAim-total_z80); |
256 | } |
257 | |
4f265db7 |
258 | update_chips(); |
cc68a136 |
259 | } |
260 | |
261 | // draw a frame just after vblank in alternative render mode |
262 | if(!PicoSkipFrame && (PicoOpt&0x10)) |
263 | PicoFrameFull(); |
264 | |
265 | return 0; |
266 | } |
267 | |
268 | |
269 | int PicoFrameMCD(void) |
270 | { |
271 | if(!(PicoOpt&0x10)) |
272 | PicoFrameStart(); |
273 | |
274 | PicoFrameHintsMCD(); |
275 | |
276 | return 0; |
277 | } |
278 | |
279 | |