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 extern unsigned char formatted_bram[8*0x10];
26 void PicoExitMCD(void)
31 int PicoResetMCD(int hard)
33 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
34 memset(Pico_mcd->word_ram, 0, sizeof(Pico_mcd->word_ram));
35 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
37 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
38 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - 8*0x10, formatted_bram, 8*0x10);
40 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
41 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
43 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
44 Pico_mcd->m.state_flags |= 2; // s68k reset pending
45 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
46 Pico_mcd->m.counter75hz = 0;
55 static __inline void SekRun(int cyc)
59 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
61 PicoCpu.cycles=cyc_do;
63 SekCycleCnt+=cyc_do-PicoCpu.cycles;
64 #elif defined(EMU_M68K)
65 m68k_set_context(&PicoM68kCPU);
66 SekCycleCnt+=m68k_execute(cyc_do);
70 static __inline void SekRunS68k(int cyc)
74 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
76 PicoCpuS68k.cycles=cyc_do;
77 CycloneRun(&PicoCpuS68k);
78 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
79 #elif defined(EMU_M68K)
80 m68k_set_context(&PicoS68kCPU);
81 SekCycleCntS68k+=m68k_execute(cyc_do);
85 static __inline void check_cd_dma(void)
89 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
91 ddx = Pico_mcd->s68k_regs[4] & 7;
92 if (ddx < 2) return; // invalid
94 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
97 if (ddx == 6) return; // invalid
99 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
102 static __inline void update_chips(void)
104 int counter_timer, int3_set;
105 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
108 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
109 Pico_mcd->m.counter75hz -= counter75hz_lim;
114 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
115 Pico_mcd->m.timer_stopwatch += counter_timer;
116 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
117 Pico_mcd->m.timer_int3 -= counter_timer;
118 if (Pico_mcd->m.timer_int3 < 0) {
119 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
120 dprintf("s68k: timer irq 3");
122 Pico_mcd->m.timer_int3 += int3_set << 16;
124 // is this really what happens if irq3 is masked out?
125 Pico_mcd->m.timer_int3 &= 0xffffff;
130 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
135 static int PicoFrameHintsMCD(void)
137 struct PicoVideo *pv=&Pico.video;
138 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
139 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
140 int skip=PicoSkipFrame || (PicoOpt&0x10);
141 int hint; // Hint counter
144 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
145 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
146 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
148 if(pv->reg[1]&8) lines_vis = 240;
150 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
151 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
157 SekCyclesResetS68k();
158 //z80ExtraCycles = 0;
162 // z80_resetCycles();
164 pv->status&=~0x88; // clear V-Int, come out of vblank
166 hint=pv->reg[10]; // Load H-Int counter
167 //dprintf("-hint: %i", hint);
169 for (y=0;y<lines;y++)
171 Pico.m.scanline=(short)y;
173 // pad delay (for 6 button pads)
175 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
176 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
182 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
184 //dprintf("rhint:old @ %06x", SekPc);
185 hint=pv->reg[10]; // Reload H-Int counter
186 pv->pending_ints|=0x10;
187 if (pv->reg[0]&0x10) SekInterrupt(4);
188 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
189 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
195 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
196 pv->status|=0x88; // V-Int happened, go into vblank
197 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)
198 /*if(Pico.m.z80Run && (PicoOpt&4)) {
199 z80CycleAim+=cycles_z80/2;
200 total_z80+=z80_run(z80CycleAim-total_z80);
201 z80CycleAim-=cycles_z80/2;
203 pv->pending_ints|=0x20;
204 if(pv->reg[1]&0x20) SekInterrupt(6);
205 if(Pico.m.z80Run && (PicoOpt&4)) // ?
207 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
210 // decide if we draw this line
211 #if CAN_HANDLE_240_LINES
212 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
219 sound_timers_and_dac(y);
221 // get samples from sound chips
222 if (y == 224 && PsndOut) {
223 int len = sound_render(0, PsndLen);
224 if (PicoWriteSound) PicoWriteSound(len);
225 // clear sound buffer
230 //dprintf("m68k starting exec @ %06x", SekPc);
231 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
233 if ((Pico_mcd->m.busreq&3) == 1) { // no busreq/no reset
236 FILE *f = fopen("prg_ram.bin", "wb");
237 for (i = 0; i < 0x80000; i+=2)
239 int tmp = Pico_mcd->prg_ram[i];
240 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
241 Pico_mcd->prg_ram[i+1] = tmp;
243 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
247 //dprintf("s68k starting exec @ %06x", SekPcS68k);
248 SekRunS68k(cycles_s68k);
251 if((PicoOpt&4) && Pico.m.z80Run) {
253 z80CycleAim+=cycles_z80;
254 total_z80+=z80_run(z80CycleAim-total_z80);
260 // draw a frame just after vblank in alternative render mode
261 if(!PicoSkipFrame && (PicoOpt&0x10))
268 int PicoFrameMCD(void)