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