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);
68 extern unsigned char m68k_regs[0x40];
69 extern unsigned char s68k_regs[0x200];
71 // Accurate but slower frame which does hints
72 static int PicoFrameHintsMCD(void)
74 struct PicoVideo *pv=&Pico.video;
75 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample,counter75hz_lim;
76 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
77 int skip=PicoSkipFrame || (PicoOpt&0x10);
78 int hint; // Hint counter
81 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
82 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
83 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
85 counter75hz_lim = 2080;
86 if(pv->reg[1]&8) lines_vis = 240;
88 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
89 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
91 counter75hz_lim = 2096;
101 // z80_resetCycles();
103 pv->status&=~0x88; // clear V-Int, come out of vblank
105 hint=pv->reg[10]; // Load H-Int counter
106 //dprintf("-hint: %i", hint);
108 for (y=0;y<lines;y++)
110 Pico.m.scanline=(short)y;
112 // pad delay (for 6 button pads)
114 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
115 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
119 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
121 //dprintf("rhint:old @ %06x", SekPc);
122 hint=pv->reg[10]; // Reload H-Int counter
123 pv->pending_ints|=0x10;
124 if (pv->reg[0]&0x10) SekInterrupt(4);
125 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
126 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
132 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
133 pv->status|=0x88; // V-Int happened, go into vblank
134 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)
135 /*if(Pico.m.z80Run && (PicoOpt&4)) {
136 z80CycleAim+=cycles_z80/2;
137 total_z80+=z80_run(z80CycleAim-total_z80);
138 z80CycleAim-=cycles_z80/2;
140 pv->pending_ints|=0x20;
141 if(pv->reg[1]&0x20) SekInterrupt(6);
142 if(Pico.m.z80Run && (PicoOpt&4)) // ?
144 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
147 // decide if we draw this line
148 #if CAN_HANDLE_240_LINES
149 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
156 sound_timers_and_dac(y);
158 // get samples from sound chips
159 if(y == 32 && PsndOut)
161 else if((y == 224 || y == line_sample) && PsndOut)
165 //dprintf("m68k starting exec @ %06x", SekPc);
166 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
168 if ((Pico_mcd->m68k_regs[1]&3) == 1) { // no busreq/no reset
171 FILE *f = fopen("prg_ram.bin", "wb");
172 for (i = 0; i < 0x80000; i+=2)
174 int tmp = Pico_mcd->prg_ram[i];
175 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
176 Pico_mcd->prg_ram[i+1] = tmp;
178 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
182 //dprintf("s68k starting exec @ %06x", SekPcS68k);
183 SekRunS68k(cycles_s68k);
186 if((PicoOpt&4) && Pico.m.z80Run) {
188 z80CycleAim+=cycles_z80;
189 total_z80+=z80_run(z80CycleAim-total_z80);
192 if ((counter75hz+=10) >= counter75hz_lim) {
193 counter75hz -= counter75hz_lim;
198 // draw a frame just after vblank in alternative render mode
199 if(!PicoSkipFrame && (PicoOpt&0x10))
206 int PicoFrameMCD(void)