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
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 m68k_set_context(&PicoM68kCPU);
53 SekCycleCnt+=m68k_execute(cyc_do);
57 static __inline void SekRunS68k(int cyc)
61 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
63 m68k_set_context(&PicoS68kCPU);
64 SekCycleCntS68k+=m68k_execute(cyc_do);
68 static __inline void check_cd_dma(void)
72 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
74 ddx = Pico_mcd->s68k_regs[4] & 7;
75 if (ddx < 2) return; // invalid
77 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
80 if (ddx == 6) return; // invalid
82 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
86 // Accurate but slower frame which does hints
87 static int PicoFrameHintsMCD(void)
89 struct PicoVideo *pv=&Pico.video;
90 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample,counter75hz_lim;
91 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
92 int skip=PicoSkipFrame || (PicoOpt&0x10);
93 int hint; // Hint counter
96 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
97 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
98 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
100 counter75hz_lim = 2080;
101 if(pv->reg[1]&8) lines_vis = 240;
103 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
104 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
106 counter75hz_lim = 2096;
111 SekCyclesResetS68k();
112 //z80ExtraCycles = 0;
116 // z80_resetCycles();
118 pv->status&=~0x88; // clear V-Int, come out of vblank
120 hint=pv->reg[10]; // Load H-Int counter
121 //dprintf("-hint: %i", hint);
123 for (y=0;y<lines;y++)
125 Pico.m.scanline=(short)y;
127 // pad delay (for 6 button pads)
129 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
130 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
136 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
138 //dprintf("rhint:old @ %06x", SekPc);
139 hint=pv->reg[10]; // Reload H-Int counter
140 pv->pending_ints|=0x10;
141 if (pv->reg[0]&0x10) SekInterrupt(4);
142 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
143 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
149 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
150 pv->status|=0x88; // V-Int happened, go into vblank
151 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)
152 /*if(Pico.m.z80Run && (PicoOpt&4)) {
153 z80CycleAim+=cycles_z80/2;
154 total_z80+=z80_run(z80CycleAim-total_z80);
155 z80CycleAim-=cycles_z80/2;
157 pv->pending_ints|=0x20;
158 if(pv->reg[1]&0x20) SekInterrupt(6);
159 if(Pico.m.z80Run && (PicoOpt&4)) // ?
161 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
164 // decide if we draw this line
165 #if CAN_HANDLE_240_LINES
166 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
173 sound_timers_and_dac(y);
175 // get samples from sound chips
176 if(y == 32 && PsndOut)
178 else if((y == 224 || y == line_sample) && PsndOut)
182 //dprintf("m68k starting exec @ %06x", SekPc);
183 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
185 if ((Pico_mcd->m.busreq&3) == 1) { // no busreq/no reset
188 FILE *f = fopen("prg_ram.bin", "wb");
189 for (i = 0; i < 0x80000; i+=2)
191 int tmp = Pico_mcd->prg_ram[i];
192 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
193 Pico_mcd->prg_ram[i+1] = tmp;
195 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
199 //dprintf("s68k starting exec @ %06x", SekPcS68k);
200 SekRunS68k(cycles_s68k);
203 if((PicoOpt&4) && Pico.m.z80Run) {
205 z80CycleAim+=cycles_z80;
206 total_z80+=z80_run(z80CycleAim-total_z80);
209 if ((counter75hz+=10) >= counter75hz_lim) {
210 counter75hz -= counter75hz_lim;
214 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
218 // draw a frame just after vblank in alternative render mode
219 if(!PicoSkipFrame && (PicoOpt&0x10))
226 int PicoFrameMCD(void)