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 PicoMCD |= 2; // s68k reset pending
36 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
45 static __inline void SekRun(int cyc)
49 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
51 m68k_set_context(&PicoM68kCPU);
52 SekCycleCnt+=m68k_execute(cyc_do);
56 static __inline void SekRunS68k(int cyc)
60 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
62 m68k_set_context(&PicoS68kCPU);
63 SekCycleCntS68k+=m68k_execute(cyc_do);
67 static __inline void check_cd_dma(void)
71 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
73 ddx = Pico_mcd->s68k_regs[4] & 7;
74 if (ddx < 2) return; // invalid
76 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
79 if (ddx == 6) return; // invalid
81 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
85 // Accurate but slower frame which does hints
86 static int PicoFrameHintsMCD(void)
88 struct PicoVideo *pv=&Pico.video;
89 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample,counter75hz_lim;
90 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
91 int skip=PicoSkipFrame || (PicoOpt&0x10);
92 int hint; // Hint counter
95 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
96 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
97 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
99 counter75hz_lim = 2080;
100 if(pv->reg[1]&8) lines_vis = 240;
102 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
103 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
105 counter75hz_lim = 2096;
110 SekCyclesResetS68k();
111 //z80ExtraCycles = 0;
115 // z80_resetCycles();
117 pv->status&=~0x88; // clear V-Int, come out of vblank
119 hint=pv->reg[10]; // Load H-Int counter
120 //dprintf("-hint: %i", hint);
122 for (y=0;y<lines;y++)
124 Pico.m.scanline=(short)y;
126 // pad delay (for 6 button pads)
128 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
129 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
135 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
137 //dprintf("rhint:old @ %06x", SekPc);
138 hint=pv->reg[10]; // Reload H-Int counter
139 pv->pending_ints|=0x10;
140 if (pv->reg[0]&0x10) SekInterrupt(4);
141 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
142 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
148 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
149 pv->status|=0x88; // V-Int happened, go into vblank
150 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)
151 /*if(Pico.m.z80Run && (PicoOpt&4)) {
152 z80CycleAim+=cycles_z80/2;
153 total_z80+=z80_run(z80CycleAim-total_z80);
154 z80CycleAim-=cycles_z80/2;
156 pv->pending_ints|=0x20;
157 if(pv->reg[1]&0x20) SekInterrupt(6);
158 if(Pico.m.z80Run && (PicoOpt&4)) // ?
160 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
163 // decide if we draw this line
164 #if CAN_HANDLE_240_LINES
165 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
172 sound_timers_and_dac(y);
174 // get samples from sound chips
175 if(y == 32 && PsndOut)
177 else if((y == 224 || y == line_sample) && PsndOut)
181 //dprintf("m68k starting exec @ %06x", SekPc);
182 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
184 if ((Pico_mcd->m.busreq&3) == 1) { // no busreq/no reset
187 FILE *f = fopen("prg_ram.bin", "wb");
188 for (i = 0; i < 0x80000; i+=2)
190 int tmp = Pico_mcd->prg_ram[i];
191 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
192 Pico_mcd->prg_ram[i+1] = tmp;
194 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
198 //dprintf("s68k starting exec @ %06x", SekPcS68k);
199 SekRunS68k(cycles_s68k);
202 if((PicoOpt&4) && Pico.m.z80Run) {
204 z80CycleAim+=cycles_z80;
205 total_z80+=z80_run(z80CycleAim-total_z80);
208 if ((counter75hz+=10) >= counter75hz_lim) {
209 counter75hz -= counter75hz_lim;
214 // draw a frame just after vblank in alternative render mode
215 if(!PicoSkipFrame && (PicoOpt&0x10))
222 int PicoFrameMCD(void)