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)
33 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
34 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
35 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
37 int fmt_size = sizeof(formatted_bram);
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));
44 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
45 Pico_mcd->m.state_flags |= 2; // s68k reset pending
46 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
47 Pico_mcd->m.counter75hz = 0;
56 static __inline void SekRun(int cyc)
60 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
62 PicoCpu.cycles=cyc_do;
64 SekCycleCnt+=cyc_do-PicoCpu.cycles;
65 #elif defined(EMU_M68K)
66 m68k_set_context(&PicoM68kCPU);
67 SekCycleCnt+=m68k_execute(cyc_do);
71 static __inline void SekRunS68k(int cyc)
75 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
77 PicoCpuS68k.cycles=cyc_do;
78 CycloneRun(&PicoCpuS68k);
79 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
80 #elif defined(EMU_M68K)
81 m68k_set_context(&PicoS68kCPU);
82 SekCycleCntS68k+=m68k_execute(cyc_do);
86 #define PS_STEP_M68K ((488<<16)/20) // ~24
87 //#define PS_STEP_S68K 13
89 #ifndef _ASM_CD_PICO_C
90 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
92 int cycn, cycn_s68k, cyc_do;
93 int d_cm = 0, d_cs = 0, ex;
94 SekCycleAim+=cyc_m68k;
95 SekCycleAimS68k+=cyc_s68k;
97 // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
98 // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
100 /* loop 488 downto 0 in steps of PS_STEP */
101 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
104 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
105 //fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k);
106 if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
107 #if defined(EMU_C68K)
108 PicoCpu.cycles = cyc_do;
109 CycloneRun(&PicoCpu);
110 SekCycleCnt += cyc_do - PicoCpu.cycles;
111 #elif defined(EMU_M68K)
112 m68k_set_context(&PicoM68kCPU);
113 SekCycleCnt += (ex = m68k_execute(cyc_do));
116 //fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0;
117 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
118 #if defined(EMU_C68K)
119 PicoCpuS68k.cycles = cyc_do;
120 CycloneRun(&PicoCpuS68k);
121 SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles;
122 #elif defined(EMU_M68K)
123 m68k_set_context(&PicoS68kCPU);
124 SekCycleCntS68k += (ex = m68k_execute(cyc_do));
127 //fprintf(stderr, "%3i\n", ex); d_cs += ex;
130 //fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs);
133 void SekRunPS(int cyc_m68k, int cyc_s68k);
137 static __inline void check_cd_dma(void)
141 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
143 ddx = Pico_mcd->s68k_regs[4] & 7;
144 if (ddx < 2) return; // invalid
146 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
149 if (ddx == 6) return; // invalid
151 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
154 static __inline void update_chips(void)
156 int counter_timer, int3_set;
157 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
160 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
161 Pico_mcd->m.counter75hz -= counter75hz_lim;
166 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
167 Pico_mcd->m.timer_stopwatch += counter_timer;
168 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
169 Pico_mcd->m.timer_int3 -= counter_timer;
170 if (Pico_mcd->m.timer_int3 < 0) {
171 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
172 dprintf("s68k: timer irq 3");
174 Pico_mcd->m.timer_int3 += int3_set << 16;
176 // is this really what happens if irq3 is masked out?
177 Pico_mcd->m.timer_int3 &= 0xffffff;
182 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
187 static int PicoFrameHintsMCD(void)
189 struct PicoVideo *pv=&Pico.video;
190 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
191 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
192 int skip=PicoSkipFrame || (PicoOpt&0x10);
193 int hint; // Hint counter
196 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
197 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
198 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
200 if(pv->reg[1]&8) lines_vis = 240;
202 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
203 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
209 SekCyclesResetS68k();
210 //z80ExtraCycles = 0;
214 // z80_resetCycles();
216 pv->status&=~0x88; // clear V-Int, come out of vblank
218 hint=pv->reg[10]; // Load H-Int counter
219 //dprintf("-hint: %i", hint);
221 for (y=0;y<lines;y++)
223 Pico.m.scanline=(short)y;
225 // pad delay (for 6 button pads)
227 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
228 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
234 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
236 //dprintf("rhint:old @ %06x", SekPc);
237 hint=pv->reg[10]; // Reload H-Int counter
238 pv->pending_ints|=0x10;
239 if (pv->reg[0]&0x10) SekInterrupt(4);
240 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
241 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
247 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
248 pv->status|=0x88; // V-Int happened, go into vblank
249 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)
250 /*if(Pico.m.z80Run && (PicoOpt&4)) {
251 z80CycleAim+=cycles_z80/2;
252 total_z80+=z80_run(z80CycleAim-total_z80);
253 z80CycleAim-=cycles_z80/2;
255 pv->pending_ints|=0x20;
256 if(pv->reg[1]&0x20) SekInterrupt(6);
257 if(Pico.m.z80Run && (PicoOpt&4)) // ?
259 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
262 // decide if we draw this line
263 #if CAN_HANDLE_240_LINES
264 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
271 sound_timers_and_dac(y);
273 // get samples from sound chips
274 if (y == 224 && PsndOut) {
275 int len = sound_render(0, PsndLen);
276 if (PicoWriteSound) PicoWriteSound(len);
277 // clear sound buffer
282 //dprintf("m68k starting exec @ %06x", SekPc);
283 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
284 if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
285 SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
288 if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
289 SekRunS68k(cycles_s68k);
292 if((PicoOpt&4) && Pico.m.z80Run) {
294 z80CycleAim+=cycles_z80;
295 total_z80+=z80_run(z80CycleAim-total_z80);
301 // draw a frame just after vblank in alternative render mode
302 if(!PicoSkipFrame && (PicoOpt&0x10))
309 int PicoFrameMCD(void)