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 static int counter75hz = 0; // TODO: move 2 context
26 void PicoExitMCD(void)
31 int PicoResetMCD(int hard)
33 // clear everything except BIOS
34 memset(Pico_mcd->prg_ram, 0, sizeof(mcd_state) - sizeof(Pico_mcd->bios));
35 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
36 PicoMCD |= 2; // s68k reset pending. TODO: move
37 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
46 static __inline void SekRun(int cyc)
50 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
52 PicoCpu.cycles=cyc_do;
54 SekCycleCnt+=cyc_do-PicoCpu.cycles;
55 #elif defined(EMU_M68K)
56 m68k_set_context(&PicoM68kCPU);
57 SekCycleCnt+=m68k_execute(cyc_do);
61 static __inline void SekRunS68k(int cyc)
65 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
67 PicoCpuS68k.cycles=cyc_do;
68 CycloneRun(&PicoCpuS68k);
69 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
70 #elif defined(EMU_M68K)
71 m68k_set_context(&PicoS68kCPU);
72 SekCycleCntS68k+=m68k_execute(cyc_do);
76 static __inline void check_cd_dma(void)
80 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
82 ddx = Pico_mcd->s68k_regs[4] & 7;
83 if (ddx < 2) return; // invalid
85 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
88 if (ddx == 6) return; // invalid
90 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
93 // to be called on 224 or line_sample scanlines only
94 static __inline void getSamples(int y)
97 //dprintf("sta%i: %i [%i]", (emustatus & 2), emustatus, y);
99 sound_render(PsndLen/2, PsndLen-PsndLen/2);
100 else sound_render(0, PsndLen);
101 if (emustatus&1) emustatus|=2; else emustatus&=~2;
102 if (PicoWriteSound) PicoWriteSound();
103 // clear sound buffer
104 memset(PsndOut, 0, (PicoOpt & 8) ? (PsndLen<<2) : (PsndLen<<1));
106 else if(emustatus & 3) {
109 sound_render(0, PsndLen/2);
115 // Accurate but slower frame which does hints
116 static int PicoFrameHintsMCD(void)
118 struct PicoVideo *pv=&Pico.video;
119 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample,counter75hz_lim;
120 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
121 int skip=PicoSkipFrame || (PicoOpt&0x10);
122 int hint; // Hint counter
125 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
126 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
127 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
129 counter75hz_lim = 2080;
130 if(pv->reg[1]&8) lines_vis = 240;
132 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
133 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
135 counter75hz_lim = 2096;
140 SekCyclesResetS68k();
141 //z80ExtraCycles = 0;
145 // z80_resetCycles();
147 pv->status&=~0x88; // clear V-Int, come out of vblank
149 hint=pv->reg[10]; // Load H-Int counter
150 //dprintf("-hint: %i", hint);
152 for (y=0;y<lines;y++)
154 Pico.m.scanline=(short)y;
156 // pad delay (for 6 button pads)
158 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
159 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
165 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
167 //dprintf("rhint:old @ %06x", SekPc);
168 hint=pv->reg[10]; // Reload H-Int counter
169 pv->pending_ints|=0x10;
170 if (pv->reg[0]&0x10) SekInterrupt(4);
171 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
172 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
178 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
179 pv->status|=0x88; // V-Int happened, go into vblank
180 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)
181 /*if(Pico.m.z80Run && (PicoOpt&4)) {
182 z80CycleAim+=cycles_z80/2;
183 total_z80+=z80_run(z80CycleAim-total_z80);
184 z80CycleAim-=cycles_z80/2;
186 pv->pending_ints|=0x20;
187 if(pv->reg[1]&0x20) SekInterrupt(6);
188 if(Pico.m.z80Run && (PicoOpt&4)) // ?
190 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
193 // decide if we draw this line
194 #if CAN_HANDLE_240_LINES
195 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
202 sound_timers_and_dac(y);
204 // get samples from sound chips
205 if(y == 32 && PsndOut)
207 else if((y == 224 || y == line_sample) && PsndOut)
211 //dprintf("m68k starting exec @ %06x", SekPc);
212 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
214 if ((Pico_mcd->m.busreq&3) == 1) { // no busreq/no reset
217 FILE *f = fopen("prg_ram.bin", "wb");
218 for (i = 0; i < 0x80000; i+=2)
220 int tmp = Pico_mcd->prg_ram[i];
221 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
222 Pico_mcd->prg_ram[i+1] = tmp;
224 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
228 //dprintf("s68k starting exec @ %06x", SekPcS68k);
229 SekRunS68k(cycles_s68k);
232 if((PicoOpt&4) && Pico.m.z80Run) {
234 z80CycleAim+=cycles_z80;
235 total_z80+=z80_run(z80CycleAim-total_z80);
238 if ((counter75hz+=10) >= counter75hz_lim) {
239 counter75hz -= counter75hz_lim;
243 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
247 // draw a frame just after vblank in alternative render mode
248 if(!PicoSkipFrame && (PicoOpt&0x10))
255 int PicoFrameMCD(void)