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];
15 extern unsigned int s68k_poll_adclk;
18 #define dump_ram(ram,fname) \
23 for (i = 0; i < sizeof(ram); i+=2) { \
24 d = (ram[i]<<8) | ram[i+1]; \
25 *(unsigned short *)(ram+i) = d; \
27 f = fopen(fname, "wb"); \
29 fwrite(ram, 1, sizeof(ram), f); \
32 for (i = 0; i < sizeof(ram); i+=2) { \
33 d = (ram[i]<<8) | ram[i+1]; \
34 *(unsigned short *)(ram+i) = d; \
48 void PicoExitMCD(void)
52 //dump_ram(Pico_mcd->prg_ram, "prg.bin");
53 //dump_ram(Pico.ram, "ram.bin");
56 int PicoResetMCD(int hard)
59 int fmt_size = sizeof(formatted_bram);
60 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
61 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
62 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
63 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
64 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
66 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
67 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
68 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
70 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
71 Pico_mcd->m.state_flags |= 1; // s68k reset pending
72 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
77 #ifdef _ASM_CD_MEMORY_C
79 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
85 static __inline void SekRun(int cyc)
89 if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
91 PicoCpu.cycles=cyc_do;
93 SekCycleCnt+=cyc_do-PicoCpu.cycles;
94 #elif defined(EMU_M68K)
95 m68k_set_context(&PicoM68kCPU);
96 SekCycleCnt+=m68k_execute(cyc_do);
100 static __inline void SekRunS68k(int cyc)
103 SekCycleAimS68k+=cyc;
104 if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
105 #if defined(EMU_C68K)
106 PicoCpuS68k.cycles=cyc_do;
107 CycloneRun(&PicoCpuS68k);
108 SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
109 #elif defined(EMU_M68K)
110 m68k_set_context(&PicoS68kCPU);
111 SekCycleCntS68k+=m68k_execute(cyc_do);
115 #define PS_STEP_M68K ((488<<16)/20) // ~24
116 //#define PS_STEP_S68K 13
118 #ifdef _ASM_CD_PICO_C
119 void SekRunPS(int cyc_m68k, int cyc_s68k);
121 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
123 int cycn, cycn_s68k, cyc_do;
124 int d_cm = 0, d_cs = 0, ex;
125 SekCycleAim+=cyc_m68k;
126 SekCycleAimS68k+=cyc_s68k;
128 // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
129 // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
131 /* loop 488 downto 0 in steps of PS_STEP */
132 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
135 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
136 //fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k);
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 //fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0;
148 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
149 #if defined(EMU_C68K)
150 PicoCpuS68k.cycles = cyc_do;
151 CycloneRun(&PicoCpuS68k);
152 SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles;
153 #elif defined(EMU_M68K)
154 m68k_set_context(&PicoS68kCPU);
155 SekCycleCntS68k += (ex = m68k_execute(cyc_do));
158 //fprintf(stderr, "%3i\n", ex); d_cs += ex;
161 //fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs);
166 static __inline void check_cd_dma(void)
170 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
172 ddx = Pico_mcd->s68k_regs[4] & 7;
173 if (ddx < 2) return; // invalid
175 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
178 if (ddx == 6) return; // invalid
180 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
183 static __inline void update_chips(void)
185 int counter_timer, int3_set;
186 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
189 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
190 Pico_mcd->m.counter75hz -= counter75hz_lim;
195 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
196 Pico_mcd->m.timer_stopwatch += counter_timer;
197 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
198 Pico_mcd->m.timer_int3 -= counter_timer;
199 if (Pico_mcd->m.timer_int3 < 0) {
200 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
201 dprintf("s68k: timer irq 3");
203 Pico_mcd->m.timer_int3 += int3_set << 16;
205 // is this really what happens if irq3 is masked out?
206 Pico_mcd->m.timer_int3 &= 0xffffff;
211 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
214 // delayed setting of DMNA bit (needed for Silpheed)
215 if (Pico_mcd->m.state_flags & 2) {
216 Pico_mcd->m.state_flags &= ~2;
217 Pico_mcd->s68k_regs[3] |= 2;
218 Pico_mcd->s68k_regs[3] &= ~1;
219 #ifdef USE_POLL_DETECT
220 if ((s68k_poll_adclk&0xfe) == 2) {
221 SekSetStopS68k(0); s68k_poll_adclk = 0;
228 static int PicoFrameHintsMCD(void)
230 struct PicoVideo *pv=&Pico.video;
231 int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
232 const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
233 int skip=PicoSkipFrame || (PicoOpt&0x10);
234 int hint; // Hint counter
237 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
238 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
239 lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
241 if(pv->reg[1]&8) lines_vis = 240;
243 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
244 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
250 SekCyclesResetS68k();
251 //z80ExtraCycles = 0;
255 // z80_resetCycles();
257 pv->status&=~0x88; // clear V-Int, come out of vblank
259 hint=pv->reg[10]; // Load H-Int counter
260 //dprintf("-hint: %i", hint);
262 for (y=0;y<lines;y++)
264 Pico.m.scanline=(short)y;
266 // pad delay (for 6 button pads)
268 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
269 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
275 if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
277 //dprintf("rhint:old @ %06x", SekPc);
278 hint=pv->reg[10]; // Reload H-Int counter
279 pv->pending_ints|=0x10;
280 if (pv->reg[0]&0x10) SekInterrupt(4);
281 //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
282 //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
288 //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
289 pv->status|=0x88; // V-Int happened, go into vblank
290 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)
291 /*if(Pico.m.z80Run && (PicoOpt&4)) {
292 z80CycleAim+=cycles_z80/2;
293 total_z80+=z80_run(z80CycleAim-total_z80);
294 z80CycleAim-=cycles_z80/2;
296 pv->pending_ints|=0x20;
297 if(pv->reg[1]&0x20) SekInterrupt(6);
298 if(Pico.m.z80Run && (PicoOpt&4)) // ?
300 //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
303 // decide if we draw this line
304 #if CAN_HANDLE_240_LINES
305 if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
312 sound_timers_and_dac(y);
314 // get samples from sound chips
315 if (y == 224 && PsndOut) {
316 int len = sound_render(0, PsndLen);
317 if (PicoWriteSound) PicoWriteSound(len);
318 // clear sound buffer
323 //dprintf("m68k starting exec @ %06x", SekPc);
324 if (Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
325 if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
326 SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
329 if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
330 SekRunS68k(cycles_s68k);
333 if ((PicoOpt&4) && Pico.m.z80Run) {
335 z80CycleAim+=cycles_z80;
336 total_z80+=z80_run(z80CycleAim-total_z80);
342 // draw a frame just after vblank in alternative render mode
343 if (!PicoSkipFrame && (PicoOpt&0x10))
350 int PicoFrameMCD(void)