platform ps2, handle audio similar to psp
[picodrive.git] / pico_cmn.c
... / ...
CommitLineData
1/*
2 * common code for base/cd/32x
3 * (C) notaz, 2007-2009,2013
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
8
9#define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC
10#define CYCLES_M68K_VINT_LAG 68
11#define CYCLES_M68K_ASD 148
12
13// pad delay (for 6 button pads)
14#define PAD_DELAY() { \
15 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
16 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
17}
18
19// CPUS_RUN
20#ifndef CPUS_RUN
21#define CPUS_RUN(m68k_cycles) \
22 SekRunM68k(m68k_cycles)
23#endif
24
25// sync m68k to SekCycleAim
26static void SekSyncM68k(void)
27{
28 int cyc_do;
29 pprof_start(m68k);
30 pevt_log_m68k_o(EVT_RUN_START);
31
32 while ((cyc_do = SekCycleAim - SekCycleCnt) > 0) {
33 SekCycleCnt += cyc_do;
34
35#if defined(EMU_C68K)
36 PicoCpuCM68k.cycles = cyc_do;
37 CycloneRun(&PicoCpuCM68k);
38 SekCycleCnt -= PicoCpuCM68k.cycles;
39#elif defined(EMU_M68K)
40 SekCycleCnt += m68k_execute(cyc_do) - cyc_do;
41#elif defined(EMU_F68K)
42 SekCycleCnt += fm68k_emulate(cyc_do, 0) - cyc_do;
43#endif
44 }
45
46 SekCyclesLeft = 0;
47
48 SekTrace(0);
49 pevt_log_m68k_o(EVT_RUN_END);
50 pprof_end(m68k);
51}
52
53static inline void SekRunM68k(int cyc)
54{
55 SekCycleAim += cyc;
56 SekSyncM68k();
57}
58
59static int PicoFrameHints(void)
60{
61 struct PicoVideo *pv=&Pico.video;
62 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
63 unsigned int cycles;
64 int hint; // Hint counter
65
66 pevt_log_m68k_o(EVT_FRAME_START);
67 pv->v_counter = Pico.m.scanline = 0;
68
69 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
70 // draw a frame just after vblank in alternative render mode
71 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
72 PicoFrameFull();
73#ifdef DRAW_FINISH_FUNC
74 DRAW_FINISH_FUNC();
75#endif
76 skip = 1;
77 }
78 else skip=PicoSkipFrame;
79
80 if (Pico.m.pal) {
81 line_sample = 68;
82 if (pv->reg[1]&8) lines_vis = 240;
83 } else {
84 line_sample = 93;
85 }
86
87 z80_resetCycles();
88 PsndDacLine = 0;
89 emustatus &= ~1;
90
91 pv->status&=~0x88; // clear V-Int, come out of vblank
92
93 hint=pv->reg[10]; // Load H-Int counter
94 //dprintf("-hint: %i", hint);
95
96 // This is to make active scan longer (needed for Double Dragon 2, mainly)
97 CPUS_RUN(CYCLES_M68K_ASD);
98
99 for (y = 0; y < lines_vis; y++)
100 {
101 pv->v_counter = Pico.m.scanline = y;
102 if ((pv->reg[12]&6) == 6) { // interlace mode 2
103 pv->v_counter <<= 1;
104 pv->v_counter |= pv->v_counter >> 8;
105 pv->v_counter &= 0xff;
106 }
107
108 // VDP FIFO
109 pv->lwrite_cnt -= 12;
110 if (pv->lwrite_cnt <= 0) {
111 pv->lwrite_cnt=0;
112 Pico.video.status|=0x200;
113 }
114
115 PAD_DELAY();
116
117 // H-Interrupts:
118 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
119 {
120 hint=pv->reg[10]; // Reload H-Int counter
121 pv->pending_ints|=0x10;
122 if (pv->reg[0]&0x10) {
123 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCyclesDone());
124 SekInterrupt(4);
125 }
126 }
127
128 // decide if we draw this line
129 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
130 {
131 // find the right moment for frame renderer, when display is no longer blanked
132 if ((pv->reg[1]&0x40) || y > 100) {
133 PicoFrameFull();
134#ifdef DRAW_FINISH_FUNC
135 DRAW_FINISH_FUNC();
136#endif
137 skip = 1;
138 }
139 }
140
141 // get samples from sound chips
142 if ((y == 224 || y == line_sample) && PsndOut)
143 {
144 cycles = SekCyclesDone();
145
146 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
147 PicoSyncZ80(cycles);
148 if (ym2612.dacen && PsndDacLine <= y)
149 PsndDoDAC(y);
150#ifdef PICO_CD
151 if (PicoAHW & PAHW_MCD)
152 pcd_sync_s68k(cycles, 0);
153#endif
154#ifdef PICO_32X
155 p32x_sync_sh2s(cycles);
156#endif
157 PsndGetSamples(y);
158 }
159
160 // Run scanline:
161 line_base_cycles = SekCyclesDone();
162 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
163 CPUS_RUN(CYCLES_M68K_LINE);
164
165 if (PicoLineHook) PicoLineHook();
166 pevt_log_m68k_o(EVT_NEXT_LINE);
167 }
168
169 if (!skip)
170 {
171 if (Pico.est.DrawScanline < y)
172 PicoDrawSync(y - 1, 0);
173#ifdef DRAW_FINISH_FUNC
174 DRAW_FINISH_FUNC();
175#endif
176 }
177
178 // V-int line (224 or 240)
179 Pico.m.scanline = y;
180 pv->v_counter = 0xe0; // bad for 240 mode
181 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
182
183 // VDP FIFO
184 pv->lwrite_cnt=0;
185 Pico.video.status|=0x200;
186
187 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
188 PAD_DELAY();
189
190 // Last H-Int:
191 if (--hint < 0)
192 {
193 hint=pv->reg[10]; // Reload H-Int counter
194 pv->pending_ints|=0x10;
195 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCyclesDone());
196 if (pv->reg[0]&0x10) SekInterrupt(4);
197 }
198
199 pv->status|=0x08; // go into vblank
200 pv->pending_ints|=0x20;
201
202 // the following SekRun is there for several reasons:
203 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
204 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
205 // also delay between last H-int and V-int (Golden Axe 3)
206 line_base_cycles = SekCyclesDone();
207 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
208 CPUS_RUN(CYCLES_M68K_VINT_LAG);
209
210 if (pv->reg[1]&0x20) {
211 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCyclesDone());
212 SekInterrupt(6);
213 }
214
215 cycles = SekCyclesDone();
216 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
217 PicoSyncZ80(cycles);
218 elprintf(EL_INTS, "zint");
219 z80_int();
220 }
221
222#ifdef PICO_CD
223 if (PicoAHW & PAHW_MCD)
224 pcd_sync_s68k(cycles, 0);
225#endif
226#ifdef PICO_32X
227 p32x_sync_sh2s(cycles);
228 p32x_start_blank();
229#endif
230
231 // get samples from sound chips
232 if (y == 224 && PsndOut)
233 {
234 if (ym2612.dacen && PsndDacLine <= y)
235 PsndDoDAC(y);
236 PsndGetSamples(y);
237 }
238
239 // Run scanline:
240 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD);
241
242 if (PicoLineHook) PicoLineHook();
243 pevt_log_m68k_o(EVT_NEXT_LINE);
244
245 lines = scanlines_total;
246 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
247
248 for (y++; y < lines; y++)
249 {
250 pv->v_counter = Pico.m.scanline = y;
251 if (y >= vcnt_wrap)
252 pv->v_counter -= Pico.m.pal ? 56 : 6;
253 if ((pv->reg[12]&6) == 6)
254 pv->v_counter = (pv->v_counter << 1) | 1;
255 pv->v_counter &= 0xff;
256
257 PAD_DELAY();
258
259 // Run scanline:
260 line_base_cycles = SekCyclesDone();
261 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
262 CPUS_RUN(CYCLES_M68K_LINE);
263
264 if (PicoLineHook) PicoLineHook();
265 pevt_log_m68k_o(EVT_NEXT_LINE);
266 }
267
268 // sync cpus
269 cycles = SekCyclesDone();
270 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
271 PicoSyncZ80(cycles);
272 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
273 PsndDoDAC(lines-1);
274
275#ifdef PICO_CD
276 if (PicoAHW & PAHW_MCD)
277 pcd_sync_s68k(cycles, 0);
278#endif
279#ifdef PICO_32X
280 p32x_sync_sh2s(cycles);
281#endif
282 timers_cycle();
283
284 return 0;
285}
286
287#undef PAD_DELAY
288#undef CPUS_RUN
289
290// vim:shiftwidth=2:ts=2:expandtab