(c) and stuff
[picodrive.git] / Pico / cd / Pico.c
CommitLineData
672ad671 1// (c) Copyright 2007 notaz, All rights reserved.
cc68a136 2
3
4#include "../PicoInt.h"
cc68a136 5
6
76276b0b 7extern unsigned char formatted_bram[4*0x10];
89fa852d 8extern unsigned int s68k_poll_adclk;
9
721cd396 10void (*PicoMCDopenTray)(void) = NULL;
11int (*PicoMCDcloseTray)(void) = NULL;
89fa852d 12
13#define dump_ram(ram,fname) \
14{ \
15 int i, d; \
16 FILE *f; \
17\
18 for (i = 0; i < sizeof(ram); i+=2) { \
19 d = (ram[i]<<8) | ram[i+1]; \
20 *(unsigned short *)(ram+i) = d; \
21 } \
22 f = fopen(fname, "wb"); \
23 if (f) { \
24 fwrite(ram, 1, sizeof(ram), f); \
25 fclose(f); \
26 } \
27 for (i = 0; i < sizeof(ram); i+=2) { \
28 d = (ram[i]<<8) | ram[i+1]; \
29 *(unsigned short *)(ram+i) = d; \
30 } \
31}
cc68a136 32
33
eff55556 34PICO_INTERNAL int PicoInitMCD(void)
cc68a136 35{
36 SekInitS68k();
37 Init_CD_Driver();
38
39 return 0;
40}
41
42
eff55556 43PICO_INTERNAL void PicoExitMCD(void)
cc68a136 44{
45 End_CD_Driver();
89fa852d 46
47 //dump_ram(Pico_mcd->prg_ram, "prg.bin");
48 //dump_ram(Pico.ram, "ram.bin");
cc68a136 49}
50
eff55556 51PICO_INTERNAL int PicoResetMCD(int hard)
cc68a136 52{
51a902ae 53 if (hard) {
a4030801 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);
51a902ae 60 }
61 memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
4f265db7 62 memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
5c69a605 63 memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
51a902ae 64
d1df8786 65 *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
c008977e 66 Pico_mcd->m.state_flags |= 1; // s68k reset pending
672ad671 67 Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
cc68a136 68
cc68a136 69 Reset_CD();
5c69a605 70 LC89510_Reset();
51a902ae 71 gfx_cd_reset();
4ff2d527 72 PicoMemResetCD(1);
3aa1e148 73#ifdef _ASM_CD_MEMORY_C
00bd648e 74 //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
4ff2d527 75#endif
cc68a136 76
6cadc2da 77 // use SRam.data for RAM cart
d6114368 78 if (PicoOpt&0x8000) {
79 if (SRam.data == NULL)
80 SRam.data = calloc(1, 0x12000);
81 }
82 else if (SRam.data != NULL) {
83 free(SRam.data);
84 SRam.data = NULL;
85 }
b542be46 86 SRam.start = SRam.end = 0; // unused
6cadc2da 87
cc68a136 88 return 0;
89}
90
eff55556 91static __inline void SekRunM68k(int cyc)
cc68a136 92{
93 int cyc_do;
94 SekCycleAim+=cyc;
3ec29f01 95 if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;
b5e5172d 96#if defined(EMU_CORE_DEBUG)
97 SekCycleCnt+=CM_compareRun(cyc_do, 0);
98#elif defined(EMU_C68K)
3aa1e148 99 PicoCpuCM68k.cycles=cyc_do;
100 CycloneRun(&PicoCpuCM68k);
101 SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
b837b69b 102#elif defined(EMU_M68K)
3aa1e148 103 m68k_set_context(&PicoCpuMM68k);
cc68a136 104 SekCycleCnt+=m68k_execute(cyc_do);
3aa1e148 105#elif defined(EMU_F68K)
106 g_m68kcontext=&PicoCpuFM68k;
8022f53d 107 SekCycleCnt+=fm68k_emulate(cyc_do, 0);
cc68a136 108#endif
109}
110
111static __inline void SekRunS68k(int cyc)
112{
113 int cyc_do;
114 SekCycleAimS68k+=cyc;
3ec29f01 115 if ((cyc_do=SekCycleAimS68k-SekCycleCntS68k) <= 0) return;
b5e5172d 116#if defined(EMU_CORE_DEBUG)
117 SekCycleCntS68k+=CM_compareRun(cyc_do, 1);
118#elif defined(EMU_C68K)
3aa1e148 119 PicoCpuCS68k.cycles=cyc_do;
120 CycloneRun(&PicoCpuCS68k);
121 SekCycleCntS68k+=cyc_do-PicoCpuCS68k.cycles;
b837b69b 122#elif defined(EMU_M68K)
3aa1e148 123 m68k_set_context(&PicoCpuMS68k);
cc68a136 124 SekCycleCntS68k+=m68k_execute(cyc_do);
3aa1e148 125#elif defined(EMU_F68K)
126 g_m68kcontext=&PicoCpuFS68k;
8022f53d 127 SekCycleCntS68k+=fm68k_emulate(cyc_do, 0);
cc68a136 128#endif
129}
130
7336a99a 131#define PS_STEP_M68K ((488<<16)/20) // ~24
132//#define PS_STEP_S68K 13
68cba51e 133
8022f53d 134#if defined(_ASM_CD_PICO_C)
135extern void SekRunPS(int cyc_m68k, int cyc_s68k);
136#elif defined(EMU_F68K)
137static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
138{
139 SekCycleAim+=cyc_m68k;
140 SekCycleAimS68k+=cyc_s68k;
141 fm68k_emulate(0, 1);
142}
a4030801 143#else
68cba51e 144static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
145{
7336a99a 146 int cycn, cycn_s68k, cyc_do;
68cba51e 147 SekCycleAim+=cyc_m68k;
148 SekCycleAimS68k+=cyc_s68k;
7336a99a 149
150// fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
151// SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
152
153 /* loop 488 downto 0 in steps of PS_STEP */
154 for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
155 {
7336a99a 156 cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
7336a99a 157 if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
68cba51e 158#if defined(EMU_C68K)
3aa1e148 159 PicoCpuCM68k.cycles = cyc_do;
160 CycloneRun(&PicoCpuCM68k);
161 SekCycleCnt += cyc_do - PicoCpuCM68k.cycles;
68cba51e 162#elif defined(EMU_M68K)
3aa1e148 163 m68k_set_context(&PicoCpuMM68k);
164 SekCycleCnt += m68k_execute(cyc_do);
165#elif defined(EMU_F68K)
166 g_m68kcontext = &PicoCpuFM68k;
8022f53d 167 SekCycleCnt += fm68k_emulate(cyc_do, 0);
68cba51e 168#endif
7336a99a 169 }
7336a99a 170 if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
68cba51e 171#if defined(EMU_C68K)
3aa1e148 172 PicoCpuCS68k.cycles = cyc_do;
173 CycloneRun(&PicoCpuCS68k);
174 SekCycleCntS68k += cyc_do - PicoCpuCS68k.cycles;
68cba51e 175#elif defined(EMU_M68K)
3aa1e148 176 m68k_set_context(&PicoCpuMS68k);
177 SekCycleCntS68k += m68k_execute(cyc_do);
178#elif defined(EMU_F68K)
179 g_m68kcontext = &PicoCpuFS68k;
8022f53d 180 SekCycleCntS68k += fm68k_emulate(cyc_do, 0);
68cba51e 181#endif
7336a99a 182 }
68cba51e 183 }
68cba51e 184}
7336a99a 185#endif
68cba51e 186
187
bf098bc5 188static __inline void check_cd_dma(void)
189{
190 int ddx;
191
c459aefd 192 if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
bf098bc5 193
194 ddx = Pico_mcd->s68k_regs[4] & 7;
195 if (ddx < 2) return; // invalid
c459aefd 196 if (ddx < 4) {
197 Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
198 return;
199 }
bf098bc5 200 if (ddx == 6) return; // invalid
201
202 Update_CDC_TRansfer(ddx); // now go and do the actual transfer
203}
204
4f265db7 205static __inline void update_chips(void)
206{
207 int counter_timer, int3_set;
208 int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
209
210 // 75Hz CDC update
211 if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
212 Pico_mcd->m.counter75hz -= counter75hz_lim;
213 Check_CD_Command();
214 }
215
216 // update timers
217 counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
218 Pico_mcd->m.timer_stopwatch += counter_timer;
219 if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
220 Pico_mcd->m.timer_int3 -= counter_timer;
221 if (Pico_mcd->m.timer_int3 < 0) {
222 if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
69996cb7 223 elprintf(EL_INTS, "s68k: timer irq 3");
4f265db7 224 SekInterruptS68k(3);
225 Pico_mcd->m.timer_int3 += int3_set << 16;
226 }
227 // is this really what happens if irq3 is masked out?
228 Pico_mcd->m.timer_int3 &= 0xffffff;
229 }
230 }
231
232 // update gfx chip
233 if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
234 gfx_cd_update();
89fa852d 235
236 // delayed setting of DMNA bit (needed for Silpheed)
237 if (Pico_mcd->m.state_flags & 2) {
238 Pico_mcd->m.state_flags &= ~2;
46969540 239 if (!(Pico_mcd->s68k_regs[3] & 4)) {
240 Pico_mcd->s68k_regs[3] |= 2;
241 Pico_mcd->s68k_regs[3] &= ~1;
89fa852d 242#ifdef USE_POLL_DETECT
46969540 243 if ((s68k_poll_adclk&0xfe) == 2) {
244 SekSetStopS68k(0); s68k_poll_adclk = 0;
245 }
89fa852d 246#endif
46969540 247 }
89fa852d 248 }
4f265db7 249}
250
b837b69b 251
bf5fbbb4 252static __inline void getSamples(int y)
cc68a136 253{
9d917eea 254 int len = PsndRender(0, PsndLen);
bf5fbbb4 255 if (PicoWriteSound) PicoWriteSound(len);
256 // clear sound buffer
9d917eea 257 PsndClear();
bf5fbbb4 258}
cc68a136 259
cc68a136 260
bf5fbbb4 261#define PICO_CD
262#include "../PicoFrameHints.c"
cc68a136 263
264
eff55556 265PICO_INTERNAL int PicoFrameMCD(void)
cc68a136 266{
267 if(!(PicoOpt&0x10))
268 PicoFrameStart();
269
bf5fbbb4 270 PicoFrameHints();
cc68a136 271
272 return 0;
273}
274
275