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