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