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 8
87 #define PS_STEP_S68K 13
89 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
91 int cyc_do_m68k, cyc_do_s68k, it=0;
92 int cyc_done_m68k=0, cyc_done_s68k=0;
93 SekCycleAim+=cyc_m68k;
94 SekCycleAimS68k+=cyc_s68k;
95 cyc_do_m68k=SekCycleAim-SekCycleCnt;
96 cyc_do_s68k=SekCycleAimS68k-SekCycleCntS68k;
97 while (cyc_done_m68k < cyc_do_m68k || cyc_done_s68k < cyc_do_s68k) {
99 if (cyc_done_m68k < cyc_do_m68k && it*PS_STEP_M68K > cyc_done_m68k) {
100 #if defined(EMU_C68K)
101 PicoCpu.cycles = PS_STEP_M68K;
102 CycloneRun(&PicoCpu);
103 cyc_done_m68k += PS_STEP_M68K - PicoCpu.cycles;
104 #elif defined(EMU_M68K)
105 m68k_set_context(&PicoM68kCPU);
106 cyc_done_m68k += m68k_execute(PS_STEP_M68K);
108 } //else dprintf("m68k skipping it #%i", it);
109 if (cyc_done_s68k < cyc_do_s68k && it*PS_STEP_S68K > cyc_done_s68k) {
110 #if defined(EMU_C68K)
111 PicoCpuS68k.cycles = PS_STEP_S68K;
112 CycloneRun(&PicoCpuS68k);
113 cyc_done_s68k += PS_STEP_S68K - PicoCpuS68k.cycles;
114 #elif defined(EMU_M68K)
115 m68k_set_context(&PicoS68kCPU);
116 cyc_done_s68k += m68k_execute(PS_STEP_S68K);
118 } //else dprintf("s68k skipping it #%i", it);
120 SekCycleCnt += cyc_done_m68k;
121 SekCycleCntS68k += cyc_done_s68k;
122 //dprintf("== end SekRunPS, it=%i ==", it);
126 static __inline void check_cd_dma(void)
130 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
132 ddx = Pico_mcd->s68k_regs[4] & 7;
133 if (ddx < 2) return; // invalid
135 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
138 if (ddx == 6) return; // invalid
140 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
143 static __inline void update_chips(void)
145 int counter_timer, int3_set;
146 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
149 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
150 Pico_mcd->m.counter75hz -= counter75hz_lim;
155 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
156 Pico_mcd->m.timer_stopwatch += counter_timer;
157 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
158 Pico_mcd->m.timer_int3 -= counter_timer;
159 if (Pico_mcd->m.timer_int3 < 0) {
160 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
161 dprintf("s68k: timer irq 3");
163 Pico_mcd->m.timer_int3 += int3_set << 16;
165 // is this really what happens if irq3 is masked out?
166 Pico_mcd->m.timer_int3 &= 0xffffff;
171 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
176 static int PicoFrameHintsMCD(void)
178 struct PicoVideo *pv=&Pico.video;
179 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
180 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
181 int skip=PicoSkipFrame || (PicoOpt&0x10);
182 int hint; // Hint counter
185 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
186 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
187 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
189 if(pv->reg[1]&8) lines_vis = 240;
191 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
192 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
198 SekCyclesResetS68k();
199 //z80ExtraCycles = 0;
203 // z80_resetCycles();
205 pv->status&=~0x88; // clear V-Int, come out of vblank
207 hint=pv->reg[10]; // Load H-Int counter
208 //dprintf("-hint: %i", hint);
210 for (y=0;y<lines;y++)
212 Pico.m.scanline=(short)y;
214 // pad delay (for 6 button pads)
216 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
217 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
223 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
225 //dprintf("rhint:old @ %06x", SekPc);
226 hint=pv->reg[10]; // Reload H-Int counter
227 pv->pending_ints|=0x10;
228 if (pv->reg[0]&0x10) SekInterrupt(4);
229 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
230 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
236 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
237 pv->status|=0x88; // V-Int happened, go into vblank
238 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)
239 /*if(Pico.m.z80Run && (PicoOpt&4)) {
240 z80CycleAim+=cycles_z80/2;
241 total_z80+=z80_run(z80CycleAim-total_z80);
242 z80CycleAim-=cycles_z80/2;
244 pv->pending_ints|=0x20;
245 if(pv->reg[1]&0x20) SekInterrupt(6);
246 if(Pico.m.z80Run && (PicoOpt&4)) // ?
248 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
251 // decide if we draw this line
252 #if CAN_HANDLE_240_LINES
253 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
260 sound_timers_and_dac(y);
262 // get samples from sound chips
263 if (y == 224 && PsndOut) {
264 int len = sound_render(0, PsndLen);
265 if (PicoWriteSound) PicoWriteSound(len);
266 // clear sound buffer
271 //dprintf("m68k starting exec @ %06x", SekPc);
272 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
273 if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
274 SekRunPS(cycles_68k, cycles_s68k); // "perfect sync"
277 if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
278 SekRunS68k(cycles_s68k);
281 if((PicoOpt&4) && Pico.m.z80Run) {
283 z80CycleAim+=cycles_z80;
284 total_z80+=z80_run(z80CycleAim-total_z80);
290 // draw a frame just after vblank in alternative render mode
291 if(!PicoSkipFrame && (PicoOpt&0x10))
298 int PicoFrameMCD(void)