8b99ab90 |
1 | // common code for Pico.c and cd/Pico.c |
fad24893 |
2 | // (c) Copyright 2007,2008 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 |
18 | #ifndef PICO_CD |
538a6098 |
19 | #define CPUS_RUN(m68k_cycles,s68k_cycles) \ |
4b9c5888 |
20 | SekRunM68k(m68k_cycles); |
bf5fbbb4 |
21 | #else |
538a6098 |
22 | #define CPUS_RUN(m68k_cycles,s68k_cycles) \ |
bf5fbbb4 |
23 | { \ |
602133e1 |
24 | if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \ |
bf5fbbb4 |
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 | } \ |
bf5fbbb4 |
31 | } |
32 | #endif |
33 | |
69996cb7 |
34 | // Accurate but slower frame which does hints |
35 | static int PicoFrameHints(void) |
36 | { |
37 | struct PicoVideo *pv=&Pico.video; |
4b9c5888 |
38 | int lines, y, lines_vis = 224, line_sample, skip; |
69996cb7 |
39 | int hint; // Hint counter |
40 | |
2aa27095 |
41 | Pico.m.scanline = 0; |
538a6098 |
42 | |
602133e1 |
43 | if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled |
03e4f2a3 |
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 | |
69996cb7 |
54 | if (Pico.m.pal) { |
69996cb7 |
55 | line_sample = 68; |
56 | if(pv->reg[1]&8) lines_vis = 240; |
57 | } else { |
69996cb7 |
58 | line_sample = 93; |
59 | } |
60 | |
61 | SekCyclesReset(); |
4b9c5888 |
62 | z80_resetCycles(); |
bf5fbbb4 |
63 | #ifdef PICO_CD |
64 | SekCyclesResetS68k(); |
65 | #endif |
4b9c5888 |
66 | PsndDacLine = 0; |
69996cb7 |
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) |
538a6098 |
74 | CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD); |
69996cb7 |
75 | |
b6d7ac70 |
76 | for (y = 0; y < lines_vis; y++) |
69996cb7 |
77 | { |
2aa27095 |
78 | Pico.m.scanline = y; |
69996cb7 |
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 |
bf5fbbb4 |
88 | #ifdef PICO_CD |
89 | check_cd_dma(); |
90 | #endif |
69996cb7 |
91 | |
92 | // H-Interrupts: |
93 | if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe |
94 | { |
95 | hint=pv->reg[10]; // Reload H-Int counter |
96 | pv->pending_ints|=0x10; |
97 | if (pv->reg[0]&0x10) { |
98 | elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt); |
99 | SekInterrupt(4); |
100 | } |
101 | } |
102 | |
103 | // decide if we draw this line |
b6d7ac70 |
104 | if (!skip && (PicoOpt & POPT_ALT_RENDERER)) |
fad24893 |
105 | { |
b6d7ac70 |
106 | // find the right moment for frame renderer, when display is no longer blanked |
107 | if ((pv->reg[1]&0x40) || y > 100) { |
108 | PicoFrameFull(); |
fad24893 |
109 | #ifdef DRAW_FINISH_FUNC |
b6d7ac70 |
110 | DRAW_FINISH_FUNC(); |
fad24893 |
111 | #endif |
b6d7ac70 |
112 | skip = 1; |
fad24893 |
113 | } |
114 | } |
69996cb7 |
115 | |
bf5fbbb4 |
116 | #ifndef PICO_CD |
69996cb7 |
117 | // get samples from sound chips |
fad24893 |
118 | if (y == 32 && PsndOut) |
69996cb7 |
119 | emustatus &= ~1; |
fad24893 |
120 | else if ((y == 224 || y == line_sample) && PsndOut) |
4b9c5888 |
121 | { |
122 | if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) |
123 | PicoSyncZ80(SekCycleCnt); |
124 | if (ym2612.dacen && PsndDacLine <= y) |
125 | PsndDoDAC(y); |
69996cb7 |
126 | getSamples(y); |
4b9c5888 |
127 | } |
bf5fbbb4 |
128 | #endif |
69996cb7 |
129 | |
130 | // Run scanline: |
131 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
538a6098 |
132 | CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE); |
bf5fbbb4 |
133 | |
134 | #ifdef PICO_CD |
135 | update_chips(); |
017512f2 |
136 | #else |
0ffefdb8 |
137 | if (PicoLineHook) PicoLineHook(1); |
bf5fbbb4 |
138 | #endif |
69996cb7 |
139 | } |
140 | |
03e4f2a3 |
141 | if (!skip) |
b6d7ac70 |
142 | { |
143 | if (DrawScanline < y) |
144 | PicoDrawSync(y - 1, 0); |
145 | #ifdef DRAW_FINISH_FUNC |
03e4f2a3 |
146 | DRAW_FINISH_FUNC(); |
8ab3e3c1 |
147 | #endif |
b6d7ac70 |
148 | } |
8ab3e3c1 |
149 | |
69996cb7 |
150 | // V-int line (224 or 240) |
2aa27095 |
151 | Pico.m.scanline = y; |
69996cb7 |
152 | |
153 | // VDP FIFO |
154 | pv->lwrite_cnt=0; |
155 | Pico.video.status|=0x200; |
156 | |
157 | PAD_DELAY |
bf5fbbb4 |
158 | #ifdef PICO_CD |
159 | check_cd_dma(); |
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 | |
171 | // V-Interrupt: |
172 | pv->status|=0x08; // go into vblank |
173 | pv->pending_ints|=0x20; |
174 | |
175 | // the following SekRun is there for several reasons: |
176 | // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga) |
177 | // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants) |
178 | // also delay between last H-int and V-int (Golden Axe 3) |
bf5fbbb4 |
179 | SekRunM68k(CYCLES_M68K_VINT_LAG); |
4b9c5888 |
180 | |
69996cb7 |
181 | if (pv->reg[1]&0x20) { |
182 | elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt); |
183 | SekInterrupt(6); |
184 | } |
583ab72c |
185 | if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) { |
4b9c5888 |
186 | PicoSyncZ80(SekCycleCnt); |
583ab72c |
187 | elprintf(EL_INTS, "zint"); |
69996cb7 |
188 | z80_int(); |
583ab72c |
189 | } |
69996cb7 |
190 | |
69996cb7 |
191 | // get samples from sound chips |
bf5fbbb4 |
192 | #ifndef PICO_CD |
193 | if (y == 224) |
194 | #endif |
195 | if (PsndOut) |
4b9c5888 |
196 | { |
197 | if (ym2612.dacen && PsndDacLine <= y) |
198 | PsndDoDAC(y); |
bf5fbbb4 |
199 | getSamples(y); |
4b9c5888 |
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, |
538a6098 |
205 | CYCLES_S68K_LINE - CYCLES_S68K_ASD); |
bf5fbbb4 |
206 | |
207 | #ifdef PICO_CD |
017512f2 |
208 | update_chips(); |
209 | #else |
0ffefdb8 |
210 | if (PicoLineHook) PicoLineHook(1); |
bf5fbbb4 |
211 | #endif |
69996cb7 |
212 | |
213 | // PAL line count might actually be 313 according to Steve Snake, but that would complicate things. |
214 | lines = Pico.m.pal ? 312 : 262; |
215 | |
b6d7ac70 |
216 | for (y++; y < lines; y++) |
69996cb7 |
217 | { |
2aa27095 |
218 | Pico.m.scanline = y; |
69996cb7 |
219 | |
220 | PAD_DELAY |
bf5fbbb4 |
221 | #ifdef PICO_CD |
222 | check_cd_dma(); |
223 | #endif |
69996cb7 |
224 | |
69996cb7 |
225 | // Run scanline: |
226 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
538a6098 |
227 | CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE); |
bf5fbbb4 |
228 | |
229 | #ifdef PICO_CD |
230 | update_chips(); |
017512f2 |
231 | #else |
0ffefdb8 |
232 | if (PicoLineHook) PicoLineHook(1); |
bf5fbbb4 |
233 | #endif |
69996cb7 |
234 | } |
235 | |
4b9c5888 |
236 | // sync z80 |
237 | if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) |
e53704e6 |
238 | PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter |
4b9c5888 |
239 | if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1) |
240 | PsndDoDAC(lines-1); |
241 | |
e53704e6 |
242 | timers_cycle(); |
243 | |
69996cb7 |
244 | return 0; |
245 | } |
246 | |
247 | #undef PAD_DELAY |
bf5fbbb4 |
248 | #undef CPUS_RUN |
69996cb7 |
249 | |