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"
27 /******************************************************************************/
32 u32 rate, irq, counterState, irqState;
33 u32 cycle, cycleStart;
38 Rc0Gate = 0x0001, // 0 not implemented
39 Rc1Gate = 0x0001, // 0 not implemented
40 Rc2Disable = 0x0001, // 0 partially implemented
41 RcUnknown1 = 0x0002, // 1 ?
42 RcUnknown2 = 0x0004, // 2 ?
43 RcCountToTarget = 0x0008, // 3
44 RcIrqOnTarget = 0x0010, // 4
45 RcIrqOnOverflow = 0x0020, // 5
46 RcIrqRegenerate = 0x0040, // 6
47 RcUnknown7 = 0x0080, // 7 ?
48 Rc0PixelClock = 0x0100, // 8 fake implementation
49 Rc1HSyncClock = 0x0100, // 8
50 Rc2Unknown8 = 0x0100, // 8 ?
51 Rc0Unknown9 = 0x0200, // 9 ?
52 Rc1Unknown9 = 0x0200, // 9 ?
53 Rc2OneEighthClock = 0x0200, // 9
54 RcUnknown10 = 0x0400, // 10 ?
55 RcCountEqTarget = 0x0800, // 11
56 RcOverflow = 0x1000, // 12
57 RcUnknown13 = 0x2000, // 13 ? (always zero)
58 RcUnknown14 = 0x4000, // 14 ? (always zero)
59 RcUnknown15 = 0x8000, // 15 ? (always zero)
62 #define CounterQuantity ( 4 )
63 //static const u32 CounterQuantity = 4;
65 static const u32 CountToOverflow = 0;
66 static const u32 CountToTarget = 1;
68 static const u32 FrameRate[] = { 60, 50 };
69 static const u32 VBlankStart[] = { 240, 256 };
70 static const u32 HSyncTotal[] = { 263, 313 };
71 static const u32 SpuUpdInterval[] = { 23, 22 };
73 static const s32 VerboseLevel = 0;
75 /******************************************************************************/
77 static Rcnt rcnts[ CounterQuantity ];
79 static u32 hSyncCount = 0;
80 static u32 spuSyncCount = 0;
82 u32 psxNextCounter = 0, psxNextsCounter = 0;
84 /******************************************************************************/
87 void setIrq( u32 irq )
89 psxHu32ref(0x1070) |= SWAPu32(irq);
93 void verboseLog( s32 level, const char *str, ... )
95 if( level <= VerboseLevel )
101 vsprintf( buf, str, va );
109 /******************************************************************************/
112 void _psxRcntWcount( u32 index, u32 value )
116 verboseLog( 1, "[RCNT %i] wcount > 0xffff: %x\n", index, value );
120 rcnts[index].cycleStart = psxRegs.cycle;
121 rcnts[index].cycleStart -= value * rcnts[index].rate;
124 if( value < rcnts[index].target )
126 rcnts[index].cycle = rcnts[index].target * rcnts[index].rate;
127 rcnts[index].counterState = CountToTarget;
131 rcnts[index].cycle = 0xffff * rcnts[index].rate;
132 rcnts[index].counterState = CountToOverflow;
137 u32 _psxRcntRcount( u32 index )
141 count = psxRegs.cycle;
142 count -= rcnts[index].cycleStart;
143 count /= rcnts[index].rate;
147 verboseLog( 1, "[RCNT %i] rcount > 0xffff: %x\n", index, count );
154 /******************************************************************************/
162 psxNextsCounter = psxRegs.cycle;
163 psxNextCounter = 0x7fffffff;
165 for( i = 0; i < CounterQuantity; ++i )
167 countToUpdate = rcnts[i].cycle - (psxNextsCounter - rcnts[i].cycleStart);
169 if( countToUpdate < 0 )
175 if( countToUpdate < (s32)psxNextCounter )
177 psxNextCounter = countToUpdate;
182 /******************************************************************************/
185 void psxRcntReset( u32 index )
189 if( rcnts[index].counterState == CountToTarget )
191 if( rcnts[index].mode & RcCountToTarget )
193 count = psxRegs.cycle;
194 count -= rcnts[index].cycleStart;
195 count /= rcnts[index].rate;
196 count -= rcnts[index].target;
200 count = _psxRcntRcount( index );
203 _psxRcntWcount( index, count );
205 if( rcnts[index].mode & RcIrqOnTarget )
207 if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) )
209 verboseLog( 3, "[RCNT %i] irq: %x\n", index, count );
210 setIrq( rcnts[index].irq );
211 rcnts[index].irqState = 1;
215 rcnts[index].mode |= RcCountEqTarget;
217 else if( rcnts[index].counterState == CountToOverflow )
219 count = psxRegs.cycle;
220 count -= rcnts[index].cycleStart;
221 count /= rcnts[index].rate;
224 _psxRcntWcount( index, count );
226 if( rcnts[index].mode & RcIrqOnOverflow )
228 if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) )
230 verboseLog( 3, "[RCNT %i] irq: %x\n", index, count );
231 setIrq( rcnts[index].irq );
232 rcnts[index].irqState = 1;
236 rcnts[index].mode |= RcOverflow;
239 rcnts[index].mode |= RcUnknown10;
248 cycle = psxRegs.cycle;
251 if( cycle - rcnts[0].cycleStart >= rcnts[0].cycle )
257 if( cycle - rcnts[1].cycleStart >= rcnts[1].cycle )
263 if( cycle - rcnts[2].cycleStart >= rcnts[2].cycle )
269 if( cycle - rcnts[3].cycleStart >= rcnts[3].cycle )
277 if( spuSyncCount >= SpuUpdInterval[Config.PsxType] )
283 SPU_async( SpuUpdInterval[Config.PsxType] * rcnts[3].target );
288 if( hSyncCount == VBlankStart[Config.PsxType] )
290 GPU_vBlank( 1, &hSyncCount );
292 // For the best times. :D
296 // Update lace. (with InuYasha fix)
297 if( hSyncCount >= (Config.VSyncWA ? HSyncTotal[Config.PsxType] / BIAS : HSyncTotal[Config.PsxType]) )
301 GPU_vBlank( 0, &hSyncCount );
312 /******************************************************************************/
314 void psxRcntWcount( u32 index, u32 value )
316 verboseLog( 2, "[RCNT %i] wcount: %x\n", index, value );
320 _psxRcntWcount( index, value );
324 void psxRcntWmode( u32 index, u32 value )
326 verboseLog( 1, "[RCNT %i] wmode: %x\n", index, value );
330 rcnts[index].mode = value;
331 rcnts[index].irqState = 0;
336 if( value & Rc0PixelClock )
338 rcnts[index].rate = 5;
342 rcnts[index].rate = 1;
346 if( value & Rc1HSyncClock )
348 rcnts[index].rate = (PSXCLK / (FrameRate[Config.PsxType] * HSyncTotal[Config.PsxType]));
352 rcnts[index].rate = 1;
356 if( value & Rc2OneEighthClock )
358 rcnts[index].rate = 8;
362 rcnts[index].rate = 1;
365 // TODO: wcount must work.
366 if( value & Rc2Disable )
368 rcnts[index].rate = 0xffffffff;
373 _psxRcntWcount( index, 0 );
377 void psxRcntWtarget( u32 index, u32 value )
379 verboseLog( 1, "[RCNT %i] wtarget: %x\n", index, value );
383 rcnts[index].target = value;
385 _psxRcntWcount( index, _psxRcntRcount( index ) );
389 /******************************************************************************/
391 u32 psxRcntRcount( u32 index )
397 count = _psxRcntRcount( index );
399 // Parasite Eve 2 fix.
404 if( rcnts[index].counterState == CountToTarget )
411 verboseLog( 2, "[RCNT %i] rcount: %x\n", index, count );
416 u32 psxRcntRmode( u32 index )
422 mode = rcnts[index].mode;
423 rcnts[index].mode &= 0xe7ff;
425 verboseLog( 2, "[RCNT %i] rmode: %x\n", index, mode );
430 u32 psxRcntRtarget( u32 index )
432 verboseLog( 2, "[RCNT %i] rtarget: %x\n", index, rcnts[index].target );
434 return rcnts[index].target;
437 /******************************************************************************/
457 rcnts[3].mode = RcCountToTarget;
458 rcnts[3].target = (PSXCLK / (FrameRate[Config.PsxType] * HSyncTotal[Config.PsxType]));
460 for( i = 0; i < CounterQuantity; ++i )
462 _psxRcntWcount( i, 0 );
471 /******************************************************************************/
473 s32 psxRcntFreeze( gzFile f, s32 Mode )
475 gzfreeze( &rcnts, sizeof(rcnts) );
476 gzfreeze( &hSyncCount, sizeof(hSyncCount) );
477 gzfreeze( &spuSyncCount, sizeof(spuSyncCount) );
478 gzfreeze( &psxNextCounter, sizeof(psxNextCounter) );
479 gzfreeze( &psxNextsCounter, sizeof(psxNextsCounter) );
484 /******************************************************************************/