gfx_cd optimization wip, line glitch fixed
[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
76276b0b 14extern unsigned char formatted_bram[4*0x10];
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{
51a902ae 33 if (hard) {
a4030801 34 int fmt_size = sizeof(formatted_bram);
35 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
36 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
37 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
38 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
39 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
51a902ae 40 }
41 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
4f265db7 42 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
5c69a605 43 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
51a902ae 44
d1df8786 45 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
51a902ae 46 Pico_mcd->m.state_flags |= 2; // s68k reset pending
672ad671 47 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
cc68a136 48
cc68a136 49 Reset_CD();
5c69a605 50 LC89510_Reset();
51a902ae 51 gfx_cd_reset();
4ff2d527 52#ifdef _ASM_CD_MEMORY_C
53 PicoMemResetCD(1);
48e8482f 54 PicoMemResetCDdecode(1);
4ff2d527 55#endif
cc68a136 56
57 return 0;
58}
59
60static __inline void SekRun(int cyc)
61{
62 int cyc_do;
63 SekCycleAim+=cyc;
64 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
b837b69b 65#if defined(EMU_C68K)
66 PicoCpu.cycles=cyc_do;
67 CycloneRun(&PicoCpu);
68 SekCycleCnt+=cyc_do-PicoCpu.cycles;
69#elif defined(EMU_M68K)
cc68a136 70 m68k_set_context(&PicoM68kCPU);
71 SekCycleCnt+=m68k_execute(cyc_do);
72#endif
73}
74
75static __inline void SekRunS68k(int cyc)
76{
77 int cyc_do;
78 SekCycleAimS68k+=cyc;
79 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
b837b69b 80#if defined(EMU_C68K)
81 PicoCpuS68k.cycles=cyc_do;
82 CycloneRun(&PicoCpuS68k);
83 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
84#elif defined(EMU_M68K)
cc68a136 85 m68k_set_context(&PicoS68kCPU);
86 SekCycleCntS68k+=m68k_execute(cyc_do);
87#endif
88}
89
7336a99a 90#define PS_STEP_M68K ((488<<16)/20) // ~24
91//#define PS_STEP_S68K 13
68cba51e 92
a4030801 93#ifdef _ASM_CD_PICO_C
94void SekRunPS(int cyc_m68k, int cyc_s68k);
95#else
68cba51e 96static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
97{
7336a99a 98 int cycn, cycn_s68k, cyc_do;
99 int d_cm = 0, d_cs = 0, ex;
68cba51e 100 SekCycleAim+=cyc_m68k;
101 SekCycleAimS68k+=cyc_s68k;
7336a99a 102
103// fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
104// SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
105
106 /* loop 488 downto 0 in steps of PS_STEP */
107 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
108 {
109 ex = 0;
110 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
111//fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k);
112 if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
68cba51e 113#if defined(EMU_C68K)
7336a99a 114 PicoCpu.cycles = cyc_do;
68cba51e 115 CycloneRun(&PicoCpu);
7336a99a 116 SekCycleCnt += cyc_do - PicoCpu.cycles;
68cba51e 117#elif defined(EMU_M68K)
118 m68k_set_context(&PicoM68kCPU);
7336a99a 119 SekCycleCnt += (ex = m68k_execute(cyc_do));
68cba51e 120#endif
7336a99a 121 }
122//fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0;
123 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
68cba51e 124#if defined(EMU_C68K)
7336a99a 125 PicoCpuS68k.cycles = cyc_do;
68cba51e 126 CycloneRun(&PicoCpuS68k);
7336a99a 127 SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles;
68cba51e 128#elif defined(EMU_M68K)
129 m68k_set_context(&PicoS68kCPU);
7336a99a 130 SekCycleCntS68k += (ex = m68k_execute(cyc_do));
68cba51e 131#endif
7336a99a 132 }
133//fprintf(stderr, "%3i\n", ex); d_cs += ex;
68cba51e 134 }
7336a99a 135
136//fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs);
68cba51e 137}
7336a99a 138#endif
68cba51e 139
140
bf098bc5 141static __inline void check_cd_dma(void)
142{
143 int ddx;
144
c459aefd 145 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
bf098bc5 146
147 ddx = Pico_mcd->s68k_regs[4] & 7;
148 if (ddx < 2) return; // invalid
c459aefd 149 if (ddx < 4) {
150 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
151 return;
152 }
bf098bc5 153 if (ddx == 6) return; // invalid
154
155 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
156}
157
4f265db7 158static __inline void update_chips(void)
159{
160 int counter_timer, int3_set;
161 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
162
163 // 75Hz CDC update
164 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
165 Pico_mcd->m.counter75hz -= counter75hz_lim;
166 Check_CD_Command();
167 }
168
169 // update timers
170 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
171 Pico_mcd->m.timer_stopwatch += counter_timer;
172 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
173 Pico_mcd->m.timer_int3 -= counter_timer;
174 if (Pico_mcd->m.timer_int3 < 0) {
175 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
176 dprintf("s68k: timer irq 3");
177 SekInterruptS68k(3);
178 Pico_mcd->m.timer_int3 += int3_set << 16;
179 }
180 // is this really what happens if irq3 is masked out?
181 Pico_mcd->m.timer_int3 &= 0xffffff;
182 }
183 }
184
185 // update gfx chip
186 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
187 gfx_cd_update();
188}
189
b837b69b 190
cc68a136 191static int PicoFrameHintsMCD(void)
192{
193 struct PicoVideo *pv=&Pico.video;
4f265db7 194 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
cc68a136 195 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
196 int skip=PicoSkipFrame || (PicoOpt&0x10);
197 int hint; // Hint counter
198
199 if(Pico.m.pal) { //
200 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
201 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
202 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
203 line_sample = 68;
204 if(pv->reg[1]&8) lines_vis = 240;
205 } else {
206 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
207 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
208 lines = 262;
209 line_sample = 93;
210 }
211
212 SekCyclesReset();
213 SekCyclesResetS68k();
214 //z80ExtraCycles = 0;
215
216 if(PicoOpt&4)
217 z80CycleAim = 0;
218// z80_resetCycles();
219
220 pv->status&=~0x88; // clear V-Int, come out of vblank
221
222 hint=pv->reg[10]; // Load H-Int counter
223 //dprintf("-hint: %i", hint);
224
225 for (y=0;y<lines;y++)
226 {
227 Pico.m.scanline=(short)y;
228
229 // pad delay (for 6 button pads)
230 if(PicoOpt&0x20) {
231 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
232 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
233 }
234
bf098bc5 235 check_cd_dma();
236
cc68a136 237 // H-Interrupts:
238 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
239 {
240 //dprintf("rhint:old @ %06x", SekPc);
241 hint=pv->reg[10]; // Reload H-Int counter
242 pv->pending_ints|=0x10;
243 if (pv->reg[0]&0x10) SekInterrupt(4);
244 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
245 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
246 }
247
248 // V-Interrupt:
249 if (y == lines_vis)
250 {
251 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
252 pv->status|=0x88; // V-Int happened, go into vblank
253 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)
254 /*if(Pico.m.z80Run && (PicoOpt&4)) {
255 z80CycleAim+=cycles_z80/2;
256 total_z80+=z80_run(z80CycleAim-total_z80);
257 z80CycleAim-=cycles_z80/2;
258 }*/
259 pv->pending_ints|=0x20;
260 if(pv->reg[1]&0x20) SekInterrupt(6);
261 if(Pico.m.z80Run && (PicoOpt&4)) // ?
262 z80_int();
263 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
264 }
265
266 // decide if we draw this line
267#if CAN_HANDLE_240_LINES
268 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
269#else
270 if(!skip && y<224)
271#endif
272 PicoLine(y);
273
274 if(PicoOpt&1)
275 sound_timers_and_dac(y);
276
277 // get samples from sound chips
7a93adeb 278 if (y == 224 && PsndOut) {
279 int len = sound_render(0, PsndLen);
280 if (PicoWriteSound) PicoWriteSound(len);
281 // clear sound buffer
282 sound_clear();
283 }
cc68a136 284
285 // Run scanline:
286 //dprintf("m68k starting exec @ %06x", SekPc);
672ad671 287 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
68cba51e 288 if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
7336a99a 289 SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
68cba51e 290 } else {
291 SekRun(cycles_68k);
292 if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
293 SekRunS68k(cycles_s68k);
cc68a136 294 }
295
296 if((PicoOpt&4) && Pico.m.z80Run) {
297 Pico.m.z80Run|=2;
298 z80CycleAim+=cycles_z80;
299 total_z80+=z80_run(z80CycleAim-total_z80);
300 }
301
4f265db7 302 update_chips();
cc68a136 303 }
304
305 // draw a frame just after vblank in alternative render mode
306 if(!PicoSkipFrame && (PicoOpt&0x10))
307 PicoFrameFull();
308
309 return 0;
310}
311
312
313int PicoFrameMCD(void)
314{
315 if(!(PicoOpt&0x10))
316 PicoFrameStart();
317
318 PicoFrameHintsMCD();
319
320 return 0;
321}
322
323