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 |
7 | #define CYCLES_Z80_LINE 228 |
8 | #define CYCLES_Z80_ASD 69 |
bf5fbbb4 |
9 | #define CYCLES_S68K_LINE 795 |
10 | #define CYCLES_S68K_ASD 241 |
69996cb7 |
11 | |
12 | // pad delay (for 6 button pads) |
13 | #define PAD_DELAY \ |
602133e1 |
14 | if (PicoOpt&POPT_6BTN_PAD) { \ |
69996cb7 |
15 | if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \ |
16 | if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \ |
17 | } |
18 | |
bf5fbbb4 |
19 | // CPUS_RUN |
20 | #ifndef PICO_CD |
21 | #define CPUS_RUN(m68k_cycles,z80_cycles,s68k_cycles) \ |
4b9c5888 |
22 | SekRunM68k(m68k_cycles); |
bf5fbbb4 |
23 | #else |
24 | #define CPUS_RUN(m68k_cycles,z80_cycles,s68k_cycles) \ |
25 | { \ |
602133e1 |
26 | if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \ |
bf5fbbb4 |
27 | SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \ |
28 | } else { \ |
29 | SekRunM68k(m68k_cycles); \ |
30 | if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \ |
31 | SekRunS68k(s68k_cycles); \ |
32 | } \ |
bf5fbbb4 |
33 | } |
34 | #endif |
35 | |
69996cb7 |
36 | // Accurate but slower frame which does hints |
37 | static int PicoFrameHints(void) |
38 | { |
39 | struct PicoVideo *pv=&Pico.video; |
4b9c5888 |
40 | int lines, y, lines_vis = 224, line_sample, skip; |
69996cb7 |
41 | int hint; // Hint counter |
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 | timers_cycle(); |
67 | PsndDacLine = 0; |
69996cb7 |
68 | |
69 | pv->status&=~0x88; // clear V-Int, come out of vblank |
70 | |
71 | hint=pv->reg[10]; // Load H-Int counter |
72 | //dprintf("-hint: %i", hint); |
73 | |
74 | // This is to make active scan longer (needed for Double Dragon 2, mainly) |
5f20bb80 |
75 | CPUS_RUN(CYCLES_M68K_ASD, 0, CYCLES_S68K_ASD); |
69996cb7 |
76 | |
77 | for (y=0;y<lines_vis;y++) |
78 | { |
79 | Pico.m.scanline=(short)y; |
80 | |
81 | // VDP FIFO |
82 | pv->lwrite_cnt -= 12; |
83 | if (pv->lwrite_cnt <= 0) { |
84 | pv->lwrite_cnt=0; |
85 | Pico.video.status|=0x200; |
86 | } |
87 | |
88 | PAD_DELAY |
bf5fbbb4 |
89 | #ifdef PICO_CD |
90 | check_cd_dma(); |
91 | #endif |
69996cb7 |
92 | |
93 | // H-Interrupts: |
94 | if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe |
95 | { |
96 | hint=pv->reg[10]; // Reload H-Int counter |
97 | pv->pending_ints|=0x10; |
98 | if (pv->reg[0]&0x10) { |
99 | elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt); |
100 | SekInterrupt(4); |
101 | } |
102 | } |
103 | |
104 | // decide if we draw this line |
fad24893 |
105 | if (!skip) |
106 | { |
602133e1 |
107 | if (PicoOpt&POPT_ALT_RENDERER) { |
fad24893 |
108 | // find the right moment for fast renderer, when display is no longer blanked |
109 | if ((pv->reg[1]&0x40) || y > 100) { |
110 | PicoFrameFull(); |
111 | #ifdef DRAW_FINISH_FUNC |
112 | DRAW_FINISH_FUNC(); |
113 | #endif |
114 | skip = 1; |
115 | } |
116 | } |
117 | else |
118 | { |
69996cb7 |
119 | #if CAN_HANDLE_240_LINES |
fad24893 |
120 | if (((!(pv->reg[1]&8) && y < 224) || (pv->reg[1]&8)) ) |
69996cb7 |
121 | #else |
fad24893 |
122 | if (y < 224) |
69996cb7 |
123 | #endif |
fad24893 |
124 | PicoLine(y); |
125 | } |
126 | } |
69996cb7 |
127 | |
bf5fbbb4 |
128 | #ifndef PICO_CD |
69996cb7 |
129 | // get samples from sound chips |
fad24893 |
130 | if (y == 32 && PsndOut) |
69996cb7 |
131 | emustatus &= ~1; |
fad24893 |
132 | else if ((y == 224 || y == line_sample) && PsndOut) |
4b9c5888 |
133 | { |
134 | if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) |
135 | PicoSyncZ80(SekCycleCnt); |
136 | if (ym2612.dacen && PsndDacLine <= y) |
137 | PsndDoDAC(y); |
69996cb7 |
138 | getSamples(y); |
4b9c5888 |
139 | } |
bf5fbbb4 |
140 | #endif |
69996cb7 |
141 | |
142 | // Run scanline: |
143 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
bf5fbbb4 |
144 | CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE); |
145 | |
146 | #ifdef PICO_CD |
147 | update_chips(); |
017512f2 |
148 | #else |
0ffefdb8 |
149 | if (PicoLineHook) PicoLineHook(1); |
bf5fbbb4 |
150 | #endif |
69996cb7 |
151 | } |
152 | |
8ab3e3c1 |
153 | #ifdef DRAW_FINISH_FUNC |
03e4f2a3 |
154 | if (!skip) |
155 | DRAW_FINISH_FUNC(); |
8ab3e3c1 |
156 | #endif |
157 | |
69996cb7 |
158 | // V-int line (224 or 240) |
159 | Pico.m.scanline=(short)y; |
160 | |
161 | // VDP FIFO |
162 | pv->lwrite_cnt=0; |
163 | Pico.video.status|=0x200; |
164 | |
165 | PAD_DELAY |
bf5fbbb4 |
166 | #ifdef PICO_CD |
167 | check_cd_dma(); |
168 | #endif |
69996cb7 |
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 | // V-Interrupt: |
180 | pv->status|=0x08; // go into vblank |
181 | pv->pending_ints|=0x20; |
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) |
bf5fbbb4 |
187 | SekRunM68k(CYCLES_M68K_VINT_LAG); |
4b9c5888 |
188 | |
69996cb7 |
189 | if (pv->reg[1]&0x20) { |
190 | elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt); |
191 | SekInterrupt(6); |
192 | } |
583ab72c |
193 | if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) { |
4b9c5888 |
194 | PicoSyncZ80(SekCycleCnt); |
583ab72c |
195 | elprintf(EL_INTS, "zint"); |
69996cb7 |
196 | z80_int(); |
583ab72c |
197 | } |
69996cb7 |
198 | |
69996cb7 |
199 | // get samples from sound chips |
bf5fbbb4 |
200 | #ifndef PICO_CD |
201 | if (y == 224) |
202 | #endif |
203 | if (PsndOut) |
4b9c5888 |
204 | { |
205 | if (ym2612.dacen && PsndDacLine <= y) |
206 | PsndDoDAC(y); |
bf5fbbb4 |
207 | getSamples(y); |
4b9c5888 |
208 | } |
69996cb7 |
209 | |
210 | // Run scanline: |
211 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
bf5fbbb4 |
212 | CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD, |
213 | CYCLES_Z80_LINE - CYCLES_Z80_ASD, CYCLES_S68K_LINE - CYCLES_S68K_ASD); |
214 | |
215 | #ifdef PICO_CD |
017512f2 |
216 | update_chips(); |
217 | #else |
0ffefdb8 |
218 | if (PicoLineHook) PicoLineHook(1); |
bf5fbbb4 |
219 | #endif |
69996cb7 |
220 | |
221 | // PAL line count might actually be 313 according to Steve Snake, but that would complicate things. |
222 | lines = Pico.m.pal ? 312 : 262; |
223 | |
224 | for (y++;y<lines;y++) |
225 | { |
226 | Pico.m.scanline=(short)y; |
227 | |
228 | PAD_DELAY |
bf5fbbb4 |
229 | #ifdef PICO_CD |
230 | check_cd_dma(); |
231 | #endif |
69996cb7 |
232 | |
69996cb7 |
233 | // Run scanline: |
234 | if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA()); |
bf5fbbb4 |
235 | CPUS_RUN(CYCLES_M68K_LINE, CYCLES_Z80_LINE, CYCLES_S68K_LINE); |
236 | |
237 | #ifdef PICO_CD |
238 | update_chips(); |
017512f2 |
239 | #else |
0ffefdb8 |
240 | if (PicoLineHook) PicoLineHook(1); |
bf5fbbb4 |
241 | #endif |
69996cb7 |
242 | } |
243 | |
4b9c5888 |
244 | // sync z80 |
245 | if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80)) |
246 | PicoSyncZ80(SekCycleCnt); |
247 | if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1) |
248 | PsndDoDAC(lines-1); |
249 | |
69996cb7 |
250 | return 0; |
251 | } |
252 | |
253 | #undef PAD_DELAY |
254 | #undef Z80_RUN |
bf5fbbb4 |
255 | #undef CPUS_RUN |
69996cb7 |
256 | |