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