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