1 // (c) Copyright 2007 notaz, All rights reserved.
4 #include "../PicoInt.h"
7 extern unsigned char formatted_bram[4*0x10];
8 extern unsigned int s68k_poll_adclk;
10 void (*PicoMCDopenTray)(void) = NULL;
11 int (*PicoMCDcloseTray)(void) = NULL;
13 #define dump_ram(ram,fname) \
18 for (i = 0; i < sizeof(ram); i+=2) { \
19 d = (ram[i]<<8) | ram[i+1]; \
20 *(unsigned short *)(ram+i) = d; \
22 f = fopen(fname, "wb"); \
24 fwrite(ram, 1, sizeof(ram), f); \
27 for (i = 0; i < sizeof(ram); i+=2) { \
28 d = (ram[i]<<8) | ram[i+1]; \
29 *(unsigned short *)(ram+i) = d; \
34 PICO_INTERNAL int PicoInitMCD(void)
43 PICO_INTERNAL void PicoExitMCD(void)
47 //dump_ram(Pico_mcd->prg_ram, "prg.bin");
48 //dump_ram(Pico.ram, "ram.bin");
51 PICO_INTERNAL int PicoResetMCD(int hard)
54 int fmt_size = sizeof(formatted_bram);
55 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
56 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
57 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
58 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
59 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
61 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
62 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
63 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
65 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
66 Pico_mcd->m.state_flags |= 1; // s68k reset pending
67 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
72 #ifdef _ASM_CD_MEMORY_C
74 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
77 // use SRam.data for RAM cart
78 if (SRam.data) free(SRam.data);
81 SRam.data = calloc(1, 0x12000);
86 static __inline void SekRunM68k(int cyc)
90 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
92 PicoCpu.cycles=cyc_do;
94 SekCycleCnt+=cyc_do-PicoCpu.cycles;
95 #elif defined(EMU_M68K)
96 m68k_set_context(&PicoM68kCPU);
97 SekCycleCnt+=m68k_execute(cyc_do);
101 static __inline void SekRunS68k(int cyc)
104 SekCycleAimS68k+=cyc;
105 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
106 #if defined(EMU_C68K)
107 PicoCpuS68k.cycles=cyc_do;
108 CycloneRun(&PicoCpuS68k);
109 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
110 #elif defined(EMU_M68K)
111 m68k_set_context(&PicoS68kCPU);
112 SekCycleCntS68k+=m68k_execute(cyc_do);
116 #define PS_STEP_M68K ((488<<16)/20) // ~24
117 //#define PS_STEP_S68K 13
119 #ifdef _ASM_CD_PICO_C
120 void SekRunPS(int cyc_m68k, int cyc_s68k);
122 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
124 int cycn, cycn_s68k, cyc_do;
126 SekCycleAim+=cyc_m68k;
127 SekCycleAimS68k+=cyc_s68k;
129 // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
130 // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
132 /* loop 488 downto 0 in steps of PS_STEP */
133 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
136 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
137 if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
138 #if defined(EMU_C68K)
139 PicoCpu.cycles = cyc_do;
140 CycloneRun(&PicoCpu);
141 SekCycleCnt += cyc_do - PicoCpu.cycles;
142 #elif defined(EMU_M68K)
143 m68k_set_context(&PicoM68kCPU);
144 SekCycleCnt += (ex = m68k_execute(cyc_do));
147 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
148 #if defined(EMU_C68K)
149 PicoCpuS68k.cycles = cyc_do;
150 CycloneRun(&PicoCpuS68k);
151 SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles;
152 #elif defined(EMU_M68K)
153 m68k_set_context(&PicoS68kCPU);
154 SekCycleCntS68k += (ex = m68k_execute(cyc_do));
162 static __inline void check_cd_dma(void)
166 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
168 ddx = Pico_mcd->s68k_regs[4] & 7;
169 if (ddx < 2) return; // invalid
171 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
174 if (ddx == 6) return; // invalid
176 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
179 static __inline void update_chips(void)
181 int counter_timer, int3_set;
182 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
185 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
186 Pico_mcd->m.counter75hz -= counter75hz_lim;
191 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
192 Pico_mcd->m.timer_stopwatch += counter_timer;
193 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
194 Pico_mcd->m.timer_int3 -= counter_timer;
195 if (Pico_mcd->m.timer_int3 < 0) {
196 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
197 elprintf(EL_INTS, "s68k: timer irq 3");
199 Pico_mcd->m.timer_int3 += int3_set << 16;
201 // is this really what happens if irq3 is masked out?
202 Pico_mcd->m.timer_int3 &= 0xffffff;
207 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
210 // delayed setting of DMNA bit (needed for Silpheed)
211 if (Pico_mcd->m.state_flags & 2) {
212 Pico_mcd->m.state_flags &= ~2;
213 if (!(Pico_mcd->s68k_regs[3] & 4)) {
214 Pico_mcd->s68k_regs[3] |= 2;
215 Pico_mcd->s68k_regs[3] &= ~1;
216 #ifdef USE_POLL_DETECT
217 if ((s68k_poll_adclk&0xfe) == 2) {
218 SekSetStopS68k(0); s68k_poll_adclk = 0;
226 static int PicoFrameHintsMCD(void)
228 struct PicoVideo *pv=&Pico.video;
229 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
230 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
231 int skip=PicoSkipFrame || (PicoOpt&0x10);
232 int hint; // Hint counter
235 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
236 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
237 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
239 if(pv->reg[1]&8) lines_vis = 240;
241 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
242 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
248 SekCyclesResetS68k();
249 //z80ExtraCycles = 0;
253 // z80_resetCycles();
255 pv->status&=~0x88; // clear V-Int, come out of vblank
257 hint=pv->reg[10]; // Load H-Int counter
258 //dprintf("-hint: %i", hint);
260 for (y=0;y<lines;y++)
262 Pico.m.scanline=(short)y;
264 // pad delay (for 6 button pads)
266 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
267 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
273 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
275 //dprintf("rhint:old @ %06x", SekPc);
276 hint=pv->reg[10]; // Reload H-Int counter
277 pv->pending_ints|=0x10;
278 if (pv->reg[0]&0x10) SekInterrupt(4);
279 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
280 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
286 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
287 pv->status|=0x88; // V-Int happened, go into vblank
288 SekRunM68k(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)
289 /*if(Pico.m.z80Run && (PicoOpt&4)) {
290 z80CycleAim+=cycles_z80/2;
291 total_z80+=z80_run(z80CycleAim-total_z80);
292 z80CycleAim-=cycles_z80/2;
294 pv->pending_ints|=0x20;
295 if(pv->reg[1]&0x20) SekInterrupt(6);
296 if(Pico.m.z80Run && (PicoOpt&4)) // ?
298 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
301 // decide if we draw this line
302 #if CAN_HANDLE_240_LINES
303 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
310 sound_timers_and_dac(y);
312 // get samples from sound chips
313 if (y == 224 && PsndOut) {
314 int len = sound_render(0, PsndLen);
315 if (PicoWriteSound) PicoWriteSound(len);
316 // clear sound buffer
321 //dprintf("m68k starting exec @ %06x", SekPc);
322 if (Pico.m.dma_xfers) SekCycleCnt+=CheckDMA();
323 if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
324 SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
326 SekRunM68k(cycles_68k);
327 if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
328 SekRunS68k(cycles_s68k);
331 if ((PicoOpt&4) && Pico.m.z80Run) {
332 if (Pico.m.z80Run & 2) z80CycleAim+=cycles_z80;
334 int cnt = SekCyclesDone() - z80startCycle;
335 cnt = (cnt>>1)-(cnt>>5);
336 //if (cnt > cycles_z80) printf("FIXME: z80 cycles: %i\n", cnt);
337 if (cnt > cycles_z80) cnt = cycles_z80;
341 total_z80+=z80_run(z80CycleAim-total_z80);
347 // draw a frame just after vblank in alternative render mode
348 if (!PicoSkipFrame && (PicoOpt&0x10))
355 PICO_INTERNAL int PicoFrameMCD(void)