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