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