clean up dac code a bit
[picodrive.git] / pico / pico_cmn.c
CommitLineData
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
11#define CYCLES_M68K_ASD 148
69996cb7 12
13// pad delay (for 6 button pads)
531a8f38 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}
69996cb7 18
bf5fbbb4 19// CPUS_RUN
c987bb5c 20#ifndef CPUS_RUN
ae214f1c 21#define CPUS_RUN(m68k_cycles) \
c987bb5c 22 SekRunM68k(m68k_cycles)
bf5fbbb4 23#endif
24
08769494 25// sync m68k to SekCycleAim
26static void SekSyncM68k(void)
ed4402a7 27{
28 int cyc_do;
29 pprof_start(m68k);
19886062 30 pevt_log_m68k_o(EVT_RUN_START);
ed4402a7 31
ae214f1c 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;
ed4402a7 39#elif defined(EMU_M68K)
ae214f1c 40 SekCycleCnt += m68k_execute(cyc_do) - cyc_do;
ed4402a7 41#elif defined(EMU_F68K)
99ade2ee 42 SekCycleCnt += fm68k_emulate(cyc_do, 0) - cyc_do;
ed4402a7 43#endif
ae214f1c 44 }
45
46 SekCyclesLeft = 0;
ed4402a7 47
12da51c2 48 SekTrace(0);
19886062 49 pevt_log_m68k_o(EVT_RUN_END);
ed4402a7 50 pprof_end(m68k);
51}
52
08769494 53static inline void SekRunM68k(int cyc)
54{
55 SekCycleAim += cyc;
56 SekSyncM68k();
57}
58
69996cb7 59static int PicoFrameHints(void)
60{
61 struct PicoVideo *pv=&Pico.video;
9761a7d0 62 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
ae214f1c 63 unsigned int cycles;
69996cb7 64 int hint; // Hint counter
65
19886062 66 pevt_log_m68k_o(EVT_FRAME_START);
9761a7d0 67 pv->v_counter = Pico.m.scanline = 0;
538a6098 68
602133e1 69 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
03e4f2a3 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
69996cb7 80 if (Pico.m.pal) {
69996cb7 81 line_sample = 68;
9761a7d0 82 if (pv->reg[1]&8) lines_vis = 240;
69996cb7 83 } else {
69996cb7 84 line_sample = 93;
85 }
86
4b9c5888 87 z80_resetCycles();
4f2cdbf5 88 PsndStartFrame();
69996cb7 89
90 pv->status&=~0x88; // clear V-Int, come out of vblank
91
92 hint=pv->reg[10]; // Load H-Int counter
93 //dprintf("-hint: %i", hint);
94
95 // This is to make active scan longer (needed for Double Dragon 2, mainly)
ae214f1c 96 CPUS_RUN(CYCLES_M68K_ASD);
69996cb7 97
b6d7ac70 98 for (y = 0; y < lines_vis; y++)
69996cb7 99 {
9761a7d0 100 pv->v_counter = Pico.m.scanline = y;
101 if ((pv->reg[12]&6) == 6) { // interlace mode 2
102 pv->v_counter <<= 1;
103 pv->v_counter |= pv->v_counter >> 8;
104 pv->v_counter &= 0xff;
105 }
69996cb7 106
107 // VDP FIFO
108 pv->lwrite_cnt -= 12;
109 if (pv->lwrite_cnt <= 0) {
110 pv->lwrite_cnt=0;
111 Pico.video.status|=0x200;
112 }
113
531a8f38 114 PAD_DELAY();
69996cb7 115
116 // H-Interrupts:
117 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
118 {
119 hint=pv->reg[10]; // Reload H-Int counter
120 pv->pending_ints|=0x10;
121 if (pv->reg[0]&0x10) {
ae214f1c 122 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCyclesDone());
69996cb7 123 SekInterrupt(4);
124 }
125 }
126
127 // decide if we draw this line
b6d7ac70 128 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 129 {
b6d7ac70 130 // find the right moment for frame renderer, when display is no longer blanked
131 if ((pv->reg[1]&0x40) || y > 100) {
132 PicoFrameFull();
fad24893 133#ifdef DRAW_FINISH_FUNC
b6d7ac70 134 DRAW_FINISH_FUNC();
fad24893 135#endif
b6d7ac70 136 skip = 1;
fad24893 137 }
138 }
69996cb7 139
69996cb7 140 // get samples from sound chips
7b3f44c6 141 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 142 {
ae214f1c 143 cycles = SekCyclesDone();
144
be297089 145 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
ae214f1c 146 PicoSyncZ80(cycles);
ae214f1c 147#ifdef PICO_CD
fa8fb754 148 if (PicoAHW & PAHW_MCD)
149 pcd_sync_s68k(cycles, 0);
ae214f1c 150#endif
a8fd6e37 151#ifdef PICO_32X
ae214f1c 152 p32x_sync_sh2s(cycles);
a8fd6e37 153#endif
7b3f44c6 154 PsndGetSamples(y);
4b9c5888 155 }
69996cb7 156
157 // Run scanline:
53f948c9 158 line_base_cycles = SekCyclesDone();
69996cb7 159 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
ae214f1c 160 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 161
b0677887 162 if (PicoLineHook) PicoLineHook();
19886062 163 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 164 }
165
03e4f2a3 166 if (!skip)
b6d7ac70 167 {
ea38612f 168 if (Pico.est.DrawScanline < y)
b6d7ac70 169 PicoDrawSync(y - 1, 0);
170#ifdef DRAW_FINISH_FUNC
03e4f2a3 171 DRAW_FINISH_FUNC();
8ab3e3c1 172#endif
b6d7ac70 173 }
8ab3e3c1 174
947fb5f9 175 // V-int line (224 or 240)
176 Pico.m.scanline = y;
177 pv->v_counter = 0xe0; // bad for 240 mode
178 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
179
69996cb7 180 // VDP FIFO
181 pv->lwrite_cnt=0;
182 Pico.video.status|=0x200;
183
5f9a0d16 184 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
531a8f38 185 PAD_DELAY();
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;
ae214f1c 192 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCyclesDone());
69996cb7 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)
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
69996cb7 207 if (pv->reg[1]&0x20) {
ae214f1c 208 elprintf(EL_INTS, "vint: @ %06x [%i]", 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:
ae214f1c 233 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD);
bf5fbbb4 234
b0677887 235 if (PicoLineHook) PicoLineHook();
19886062 236 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 237
5e128c6d 238 lines = scanlines_total;
239 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
69996cb7 240
b6d7ac70 241 for (y++; y < lines; y++)
69996cb7 242 {
9761a7d0 243 pv->v_counter = Pico.m.scanline = y;
244 if (y >= vcnt_wrap)
245 pv->v_counter -= Pico.m.pal ? 56 : 6;
246 if ((pv->reg[12]&6) == 6)
247 pv->v_counter = (pv->v_counter << 1) | 1;
248 pv->v_counter &= 0xff;
69996cb7 249
531a8f38 250 PAD_DELAY();
69996cb7 251
69996cb7 252 // Run scanline:
53f948c9 253 line_base_cycles = SekCyclesDone();
69996cb7 254 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
ae214f1c 255 CPUS_RUN(CYCLES_M68K_LINE);
bf5fbbb4 256
b0677887 257 if (PicoLineHook) PicoLineHook();
19886062 258 pevt_log_m68k_o(EVT_NEXT_LINE);
69996cb7 259 }
260
ae214f1c 261 // sync cpus
262 cycles = SekCyclesDone();
be297089 263 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
ae214f1c 264 PicoSyncZ80(cycles);
4f2cdbf5 265 if (PsndOut && ym2612.dacen && PsndDacLine < lines)
266 PsndDoDAC(lines - 1);
4b9c5888 267
ae214f1c 268#ifdef PICO_CD
fa8fb754 269 if (PicoAHW & PAHW_MCD)
270 pcd_sync_s68k(cycles, 0);
ae214f1c 271#endif
a8fd6e37 272#ifdef PICO_32X
ae214f1c 273 p32x_sync_sh2s(cycles);
a8fd6e37 274#endif
e53704e6 275 timers_cycle();
276
69996cb7 277 return 0;
278}
279
280#undef PAD_DELAY
bf5fbbb4 281#undef CPUS_RUN
69996cb7 282
ae214f1c 283// vim:shiftwidth=2:ts=2:expandtab