| 1 | // This is part of Pico Library |
| 2 | |
| 3 | // (c) Copyright 2004 Dave, All rights reserved. |
| 4 | // (c) Copyright 2007 notaz, All rights reserved. |
| 5 | // Free for non-commercial use. |
| 6 | |
| 7 | // For commercial use, separate licencing terms must be obtained. |
| 8 | |
| 9 | |
| 10 | #include "../PicoInt.h" |
| 11 | #include "../sound/sound.h" |
| 12 | |
| 13 | |
| 14 | extern unsigned char formatted_bram[4*0x10]; |
| 15 | |
| 16 | |
| 17 | int PicoInitMCD(void) |
| 18 | { |
| 19 | SekInitS68k(); |
| 20 | Init_CD_Driver(); |
| 21 | |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | void PicoExitMCD(void) |
| 27 | { |
| 28 | End_CD_Driver(); |
| 29 | } |
| 30 | |
| 31 | int PicoResetMCD(int hard) |
| 32 | { |
| 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)); |
| 36 | if (hard) { |
| 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); |
| 40 | } |
| 41 | memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs)); |
| 42 | memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm)); |
| 43 | |
| 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; |
| 48 | |
| 49 | LC89510_Reset(); |
| 50 | Reset_CD(); |
| 51 | gfx_cd_reset(); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static __inline void SekRun(int cyc) |
| 57 | { |
| 58 | int cyc_do; |
| 59 | SekCycleAim+=cyc; |
| 60 | if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return; |
| 61 | #if defined(EMU_C68K) |
| 62 | PicoCpu.cycles=cyc_do; |
| 63 | CycloneRun(&PicoCpu); |
| 64 | SekCycleCnt+=cyc_do-PicoCpu.cycles; |
| 65 | #elif defined(EMU_M68K) |
| 66 | m68k_set_context(&PicoM68kCPU); |
| 67 | SekCycleCnt+=m68k_execute(cyc_do); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | static __inline void SekRunS68k(int cyc) |
| 72 | { |
| 73 | int cyc_do; |
| 74 | SekCycleAimS68k+=cyc; |
| 75 | if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return; |
| 76 | #if defined(EMU_C68K) |
| 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); |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | #define PS_STEP_M68K ((488<<16)/20) // ~24 |
| 87 | //#define PS_STEP_S68K 13 |
| 88 | |
| 89 | #ifndef _ASM_CD_PICO_C |
| 90 | static __inline void SekRunPS(int cyc_m68k, int cyc_s68k) |
| 91 | { |
| 92 | int cycn, cycn_s68k, cyc_do; |
| 93 | int d_cm = 0, d_cs = 0, ex; |
| 94 | SekCycleAim+=cyc_m68k; |
| 95 | SekCycleAimS68k+=cyc_s68k; |
| 96 | |
| 97 | // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k, |
| 98 | // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline); |
| 99 | |
| 100 | /* loop 488 downto 0 in steps of PS_STEP */ |
| 101 | for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K) |
| 102 | { |
| 103 | ex = 0; |
| 104 | cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16; |
| 105 | //fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k); |
| 106 | if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) { |
| 107 | #if defined(EMU_C68K) |
| 108 | PicoCpu.cycles = cyc_do; |
| 109 | CycloneRun(&PicoCpu); |
| 110 | SekCycleCnt += cyc_do - PicoCpu.cycles; |
| 111 | #elif defined(EMU_M68K) |
| 112 | m68k_set_context(&PicoM68kCPU); |
| 113 | SekCycleCnt += (ex = m68k_execute(cyc_do)); |
| 114 | #endif |
| 115 | } |
| 116 | //fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0; |
| 117 | if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) { |
| 118 | #if defined(EMU_C68K) |
| 119 | PicoCpuS68k.cycles = cyc_do; |
| 120 | CycloneRun(&PicoCpuS68k); |
| 121 | SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles; |
| 122 | #elif defined(EMU_M68K) |
| 123 | m68k_set_context(&PicoS68kCPU); |
| 124 | SekCycleCntS68k += (ex = m68k_execute(cyc_do)); |
| 125 | #endif |
| 126 | } |
| 127 | //fprintf(stderr, "%3i\n", ex); d_cs += ex; |
| 128 | } |
| 129 | |
| 130 | //fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs); |
| 131 | } |
| 132 | #else |
| 133 | void SekRunPS(int cyc_m68k, int cyc_s68k); |
| 134 | #endif |
| 135 | |
| 136 | |
| 137 | static __inline void check_cd_dma(void) |
| 138 | { |
| 139 | int ddx; |
| 140 | |
| 141 | if (!(Pico_mcd->scd.Status_CDC & 0x08)) return; |
| 142 | |
| 143 | ddx = Pico_mcd->s68k_regs[4] & 7; |
| 144 | if (ddx < 2) return; // invalid |
| 145 | if (ddx < 4) { |
| 146 | Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port |
| 147 | return; |
| 148 | } |
| 149 | if (ddx == 6) return; // invalid |
| 150 | |
| 151 | Update_CDC_TRansfer(ddx); // now go and do the actual transfer |
| 152 | } |
| 153 | |
| 154 | static __inline void update_chips(void) |
| 155 | { |
| 156 | int counter_timer, int3_set; |
| 157 | int counter75hz_lim = Pico.m.pal ? 2080 : 2096; |
| 158 | |
| 159 | // 75Hz CDC update |
| 160 | if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) { |
| 161 | Pico_mcd->m.counter75hz -= counter75hz_lim; |
| 162 | Check_CD_Command(); |
| 163 | } |
| 164 | |
| 165 | // update timers |
| 166 | counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708; |
| 167 | Pico_mcd->m.timer_stopwatch += counter_timer; |
| 168 | if ((int3_set = Pico_mcd->s68k_regs[0x31])) { |
| 169 | Pico_mcd->m.timer_int3 -= counter_timer; |
| 170 | if (Pico_mcd->m.timer_int3 < 0) { |
| 171 | if (Pico_mcd->s68k_regs[0x33] & (1<<3)) { |
| 172 | dprintf("s68k: timer irq 3"); |
| 173 | SekInterruptS68k(3); |
| 174 | Pico_mcd->m.timer_int3 += int3_set << 16; |
| 175 | } |
| 176 | // is this really what happens if irq3 is masked out? |
| 177 | Pico_mcd->m.timer_int3 &= 0xffffff; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // update gfx chip |
| 182 | if (Pico_mcd->rot_comp.Reg_58 & 0x8000) |
| 183 | gfx_cd_update(); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static int PicoFrameHintsMCD(void) |
| 188 | { |
| 189 | struct PicoVideo *pv=&Pico.video; |
| 190 | int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample; |
| 191 | const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values |
| 192 | int skip=PicoSkipFrame || (PicoOpt&0x10); |
| 193 | int hint; // Hint counter |
| 194 | |
| 195 | if(Pico.m.pal) { // |
| 196 | //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488) |
| 197 | //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228 |
| 198 | lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well |
| 199 | line_sample = 68; |
| 200 | if(pv->reg[1]&8) lines_vis = 240; |
| 201 | } else { |
| 202 | //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488 |
| 203 | //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228 |
| 204 | lines = 262; |
| 205 | line_sample = 93; |
| 206 | } |
| 207 | |
| 208 | SekCyclesReset(); |
| 209 | SekCyclesResetS68k(); |
| 210 | //z80ExtraCycles = 0; |
| 211 | |
| 212 | if(PicoOpt&4) |
| 213 | z80CycleAim = 0; |
| 214 | // z80_resetCycles(); |
| 215 | |
| 216 | pv->status&=~0x88; // clear V-Int, come out of vblank |
| 217 | |
| 218 | hint=pv->reg[10]; // Load H-Int counter |
| 219 | //dprintf("-hint: %i", hint); |
| 220 | |
| 221 | for (y=0;y<lines;y++) |
| 222 | { |
| 223 | Pico.m.scanline=(short)y; |
| 224 | |
| 225 | // pad delay (for 6 button pads) |
| 226 | if(PicoOpt&0x20) { |
| 227 | if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; |
| 228 | if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; |
| 229 | } |
| 230 | |
| 231 | check_cd_dma(); |
| 232 | |
| 233 | // H-Interrupts: |
| 234 | if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe |
| 235 | { |
| 236 | //dprintf("rhint:old @ %06x", SekPc); |
| 237 | hint=pv->reg[10]; // Reload H-Int counter |
| 238 | pv->pending_ints|=0x10; |
| 239 | if (pv->reg[0]&0x10) SekInterrupt(4); |
| 240 | //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt); |
| 241 | //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86)); |
| 242 | } |
| 243 | |
| 244 | // V-Interrupt: |
| 245 | if (y == lines_vis) |
| 246 | { |
| 247 | //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt); |
| 248 | pv->status|=0x88; // V-Int happened, go into vblank |
| 249 | 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) |
| 250 | /*if(Pico.m.z80Run && (PicoOpt&4)) { |
| 251 | z80CycleAim+=cycles_z80/2; |
| 252 | total_z80+=z80_run(z80CycleAim-total_z80); |
| 253 | z80CycleAim-=cycles_z80/2; |
| 254 | }*/ |
| 255 | pv->pending_ints|=0x20; |
| 256 | if(pv->reg[1]&0x20) SekInterrupt(6); |
| 257 | if(Pico.m.z80Run && (PicoOpt&4)) // ? |
| 258 | z80_int(); |
| 259 | //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0)); |
| 260 | } |
| 261 | |
| 262 | // decide if we draw this line |
| 263 | #if CAN_HANDLE_240_LINES |
| 264 | if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) ) |
| 265 | #else |
| 266 | if(!skip && y<224) |
| 267 | #endif |
| 268 | PicoLine(y); |
| 269 | |
| 270 | if(PicoOpt&1) |
| 271 | sound_timers_and_dac(y); |
| 272 | |
| 273 | // get samples from sound chips |
| 274 | if (y == 224 && PsndOut) { |
| 275 | int len = sound_render(0, PsndLen); |
| 276 | if (PicoWriteSound) PicoWriteSound(len); |
| 277 | // clear sound buffer |
| 278 | sound_clear(); |
| 279 | } |
| 280 | |
| 281 | // Run scanline: |
| 282 | //dprintf("m68k starting exec @ %06x", SekPc); |
| 283 | if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA(); |
| 284 | if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) { |
| 285 | SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync" |
| 286 | } else { |
| 287 | SekRun(cycles_68k); |
| 288 | if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset |
| 289 | SekRunS68k(cycles_s68k); |
| 290 | } |
| 291 | |
| 292 | if((PicoOpt&4) && Pico.m.z80Run) { |
| 293 | Pico.m.z80Run|=2; |
| 294 | z80CycleAim+=cycles_z80; |
| 295 | total_z80+=z80_run(z80CycleAim-total_z80); |
| 296 | } |
| 297 | |
| 298 | update_chips(); |
| 299 | } |
| 300 | |
| 301 | // draw a frame just after vblank in alternative render mode |
| 302 | if(!PicoSkipFrame && (PicoOpt&0x10)) |
| 303 | PicoFrameFull(); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | int PicoFrameMCD(void) |
| 310 | { |
| 311 | if(!(PicoOpt&0x10)) |
| 312 | PicoFrameStart(); |
| 313 | |
| 314 | PicoFrameHintsMCD(); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |