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