1 // This is part of Pico Library
3 // (c) Copyright 2004 Dave, All rights reserved.
4 // (c) Copyright 2006 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"
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
44 static __inline void SekRun(int cyc)
48 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
50 m68k_set_context(&PicoM68kCPU);
51 SekCycleCnt+=m68k_execute(cyc_do);
55 static __inline void SekRunS68k(int cyc)
59 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
61 m68k_set_context(&PicoS68kCPU);
62 SekCycleCntS68k+=m68k_execute(cyc_do);
67 extern unsigned char m68k_regs[0x40];
68 extern unsigned char s68k_regs[0x200];
70 // Accurate but slower frame which does hints
71 static int PicoFrameHintsMCD(void)
73 struct PicoVideo *pv=&Pico.video;
74 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
75 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
76 int skip=PicoSkipFrame || (PicoOpt&0x10);
77 int hint; // Hint counter
80 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
81 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
82 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
84 if(pv->reg[1]&8) lines_vis = 240;
86 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
87 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
100 pv->status&=~0x88; // clear V-Int, come out of vblank
102 hint=pv->reg[10]; // Load H-Int counter
103 //dprintf("-hint: %i", hint);
105 for (y=0;y<lines;y++)
107 Pico.m.scanline=(short)y;
109 // pad delay (for 6 button pads)
111 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
112 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
116 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
118 //dprintf("rhint:old @ %06x", SekPc);
119 hint=pv->reg[10]; // Reload H-Int counter
120 pv->pending_ints|=0x10;
121 if (pv->reg[0]&0x10) SekInterrupt(4);
122 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
123 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
129 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
130 pv->status|=0x88; // V-Int happened, go into vblank
131 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)
132 /*if(Pico.m.z80Run && (PicoOpt&4)) {
133 z80CycleAim+=cycles_z80/2;
134 total_z80+=z80_run(z80CycleAim-total_z80);
135 z80CycleAim-=cycles_z80/2;
137 pv->pending_ints|=0x20;
138 if(pv->reg[1]&0x20) SekInterrupt(6);
139 if(Pico.m.z80Run && (PicoOpt&4)) // ?
141 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
144 // decide if we draw this line
145 #if CAN_HANDLE_240_LINES
146 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
153 sound_timers_and_dac(y);
155 // get samples from sound chips
156 if(y == 32 && PsndOut)
158 else if((y == 224 || y == line_sample) && PsndOut)
162 //dprintf("m68k starting exec @ %06x", SekPc);
164 if ((Pico_mcd->m68k_regs[1]&3) == 1) { // no busreq/no reset
167 FILE *f = fopen("prg_ram.bin", "wb");
168 for (i = 0; i < 0x80000; i+=2)
170 int tmp = Pico_mcd->prg_ram[i];
171 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
172 Pico_mcd->prg_ram[i+1] = tmp;
174 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
178 //dprintf("s68k starting exec @ %06x", SekPcS68k);
179 SekRunS68k(cycles_s68k);
182 if((PicoOpt&4) && Pico.m.z80Run) {
184 z80CycleAim+=cycles_z80;
185 total_z80+=z80_run(z80CycleAim-total_z80);
188 // if cdd is on, counter elapsed and irq4 is not masked, do irq4
189 if ((Pico_mcd->s68k_regs[0x37]&4) && ++counter75hz > 209 && (Pico_mcd->s68k_regs[0x33]&(1<<4))) {
195 // draw a frame just after vblank in alternative render mode
196 if(!PicoSkipFrame && (PicoOpt&0x10))
203 int PicoFrameMCD(void)