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