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