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[4*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_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
35 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
37 int fmt_size = sizeof(formatted_bram);
38 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
39 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
41 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
42 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
44 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
45 Pico_mcd->m.state_flags |= 2; // s68k reset pending
46 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
47 Pico_mcd->m.counter75hz = 0;
56 static __inline void SekRun(int cyc)
60 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
62 PicoCpu.cycles=cyc_do;
64 SekCycleCnt+=cyc_do-PicoCpu.cycles;
65 #elif defined(EMU_M68K)
66 m68k_set_context(&PicoM68kCPU);
67 SekCycleCnt+=m68k_execute(cyc_do);
71 static __inline void SekRunS68k(int cyc)
75 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
77 PicoCpuS68k.cycles=cyc_do;
78 CycloneRun(&PicoCpuS68k);
79 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
80 #elif defined(EMU_M68K)
81 m68k_set_context(&PicoS68kCPU);
82 SekCycleCntS68k+=m68k_execute(cyc_do);
86 static __inline void check_cd_dma(void)
90 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
92 ddx = Pico_mcd->s68k_regs[4] & 7;
93 if (ddx < 2) return; // invalid
95 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
98 if (ddx == 6) return; // invalid
100 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
103 static __inline void update_chips(void)
105 int counter_timer, int3_set;
106 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
109 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
110 Pico_mcd->m.counter75hz -= counter75hz_lim;
115 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
116 Pico_mcd->m.timer_stopwatch += counter_timer;
117 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
118 Pico_mcd->m.timer_int3 -= counter_timer;
119 if (Pico_mcd->m.timer_int3 < 0) {
120 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
121 dprintf("s68k: timer irq 3");
123 Pico_mcd->m.timer_int3 += int3_set << 16;
125 // is this really what happens if irq3 is masked out?
126 Pico_mcd->m.timer_int3 &= 0xffffff;
131 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
136 static int PicoFrameHintsMCD(void)
138 struct PicoVideo *pv=&Pico.video;
139 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
140 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
141 int skip=PicoSkipFrame || (PicoOpt&0x10);
142 int hint; // Hint counter
145 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
146 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
147 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
149 if(pv->reg[1]&8) lines_vis = 240;
151 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
152 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
158 SekCyclesResetS68k();
159 //z80ExtraCycles = 0;
163 // z80_resetCycles();
165 pv->status&=~0x88; // clear V-Int, come out of vblank
167 hint=pv->reg[10]; // Load H-Int counter
168 //dprintf("-hint: %i", hint);
170 for (y=0;y<lines;y++)
172 Pico.m.scanline=(short)y;
174 // pad delay (for 6 button pads)
176 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
177 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
183 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
185 //dprintf("rhint:old @ %06x", SekPc);
186 hint=pv->reg[10]; // Reload H-Int counter
187 pv->pending_ints|=0x10;
188 if (pv->reg[0]&0x10) SekInterrupt(4);
189 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
190 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
196 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
197 pv->status|=0x88; // V-Int happened, go into vblank
198 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)
199 /*if(Pico.m.z80Run && (PicoOpt&4)) {
200 z80CycleAim+=cycles_z80/2;
201 total_z80+=z80_run(z80CycleAim-total_z80);
202 z80CycleAim-=cycles_z80/2;
204 pv->pending_ints|=0x20;
205 if(pv->reg[1]&0x20) SekInterrupt(6);
206 if(Pico.m.z80Run && (PicoOpt&4)) // ?
208 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
211 // decide if we draw this line
212 #if CAN_HANDLE_240_LINES
213 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
220 sound_timers_and_dac(y);
222 // get samples from sound chips
223 if (y == 224 && PsndOut) {
224 int len = sound_render(0, PsndLen);
225 if (PicoWriteSound) PicoWriteSound(len);
226 // clear sound buffer
231 //dprintf("m68k starting exec @ %06x", SekPc);
232 if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
234 if ((Pico_mcd->m.busreq&3) == 1) { // no busreq/no reset
237 FILE *f = fopen("prg_ram.bin", "wb");
238 for (i = 0; i < 0x80000; i+=2)
240 int tmp = Pico_mcd->prg_ram[i];
241 Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
242 Pico_mcd->prg_ram[i+1] = tmp;
244 fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
248 //dprintf("s68k starting exec @ %06x", SekPcS68k);
249 SekRunS68k(cycles_s68k);
252 if((PicoOpt&4) && Pico.m.z80Run) {
254 z80CycleAim+=cycles_z80;
255 total_z80+=z80_run(z80CycleAim-total_z80);
261 // draw a frame just after vblank in alternative render mode
262 if(!PicoSkipFrame && (PicoOpt&0x10))
269 int PicoFrameMCD(void)