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 void PicoPowerMCD(void)
53 int fmt_size = sizeof(formatted_bram);
54 memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
55 memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
56 memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
57 memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
58 memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
61 PICO_INTERNAL int PicoResetMCD(void)
63 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
64 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
65 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
67 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
68 Pico_mcd->m.state_flags |= 1; // s68k reset pending
69 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
75 #ifdef _ASM_CD_MEMORY_C
76 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
79 // use SRam.data for RAM cart
80 if (PicoOpt&POPT_EN_MCD_RAMCART) {
81 if (SRam.data == NULL)
82 SRam.data = calloc(1, 0x12000);
84 else if (SRam.data != NULL) {
88 SRam.start = SRam.end = 0; // unused
93 static __inline void SekRunM68k(int cyc)
97 if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;
98 #if defined(EMU_CORE_DEBUG)
99 SekCycleCnt+=CM_compareRun(cyc_do, 0);
100 #elif defined(EMU_C68K)
101 PicoCpuCM68k.cycles=cyc_do;
102 CycloneRun(&PicoCpuCM68k);
103 SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
104 #elif defined(EMU_M68K)
105 m68k_set_context(&PicoCpuMM68k);
106 SekCycleCnt+=m68k_execute(cyc_do);
107 #elif defined(EMU_F68K)
108 g_m68kcontext=&PicoCpuFM68k;
109 SekCycleCnt+=fm68k_emulate(cyc_do, 0);
113 static __inline void SekRunS68k(int cyc)
116 SekCycleAimS68k+=cyc;
117 if ((cyc_do=SekCycleAimS68k-SekCycleCntS68k) <= 0) return;
118 #if defined(EMU_CORE_DEBUG)
119 SekCycleCntS68k+=CM_compareRun(cyc_do, 1);
120 #elif defined(EMU_C68K)
121 PicoCpuCS68k.cycles=cyc_do;
122 CycloneRun(&PicoCpuCS68k);
123 SekCycleCntS68k+=cyc_do-PicoCpuCS68k.cycles;
124 #elif defined(EMU_M68K)
125 m68k_set_context(&PicoCpuMS68k);
126 SekCycleCntS68k+=m68k_execute(cyc_do);
127 #elif defined(EMU_F68K)
128 g_m68kcontext=&PicoCpuFS68k;
129 SekCycleCntS68k+=fm68k_emulate(cyc_do, 0);
133 #define PS_STEP_M68K ((488<<16)/20) // ~24
134 //#define PS_STEP_S68K 13
136 #if defined(_ASM_CD_PICO_C)
137 extern void SekRunPS(int cyc_m68k, int cyc_s68k);
138 #elif defined(EMU_F68K)
139 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
141 SekCycleAim+=cyc_m68k;
142 SekCycleAimS68k+=cyc_s68k;
146 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
148 int cycn, cycn_s68k, cyc_do;
149 SekCycleAim+=cyc_m68k;
150 SekCycleAimS68k+=cyc_s68k;
152 // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
153 // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
155 /* loop 488 downto 0 in steps of PS_STEP */
156 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
158 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
159 if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
160 #if defined(EMU_C68K)
161 PicoCpuCM68k.cycles = cyc_do;
162 CycloneRun(&PicoCpuCM68k);
163 SekCycleCnt += cyc_do - PicoCpuCM68k.cycles;
164 #elif defined(EMU_M68K)
165 m68k_set_context(&PicoCpuMM68k);
166 SekCycleCnt += m68k_execute(cyc_do);
167 #elif defined(EMU_F68K)
168 g_m68kcontext = &PicoCpuFM68k;
169 SekCycleCnt += fm68k_emulate(cyc_do, 0);
172 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
173 #if defined(EMU_C68K)
174 PicoCpuCS68k.cycles = cyc_do;
175 CycloneRun(&PicoCpuCS68k);
176 SekCycleCntS68k += cyc_do - PicoCpuCS68k.cycles;
177 #elif defined(EMU_M68K)
178 m68k_set_context(&PicoCpuMS68k);
179 SekCycleCntS68k += m68k_execute(cyc_do);
180 #elif defined(EMU_F68K)
181 g_m68kcontext = &PicoCpuFS68k;
182 SekCycleCntS68k += fm68k_emulate(cyc_do, 0);
190 static __inline void check_cd_dma(void)
194 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
196 ddx = Pico_mcd->s68k_regs[4] & 7;
197 if (ddx < 2) return; // invalid
199 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
202 if (ddx == 6) return; // invalid
204 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
207 static __inline void update_chips(void)
209 int counter_timer, int3_set;
210 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
213 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
214 Pico_mcd->m.counter75hz -= counter75hz_lim;
219 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
220 Pico_mcd->m.timer_stopwatch += counter_timer;
221 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
222 Pico_mcd->m.timer_int3 -= counter_timer;
223 if (Pico_mcd->m.timer_int3 < 0) {
224 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
225 elprintf(EL_INTS, "s68k: timer irq 3");
227 Pico_mcd->m.timer_int3 += int3_set << 16;
229 // is this really what happens if irq3 is masked out?
230 Pico_mcd->m.timer_int3 &= 0xffffff;
235 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
238 // delayed setting of DMNA bit (needed for Silpheed)
239 if (Pico_mcd->m.state_flags & 2) {
240 Pico_mcd->m.state_flags &= ~2;
241 if (!(Pico_mcd->s68k_regs[3] & 4)) {
242 Pico_mcd->s68k_regs[3] |= 2;
243 Pico_mcd->s68k_regs[3] &= ~1;
244 #ifdef USE_POLL_DETECT
245 if ((s68k_poll_adclk&0xfe) == 2) {
246 SekSetStopS68k(0); s68k_poll_adclk = 0;
254 static __inline void getSamples(int y)
256 int len = PsndRender(0, PsndLen);
257 if (PicoWriteSound) PicoWriteSound(len);
258 // clear sound buffer
264 #include "../PicoFrameHints.c"
267 PICO_INTERNAL int PicoFrameMCD(void)
269 if (!(PicoOpt&POPT_ALT_RENDERER))