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