Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | /*************************************************************************** |
2 | * Copyright (C) 2010 by Blade_Arma * | |
3 | * * | |
4 | * This program is free software; you can redistribute it and/or modify * | |
5 | * it under the terms of the GNU General Public License as published by * | |
6 | * the Free Software Foundation; either version 2 of the License, or * | |
7 | * (at your option) any later version. * | |
8 | * * | |
9 | * This program is distributed in the hope that it will be useful, * | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 | * GNU General Public License for more details. * | |
13 | * * | |
14 | * You should have received a copy of the GNU General Public License * | |
15 | * along with this program; if not, write to the * | |
16 | * Free Software Foundation, Inc., * | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. * | |
18 | ***************************************************************************/ | |
19 | ||
20 | /* | |
21 | * Internal PSX counters. | |
22 | */ | |
23 | ||
24 | #include "psxcounters.h" | |
ddbaf678 | 25 | #include "gpu.h" |
fc8145b7 | 26 | #include "debug.h" |
ef79bbde P |
27 | |
28 | /******************************************************************************/ | |
29 | ||
ef79bbde P |
30 | enum |
31 | { | |
32 | Rc0Gate = 0x0001, // 0 not implemented | |
33 | Rc1Gate = 0x0001, // 0 not implemented | |
34 | Rc2Disable = 0x0001, // 0 partially implemented | |
35 | RcUnknown1 = 0x0002, // 1 ? | |
36 | RcUnknown2 = 0x0004, // 2 ? | |
37 | RcCountToTarget = 0x0008, // 3 | |
38 | RcIrqOnTarget = 0x0010, // 4 | |
39 | RcIrqOnOverflow = 0x0020, // 5 | |
40 | RcIrqRegenerate = 0x0040, // 6 | |
41 | RcUnknown7 = 0x0080, // 7 ? | |
42 | Rc0PixelClock = 0x0100, // 8 fake implementation | |
43 | Rc1HSyncClock = 0x0100, // 8 | |
44 | Rc2Unknown8 = 0x0100, // 8 ? | |
45 | Rc0Unknown9 = 0x0200, // 9 ? | |
46 | Rc1Unknown9 = 0x0200, // 9 ? | |
47 | Rc2OneEighthClock = 0x0200, // 9 | |
48 | RcUnknown10 = 0x0400, // 10 ? | |
49 | RcCountEqTarget = 0x0800, // 11 | |
50 | RcOverflow = 0x1000, // 12 | |
51 | RcUnknown13 = 0x2000, // 13 ? (always zero) | |
52 | RcUnknown14 = 0x4000, // 14 ? (always zero) | |
53 | RcUnknown15 = 0x8000, // 15 ? (always zero) | |
54 | }; | |
55 | ||
56 | #define CounterQuantity ( 4 ) | |
57 | //static const u32 CounterQuantity = 4; | |
58 | ||
59 | static const u32 CountToOverflow = 0; | |
60 | static const u32 CountToTarget = 1; | |
61 | ||
62 | static const u32 FrameRate[] = { 60, 50 }; | |
aecf98c5 | 63 | static const u32 HSyncTotal[] = { 263, 313 }; |
0486fdc9 | 64 | #define VBlankStart 240 |
ef79bbde | 65 | |
9f7ee52e | 66 | #define VERBOSE_LEVEL 0 |
4b7da20a | 67 | #if VERBOSE_LEVEL > 0 |
9f7ee52e | 68 | static const s32 VerboseLevel = VERBOSE_LEVEL; |
4b7da20a | 69 | #endif |
ef79bbde P |
70 | |
71 | /******************************************************************************/ | |
72 | ||
93c9e42a A |
73 | Rcnt rcnts[ CounterQuantity ]; |
74 | ||
24de2dd4 | 75 | u32 hSyncCount = 0; |
76 | u32 frame_counter = 0; | |
61ef5cf4 | 77 | static u32 hsync_steps = 0; |
4f55097d | 78 | static u32 base_cycle = 0; |
ef79bbde P |
79 | |
80 | u32 psxNextCounter = 0, psxNextsCounter = 0; | |
81 | ||
82 | /******************************************************************************/ | |
83 | ||
84 | static inline | |
85 | void setIrq( u32 irq ) | |
86 | { | |
87 | psxHu32ref(0x1070) |= SWAPu32(irq); | |
88 | } | |
89 | ||
90 | static | |
9f7ee52e | 91 | void verboseLog( u32 level, const char *str, ... ) |
ef79bbde | 92 | { |
9f7ee52e | 93 | #if VERBOSE_LEVEL > 0 |
ef79bbde P |
94 | if( level <= VerboseLevel ) |
95 | { | |
96 | va_list va; | |
97 | char buf[ 4096 ]; | |
98 | ||
99 | va_start( va, str ); | |
100 | vsprintf( buf, str, va ); | |
101 | va_end( va ); | |
102 | ||
ab948f7e | 103 | printf( "%s", buf ); |
ef79bbde P |
104 | fflush( stdout ); |
105 | } | |
9f7ee52e | 106 | #endif |
ef79bbde P |
107 | } |
108 | ||
109 | /******************************************************************************/ | |
110 | ||
111 | static inline | |
112 | void _psxRcntWcount( u32 index, u32 value ) | |
113 | { | |
114 | if( value > 0xffff ) | |
115 | { | |
116 | verboseLog( 1, "[RCNT %i] wcount > 0xffff: %x\n", index, value ); | |
117 | value &= 0xffff; | |
118 | } | |
119 | ||
120 | rcnts[index].cycleStart = psxRegs.cycle; | |
121 | rcnts[index].cycleStart -= value * rcnts[index].rate; | |
122 | ||
123 | // TODO: <=. | |
124 | if( value < rcnts[index].target ) | |
125 | { | |
126 | rcnts[index].cycle = rcnts[index].target * rcnts[index].rate; | |
127 | rcnts[index].counterState = CountToTarget; | |
128 | } | |
129 | else | |
130 | { | |
8ca6b0a6 | 131 | rcnts[index].cycle = 0x10000 * rcnts[index].rate; |
ef79bbde P |
132 | rcnts[index].counterState = CountToOverflow; |
133 | } | |
134 | } | |
135 | ||
136 | static inline | |
137 | u32 _psxRcntRcount( u32 index ) | |
138 | { | |
139 | u32 count; | |
140 | ||
141 | count = psxRegs.cycle; | |
142 | count -= rcnts[index].cycleStart; | |
61ef5cf4 | 143 | if (rcnts[index].rate > 1) |
144 | count /= rcnts[index].rate; | |
ef79bbde | 145 | |
8ca6b0a6 | 146 | if( count > 0x10000 ) |
ef79bbde | 147 | { |
8ca6b0a6 | 148 | verboseLog( 1, "[RCNT %i] rcount > 0x10000: %x\n", index, count ); |
ef79bbde | 149 | } |
8ca6b0a6 | 150 | count &= 0xffff; |
ef79bbde P |
151 | |
152 | return count; | |
153 | } | |
154 | ||
a29f182f | 155 | static |
156 | void _psxRcntWmode( u32 index, u32 value ) | |
157 | { | |
158 | rcnts[index].mode = value; | |
159 | ||
160 | switch( index ) | |
161 | { | |
162 | case 0: | |
163 | if( value & Rc0PixelClock ) | |
164 | { | |
165 | rcnts[index].rate = 5; | |
166 | } | |
167 | else | |
168 | { | |
169 | rcnts[index].rate = 1; | |
170 | } | |
171 | break; | |
172 | case 1: | |
173 | if( value & Rc1HSyncClock ) | |
174 | { | |
175 | rcnts[index].rate = (PSXCLK / (FrameRate[Config.PsxType] * HSyncTotal[Config.PsxType])); | |
176 | } | |
177 | else | |
178 | { | |
179 | rcnts[index].rate = 1; | |
180 | } | |
181 | break; | |
182 | case 2: | |
183 | if( value & Rc2OneEighthClock ) | |
184 | { | |
185 | rcnts[index].rate = 8; | |
186 | } | |
187 | else | |
188 | { | |
189 | rcnts[index].rate = 1; | |
190 | } | |
191 | ||
192 | // TODO: wcount must work. | |
193 | if( value & Rc2Disable ) | |
194 | { | |
195 | rcnts[index].rate = 0xffffffff; | |
196 | } | |
197 | break; | |
198 | } | |
199 | } | |
200 | ||
ef79bbde P |
201 | /******************************************************************************/ |
202 | ||
203 | static | |
204 | void psxRcntSet() | |
205 | { | |
206 | s32 countToUpdate; | |
207 | u32 i; | |
208 | ||
209 | psxNextsCounter = psxRegs.cycle; | |
210 | psxNextCounter = 0x7fffffff; | |
211 | ||
212 | for( i = 0; i < CounterQuantity; ++i ) | |
213 | { | |
214 | countToUpdate = rcnts[i].cycle - (psxNextsCounter - rcnts[i].cycleStart); | |
215 | ||
216 | if( countToUpdate < 0 ) | |
217 | { | |
218 | psxNextCounter = 0; | |
219 | break; | |
220 | } | |
221 | ||
222 | if( countToUpdate < (s32)psxNextCounter ) | |
223 | { | |
224 | psxNextCounter = countToUpdate; | |
225 | } | |
226 | } | |
5b8c000f | 227 | |
228 | psxRegs.interrupt |= (1 << PSXINT_RCNT); | |
229 | new_dyna_set_event(PSXINT_RCNT, psxNextCounter); | |
ef79bbde P |
230 | } |
231 | ||
232 | /******************************************************************************/ | |
233 | ||
234 | static | |
235 | void psxRcntReset( u32 index ) | |
236 | { | |
8ca6b0a6 | 237 | u32 rcycles; |
ef79bbde | 238 | |
53c361f0 | 239 | rcnts[index].mode |= RcUnknown10; |
240 | ||
ef79bbde P |
241 | if( rcnts[index].counterState == CountToTarget ) |
242 | { | |
8ca6b0a6 | 243 | rcycles = psxRegs.cycle - rcnts[index].cycleStart; |
ef79bbde | 244 | if( rcnts[index].mode & RcCountToTarget ) |
8ca6b0a6 | 245 | { |
246 | rcycles -= rcnts[index].target * rcnts[index].rate; | |
247 | rcnts[index].cycleStart = psxRegs.cycle - rcycles; | |
248 | } | |
249 | else | |
250 | { | |
251 | rcnts[index].cycle = 0x10000 * rcnts[index].rate; | |
252 | rcnts[index].counterState = CountToOverflow; | |
253 | } | |
ef79bbde P |
254 | |
255 | if( rcnts[index].mode & RcIrqOnTarget ) | |
256 | { | |
257 | if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) ) | |
258 | { | |
8ca6b0a6 | 259 | verboseLog( 3, "[RCNT %i] irq\n", index ); |
ef79bbde P |
260 | setIrq( rcnts[index].irq ); |
261 | rcnts[index].irqState = 1; | |
262 | } | |
263 | } | |
264 | ||
265 | rcnts[index].mode |= RcCountEqTarget; | |
53c361f0 | 266 | |
8ca6b0a6 | 267 | if( rcycles < 0x10000 * rcnts[index].rate ) |
53c361f0 | 268 | return; |
ef79bbde | 269 | } |
53c361f0 | 270 | |
271 | if( rcnts[index].counterState == CountToOverflow ) | |
ef79bbde | 272 | { |
8ca6b0a6 | 273 | rcycles = psxRegs.cycle - rcnts[index].cycleStart; |
274 | rcycles -= 0x10000 * rcnts[index].rate; | |
275 | ||
276 | rcnts[index].cycleStart = psxRegs.cycle - rcycles; | |
ef79bbde | 277 | |
8ca6b0a6 | 278 | if( rcycles < rcnts[index].target * rcnts[index].rate ) |
279 | { | |
280 | rcnts[index].cycle = rcnts[index].target * rcnts[index].rate; | |
281 | rcnts[index].counterState = CountToTarget; | |
282 | } | |
ef79bbde P |
283 | |
284 | if( rcnts[index].mode & RcIrqOnOverflow ) | |
285 | { | |
286 | if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) ) | |
287 | { | |
8ca6b0a6 | 288 | verboseLog( 3, "[RCNT %i] irq\n", index ); |
ef79bbde P |
289 | setIrq( rcnts[index].irq ); |
290 | rcnts[index].irqState = 1; | |
291 | } | |
292 | } | |
293 | ||
294 | rcnts[index].mode |= RcOverflow; | |
295 | } | |
ef79bbde P |
296 | } |
297 | ||
298 | void psxRcntUpdate() | |
299 | { | |
300 | u32 cycle; | |
301 | ||
302 | cycle = psxRegs.cycle; | |
303 | ||
304 | // rcnt 0. | |
305 | if( cycle - rcnts[0].cycleStart >= rcnts[0].cycle ) | |
306 | { | |
307 | psxRcntReset( 0 ); | |
308 | } | |
309 | ||
310 | // rcnt 1. | |
311 | if( cycle - rcnts[1].cycleStart >= rcnts[1].cycle ) | |
312 | { | |
313 | psxRcntReset( 1 ); | |
314 | } | |
315 | ||
316 | // rcnt 2. | |
317 | if( cycle - rcnts[2].cycleStart >= rcnts[2].cycle ) | |
318 | { | |
319 | psxRcntReset( 2 ); | |
320 | } | |
321 | ||
322 | // rcnt base. | |
323 | if( cycle - rcnts[3].cycleStart >= rcnts[3].cycle ) | |
324 | { | |
61ef5cf4 | 325 | u32 leftover_cycles = cycle - rcnts[3].cycleStart - rcnts[3].cycle; |
d618a240 | 326 | u32 next_vsync; |
ef79bbde | 327 | |
61ef5cf4 | 328 | hSyncCount += hsync_steps; |
ef79bbde | 329 | |
ef79bbde | 330 | // VSync irq. |
0486fdc9 | 331 | if( hSyncCount == VBlankStart ) |
ef79bbde | 332 | { |
0486fdc9 | 333 | HW_GPU_STATUS &= ~PSXGPU_LCF; |
72e5023f | 334 | GPU_vBlank( 1, 0 ); |
8bbbd091 | 335 | setIrq( 0x01 ); |
336 | ||
337 | EmuUpdate(); | |
338 | GPU_updateLace(); | |
d618a240 | 339 | |
340 | if( SPU_async ) | |
341 | { | |
342 | SPU_async( cycle, 1 ); | |
343 | } | |
ef79bbde P |
344 | } |
345 | ||
346 | // Update lace. (with InuYasha fix) | |
347 | if( hSyncCount >= (Config.VSyncWA ? HSyncTotal[Config.PsxType] / BIAS : HSyncTotal[Config.PsxType]) ) | |
348 | { | |
349 | hSyncCount = 0; | |
ddbaf678 | 350 | frame_counter++; |
ef79bbde | 351 | |
0486fdc9 | 352 | gpuSyncPluginSR(); |
353 | if( (HW_GPU_STATUS & PSXGPU_ILACE_BITS) == PSXGPU_ILACE_BITS ) | |
ddbaf678 | 354 | HW_GPU_STATUS |= frame_counter << 31; |
72e5023f | 355 | GPU_vBlank( 0, HW_GPU_STATUS >> 31 ); |
ef79bbde | 356 | } |
61ef5cf4 | 357 | |
358 | // Schedule next call, in hsyncs | |
d618a240 | 359 | hsync_steps = HSyncTotal[Config.PsxType] - hSyncCount; |
0486fdc9 | 360 | next_vsync = VBlankStart - hSyncCount; // ok to overflow |
61ef5cf4 | 361 | if( next_vsync && next_vsync < hsync_steps ) |
362 | hsync_steps = next_vsync; | |
61ef5cf4 | 363 | |
364 | rcnts[3].cycleStart = cycle - leftover_cycles; | |
4f55097d | 365 | if (Config.PsxType) |
366 | // 20.12 precision, clk / 50 / 313 ~= 2164.14 | |
367 | base_cycle += hsync_steps * 8864320; | |
368 | else | |
369 | // clk / 60 / 263 ~= 2146.31 | |
370 | base_cycle += hsync_steps * 8791293; | |
371 | rcnts[3].cycle = base_cycle >> 12; | |
372 | base_cycle &= 0xfff; | |
ef79bbde P |
373 | } |
374 | ||
95df1a04 | 375 | psxRcntSet(); |
376 | ||
61ef5cf4 | 377 | #ifndef NDEBUG |
ef79bbde | 378 | DebugVSync(); |
61ef5cf4 | 379 | #endif |
ef79bbde P |
380 | } |
381 | ||
382 | /******************************************************************************/ | |
383 | ||
384 | void psxRcntWcount( u32 index, u32 value ) | |
385 | { | |
386 | verboseLog( 2, "[RCNT %i] wcount: %x\n", index, value ); | |
387 | ||
ef79bbde P |
388 | _psxRcntWcount( index, value ); |
389 | psxRcntSet(); | |
390 | } | |
391 | ||
392 | void psxRcntWmode( u32 index, u32 value ) | |
393 | { | |
394 | verboseLog( 1, "[RCNT %i] wmode: %x\n", index, value ); | |
395 | ||
a29f182f | 396 | _psxRcntWmode( index, value ); |
ef79bbde | 397 | _psxRcntWcount( index, 0 ); |
a29f182f | 398 | |
399 | rcnts[index].irqState = 0; | |
ef79bbde P |
400 | psxRcntSet(); |
401 | } | |
402 | ||
403 | void psxRcntWtarget( u32 index, u32 value ) | |
404 | { | |
405 | verboseLog( 1, "[RCNT %i] wtarget: %x\n", index, value ); | |
406 | ||
ef79bbde P |
407 | rcnts[index].target = value; |
408 | ||
409 | _psxRcntWcount( index, _psxRcntRcount( index ) ); | |
410 | psxRcntSet(); | |
411 | } | |
412 | ||
413 | /******************************************************************************/ | |
414 | ||
415 | u32 psxRcntRcount( u32 index ) | |
416 | { | |
417 | u32 count; | |
418 | ||
ef79bbde P |
419 | count = _psxRcntRcount( index ); |
420 | ||
421 | // Parasite Eve 2 fix. | |
422 | if( Config.RCntFix ) | |
423 | { | |
424 | if( index == 2 ) | |
425 | { | |
426 | if( rcnts[index].counterState == CountToTarget ) | |
427 | { | |
428 | count /= BIAS; | |
429 | } | |
430 | } | |
431 | } | |
432 | ||
433 | verboseLog( 2, "[RCNT %i] rcount: %x\n", index, count ); | |
434 | ||
435 | return count; | |
436 | } | |
437 | ||
438 | u32 psxRcntRmode( u32 index ) | |
439 | { | |
440 | u16 mode; | |
441 | ||
ef79bbde P |
442 | mode = rcnts[index].mode; |
443 | rcnts[index].mode &= 0xe7ff; | |
444 | ||
445 | verboseLog( 2, "[RCNT %i] rmode: %x\n", index, mode ); | |
446 | ||
447 | return mode; | |
448 | } | |
449 | ||
450 | u32 psxRcntRtarget( u32 index ) | |
451 | { | |
452 | verboseLog( 2, "[RCNT %i] rtarget: %x\n", index, rcnts[index].target ); | |
453 | ||
454 | return rcnts[index].target; | |
455 | } | |
456 | ||
457 | /******************************************************************************/ | |
458 | ||
459 | void psxRcntInit() | |
460 | { | |
461 | s32 i; | |
462 | ||
463 | // rcnt 0. | |
464 | rcnts[0].rate = 1; | |
465 | rcnts[0].irq = 0x10; | |
466 | ||
467 | // rcnt 1. | |
468 | rcnts[1].rate = 1; | |
469 | rcnts[1].irq = 0x20; | |
470 | ||
471 | // rcnt 2. | |
472 | rcnts[2].rate = 1; | |
473 | rcnts[2].irq = 0x40; | |
474 | ||
475 | // rcnt base. | |
476 | rcnts[3].rate = 1; | |
477 | rcnts[3].mode = RcCountToTarget; | |
478 | rcnts[3].target = (PSXCLK / (FrameRate[Config.PsxType] * HSyncTotal[Config.PsxType])); | |
479 | ||
480 | for( i = 0; i < CounterQuantity; ++i ) | |
481 | { | |
482 | _psxRcntWcount( i, 0 ); | |
483 | } | |
484 | ||
c62b43c9 | 485 | hSyncCount = 0; |
61ef5cf4 | 486 | hsync_steps = 1; |
c62b43c9 | 487 | |
ef79bbde P |
488 | psxRcntSet(); |
489 | } | |
490 | ||
491 | /******************************************************************************/ | |
492 | ||
496d88d4 | 493 | s32 psxRcntFreeze( void *f, s32 Mode ) |
ef79bbde | 494 | { |
d618a240 | 495 | u32 spuSyncCount = 0; |
a29f182f | 496 | u32 count; |
497 | s32 i; | |
498 | ||
93c9e42a | 499 | gzfreeze( &rcnts, sizeof(rcnts) ); |
ef79bbde P |
500 | gzfreeze( &hSyncCount, sizeof(hSyncCount) ); |
501 | gzfreeze( &spuSyncCount, sizeof(spuSyncCount) ); | |
502 | gzfreeze( &psxNextCounter, sizeof(psxNextCounter) ); | |
503 | gzfreeze( &psxNextsCounter, sizeof(psxNextsCounter) ); | |
504 | ||
61ef5cf4 | 505 | if (Mode == 0) |
a29f182f | 506 | { |
507 | // don't trust things from a savestate | |
508 | for( i = 0; i < CounterQuantity; ++i ) | |
509 | { | |
510 | _psxRcntWmode( i, rcnts[i].mode ); | |
511 | count = (psxRegs.cycle - rcnts[i].cycleStart) / rcnts[i].rate; | |
512 | _psxRcntWcount( i, count ); | |
513 | } | |
61ef5cf4 | 514 | hsync_steps = (psxRegs.cycle - rcnts[3].cycleStart) / rcnts[3].target; |
a29f182f | 515 | psxRcntSet(); |
61ef5cf4 | 516 | |
a29f182f | 517 | base_cycle = 0; |
518 | } | |
4f55097d | 519 | |
ef79bbde P |
520 | return 0; |
521 | } | |
522 | ||
523 | /******************************************************************************/ |