mcd: text shows up
[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 static int counter75hz = 0; // TODO: move 2 context
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   // clear everything except BIOS
34   memset(Pico_mcd->prg_ram, 0, sizeof(mcd_state) - sizeof(Pico_mcd->bios));
35   PicoMCD |= 2; // s68k reset pending
36   Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
37   counter75hz = 0;
38
39   LC89510_Reset();
40   Reset_CD();
41
42   return 0;
43 }
44
45 static __inline void SekRun(int cyc)
46 {
47   int cyc_do;
48   SekCycleAim+=cyc;
49   if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
50 #if defined(EMU_M68K)
51   m68k_set_context(&PicoM68kCPU);
52   SekCycleCnt+=m68k_execute(cyc_do);
53 #endif
54 }
55
56 static __inline void SekRunS68k(int cyc)
57 {
58   int cyc_do;
59   SekCycleAimS68k+=cyc;
60   if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
61 #if defined(EMU_M68K)
62   m68k_set_context(&PicoS68kCPU);
63   SekCycleCntS68k+=m68k_execute(cyc_do);
64 #endif
65 }
66
67 // TODO: tidy
68 extern unsigned char m68k_regs[0x40];
69 extern unsigned char s68k_regs[0x200];
70
71 // Accurate but slower frame which does hints
72 static int PicoFrameHintsMCD(void)
73 {
74   struct PicoVideo *pv=&Pico.video;
75   int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample,counter75hz_lim;
76   const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
77   int skip=PicoSkipFrame || (PicoOpt&0x10);
78   int hint; // Hint counter
79
80   if(Pico.m.pal) { //
81     //cycles_68k = (int) ((double) OSC_PAL  /  7 / 50 / 312 + 0.4); // should compile to a constant (488)
82     //cycles_z80 = (int) ((double) OSC_PAL  / 15 / 50 / 312 + 0.4); // 228
83     lines  = 312;    // Steve Snake says there are 313 lines, but this seems to also work well
84     line_sample = 68;
85     counter75hz_lim = 2080;
86     if(pv->reg[1]&8) lines_vis = 240;
87   } else {
88     //cycles_68k = (int) ((double) OSC_NTSC /  7 / 60 / 262 + 0.4); // 488
89     //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
90     lines  = 262;
91     counter75hz_lim = 2096;
92     line_sample = 93;
93   }
94
95   SekCyclesReset();
96   SekCyclesResetS68k();
97   //z80ExtraCycles = 0;
98
99   if(PicoOpt&4)
100     z80CycleAim = 0;
101 //    z80_resetCycles();
102
103   pv->status&=~0x88; // clear V-Int, come out of vblank
104
105   hint=pv->reg[10]; // Load H-Int counter
106   //dprintf("-hint: %i", hint);
107
108   for (y=0;y<lines;y++)
109   {
110     Pico.m.scanline=(short)y;
111
112     // pad delay (for 6 button pads)
113     if(PicoOpt&0x20) {
114       if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
115       if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
116     }
117
118     // H-Interrupts:
119     if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
120     {
121       //dprintf("rhint:old @ %06x", SekPc);
122       hint=pv->reg[10]; // Reload H-Int counter
123       pv->pending_ints|=0x10;
124       if (pv->reg[0]&0x10) SekInterrupt(4);
125       //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
126       //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
127     }
128
129     // V-Interrupt:
130     if (y == lines_vis)
131     {
132       //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
133       pv->status|=0x88; // V-Int happened, go into vblank
134       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)
135       /*if(Pico.m.z80Run && (PicoOpt&4)) {
136         z80CycleAim+=cycles_z80/2;
137         total_z80+=z80_run(z80CycleAim-total_z80);
138         z80CycleAim-=cycles_z80/2;
139       }*/
140       pv->pending_ints|=0x20;
141       if(pv->reg[1]&0x20) SekInterrupt(6);
142       if(Pico.m.z80Run && (PicoOpt&4)) // ?
143         z80_int();
144       //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
145     }
146
147     // decide if we draw this line
148 #if CAN_HANDLE_240_LINES
149     if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
150 #else
151     if(!skip && y<224)
152 #endif
153       PicoLine(y);
154
155     if(PicoOpt&1)
156       sound_timers_and_dac(y);
157
158     // get samples from sound chips
159     if(y == 32 && PsndOut)
160       emustatus &= ~1;
161     else if((y == 224 || y == line_sample) && PsndOut)
162       ;//getSamples(y);
163
164     // Run scanline:
165       //dprintf("m68k starting exec @ %06x", SekPc);
166     if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
167     SekRun(cycles_68k);
168     if ((Pico_mcd->m68k_regs[1]&3) == 1) { // no busreq/no reset
169 #if 0
170             int i;
171             FILE *f = fopen("prg_ram.bin", "wb");
172             for (i = 0; i < 0x80000; i+=2)
173             {
174                     int tmp = Pico_mcd->prg_ram[i];
175                     Pico_mcd->prg_ram[i] = Pico_mcd->prg_ram[i+1];
176                     Pico_mcd->prg_ram[i+1] = tmp;
177             }
178             fwrite(Pico_mcd->prg_ram, 1, 0x80000, f);
179             fclose(f);
180             exit(1);
181 #endif
182       //dprintf("s68k starting exec @ %06x", SekPcS68k);
183       SekRunS68k(cycles_s68k);
184     }
185
186     if((PicoOpt&4) && Pico.m.z80Run) {
187       Pico.m.z80Run|=2;
188       z80CycleAim+=cycles_z80;
189       total_z80+=z80_run(z80CycleAim-total_z80);
190     }
191
192     if ((counter75hz+=10) >= counter75hz_lim) {
193       counter75hz -= counter75hz_lim;
194       Check_CD_Command();
195     }
196   }
197
198   // draw a frame just after vblank in alternative render mode
199   if(!PicoSkipFrame && (PicoOpt&0x10))
200     PicoFrameFull();
201
202   return 0;
203 }
204
205
206 int PicoFrameMCD(void)
207 {
208   if(!(PicoOpt&0x10))
209     PicoFrameStart();
210
211   PicoFrameHintsMCD();
212
213   return 0;
214 }
215
216