1 // This is part of Pico Library
3 // (c) Copyright 2004 Dave, All rights reserved.
4 // (c) Copyright 2007 notaz, All rights reserved.
5 // Free for non-commercial use.
7 // For commercial use, separate licencing terms must be obtained.
10 #include "../PicoInt.h"
11 #include "../sound/sound.h"
14 extern unsigned char formatted_bram[4*0x10];
26 void PicoExitMCD(void)
31 int PicoResetMCD(int hard)
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);
41 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
42 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
43 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
45 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
46 Pico_mcd->m.state_flags |= 2; // s68k reset pending
47 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
52 #ifdef _ASM_CD_MEMORY_C
54 PicoMemResetCDdecode(1);
60 static __inline void SekRun(int cyc)
64 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
66 PicoCpu.cycles=cyc_do;
68 SekCycleCnt+=cyc_do-PicoCpu.cycles;
69 #elif defined(EMU_M68K)
70 m68k_set_context(&PicoM68kCPU);
71 SekCycleCnt+=m68k_execute(cyc_do);
75 static __inline void SekRunS68k(int cyc)
79 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
81 PicoCpuS68k.cycles=cyc_do;
82 CycloneRun(&PicoCpuS68k);
83 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
84 #elif defined(EMU_M68K)
85 m68k_set_context(&PicoS68kCPU);
86 SekCycleCntS68k+=m68k_execute(cyc_do);
90 #define PS_STEP_M68K ((488<<16)/20) // ~24
91 //#define PS_STEP_S68K 13
93 #ifndef _ASM_CD_PICO_C
94 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
96 int cycn, cycn_s68k, cyc_do;
97 int d_cm = 0, d_cs = 0, ex;
98 SekCycleAim+=cyc_m68k;
99 SekCycleAimS68k+=cyc_s68k;
101 // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
102 // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
104 /* loop 488 downto 0 in steps of PS_STEP */
105 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
108 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
109 //fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k);
110 if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
111 #if defined(EMU_C68K)
112 PicoCpu.cycles = cyc_do;
113 CycloneRun(&PicoCpu);
114 SekCycleCnt += cyc_do - PicoCpu.cycles;
115 #elif defined(EMU_M68K)
116 m68k_set_context(&PicoM68kCPU);
117 SekCycleCnt += (ex = m68k_execute(cyc_do));
120 //fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0;
121 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
122 #if defined(EMU_C68K)
123 PicoCpuS68k.cycles = cyc_do;
124 CycloneRun(&PicoCpuS68k);
125 SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles;
126 #elif defined(EMU_M68K)
127 m68k_set_context(&PicoS68kCPU);
128 SekCycleCntS68k += (ex = m68k_execute(cyc_do));
131 //fprintf(stderr, "%3i\n", ex); d_cs += ex;
134 //fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs);
137 void SekRunPS(int cyc_m68k, int cyc_s68k);
141 static __inline void check_cd_dma(void)
145 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
147 ddx = Pico_mcd->s68k_regs[4] & 7;
148 if (ddx < 2) return; // invalid
150 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
153 if (ddx == 6) return; // invalid
155 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
158 static __inline void update_chips(void)
160 int counter_timer, int3_set;
161 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
164 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
165 Pico_mcd->m.counter75hz -= counter75hz_lim;
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");
178 Pico_mcd->m.timer_int3 += int3_set << 16;
180 // is this really what happens if irq3 is masked out?
181 Pico_mcd->m.timer_int3 &= 0xffffff;
186 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
191 static int PicoFrameHintsMCD(void)
193 struct PicoVideo *pv=&Pico.video;
194 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
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
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
204 if(pv->reg[1]&8) lines_vis = 240;
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
213 SekCyclesResetS68k();
214 //z80ExtraCycles = 0;
218 // z80_resetCycles();
220 pv->status&=~0x88; // clear V-Int, come out of vblank
222 hint=pv->reg[10]; // Load H-Int counter
223 //dprintf("-hint: %i", hint);
225 for (y=0;y<lines;y++)
227 Pico.m.scanline=(short)y;
229 // pad delay (for 6 button pads)
231 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
232 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
238 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
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));
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;
259 pv->pending_ints|=0x20;
260 if(pv->reg[1]&0x20) SekInterrupt(6);
261 if(Pico.m.z80Run && (PicoOpt&4)) // ?
263 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
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)) )
275 sound_timers_and_dac(y);
277 // get samples from sound chips
278 if (y == 224 && PsndOut) {
279 int len = sound_render(0, PsndLen);
280 if (PicoWriteSound) PicoWriteSound(len);
281 // clear sound buffer
286 //dprintf("m68k starting exec @ %06x", SekPc);
287 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
288 if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
289 SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
292 if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
293 SekRunS68k(cycles_s68k);
296 if((PicoOpt&4) && Pico.m.z80Run) {
298 z80CycleAim+=cycles_z80;
299 total_z80+=z80_run(z80CycleAim-total_z80);
305 // draw a frame just after vblank in alternative render mode
306 if(!PicoSkipFrame && (PicoOpt&0x10))
313 int PicoFrameMCD(void)