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