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