8b99ab90 |
1 | // common code for Pico.c and cd/Pico.c |
acd35d4c |
2 | // (c) Copyright 2007-2009 Grazvydas "notaz" Ignotas |
8b99ab90 |
3 | |
69996cb7 |
4 | #define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC |
5 | #define CYCLES_M68K_VINT_LAG 68 |
6 | #define CYCLES_M68K_ASD 148 |
bf5fbbb4 |
7 | #define CYCLES_S68K_LINE 795 |
8 | #define CYCLES_S68K_ASD 241 |
69996cb7 |
9 | |
10 | // pad delay (for 6 button pads) |
11 | #define PAD_DELAY \ |
602133e1 |
12 | if (PicoOpt&POPT_6BTN_PAD) { \ |
69996cb7 |
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 | |
bf5fbbb4 |
17 | // CPUS_RUN |
acd35d4c |
18 | #ifndef RUN_SH2S |
19 | #define RUN_SH2S |
20 | #endif |
21 | |
bf5fbbb4 |
22 | #ifndef PICO_CD |
538a6098 |
23 | #define CPUS_RUN(m68k_cycles,s68k_cycles) \ |
acd35d4c |
24 | { \ |
25 | SekRunM68k(m68k_cycles); \ |
26 | RUN_SH2S \ |
27 | } |
bf5fbbb4 |
28 | #else |
538a6098 |
29 | #define CPUS_RUN(m68k_cycles,s68k_cycles) \ |
bf5fbbb4 |
30 | { \ |
602133e1 |
31 | if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \ |
bf5fbbb4 |
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 | } \ |
bf5fbbb4 |
38 | } |
39 | #endif |
40 | |
69996cb7 |
41 | static int PicoFrameHints(void) |
42 | { |
43 | struct PicoVideo *pv=&Pico.video; |
9761a7d0 |
44 | int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap; |
69996cb7 |
45 | int hint; // Hint counter |
46 | |
9761a7d0 |
47 | pv->v_counter = Pico.m.scanline = 0; |
538a6098 |
48 | |
602133e1 |
49 | if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled |
03e4f2a3 |
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 | |
69996cb7 |
60 | if (Pico.m.pal) { |
69996cb7 |
61 | line_sample = 68; |
9761a7d0 |
62 | if (pv->reg[1]&8) lines_vis = 240; |
69996cb7 |
63 | } else { |
69996cb7 |
64 | line_sample = 93; |
65 | } |
66 | |
67 | SekCyclesReset(); |
4b9c5888 |
68 | z80_resetCycles(); |
bf5fbbb4 |
69 | #ifdef PICO_CD |
70 | SekCyclesResetS68k(); |
71 | #endif |
4b9c5888 |
72 | PsndDacLine = 0; |
7b3f44c6 |
73 | emustatus &= ~1; |
69996cb7 |
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) |
538a6098 |
81 | CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD); |
69996cb7 |
82 | |
b6d7ac70 |
83 | for (y = 0; y < lines_vis; y++) |
69996cb7 |
84 | { |
9761a7d0 |
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 | } |
69996cb7 |
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 |
bf5fbbb4 |
100 | #ifdef PICO_CD |
101 | check_cd_dma(); |
102 | #endif |
69996cb7 |
103 | |
104 | // H-Interrupts: |
105 | if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe |
106 | { |
107 | hint=pv->reg[10]; // Reload H-Int counter |
108 | pv->pending_ints|=0x10; |
109 | if (pv->reg[0]&0x10) { |
110 | elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt); |
111 | SekInterrupt(4); |
112 | } |
113 | } |
114 | |
115 | // decide if we draw this line |
b6d7ac70 |
116 | if (!skip && (PicoOpt & POPT_ALT_RENDERER)) |
fad24893 |
117 | { |
b6d7ac70 |
118 | // find the right moment for frame renderer, when display is no longer blanked |
119 | if ((pv->reg[1]&0x40) || y > 100) { |
120 | PicoFrameFull(); |
fad24893 |
121 | #ifdef DRAW_FINISH_FUNC |
b6d7ac70 |
122 | DRAW_FINISH_FUNC(); |
fad24893 |
123 | #endif |
b6d7ac70 |
124 | skip = 1; |
fad24893 |
125 | } |
126 | } |
69996cb7 |
127 | |
69996cb7 |
128 | // get samples from sound chips |
7b3f44c6 |
129 | if ((y == 224 || y == line_sample) && PsndOut) |
4b9c5888 |
130 | { |
be297089 |
131 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) |
4b9c5888 |
132 | PicoSyncZ80(SekCycleCnt); |
133 | if (ym2612.dacen && PsndDacLine <= y) |
134 | PsndDoDAC(y); |
7b3f44c6 |
135 | PsndGetSamples(y); |
4b9c5888 |
136 | } |
69996cb7 |
137 | |
138 | // Run scanline: |
139 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
538a6098 |
140 | CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE); |
bf5fbbb4 |
141 | |
142 | #ifdef PICO_CD |
143 | update_chips(); |
017512f2 |
144 | #else |
b0677887 |
145 | if (PicoLineHook) PicoLineHook(); |
bf5fbbb4 |
146 | #endif |
69996cb7 |
147 | } |
148 | |
03e4f2a3 |
149 | if (!skip) |
b6d7ac70 |
150 | { |
151 | if (DrawScanline < y) |
152 | PicoDrawSync(y - 1, 0); |
153 | #ifdef DRAW_FINISH_FUNC |
03e4f2a3 |
154 | DRAW_FINISH_FUNC(); |
8ab3e3c1 |
155 | #endif |
b6d7ac70 |
156 | } |
8ab3e3c1 |
157 | |
947fb5f9 |
158 | // V-int line (224 or 240) |
159 | Pico.m.scanline = y; |
160 | pv->v_counter = 0xe0; // bad for 240 mode |
161 | if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1; |
162 | |
69996cb7 |
163 | // VDP FIFO |
164 | pv->lwrite_cnt=0; |
165 | Pico.video.status|=0x200; |
166 | |
5f9a0d16 |
167 | memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt)); |
69996cb7 |
168 | PAD_DELAY |
bf5fbbb4 |
169 | #ifdef PICO_CD |
170 | check_cd_dma(); |
171 | #endif |
69996cb7 |
172 | |
173 | // Last H-Int: |
174 | if (--hint < 0) |
175 | { |
176 | hint=pv->reg[10]; // Reload H-Int counter |
177 | pv->pending_ints|=0x10; |
178 | //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt); |
179 | if (pv->reg[0]&0x10) SekInterrupt(4); |
180 | } |
181 | |
69996cb7 |
182 | pv->status|=0x08; // go into vblank |
183 | pv->pending_ints|=0x20; |
184 | |
974fdb5b |
185 | #ifdef PICO_32X |
186 | p32x_start_blank(); |
187 | #endif |
188 | |
69996cb7 |
189 | // the following SekRun is there for several reasons: |
190 | // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga) |
191 | // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants) |
192 | // also delay between last H-int and V-int (Golden Axe 3) |
bf5fbbb4 |
193 | SekRunM68k(CYCLES_M68K_VINT_LAG); |
4b9c5888 |
194 | |
69996cb7 |
195 | if (pv->reg[1]&0x20) { |
196 | elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt); |
197 | SekInterrupt(6); |
198 | } |
be297089 |
199 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) { |
4b9c5888 |
200 | PicoSyncZ80(SekCycleCnt); |
583ab72c |
201 | elprintf(EL_INTS, "zint"); |
69996cb7 |
202 | z80_int(); |
583ab72c |
203 | } |
69996cb7 |
204 | |
69996cb7 |
205 | // get samples from sound chips |
7b3f44c6 |
206 | if (y == 224 && PsndOut) |
207 | { |
208 | if (ym2612.dacen && PsndDacLine <= y) |
209 | PsndDoDAC(y); |
210 | PsndGetSamples(y); |
211 | } |
69996cb7 |
212 | |
213 | // Run scanline: |
214 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
bf5fbbb4 |
215 | CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD, |
538a6098 |
216 | CYCLES_S68K_LINE - CYCLES_S68K_ASD); |
bf5fbbb4 |
217 | |
218 | #ifdef PICO_CD |
017512f2 |
219 | update_chips(); |
220 | #else |
b0677887 |
221 | if (PicoLineHook) PicoLineHook(); |
bf5fbbb4 |
222 | #endif |
69996cb7 |
223 | |
224 | // PAL line count might actually be 313 according to Steve Snake, but that would complicate things. |
225 | lines = Pico.m.pal ? 312 : 262; |
9761a7d0 |
226 | vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens |
69996cb7 |
227 | |
b6d7ac70 |
228 | for (y++; y < lines; y++) |
69996cb7 |
229 | { |
9761a7d0 |
230 | pv->v_counter = Pico.m.scanline = y; |
231 | if (y >= vcnt_wrap) |
232 | pv->v_counter -= Pico.m.pal ? 56 : 6; |
233 | if ((pv->reg[12]&6) == 6) |
234 | pv->v_counter = (pv->v_counter << 1) | 1; |
235 | pv->v_counter &= 0xff; |
69996cb7 |
236 | |
237 | PAD_DELAY |
bf5fbbb4 |
238 | #ifdef PICO_CD |
239 | check_cd_dma(); |
240 | #endif |
69996cb7 |
241 | |
69996cb7 |
242 | // Run scanline: |
243 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
538a6098 |
244 | CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE); |
bf5fbbb4 |
245 | |
246 | #ifdef PICO_CD |
247 | update_chips(); |
017512f2 |
248 | #else |
b0677887 |
249 | if (PicoLineHook) PicoLineHook(); |
bf5fbbb4 |
250 | #endif |
69996cb7 |
251 | } |
252 | |
4b9c5888 |
253 | // sync z80 |
be297089 |
254 | if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) |
e53704e6 |
255 | PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter |
4b9c5888 |
256 | if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1) |
257 | PsndDoDAC(lines-1); |
258 | |
e53704e6 |
259 | timers_cycle(); |
260 | |
69996cb7 |
261 | return 0; |
262 | } |
263 | |
264 | #undef PAD_DELAY |
bf5fbbb4 |
265 | #undef CPUS_RUN |
69996cb7 |
266 | |