preliminary irq10 support
[pcsx_rearmed.git] / libpcsxcore / psxcounters.c
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"
25 #include "gpu.h"
26 //#include "debug.h"
27 #define DebugVSync()
28
29 /******************************************************************************/
30
31 enum
32 {
33     RcSyncModeEnable  = 0x0001, // 0
34     Rc01BlankPause    = 0 << 1, // 1,2
35     Rc01UnblankReset  = 1 << 1, // 1,2
36     Rc01UnblankReset2 = 2 << 1, // 1,2
37     Rc2Stop           = 0 << 1, // 1,2
38     Rc2Stop2          = 3 << 1, // 1,2
39     RcCountToTarget   = 0x0008, // 3
40     RcIrqOnTarget     = 0x0010, // 4
41     RcIrqOnOverflow   = 0x0020, // 5
42     RcIrqRegenerate   = 0x0040, // 6
43     RcUnknown7        = 0x0080, // 7    ?
44     Rc0PixelClock     = 0x0100, // 8    fake implementation
45     Rc1HSyncClock     = 0x0100, // 8
46     Rc2Unknown8       = 0x0100, // 8    ?
47     Rc0Unknown9       = 0x0200, // 9    ?
48     Rc1Unknown9       = 0x0200, // 9    ?
49     Rc2OneEighthClock = 0x0200, // 9
50     RcUnknown10       = 0x0400, // 10   ?
51     RcCountEqTarget   = 0x0800, // 11
52     RcOverflow        = 0x1000, // 12
53     RcUnknown13       = 0x2000, // 13   ? (always zero)
54     RcUnknown14       = 0x4000, // 14   ? (always zero)
55     RcUnknown15       = 0x8000, // 15   ? (always zero)
56 };
57
58 #define CounterQuantity           ( 4 )
59 //static const u32 CounterQuantity  = 4;
60
61 static const u32 CountToOverflow  = 0;
62 static const u32 CountToTarget    = 1;
63
64 static const u32 FrameRate[]      = { 60, 50 };
65 static const u32 HSyncTotal[]     = { 263, 314 }; // actually one more on odd lines for PAL
66 #define VBlankStart 240
67
68 #define VERBOSE_LEVEL 0
69
70 /******************************************************************************/
71 #ifdef DRC_DISABLE
72 Rcnt rcnts[ CounterQuantity ];
73 #endif
74 u32 hSyncCount = 0;
75 u32 frame_counter = 0;
76 static u32 hsync_steps = 0;
77
78 u32 psxNextCounter = 0, psxNextsCounter = 0;
79
80 /******************************************************************************/
81
82 static inline
83 void setIrq( u32 irq )
84 {
85     psxHu32ref(0x1070) |= SWAPu32(irq);
86 }
87
88 static
89 void verboseLog( u32 level, const char *str, ... )
90 {
91 #if VERBOSE_LEVEL > 0
92     if( level <= VERBOSE_LEVEL )
93     {
94         va_list va;
95         char buf[ 4096 ];
96
97         va_start( va, str );
98         vsprintf( buf, str, va );
99         va_end( va );
100
101         printf( "%s", buf );
102         fflush( stdout );
103     }
104 #endif
105 }
106
107 /******************************************************************************/
108
109 static inline
110 void _psxRcntWcount( u32 index, u32 value )
111 {
112     if( value > 0xffff )
113     {
114         verboseLog( 1, "[RCNT %i] wcount > 0xffff: %x\n", index, value );
115         value &= 0xffff;
116     }
117
118     rcnts[index].cycleStart  = psxRegs.cycle;
119     rcnts[index].cycleStart -= value * rcnts[index].rate;
120
121     // TODO: <=.
122     if( value < rcnts[index].target )
123     {
124         rcnts[index].cycle = rcnts[index].target * rcnts[index].rate;
125         rcnts[index].counterState = CountToTarget;
126     }
127     else
128     {
129         rcnts[index].cycle = 0x10000 * rcnts[index].rate;
130         rcnts[index].counterState = CountToOverflow;
131     }
132 }
133
134 static inline
135 u32 _psxRcntRcount( u32 index )
136 {
137     u32 count;
138
139     count  = psxRegs.cycle;
140     count -= rcnts[index].cycleStart;
141     if (rcnts[index].rate > 1)
142         count /= rcnts[index].rate;
143
144     if( count > 0x10000 )
145     {
146         verboseLog( 1, "[RCNT %i] rcount > 0x10000: %x\n", index, count );
147     }
148     count &= 0xffff;
149
150     return count;
151 }
152
153 static
154 void _psxRcntWmode( u32 index, u32 value )
155 {
156     rcnts[index].mode = value;
157
158     switch( index )
159     {
160         case 0:
161             if( value & Rc0PixelClock )
162             {
163                 rcnts[index].rate = 5;
164             }
165             else
166             {
167                 rcnts[index].rate = 1;
168             }
169         break;
170         case 1:
171             if( value & Rc1HSyncClock )
172             {
173                 rcnts[index].rate = (PSXCLK / (FrameRate[Config.PsxType] * HSyncTotal[Config.PsxType]));
174             }
175             else
176             {
177                 rcnts[index].rate = 1;
178             }
179         break;
180         case 2:
181             if( value & Rc2OneEighthClock )
182             {
183                 rcnts[index].rate = 8;
184             }
185             else
186             {
187                 rcnts[index].rate = 1;
188             }
189
190             // TODO: wcount must work.
191             if( (value & 7) == (RcSyncModeEnable | Rc2Stop) ||
192                 (value & 7) == (RcSyncModeEnable | Rc2Stop2) )
193             {
194                 rcnts[index].rate = 0xffffffff;
195             }
196         break;
197     }
198 }
199
200 /******************************************************************************/
201
202 static
203 void psxRcntSet()
204 {
205     s32 countToUpdate;
206     u32 i;
207
208     psxNextsCounter = psxRegs.cycle;
209     psxNextCounter  = 0x7fffffff;
210
211     for( i = 0; i < CounterQuantity; ++i )
212     {
213         countToUpdate = rcnts[i].cycle - (psxNextsCounter - rcnts[i].cycleStart);
214
215         if( countToUpdate < 0 )
216         {
217             psxNextCounter = 0;
218             break;
219         }
220
221         if( countToUpdate < (s32)psxNextCounter )
222         {
223             psxNextCounter = countToUpdate;
224         }
225     }
226
227     psxRegs.interrupt |= (1 << PSXINT_RCNT);
228     new_dyna_set_event(PSXINT_RCNT, psxNextCounter);
229 }
230
231 /******************************************************************************/
232
233 static
234 void psxRcntReset( u32 index )
235 {
236     u32 rcycles;
237
238     rcnts[index].mode |= RcUnknown10;
239
240     if( rcnts[index].counterState == CountToTarget )
241     {
242         rcycles = psxRegs.cycle - rcnts[index].cycleStart;
243         if( rcnts[index].mode & RcCountToTarget )
244         {
245             rcycles -= rcnts[index].target * rcnts[index].rate;
246             rcnts[index].cycleStart = psxRegs.cycle - rcycles;
247         }
248         else
249         {
250             rcnts[index].cycle = 0x10000 * rcnts[index].rate;
251             rcnts[index].counterState = CountToOverflow;
252         }
253
254         if( rcnts[index].mode & RcIrqOnTarget )
255         {
256             if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) )
257             {
258                 verboseLog( 3, "[RCNT %i] irq\n", index );
259                 setIrq( rcnts[index].irq );
260                 rcnts[index].irqState = 1;
261             }
262         }
263
264         rcnts[index].mode |= RcCountEqTarget;
265
266         if( rcycles < 0x10000 * rcnts[index].rate )
267             return;
268     }
269
270     if( rcnts[index].counterState == CountToOverflow )
271     {
272         rcycles = psxRegs.cycle - rcnts[index].cycleStart;
273         rcycles -= 0x10000 * rcnts[index].rate;
274
275         rcnts[index].cycleStart = psxRegs.cycle - rcycles;
276
277         if( rcycles < rcnts[index].target * rcnts[index].rate )
278         {
279             rcnts[index].cycle = rcnts[index].target * rcnts[index].rate;
280             rcnts[index].counterState = CountToTarget;
281         }
282
283         if( rcnts[index].mode & RcIrqOnOverflow )
284         {
285             if( (rcnts[index].mode & RcIrqRegenerate) || (!rcnts[index].irqState) )
286             {
287                 verboseLog( 3, "[RCNT %i] irq\n", index );
288                 setIrq( rcnts[index].irq );
289                 rcnts[index].irqState = 1;
290             }
291         }
292
293         rcnts[index].mode |= RcOverflow;
294     }
295 }
296
297 static void scheduleRcntBase(void)
298 {
299     // Schedule next call, in hsyncs
300     if (hSyncCount < VBlankStart)
301         hsync_steps = VBlankStart - hSyncCount;
302     else
303         hsync_steps = HSyncTotal[Config.PsxType] - hSyncCount;
304
305     if (hSyncCount + hsync_steps == HSyncTotal[Config.PsxType])
306     {
307         rcnts[3].cycle = Config.PsxType ? PSXCLK / 50 : PSXCLK / 60;
308     }
309     else
310     {
311         // clk / 50 / 314 ~= 2157.25
312         // clk / 60 / 263 ~= 2146.31
313         u32 mult = Config.PsxType ? 8836089 : 8791293;
314         rcnts[3].cycle = hsync_steps * mult >> 12;
315     }
316 }
317
318 void psxRcntUpdate()
319 {
320     u32 cycle, cycles_passed;
321
322     cycle = psxRegs.cycle;
323
324     // rcnt 0.
325     cycles_passed = cycle - rcnts[0].cycleStart;
326     while( cycles_passed >= rcnts[0].cycle )
327     {
328         if (((rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
329              (rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
330             && cycles_passed > PSXCLK / 60 / 263)
331         {
332             u32 q = cycles_passed / (PSXCLK / 60 / 263 + 1u);
333             rcnts[0].cycleStart += q * (PSXCLK / 60) / 263u;
334             break;
335         }
336         else
337             psxRcntReset( 0 );
338
339         cycles_passed = cycle - rcnts[0].cycleStart;
340     }
341
342     // rcnt 1.
343     while( cycle - rcnts[1].cycleStart >= rcnts[1].cycle )
344     {
345         psxRcntReset( 1 );
346     }
347
348     // rcnt 2.
349     while( cycle - rcnts[2].cycleStart >= rcnts[2].cycle )
350     {
351         psxRcntReset( 2 );
352     }
353
354     // rcnt base.
355     if( cycle - rcnts[3].cycleStart >= rcnts[3].cycle )
356     {
357         hSyncCount += hsync_steps;
358
359         // VSync irq.
360         if( hSyncCount == VBlankStart )
361         {
362             HW_GPU_STATUS &= SWAP32(~PSXGPU_LCF);
363             GPU_vBlank( 1, 0 );
364             setIrq( 0x01 );
365
366             EmuUpdate();
367             GPU_updateLace();
368
369             if( SPU_async )
370             {
371                 SPU_async( cycle, 1 );
372             }
373         }
374         
375         // Update lace.
376         if( hSyncCount >= HSyncTotal[Config.PsxType] )
377         {
378             u32 status, field = 0, i;
379             rcnts[3].cycleStart += Config.PsxType ? PSXCLK / 50 : PSXCLK / 60;
380             hSyncCount = 0;
381             frame_counter++;
382
383             gpuSyncPluginSR();
384             status = SWAP32(HW_GPU_STATUS) | PSXGPU_FIELD;
385             if ((status & PSXGPU_ILACE_BITS) == PSXGPU_ILACE_BITS) {
386                 field = frame_counter & 1;
387                 status |= field << 31;
388                 status ^= field << 13;
389             }
390             HW_GPU_STATUS = SWAP32(status);
391             GPU_vBlank(0, field);
392
393             for (i = 0; i < 2; i++)
394             {
395                 if ((rcnts[i].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
396                     (rcnts[i].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
397                 {
398                     rcnts[i].cycleStart = rcnts[3].cycleStart;
399                 }
400             }
401         }
402
403         scheduleRcntBase();
404     }
405
406     psxRcntSet();
407
408 #if 0 //ndef NDEBUG
409     DebugVSync();
410 #endif
411 }
412
413 /******************************************************************************/
414
415 void psxRcntWcount( u32 index, u32 value )
416 {
417     verboseLog( 2, "[RCNT %i] wcount: %x\n", index, value );
418
419     _psxRcntWcount( index, value );
420     psxRcntSet();
421 }
422
423 void psxRcntWmode( u32 index, u32 value )
424 {
425     verboseLog( 1, "[RCNT %i] wmode: %x\n", index, value );
426
427     _psxRcntWmode( index, value );
428     _psxRcntWcount( index, 0 );
429
430     rcnts[index].irqState = 0;
431     psxRcntSet();
432 }
433
434 void psxRcntWtarget( u32 index, u32 value )
435 {
436     verboseLog( 1, "[RCNT %i] wtarget: %x\n", index, value );
437
438     rcnts[index].target = value;
439
440     _psxRcntWcount( index, _psxRcntRcount( index ) );
441     psxRcntSet();
442 }
443
444 /******************************************************************************/
445
446 u32 psxRcntRcount0()
447 {
448     u32 index = 0;
449     u32 count;
450
451     if ((rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset) ||
452         (rcnts[0].mode & 7) == (RcSyncModeEnable | Rc01UnblankReset2))
453     {
454         count = psxRegs.cycle - rcnts[index].cycleStart;
455         count = ((16u * count) % (16u * PSXCLK / 60 / 263)) / 16u;
456         rcnts[index].cycleStart = psxRegs.cycle - count;
457     }
458     else
459         count = _psxRcntRcount( index );
460
461     verboseLog( 2, "[RCNT 0] rcount: %04x m: %04x\n", count, rcnts[index].mode);
462
463     return count;
464 }
465
466 u32 psxRcntRcount1()
467 {
468     u32 index = 1;
469     u32 count;
470
471     count = _psxRcntRcount( index );
472
473     verboseLog( 2, "[RCNT 1] rcount: %04x m: %04x\n", count, rcnts[index].mode);
474
475     return count;
476 }
477
478 u32 psxRcntRcount2()
479 {
480     u32 index = 2;
481     u32 count;
482
483     count = _psxRcntRcount( index );
484
485     verboseLog( 2, "[RCNT 2] rcount: %04x m: %04x\n", count, rcnts[index].mode);
486
487     return count;
488 }
489
490 u32 psxRcntRmode( u32 index )
491 {
492     u16 mode;
493
494     mode = rcnts[index].mode;
495     rcnts[index].mode &= 0xe7ff;
496
497     verboseLog( 2, "[RCNT %i] rmode: %x\n", index, mode );
498
499     return mode;
500 }
501
502 u32 psxRcntRtarget( u32 index )
503 {
504     verboseLog( 2, "[RCNT %i] rtarget: %x\n", index, rcnts[index].target );
505
506     return rcnts[index].target;
507 }
508
509 /******************************************************************************/
510
511 void psxRcntInit()
512 {
513     s32 i;
514
515     // rcnt 0.
516     rcnts[0].rate   = 1;
517     rcnts[0].irq    = 0x10;
518
519     // rcnt 1.
520     rcnts[1].rate   = 1;
521     rcnts[1].irq    = 0x20;
522
523     // rcnt 2.
524     rcnts[2].rate   = 1;
525     rcnts[2].irq    = 0x40;
526
527     // rcnt base.
528     rcnts[3].rate   = 1;
529     rcnts[3].mode   = RcCountToTarget;
530     rcnts[3].target = (PSXCLK / (FrameRate[Config.PsxType] * HSyncTotal[Config.PsxType]));
531
532     for( i = 0; i < CounterQuantity; ++i )
533     {
534         _psxRcntWcount( i, 0 );
535     }
536
537     hSyncCount = 0;
538     hsync_steps = 1;
539
540     psxRcntSet();
541 }
542
543 /******************************************************************************/
544
545 s32 psxRcntFreeze( void *f, s32 Mode )
546 {
547     u32 spuSyncCount = 0;
548     u32 count;
549     s32 i;
550
551     gzfreeze( &rcnts, sizeof(Rcnt) * CounterQuantity );
552     gzfreeze( &hSyncCount, sizeof(hSyncCount) );
553     gzfreeze( &spuSyncCount, sizeof(spuSyncCount) );
554     gzfreeze( &psxNextCounter, sizeof(psxNextCounter) );
555     gzfreeze( &psxNextsCounter, sizeof(psxNextsCounter) );
556
557     if (Mode == 0)
558     {
559         // don't trust things from a savestate
560         rcnts[3].rate = 1;
561         for( i = 0; i < CounterQuantity; ++i )
562         {
563             _psxRcntWmode( i, rcnts[i].mode );
564             count = (psxRegs.cycle - rcnts[i].cycleStart) / rcnts[i].rate;
565             _psxRcntWcount( i, count );
566         }
567         scheduleRcntBase();
568         psxRcntSet();
569     }
570
571     return 0;
572 }
573
574 /******************************************************************************/
575 // vim:ts=4:shiftwidth=4:expandtab