drc: tune invalidation
[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
69996cb7 114
115 // H-Interrupts:
116 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
117 {
118 hint=pv->reg[10]; // Reload H-Int counter
119 pv->pending_ints|=0x10;
120 if (pv->reg[0]&0x10) {
121 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
122 SekInterrupt(4);
123 }
124 }
125
126 // decide if we draw this line
b6d7ac70 127 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 128 {
b6d7ac70 129 // find the right moment for frame renderer, when display is no longer blanked
130 if ((pv->reg[1]&0x40) || y > 100) {
131 PicoFrameFull();
fad24893 132#ifdef DRAW_FINISH_FUNC
b6d7ac70 133 DRAW_FINISH_FUNC();
fad24893 134#endif
b6d7ac70 135 skip = 1;
fad24893 136 }
137 }
69996cb7 138
69996cb7 139 // get samples from sound chips
7b3f44c6 140 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 141 {
be297089 142 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
4b9c5888 143 PicoSyncZ80(SekCycleCnt);
144 if (ym2612.dacen && PsndDacLine <= y)
145 PsndDoDAC(y);
a8fd6e37 146#ifdef PICO_32X
147 p32x_sync_sh2s(SekCycleCntT + SekCycleCnt);
148#endif
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
69996cb7 186
187 // Last H-Int:
188 if (--hint < 0)
189 {
190 hint=pv->reg[10]; // Reload H-Int counter
191 pv->pending_ints|=0x10;
192 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
193 if (pv->reg[0]&0x10) SekInterrupt(4);
194 }
195
69996cb7 196 pv->status|=0x08; // go into vblank
197 pv->pending_ints|=0x20;
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)
236990cf 203 CPUS_RUN(CYCLES_M68K_VINT_LAG, CYCLES_S68K_VINT_LAG);
4b9c5888 204
69996cb7 205 if (pv->reg[1]&0x20) {
206 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
207 SekInterrupt(6);
208 }
be297089 209 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
4b9c5888 210 PicoSyncZ80(SekCycleCnt);
583ab72c 211 elprintf(EL_INTS, "zint");
69996cb7 212 z80_int();
583ab72c 213 }
69996cb7 214
a8fd6e37 215#ifdef PICO_32X
216 p32x_sync_sh2s(SekCycleCntT + SekCycleCnt);
217 p32x_start_blank();
218#endif
219
69996cb7 220 // get samples from sound chips
7b3f44c6 221 if (y == 224 && PsndOut)
222 {
223 if (ym2612.dacen && PsndDacLine <= y)
224 PsndDoDAC(y);
225 PsndGetSamples(y);
226 }
69996cb7 227
228 // Run scanline:
229 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 230 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
236990cf 231 CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
bf5fbbb4 232
233#ifdef PICO_CD
017512f2 234 update_chips();
235#else
b0677887 236 if (PicoLineHook) PicoLineHook();
bf5fbbb4 237#endif
69996cb7 238
5e128c6d 239 lines = scanlines_total;
240 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
69996cb7 241
b6d7ac70 242 for (y++; y < lines; y++)
69996cb7 243 {
9761a7d0 244 pv->v_counter = Pico.m.scanline = y;
245 if (y >= vcnt_wrap)
246 pv->v_counter -= Pico.m.pal ? 56 : 6;
247 if ((pv->reg[12]&6) == 6)
248 pv->v_counter = (pv->v_counter << 1) | 1;
249 pv->v_counter &= 0xff;
69996cb7 250
251 PAD_DELAY
bf5fbbb4 252#ifdef PICO_CD
253 check_cd_dma();
254#endif
69996cb7 255
69996cb7 256 // Run scanline:
257 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 258 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 259
260#ifdef PICO_CD
261 update_chips();
017512f2 262#else
b0677887 263 if (PicoLineHook) PicoLineHook();
bf5fbbb4 264#endif
69996cb7 265 }
266
4b9c5888 267 // sync z80
be297089 268 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
e53704e6 269 PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
4b9c5888 270 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
271 PsndDoDAC(lines-1);
272
a8fd6e37 273#ifdef PICO_32X
274 p32x_sync_sh2s(SekCycleCntT + SekCycleCnt);
275#endif
e53704e6 276 timers_cycle();
277
69996cb7 278 return 0;
279}
280
281#undef PAD_DELAY
bf5fbbb4 282#undef CPUS_RUN
69996cb7 283