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 int Status_CDC;
69 static __inline void check_cd_dma(void)
73 if (!(Status_CDC & 0x08)) return;
75 ddx = Pico_mcd->s68k_regs[4] & 7;
76 if (ddx < 2) return; // invalid
77 if (ddx < 4) Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
78 if (ddx == 6) return; // invalid
80 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
84 // Accurate but slower frame which does hints
85 static int PicoFrameHintsMCD(void)
87 struct PicoVideo *pv=&Pico.video;
88 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample,counter75hz_lim;
89 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
90 int skip=PicoSkipFrame || (PicoOpt&0x10);
91 int hint; // Hint counter
94 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
95 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
96 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
98 counter75hz_lim = 2080;
99 if(pv->reg[1]&8) lines_vis = 240;
101 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
102 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
104 counter75hz_lim = 2096;
109 SekCyclesResetS68k();
110 //z80ExtraCycles = 0;
114 // z80_resetCycles();
116 pv->status&=~0x88; // clear V-Int, come out of vblank
118 hint=pv->reg[10]; // Load H-Int counter
119 //dprintf("-hint: %i", hint);
121 for (y=0;y<lines;y++)
123 Pico.m.scanline=(short)y;
125 // pad delay (for 6 button pads)
127 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
128 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
134 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
136 //dprintf("rhint:old @ %06x", SekPc);
137 hint=pv->reg[10]; // Reload H-Int counter
138 pv->pending_ints|=0x10;
139 if (pv->reg[0]&0x10) SekInterrupt(4);
140 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
141 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
147 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
148 pv->status|=0x88; // V-Int happened, go into vblank
149 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)
150 /*if(Pico.m.z80Run && (PicoOpt&4)) {
151 z80CycleAim+=cycles_z80/2;
152 total_z80+=z80_run(z80CycleAim-total_z80);
153 z80CycleAim-=cycles_z80/2;
155 pv->pending_ints|=0x20;
156 if(pv->reg[1]&0x20) SekInterrupt(6);
157 if(Pico.m.z80Run && (PicoOpt&4)) // ?
159 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
162 // decide if we draw this line
163 #if CAN_HANDLE_240_LINES
164 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
171 sound_timers_and_dac(y);
173 // get samples from sound chips
174 if(y == 32 && PsndOut)
176 else if((y == 224 || y == line_sample) && PsndOut)
180 //dprintf("m68k starting exec @ %06x", SekPc);
181 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
183 if ((Pico_mcd->m68k_regs[1]&3) == 1) { // no busreq/no reset
186 FILE *f = fopen("prg_ram.bin", "wb");
187 for (i = 0; i < 0x80000; i+=2)
189 int tmp = Pico_mcd->prg_ram[i];
190 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
191 Pico_mcd->prg_ram[i+1] = tmp;
193 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
197 //dprintf("s68k starting exec @ %06x", SekPcS68k);
198 SekRunS68k(cycles_s68k);
201 if((PicoOpt&4) && Pico.m.z80Run) {
203 z80CycleAim+=cycles_z80;
204 total_z80+=z80_run(z80CycleAim-total_z80);
207 if ((counter75hz+=10) >= counter75hz_lim) {
208 counter75hz -= counter75hz_lim;
213 // draw a frame just after vblank in alternative render mode
214 if(!PicoSkipFrame && (PicoOpt&0x10))
221 int PicoFrameMCD(void)