32x: memhandler improvements
[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);
19886062 33 pevt_log_m68k_o(EVT_RUN_START);
ed4402a7 34
35 SekCycleAim+=cyc;
19886062 36 if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0)
37 goto out;
38
ed4402a7 39#if defined(EMU_CORE_DEBUG)
40 // this means we do run-compare
41 SekCycleCnt+=CM_compareRun(cyc_do, 0);
42#elif defined(EMU_C68K)
43 PicoCpuCM68k.cycles=cyc_do;
44 CycloneRun(&PicoCpuCM68k);
45 SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
46#elif defined(EMU_M68K)
47 SekCycleCnt+=m68k_execute(cyc_do);
48#elif defined(EMU_F68K)
49 SekCycleCnt+=fm68k_emulate(cyc_do+1, 0, 0);
50#endif
51
19886062 52out:
53 pevt_log_m68k_o(EVT_RUN_END);
ed4402a7 54 pprof_end(m68k);
55}
56
69996cb7 57static int PicoFrameHints(void)
58{
59 struct PicoVideo *pv=&Pico.video;
9761a7d0 60 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
69996cb7 61 int hint; // Hint counter
62
19886062 63 pevt_log_m68k_o(EVT_FRAME_START);
9761a7d0 64 pv->v_counter = Pico.m.scanline = 0;
538a6098 65
602133e1 66 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
03e4f2a3 67 // draw a frame just after vblank in alternative render mode
68 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
69 PicoFrameFull();
70#ifdef DRAW_FINISH_FUNC
71 DRAW_FINISH_FUNC();
72#endif
73 skip = 1;
74 }
75 else skip=PicoSkipFrame;
76
69996cb7 77 if (Pico.m.pal) {
69996cb7 78 line_sample = 68;
9761a7d0 79 if (pv->reg[1]&8) lines_vis = 240;
69996cb7 80 } else {
69996cb7 81 line_sample = 93;
82 }
83
84 SekCyclesReset();
4b9c5888 85 z80_resetCycles();
bf5fbbb4 86#ifdef PICO_CD
87 SekCyclesResetS68k();
88#endif
4b9c5888 89 PsndDacLine = 0;
7b3f44c6 90 emustatus &= ~1;
69996cb7 91
92 pv->status&=~0x88; // clear V-Int, come out of vblank
93
94 hint=pv->reg[10]; // Load H-Int counter
95 //dprintf("-hint: %i", hint);
96
97 // This is to make active scan longer (needed for Double Dragon 2, mainly)
538a6098 98 CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
69996cb7 99
b6d7ac70 100 for (y = 0; y < lines_vis; y++)
69996cb7 101 {
9761a7d0 102 pv->v_counter = Pico.m.scanline = y;
103 if ((pv->reg[12]&6) == 6) { // interlace mode 2
104 pv->v_counter <<= 1;
105 pv->v_counter |= pv->v_counter >> 8;
106 pv->v_counter &= 0xff;
107 }
69996cb7 108
109 // VDP FIFO
110 pv->lwrite_cnt -= 12;
111 if (pv->lwrite_cnt <= 0) {
112 pv->lwrite_cnt=0;
113 Pico.video.status|=0x200;
114 }
115
116 PAD_DELAY
bf5fbbb4 117#ifdef PICO_CD
118 check_cd_dma();
119#endif
69996cb7 120
121 // H-Interrupts:
122 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
123 {
124 hint=pv->reg[10]; // Reload H-Int counter
125 pv->pending_ints|=0x10;
126 if (pv->reg[0]&0x10) {
127 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
128 SekInterrupt(4);
129 }
130 }
131
132 // decide if we draw this line
b6d7ac70 133 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 134 {
b6d7ac70 135 // find the right moment for frame renderer, when display is no longer blanked
136 if ((pv->reg[1]&0x40) || y > 100) {
137 PicoFrameFull();
fad24893 138#ifdef DRAW_FINISH_FUNC
b6d7ac70 139 DRAW_FINISH_FUNC();
fad24893 140#endif
b6d7ac70 141 skip = 1;
fad24893 142 }
143 }
69996cb7 144
69996cb7 145 // get samples from sound chips
7b3f44c6 146 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 147 {
be297089 148 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
4b9c5888 149 PicoSyncZ80(SekCycleCnt);
150 if (ym2612.dacen && PsndDacLine <= y)
151 PsndDoDAC(y);
a8fd6e37 152#ifdef PICO_32X
19886062 153 p32x_sync_sh2s(SekCyclesDoneT2());
a8fd6e37 154#endif
7b3f44c6 155 PsndGetSamples(y);
4b9c5888 156 }
69996cb7 157
158 // Run scanline:
159 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 160 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 161
162#ifdef PICO_CD
163 update_chips();
017512f2 164#else
b0677887 165 if (PicoLineHook) PicoLineHook();
bf5fbbb4 166#endif
19886062 167 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 168 }
169
03e4f2a3 170 if (!skip)
b6d7ac70 171 {
172 if (DrawScanline < y)
173 PicoDrawSync(y - 1, 0);
174#ifdef DRAW_FINISH_FUNC
03e4f2a3 175 DRAW_FINISH_FUNC();
8ab3e3c1 176#endif
b6d7ac70 177 }
8ab3e3c1 178
947fb5f9 179 // V-int line (224 or 240)
180 Pico.m.scanline = y;
181 pv->v_counter = 0xe0; // bad for 240 mode
182 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
183
69996cb7 184 // VDP FIFO
185 pv->lwrite_cnt=0;
186 Pico.video.status|=0x200;
187
5f9a0d16 188 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
69996cb7 189 PAD_DELAY
bf5fbbb4 190#ifdef PICO_CD
191 check_cd_dma();
192#endif
69996cb7 193
194 // Last H-Int:
195 if (--hint < 0)
196 {
197 hint=pv->reg[10]; // Reload H-Int counter
198 pv->pending_ints|=0x10;
199 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
200 if (pv->reg[0]&0x10) SekInterrupt(4);
201 }
202
69996cb7 203 pv->status|=0x08; // go into vblank
204 pv->pending_ints|=0x20;
205
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
a8fd6e37 222#ifdef PICO_32X
19886062 223 p32x_sync_sh2s(SekCyclesDoneT2());
a8fd6e37 224 p32x_start_blank();
225#endif
226
69996cb7 227 // get samples from sound chips
7b3f44c6 228 if (y == 224 && PsndOut)
229 {
230 if (ym2612.dacen && PsndDacLine <= y)
231 PsndDoDAC(y);
232 PsndGetSamples(y);
233 }
69996cb7 234
235 // Run scanline:
236 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 237 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
236990cf 238 CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
bf5fbbb4 239
240#ifdef PICO_CD
017512f2 241 update_chips();
242#else
b0677887 243 if (PicoLineHook) PicoLineHook();
bf5fbbb4 244#endif
19886062 245 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 246
5e128c6d 247 lines = scanlines_total;
248 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
69996cb7 249
b6d7ac70 250 for (y++; y < lines; y++)
69996cb7 251 {
9761a7d0 252 pv->v_counter = Pico.m.scanline = y;
253 if (y >= vcnt_wrap)
254 pv->v_counter -= Pico.m.pal ? 56 : 6;
255 if ((pv->reg[12]&6) == 6)
256 pv->v_counter = (pv->v_counter << 1) | 1;
257 pv->v_counter &= 0xff;
69996cb7 258
259 PAD_DELAY
bf5fbbb4 260#ifdef PICO_CD
261 check_cd_dma();
262#endif
69996cb7 263
69996cb7 264 // Run scanline:
265 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 266 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 267
268#ifdef PICO_CD
269 update_chips();
017512f2 270#else
b0677887 271 if (PicoLineHook) PicoLineHook();
bf5fbbb4 272#endif
19886062 273 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 274 }
275
4b9c5888 276 // sync z80
be297089 277 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
e53704e6 278 PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
4b9c5888 279 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
280 PsndDoDAC(lines-1);
281
a8fd6e37 282#ifdef PICO_32X
19886062 283 p32x_sync_sh2s(SekCyclesDoneT2());
a8fd6e37 284#endif
e53704e6 285 timers_cycle();
286
69996cb7 287 return 0;
288}
289
290#undef PAD_DELAY
bf5fbbb4 291#undef CPUS_RUN
69996cb7 292