1 // common code for Pico.c and cd/Pico.c
2 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
4 #define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC
5 #define CYCLES_M68K_VINT_LAG 68
6 #define CYCLES_M68K_ASD 148
7 #define CYCLES_Z80_LINE 228
8 #define CYCLES_Z80_ASD 69
9 #define CYCLES_S68K_LINE 795
10 #define CYCLES_S68K_ASD 241
12 // pad delay (for 6 button pads)
15 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
16 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
19 #define Z80_RUN(z80_cycles) \
21 if ((PicoOpt&4) && Pico.m.z80Run) \
24 if (Pico.m.z80Run & 2) z80CycleAim += z80_cycles; \
26 cnt = SekCyclesDone() - z80startCycle; \
27 cnt = (cnt>>1)-(cnt>>5); \
28 if (cnt < 0 || cnt > (z80_cycles)) cnt = z80_cycles; \
32 cnt=z80CycleAim-total_z80; \
33 if (cnt > 0) total_z80+=z80_run(cnt); \
39 #define CPUS_RUN(m68k_cycles,z80_cycles,s68k_cycles) \
40 SekRunM68k(m68k_cycles); \
43 #define CPUS_RUN(m68k_cycles,z80_cycles,s68k_cycles) \
45 if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) { \
46 SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \
48 SekRunM68k(m68k_cycles); \
49 if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \
50 SekRunS68k(s68k_cycles); \
52 Z80_RUN(z80_cycles); \
56 // Accurate but slower frame which does hints
57 static int PicoFrameHints(void)
59 struct PicoVideo *pv=&Pico.video;
60 int lines, y, lines_vis = 224, total_z80 = 0, z80CycleAim = 0, line_sample, skip;
61 int hint; // Hint counter
63 if ((PicoOpt&0x10) && !PicoSkipFrame) {
64 // draw a frame just after vblank in alternative render mode
65 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
67 #ifdef DRAW_FINISH_FUNC
72 else skip=PicoSkipFrame;
75 //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
76 //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
78 if(pv->reg[1]&8) lines_vis = 240;
80 //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
81 //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
90 pv->status&=~0x88; // clear V-Int, come out of vblank
92 hint=pv->reg[10]; // Load H-Int counter
93 //dprintf("-hint: %i", hint);
95 // This is to make active scan longer (needed for Double Dragon 2, mainly)
96 // also trying to adjust for z80 overclock here (due to int line cycle counts)
97 z80CycleAim = Pico.m.pal ? -40 : 7;
98 CPUS_RUN(CYCLES_M68K_ASD, 0, CYCLES_S68K_ASD);
100 for (y=0;y<lines_vis;y++)
102 Pico.m.scanline=(short)y;
105 pv->lwrite_cnt -= 12;
106 if (pv->lwrite_cnt <= 0) {
108 Pico.video.status|=0x200;
117 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
119 hint=pv->reg[10]; // Reload H-Int counter
120 pv->pending_ints|=0x10;
121 if (pv->reg[0]&0x10) {
122 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
127 // decide if we draw this line
128 #if CAN_HANDLE_240_LINES
129 if(!skip && ((!(pv->reg[1]&8) && y<224) || (pv->reg[1]&8)) )
136 Psnd_timers_and_dac(y);
139 // get samples from sound chips
140 if(y == 32 && PsndOut)
142 else if((y == 224 || y == line_sample) && PsndOut)
147 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
148 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE);
153 if (PicoLineHook) PicoLineHook(1);
157 #ifdef DRAW_FINISH_FUNC
162 // V-int line (224 or 240)
163 Pico.m.scanline=(short)y;
167 Pico.video.status|=0x200;
177 hint=pv->reg[10]; // Reload H-Int counter
178 pv->pending_ints|=0x10;
179 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
180 if (pv->reg[0]&0x10) SekInterrupt(4);
184 pv->status|=0x08; // go into vblank
185 pv->pending_ints|=0x20;
187 // the following SekRun is there for several reasons:
188 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
189 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
190 // also delay between last H-int and V-int (Golden Axe 3)
191 SekRunM68k(CYCLES_M68K_VINT_LAG);
192 if (pv->reg[1]&0x20) {
193 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
196 if (Pico.m.z80Run && (PicoOpt&4))
200 Psnd_timers_and_dac(y);
202 // get samples from sound chips
210 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
211 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
212 CYCLES_Z80_LINE - CYCLES_Z80_ASD, CYCLES_S68K_LINE - CYCLES_S68K_ASD);
217 if (PicoLineHook) PicoLineHook(1);
220 // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
221 lines = Pico.m.pal ? 312 : 262;
223 for (y++;y<lines;y++)
225 Pico.m.scanline=(short)y;
233 Psnd_timers_and_dac(y);
236 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
237 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE);
242 if (PicoLineHook) PicoLineHook(1);