1 /***************************************************************************
2 * Copyright (C) 2010 by Blade_Arma *
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. *
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. *
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 ***************************************************************************/
21 * Internal PSX counters.
24 #include "psxcounters.h"
25 #include "psxevents.h"
30 /******************************************************************************/
34 RcSyncModeEnable = 0x0001, // 0
35 Rc01BlankPause = 0 << 1, // 1,2
36 Rc01UnblankReset = 1 << 1, // 1,2
37 Rc01UnblankReset2 = 2 << 1, // 1,2
38 Rc2Stop = 0 << 1, // 1,2
39 Rc2Stop2 = 3 << 1, // 1,2
40 RcCountToTarget = 0x0008, // 3
41 RcIrqOnTarget = 0x0010, // 4
42 RcIrqOnOverflow = 0x0020, // 5
43 RcIrqRegenerate = 0x0040, // 6
44 RcUnknown7 = 0x0080, // 7 ?
45 Rc0PixelClock = 0x0100, // 8 fake implementation
46 Rc1HSyncClock = 0x0100, // 8
47 Rc2Unknown8 = 0x0100, // 8 ?
48 Rc0Unknown9 = 0x0200, // 9 ?
49 Rc1Unknown9 = 0x0200, // 9 ?
50 Rc2OneEighthClock = 0x0200, // 9
51 RcUnknown10 = 0x0400, // 10 ?
52 RcCountEqTarget = 0x0800, // 11
53 RcOverflow = 0x1000, // 12
54 RcUnknown13 = 0x2000, // 13 ? (always zero)
55 RcUnknown14 = 0x4000, // 14 ? (always zero)
56 RcUnknown15 = 0x8000, // 15 ? (always zero)
59 #define CounterQuantity ( 4 )
60 //static const u32 CounterQuantity = 4;
62 static const u32 CountToOverflow = 0;
63 static const u32 CountToTarget = 1;
65 static const u32 HSyncTotal[] = { 263, 314 };
66 #define VBlankStart 240 // todo: depend on the actual GPU setting
68 #define VERBOSE_LEVEL 0
70 /******************************************************************************/
72 Rcnt rcnts[ CounterQuantity ];
74 unsigned int hSyncCount = 0;
75 unsigned int frame_counter = 0;
76 static u32 hsync_steps = 0;
78 /******************************************************************************/
80 #define FPS_FRACTIONAL_PAL (53203425/314./3406) // ~49.75
81 #define FPS_FRACTIONAL_NTSC (53693175/263./3413) // ~59.81
86 int ff = Config.FractionalFramerate >= 0
87 ? Config.FractionalFramerate : Config.hacks.fractional_Framerate;
91 return (u32)(PSXCLK / FPS_FRACTIONAL_PAL);
93 return (u32)(PSXCLK / FPS_FRACTIONAL_NTSC);
95 return Config.PsxType ? (PSXCLK / 50) : (PSXCLK / 60);
98 // used to inform the frontend about the exact framerate
101 int ff = Config.FractionalFramerate >= 0
102 ? Config.FractionalFramerate : Config.hacks.fractional_Framerate;
104 return Config.PsxType ? FPS_FRACTIONAL_PAL : FPS_FRACTIONAL_NTSC;
106 return Config.PsxType ? 50.0 : 60.0;
109 // to inform the frontend about the exact famerate
113 // should be more like above, but our timing is already poor anyway
115 return PSXCLK / 50 / HSyncTotal[1];
117 return PSXCLK / 60 / HSyncTotal[0];
121 void setIrq( u32 irq )
123 psxHu32ref(0x1070) |= SWAPu32(irq);
127 void verboseLog( u32 level, const char *str, ... )
129 #if VERBOSE_LEVEL > 0
130 if( level <= VERBOSE_LEVEL )
136 vsprintf( buf, str, va );
145 /******************************************************************************/
148 void _psxRcntWcount( u32 index, u32 value )
152 rcnts[index].cycleStart = psxRegs.cycle;
153 rcnts[index].cycleStart -= value * rcnts[index].rate;
156 if( value < rcnts[index].target )
158 rcnts[index].cycle = rcnts[index].target * rcnts[index].rate;
159 rcnts[index].counterState = CountToTarget;
163 rcnts[index].cycle = 0x10000 * rcnts[index].rate;
164 rcnts[index].counterState = CountToOverflow;
169 u32 _psxRcntRcount( u32 index )
173 count = psxRegs.cycle;
174 count -= rcnts[index].cycleStart;
175 if (rcnts[index].rate > 1)
176 count /= rcnts[index].rate;
178 if( count > 0x10000 )
180 verboseLog( 1, "[RCNT %i] rcount > 0x10000: %x\n", index, count );
188 void _psxRcntWmode( u32 index, u32 value )
190 rcnts[index].mode = value;
195 if( value & Rc0PixelClock )
197 rcnts[index].rate = 5;
201 rcnts[index].rate = 1;
205 if( value & Rc1HSyncClock )
207 rcnts[index].rate = lineCycles();
211 rcnts[index].rate = 1;
215 if( value & Rc2OneEighthClock )
217 rcnts[index].rate = 8;
221 rcnts[index].rate = 1;
224 // TODO: wcount must work.
225 if( (value & 7) == (RcSyncModeEnable | Rc2Stop) ||
226 (value & 7) == (RcSyncModeEnable | Rc2Stop2) )
228 rcnts[index].rate = 0xffffffff;
234 /******************************************************************************/
242 psxRegs.psxNextsCounter = psxRegs.cycle;
243 psxRegs.psxNextCounter = 0x7fffffff;
245 for( i = 0; i < CounterQuantity; ++i )
247 countToUpdate = rcnts[i].cycle - (psxRegs.psxNextsCounter - rcnts[i].cycleStart);
249 if( countToUpdate < 0 )
251 psxRegs.psxNextCounter = 0;
255 if( countToUpdate < (s32)psxRegs.psxNextCounter )
257 psxRegs.psxNextCounter = countToUpdate;
261 set_event(PSXINT_RCNT, psxRegs.psxNextCounter);
264 /******************************************************************************/
267 void psxRcntReset( u32 index )
271 rcnts[index].mode |= RcUnknown10;
273 if( rcnts[index].counterState == CountToTarget )
275 rcycles = psxRegs.cycle - rcnts[index].cycleStart;
276 if( rcnts[index].mode & RcCountToTarget )
278 rcycles -= rcnts[index].target * rcnts[index].rate;
279 rcnts[index].cycleStart = psxRegs.cycle - rcycles;
283 rcnts[index].cycle = 0x10000 * rcnts[index].rate;
284 rcnts[index].counterState = CountToOverflow;
287 if( rcnts[index].mode & RcIrqOnTarget )
289 if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) )
291 verboseLog( 3, "[RCNT %i] irq\n", index );
292 setIrq( rcnts[index].irq );
293 rcnts[index].irqState = 1;
297 rcnts[index].mode |= RcCountEqTarget;
299 if( rcycles < 0x10000 * rcnts[index].rate )
303 if( rcnts[index].counterState == CountToOverflow )
305 rcycles = psxRegs.cycle - rcnts[index].cycleStart;
306 rcycles -= 0x10000 * rcnts[index].rate;
308 rcnts[index].cycleStart = psxRegs.cycle - rcycles;
310 if( rcycles < rcnts[index].target * rcnts[index].rate )
312 rcnts[index].cycle = rcnts[index].target * rcnts[index].rate;
313 rcnts[index].counterState = CountToTarget;
316 if( rcnts[index].mode & RcIrqOnOverflow )
318 if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) )
320 verboseLog( 3, "[RCNT %i] irq\n", index );
321 setIrq( rcnts[index].irq );
322 rcnts[index].irqState = 1;
326 rcnts[index].mode |= RcOverflow;
330 static void scheduleRcntBase(void)
332 // Schedule next call, in hsyncs
333 if (hSyncCount < VBlankStart)
334 hsync_steps = VBlankStart - hSyncCount;
336 hsync_steps = HSyncTotal[Config.PsxType] - hSyncCount;
338 if (hSyncCount + hsync_steps == HSyncTotal[Config.PsxType])
340 rcnts[3].cycle = frameCycles();
344 // clk / 50 / 314 ~= 2157.25
345 // clk / 60 / 263 ~= 2146.31
346 u32 mult = Config.PsxType ? 8836089 : 8791293;
347 rcnts[3].cycle = hsync_steps * mult >> 12;
353 u32 cycle, cycles_passed;
355 cycle = psxRegs.cycle;
358 cycles_passed = cycle - rcnts[0].cycleStart;
359 while( cycles_passed >= rcnts[0].cycle )
361 if (((rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
362 (rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
363 && cycles_passed > lineCycles())
365 u32 q = cycles_passed / (lineCycles() + 1u);
366 rcnts[0].cycleStart += q * lineCycles();
372 cycles_passed = cycle - rcnts[0].cycleStart;
376 while( cycle - rcnts[1].cycleStart >= rcnts[1].cycle )
382 while( cycle - rcnts[2].cycleStart >= rcnts[2].cycle )
388 if( cycle - rcnts[3].cycleStart >= rcnts[3].cycle )
390 hSyncCount += hsync_steps;
393 if( hSyncCount == VBlankStart )
395 HW_GPU_STATUS &= SWAP32(~PSXGPU_LCF);
404 SPU_async( cycle, 1 );
409 if( hSyncCount >= HSyncTotal[Config.PsxType] )
411 u32 status, field = 0;
412 rcnts[3].cycleStart += frameCycles();
417 status = SWAP32(HW_GPU_STATUS) | PSXGPU_FIELD;
418 if ((status & PSXGPU_ILACE_BITS) == PSXGPU_ILACE_BITS) {
419 field = frame_counter & 1;
420 status |= field << 31;
421 status ^= field << 13;
423 HW_GPU_STATUS = SWAP32(status);
424 GPU_vBlank(0, field);
425 if ((s32)(psxRegs.gpuIdleAfter - psxRegs.cycle) < 0)
426 psxRegs.gpuIdleAfter = psxRegs.cycle - 1; // prevent overflow
428 if ((rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
429 (rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
431 rcnts[0].cycleStart = rcnts[3].cycleStart;
434 if ((rcnts[1].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
435 (rcnts[1].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
437 rcnts[1].cycleStart = rcnts[3].cycleStart;
439 else if (rcnts[1].mode & Rc1HSyncClock)
441 // adjust to remove the rounding error
442 _psxRcntWcount(1, (psxRegs.cycle - rcnts[1].cycleStart) / rcnts[1].rate);
456 /******************************************************************************/
458 void psxRcntWcount( u32 index, u32 value )
460 verboseLog( 2, "[RCNT %i] wcount: %x\n", index, value );
462 _psxRcntWcount( index, value );
466 void psxRcntWmode( u32 index, u32 value )
468 verboseLog( 1, "[RCNT %i] wmode: %x\n", index, value );
470 _psxRcntWmode( index, value );
471 _psxRcntWcount( index, 0 );
473 rcnts[index].irqState = 0;
477 void psxRcntWtarget( u32 index, u32 value )
479 verboseLog( 1, "[RCNT %i] wtarget: %x\n", index, value );
481 rcnts[index].target = value;
483 _psxRcntWcount( index, _psxRcntRcount( index ) );
487 /******************************************************************************/
494 if ((rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
495 (rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
497 count = psxRegs.cycle - rcnts[index].cycleStart;
498 //count = ((16u * count) % (16u * PSXCLK / 60 / 263)) / 16u;
499 count = count % lineCycles();
500 rcnts[index].cycleStart = psxRegs.cycle - count;
503 count = _psxRcntRcount( index );
505 verboseLog( 2, "[RCNT 0] rcount: %04x m: %04x\n", count, rcnts[index].mode);
515 count = _psxRcntRcount( index );
517 verboseLog( 2, "[RCNT 1] rcount: %04x m: %04x\n", count, rcnts[index].mode);
527 count = _psxRcntRcount( index );
529 verboseLog( 2, "[RCNT 2] rcount: %04x m: %04x\n", count, rcnts[index].mode);
534 u32 psxRcntRmode( u32 index )
538 mode = rcnts[index].mode;
539 rcnts[index].mode &= 0xe7ff;
541 verboseLog( 2, "[RCNT %i] rmode: %x\n", index, mode );
546 u32 psxRcntRtarget( u32 index )
548 verboseLog( 2, "[RCNT %i] rtarget: %x\n", index, rcnts[index].target );
550 return rcnts[index].target;
553 /******************************************************************************/
574 for( i = 0; i < CounterQuantity; ++i )
576 _psxRcntWcount( i, 0 );
586 /******************************************************************************/
588 s32 psxRcntFreeze( void *f, s32 Mode )
590 u32 spuSyncCount = 0;
594 gzfreeze( &rcnts, sizeof(Rcnt) * CounterQuantity );
595 gzfreeze( &hSyncCount, sizeof(hSyncCount) );
596 gzfreeze( &spuSyncCount, sizeof(spuSyncCount) );
597 gzfreeze( &psxRegs.psxNextCounter, sizeof(psxRegs.psxNextCounter) );
598 gzfreeze( &psxRegs.psxNextsCounter, sizeof(psxRegs.psxNextsCounter) );
603 for( i = 0; i < CounterQuantity - 1; ++i )
605 _psxRcntWmode( i, rcnts[i].mode );
606 count = (psxRegs.cycle - rcnts[i].cycleStart) / rcnts[i].rate;
608 _psxRcntWcount( i, count & 0xffff );
617 /******************************************************************************/
618 // vim:ts=4:shiftwidth=4:expandtab