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