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