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