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