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