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