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