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