formated and cleaned up counters.c a little bit.

git-svn-id: http://pcsx2-playground.googlecode.com/svn/trunk@182 a6443dda-0b58-4228-96e9-037be469359c
This commit is contained in:
cottonvibes 2008-10-08 22:40:21 +00:00 committed by Gregory Hainaut
parent 706dc3689b
commit d63edda5ca
1 changed files with 120 additions and 179 deletions

View File

@ -62,31 +62,23 @@ void rcntSet() {
for (i = 0; i < 4; i++) {
if (!(counters[i].mode & 0x80) || (counters[i].mode & 0x3) == 0x3) continue; // Stopped
c = ((0x10000 - counters[i].count) * counters[i].rate) - (cpuRegs.cycle - counters[i].sCycleT);
if (c < nextCounter) {
nextCounter = c;
}
// the + 10 is just in case of overflow
//if(!(counters[i].mode & 0x100) || counters[i].target > 0xffff) continue;
c = ((counters[i].target - counters[i].count) * counters[i].rate) - (cpuRegs.cycle - counters[i].sCycleT);
if (c < nextCounter) {
nextCounter = c;
}
c = ((0x10000 - counters[i].count) * counters[i].rate) - (cpuRegs.cycle - counters[i].sCycleT);
if (c < nextCounter) nextCounter = c;
//if(!(counters[i].mode & 0x100) || counters[i].target > 0xffff) continue;
c = ((counters[i].target - counters[i].count) * counters[i].rate) - (cpuRegs.cycle - counters[i].sCycleT);
if (c < nextCounter) nextCounter = c;
}
//Calculate HBlank
c = counters[4].CycleT - (cpuRegs.cycle - counters[4].sCycleT);
if (c < nextCounter) {
nextCounter = c;
}
if (c < nextCounter) nextCounter = c;
//if(nextCounter > 0x1000) SysPrintf("Nextcounter %x HBlank %x VBlank %x\n", nextCounter, c, counters[5].CycleT - (cpuRegs.cycle - counters[5].sCycleT));
//Calculate VBlank
c = counters[5].CycleT - (cpuRegs.cycle - counters[5].sCycleT);
if (c < nextCounter) {
nextCounter = c;
}
if (c < nextCounter) nextCounter = c;
}
void rcntInit() {
@ -106,8 +98,6 @@ void rcntInit() {
counters[4].mode = 0x3c0; // The VSync counter mode
counters[5].mode = 0x3c0;
UpdateVSyncRate();
/*hblankend = 0;
counters[5].mode &= ~0x10000;
@ -166,16 +156,15 @@ void UpdateVSyncRate() {
if (Config.PsxType & 1) {
SysPrintf("PAL\n");
counters[4].Cycle = 227000;
/*if(Config.PsxType & 2)counters[5].rate = PS2VBLANK_PAL_INT;
else counters[5].rate = PS2VBLANK_PAL;*/
//if(Config.PsxType & 2)counters[5].rate = PS2VBLANK_PAL_INT;
//else counters[5].rate = PS2VBLANK_PAL;
counters[5].Cycle = 720;
} else {
}
else {
SysPrintf("NTSC\n");
counters[4].Cycle = 227000;
/*if(Config.PsxType & 2)counters[5].rate = PS2VBLANK_NTSC_INT;
else counters[5].rate = PS2VBLANK_NTSC;*/
//if(Config.PsxType & 2)counters[5].rate = PS2VBLANK_NTSC_INT;
//else counters[5].rate = PS2VBLANK_NTSC;
counters[5].Cycle = 720;
}
@ -190,39 +179,35 @@ void UpdateVSyncRate() {
//rcntUpdTarget(4);
//counters[4].CycleT = counters[4].rate;
///rcntUpdTarget(5);
/*counters[5].CycleT = counters[5].rate;
counters[5].Cycle = PS2VBLANKEND;*/
//rcntUpdTarget(5);
//counters[5].CycleT = counters[5].rate;
//counters[5].Cycle = PS2VBLANKEND;
{
if (Config.CustomFps > 0) {
iTicks = GetTickFrequency() / Config.CustomFps;
SysPrintf("Framelimiter rate updated (UpdateVSyncRate): %d fps\n", Config.CustomFps);
}
else if (Config.PsxType & 1) {
iTicks = (GetTickFrequency() / 5000) * 100;
SysPrintf("Framelimiter rate updated (UpdateVSyncRate): 50 fps\n");
}
else {
iTicks = (GetTickFrequency() / 5994) * 100;
SysPrintf("Framelimiter rate updated (UpdateVSyncRate): 59.94 fps\n");
}
if (Config.CustomFps > 0) {
iTicks = GetTickFrequency() / Config.CustomFps;
SysPrintf("Framelimiter rate updated (UpdateVSyncRate): %d fps\n", Config.CustomFps);
}
else if (Config.PsxType & 1) {
iTicks = (GetTickFrequency() / 5000) * 100;
SysPrintf("Framelimiter rate updated (UpdateVSyncRate): 50 fps\n");
}
else {
iTicks = (GetTickFrequency() / 5994) * 100;
SysPrintf("Framelimiter rate updated (UpdateVSyncRate): 59.94 fps\n");
}
rcntSet();
}
void FrameLimiter()
{
static u64 iStart=0, iEnd=0, iExpectedEnd=0;
if(iStart==0) iStart = GetCPUTicks();
if (iStart==0) iStart = GetCPUTicks();
iExpectedEnd = iStart + iTicks;
iEnd = GetCPUTicks();
if(iEnd>=iExpectedEnd)
{
if (iEnd>=iExpectedEnd) {
u64 diff = iEnd-iExpectedEnd;
if((diff>>3)>iTicks) iExpectedEnd=iEnd;
}
@ -230,6 +215,7 @@ void FrameLimiter()
Sleep(1);
iEnd = GetCPUTicks();
} while(iEnd<iExpectedEnd);
iStart = iExpectedEnd; //remember the expected value frame. improves smoothness
}
@ -245,7 +231,6 @@ extern void DummyExecuteVU1Block(void);
#include "VU.h"
void VSync()
{
if (counters[5].mode & 0x10000) { // VSync End (22 hsyncs)
// swap the vsync field
@ -336,7 +321,6 @@ void VSync()
}
}
} else { // VSync Start (240 hsyncs)
//UpdateVSyncRateEnd();
#ifdef EE_PROFILING
if( (iFrame%20) == 0 ) {
SysPrintf("Profiled Cycles at %d frames %d\n", iFrame, profile_totalticks);
@ -344,15 +328,6 @@ void VSync()
}
#endif
//SysPrintf("c: %x, %x\n", cpuRegs.cycle, *(u32*)&VU1.Micro[16]);
//if( (iFrame%20) == 0 ) SysPrintf("svu time: %d\n", SuperVUGetRecTimes(1) * 100000 / lfreq.QuadPart);
// if( (iFrame%10) == 0 ) {
// SysPrintf("vu0 time: %d\n", vu0time);
// vu0time = 0;
// }
#ifdef PCSX2_DEVBUILD
if( g_TestRun.enabled && g_TestRun.frame > 0 ) {
if( iFrame > g_TestRun.frame ) {
@ -425,22 +400,13 @@ void VSync()
}
#endif
//counters[5].mode&= ~0x10000;
//UpdateVSyncRate();
//SysPrintf("ctrs: %d %d %d %d\n", g_nCounters[0], g_nCounters[1], g_nCounters[2], g_nCounters[3]);
//SysPrintf("vif: %d\n", (((LARGE_INTEGER*)g_nCounters)->QuadPart * 1000000) / lfreq.QuadPart);
//memset(g_nCounters, 0, 16);
counters[5].mode|= 0x10000;
if ((CSRw & 0x8))
GSCSRr|= 0x8;
if (!(GSIMR&0x800) )
gsIrq();
counters[5].mode|= 0x10000;
if ((CSRw & 0x8)) GSCSRr|= 0x8;
if (!(GSIMR&0x800)) gsIrq();
hwIntcIrq(2);
psxVSyncStart();
@ -448,55 +414,49 @@ void VSync()
if(Config.Patch) applypatch(1);
if(gates)rcntStartGate(0x8);
// __Log("%u %u 0\n", cpuRegs.cycle-s_lastvsync[1], timeGetTime()-s_lastvsync[0]);
// s_lastvsync[0] = timeGetTime();
// s_lastvsync[1] = cpuRegs.cycle;
//__Log("%u %u 0\n", cpuRegs.cycle-s_lastvsync[1], timeGetTime()-s_lastvsync[0]);
//s_lastvsync[0] = timeGetTime();
//s_lastvsync[1] = cpuRegs.cycle;
}
}
void rcntUpdate()
{
int i;
u32 change = 0;
for (i=0; i<=3; i++) {
if(gates & (1<<i) ){
if(gates & (1<<i) ) {
//SysPrintf("Stopped accidental update of ee counter %x when using a gate\n", i);
continue;
}
if ((counters[i].mode & 0x80) && (counters[i].mode & 0x3) != 0x3){
change = cpuRegs.cycle - counters[i].sCycleT;
counters[i].count += (int)(change / counters[i].rate);
change -= (change / counters[i].rate) * counters[i].rate;
} else change = 0;
}
if ((counters[i].mode & 0x80) && (counters[i].mode & 0x3) != 0x3) {
change = cpuRegs.cycle - counters[i].sCycleT;
counters[i].count += (int)(change / counters[i].rate);
change -= (change / counters[i].rate) * counters[i].rate;
}
else change = 0;
counters[i].sCycleT = cpuRegs.cycle - change;
//if(change > 0) SysPrintf("Change saved on %x = %x\n", i, change);
}
if ((u32)(cpuRegs.cycle - counters[4].sCycleT) >= (u32)counters[4].CycleT && hblankend == 1){
if ((u32)(cpuRegs.cycle - counters[4].sCycleT) >= (u32)counters[4].CycleT && hblankend == 1) {
if ((CSRw & 0x4))
GSCSRr |= 4; // signal
if (!(GSIMR&0x400) )
gsIrq();
if(gates)rcntEndGate(0);
if(psxhblankgate)psxCheckEndGate(0);
if (CSRw & 0x4) GSCSRr |= 4; // signal
if (!(GSIMR&0x400)) gsIrq();
if (gates) rcntEndGate(0);
if (psxhblankgate) psxCheckEndGate(0);
hblankend = 0;
counters[4].CycleT = HBLANKCNT(counters[4].count);
} else
if ((u32)(cpuRegs.cycle - counters[4].sCycleT) >= (u32)counters[4].CycleT) {
}
else if ((u32)(cpuRegs.cycle - counters[4].sCycleT) >= (u32)counters[4].CycleT) {
if(counters[4].count >= counters[4].Cycle){
if(counters[4].count >= counters[4].Cycle) {
//SysPrintf("%x of %x hblanks reorder in %x cycles cpuRegs.cycle = %x\n", counters[4].count, counters[4].Cycle, cpuRegs.cycle - counters[4].sCycleT, cpuRegs.cycle);
counters[4].sCycleT += HBLANKCNT(counters[4].Cycle);
counters[4].count -= counters[4].Cycle;
}
}
//counters[4].sCycleT += HBLANKCNT(1);
counters[4].count++;
@ -509,94 +469,84 @@ void rcntUpdate()
//SysPrintf("%x hsync done in %x cycles cpuRegs.cycle = %x next will happen on %x\n", counters[4].count, counters[4].CycleT, cpuRegs.cycle, (u32)(counters[4].sCycleT + counters[4].CycleT));
}
if((counters[5].mode & 0x10000)){
if((counters[5].mode & 0x10000)) {
if ((cpuRegs.cycle - counters[5].sCycleT) >= counters[5].CycleT){
//counters[5].sCycleT = cpuRegs.cycle;
counters[5].CycleT = VBLANKCNT(counters[5].count);
VSync();
}
} else if ((cpuRegs.cycle - counters[5].sCycleT) >= counters[5].CycleT) {
if(counters[5].count >= counters[5].Cycle){
//SysPrintf("reset %x of %x frames done in %x cycles cpuRegs.cycle = %x\n", counters[5].count, counters[5].Cycle, cpuRegs.cycle - counters[5].sCycleT, cpuRegs.cycle);
counters[5].sCycleT += VBLANKCNT(counters[5].Cycle);
counters[5].count -= counters[5].Cycle;
}
counters[5].count++;
//the line below brings back magna carta loading animation, but breaks vsyncs >< (rama)
//counters[5].sCycleT += VBLANKCNT(1);
counters[5].CycleT = VBLANKCNT(counters[5].count) - (VBLANKCNT(1)/2);
//SysPrintf("%x frames done in %x cycles cpuRegs.cycle = %x cycletdiff %x\n", counters[5].Cycle, counters[5].sCycleT, cpuRegs.cycle, (counters[5].CycleT - VBLANKCNT(1)) - (cpuRegs.cycle - counters[5].sCycleT));
//counters[5].sCycleT = cpuRegs.cycle;
counters[5].CycleT = VBLANKCNT(counters[5].count);
VSync();
}
}
else if ((cpuRegs.cycle - counters[5].sCycleT) >= counters[5].CycleT) {
if(counters[5].count >= counters[5].Cycle) {
//SysPrintf("reset %x of %x frames done in %x cycles cpuRegs.cycle = %x\n", counters[5].count, counters[5].Cycle, cpuRegs.cycle - counters[5].sCycleT, cpuRegs.cycle);
counters[5].sCycleT += VBLANKCNT(counters[5].Cycle);
counters[5].count -= counters[5].Cycle;
}
counters[5].count++;
//counters[5].sCycleT += VBLANKCNT(1); //this line brings back magna carta loading animation, but breaks vsyncs >< (rama)
counters[5].CycleT = VBLANKCNT(counters[5].count) - (VBLANKCNT(1)/2);
//SysPrintf("%x frames done in %x cycles cpuRegs.cycle = %x cycletdiff %x\n", counters[5].Cycle, counters[5].sCycleT, cpuRegs.cycle, (counters[5].CycleT - VBLANKCNT(1)) - (cpuRegs.cycle - counters[5].sCycleT));
VSync();
}
for (i=0; i<=3; i++) {
if (!(counters[i].mode & 0x80)) continue; // Stopped
if ((s64)(counters[i].target - counters[i].count) <= 0 /*&& (counters[i].target & 0xffff) > 0*/) { // Target interrupt
if((counters[i].target > 0xffff)) {
//SysPrintf("EE Correcting target %x after reset on target\n", i);
counters[i].target &= 0xffff;
}
//if((counters[i].target > 0xffff)) {
//SysPrintf("EE Correcting target %x after reset on target\n", i);
counters[i].target &= 0xffff;
//}
if(counters[i].mode & 0x100 ) {
#ifdef EECNT_LOG
EECNT_LOG("EE counter %d target reached mode %x count %x target %x\n", i, counters[i].mode, counters[i].count, counters[i].target);
#endif
EECNT_LOG("EE counter %d target reached mode %x count %x target %x\n", i, counters[i].mode, counters[i].count, counters[i].target);
counters[i].mode|= 0x0400; // Target flag
hwIntcIrq(counters[i].interrupt);
if (counters[i].mode & 0x40) { //The PS2 only resets if the interrupt is enabled - Tested on PS2
counters[i].count -= counters[i].target; // Reset on target
}
else counters[i].target += 0x10000000;
} else counters[i].target += 0x10000000;
}
else counters[i].target += 0x10000000;
}
if (counters[i].count > 0xffff) {
if (counters[i].mode & 0x0200) { // Overflow interrupt
#ifdef EECNT_LOG
EECNT_LOG("EE counter %d overflow mode %x count %x target %x\n", i, counters[i].mode, counters[i].count, counters[i].target);
#endif
EECNT_LOG("EE counter %d overflow mode %x count %x target %x\n", i, counters[i].mode, counters[i].count, counters[i].target);
counters[i].mode|= 0x0800; // Overflow flag
hwIntcIrq(counters[i].interrupt);
//SysPrintf("counter[%d] overflow interrupt (%x)\n", i, cpuRegs.cycle);
}
counters[i].count -= 0x10000;
if(counters[i].target > 0xffff) {
//if(counters[i].target > 0xffff) {
//SysPrintf("EE %x Correcting target on overflow\n", i);
counters[i].target &= 0xffff;
}
//}
}
}
rcntSet();
}
void rcntWcount(int index, u32 value) {
u32 change = 0;
#ifdef EECNT_LOG
EECNT_LOG("EE count write %d count %x with %x target %x eecycle %x\n", index, counters[index].count, value, counters[index].target, cpuRegs.eCycle);
#endif
counters[index].count = value & 0xffff;
if(counters[index].target > 0xffff) {
counters[index].target &= 0xffff;
//if(counters[index].target > 0xffff) {
//SysPrintf("EE Counter %x count write, target > 0xffff\n", index);
}
counters[index].target &= 0xffff;
//}
//rcntUpd(index);
if((counters[index].mode & 0x3) != 0x3){
change = cpuRegs.cycle - counters[index].sCycleT;
change -= (change / counters[index].rate) * counters[index].rate;
counters[index].sCycleT = cpuRegs.cycle - change;
}/* else {
if((counters[index].mode & 0x3) != 0x3) {
change = cpuRegs.cycle - counters[index].sCycleT;
change -= (change / counters[index].rate) * counters[index].rate;
counters[index].sCycleT = cpuRegs.cycle - change;
}
/* else {
SysPrintf("EE Counter %x count write %x\n", index, value);
}*/
rcntSet();
@ -606,12 +556,11 @@ void rcntWmode(int index, u32 value)
{
u32 change = 0;
if (value & 0xc00) { //Clear status flags, the ps2 only clears what is given in the value
counters[index].mode &= ~(value & 0xc00);
}
if(counters[index].mode & 0x80){
if(counters[index].mode & 0x80) {
if((counters[index].mode & 0x3) != 0x3){
change = cpuRegs.cycle - counters[index].sCycleT;
counters[index].count += (int)(change / counters[index].rate);
@ -623,27 +572,25 @@ void rcntWmode(int index, u32 value)
#ifdef PCSX2_DEVBUILD
if(!(value & 0x80)) SysPrintf("Stopping\n");
#endif
}
}
else {
#ifdef PCSX2_DEVBUILD
SysPrintf("Counter %d not running c%x s%x c%x\n", index, counters[index].count, counters[index].sCycleT, cpuRegs.cycle);
if(value & 0x80) SysPrintf("Starting %d, v%x\n", index, value);
#endif
counters[index].sCycleT = cpuRegs.cycle;
}
//if((value & 0x80) && !(counters[index].mode & 0x80)) rcntUpd(index); //Counter wasnt started, so set the start cycle
counters[index].mode = (counters[index].mode & 0xc00) | (value & 0x3ff);
}
#ifdef EECNT_LOG
//if((value & 0x80) && !(counters[index].mode & 0x80)) rcntUpd(index); //Counter wasnt started, so set the start cycle
counters[index].mode = (counters[index].mode & 0xc00) | (value & 0x3ff);
EECNT_LOG("EE counter set %d mode %x count %x\n", index, counters[index].mode, rcntCycle(index));
#endif
/*if((value & 0x3) && (counters[index].mode & 0x3) != 0x3){
//SysPrintf("Syncing %d with HBLANK clock\n", index);
counters[index].CycleT = counters[4].CycleT;
}*/
switch (value & 0x3) { //Clock rate divisers *2, they use BUSCLK speed not PS2CLK
switch (value & 0x3) { //Clock rate divisers *2, they use BUSCLK speed not PS2CLK
case 0: counters[index].rate = 2; break;
case 1: counters[index].rate = 32; break;
case 2: counters[index].rate = 512; break;
@ -653,8 +600,9 @@ void rcntWmode(int index, u32 value)
if((counters[index].mode & 0xF) == 0x7) {
gates &= ~(1<<index);
SysPrintf("Gate Disabled\n");
// counters[index].mode &= ~0x80;
}else if(counters[index].mode & 0x4){
//counters[index].mode &= ~0x80;
}
else if (counters[index].mode & 0x4) {
// SysPrintf("Gate enable on counter %x mode %x\n", index, counters[index].mode);
gates |= 1<<index;
counters[index].mode &= ~0x80;
@ -663,11 +611,11 @@ void rcntWmode(int index, u32 value)
else gates &= ~(1<<index);
/*if((counters[index].target > 0xffff) && (counters[index].target & 0xffff) > rcntCycle(index)) {
//SysPrintf("EE Correcting target %x after mode write\n", index);
counters[index].target &= 0xffff;
}*/
rcntSet();
//SysPrintf("EE Correcting target %x after mode write\n", index);
counters[index].target &= 0xffff;
}*/
rcntSet();
}
void rcntStartGate(unsigned int mode){
@ -679,11 +627,11 @@ void rcntStartGate(unsigned int mode){
}
}
for(i=0; i <=3; i++){ //Gates for counters
for(i=0; i <=3; i++){ //Gates for counters
if(!(gates & (1<<i))) continue;
if ((counters[i].mode & 0x8) != mode) continue;
//SysPrintf("Gate %d mode %d Start\n", i, (counters[i].mode & 0x30) >> 4);
switch((counters[i].mode & 0x30) >> 4){
switch((counters[i].mode & 0x30) >> 4) {
case 0x0: //Count When Signal is low (off)
counters[i].count = rcntRcount(i);
rcntUpd(i);
@ -708,10 +656,10 @@ void rcntStartGate(unsigned int mode){
}
}
}
void rcntEndGate(unsigned int mode){
void rcntEndGate(unsigned int mode) {
int i;
for(i=0; i <=3; i++){ //Gates for counters
for(i=0; i <=3; i++) { //Gates for counters
if(!(gates & (1<<i))) continue;
if ((counters[i].mode & 0x8) != mode) continue;
//SysPrintf("Gate %d mode %d End\n", i, (counters[i].mode & 0x30) >> 4);
@ -741,45 +689,38 @@ void rcntEndGate(unsigned int mode){
}
void rcntWtarget(int index, u32 value) {
#ifdef EECNT_LOG
EECNT_LOG("EE target write %d target %x value %x\n", index, counters[index].target, value);
#endif
counters[index].target = value & 0xffff;
if(counters[index].target <= rcntCycle(index)/* && counters[index].target != 0*/) {
//SysPrintf("EE Saving target %d from early trigger, target = %x, count = %x\n", index, counters[index].target, rcntCycle(index));
counters[index].target += 0x10000000;
}
if (counters[index].target <= rcntCycle(index)/* && counters[index].target != 0*/) {
//SysPrintf("EE Saving target %d from early trigger, target = %x, count = %x\n", index, counters[index].target, rcntCycle(index));
counters[index].target += 0x10000000;
}
rcntSet();
}
void rcntWhold(int index, u32 value) {
#ifdef EECNT_LOG
EECNT_LOG("EE hold write %d value %x\n", index, value);
#endif
counters[index].hold = value;
}
u32 rcntRcount(int index) {
u32 ret;
if ((counters[index].mode & 0x80)) {
if ((counters[index].mode & 0x80))
ret = counters[index].count + (int)((cpuRegs.cycle - counters[index].sCycleT) / counters[index].rate);
}else{
else
ret = counters[index].count;
}
#ifdef EECNT_LOG
EECNT_LOG("EE count read %d value %x\n", index, ret);
#endif
return ret;
}
u32 rcntCycle(int index) {
if ((counters[index].mode & 0x80)) {
if ((counters[index].mode & 0x80))
return (u32)counters[index].count + (int)((cpuRegs.cycle - counters[index].sCycleT) / counters[index].rate);
}else{
else
return (u32)counters[index].count;
}
}
int rcntFreeze(gzFile f, int Mode) {