gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > FPS-meter through GetTickCount()-style function. How??

#99479 - HotChilli - Tue Aug 22, 2006 12:19 am

Help!

For example I have a code.

Code:

while(1) {
    old_time = GetTime(); // in milliseconds!
    DrawAll();
    cur_time = GetTime();
    printf("fps: %d", 1000 / (cur_time-old_time));

    swiWaitForVBlank();
}


How can I implement GetTime() function?
1. I trying use TIMERS but that's work mystycal. :(
2. VCOUNT can't work properly with calculation more than 1000/60 ms...
3. VBLANK counting method not pass also because not profile fast and slow functions in one frame-calculation...

Can somebody write and explain code of GetTime()? GetTime() returns current time in milliseconds...

#99495 - josath - Tue Aug 22, 2006 1:53 am

Timers are not that mystical...
Code:

TIMER_DATA(0) = 0;
TIMER_CR(0) = TIMER_ENABLE | TIMER_DIV_1024;
(untested, but should work)

Then TIMER_DATA will increase at (33.514MHz / 1024), or 32728.5 times per second. If you want milliseconds, it's easy enough to convert. However, this will only count up to 2^16, or 65535(about 2 seconds), before wrapping around back to 0.

#100851 - HotChilli - Thu Aug 31, 2006 3:07 am

josath - thanx. =)

For profiling i have made and successfully use now class NdsTimer.

Use for your own risk. ;)

Code:
class NdsTimer
{
public:
   NdsTimer() {
      TIMER0_CR &= ~TIMER_ENABLE; TIMER0_DATA = 0; TIMER0_CR = TIMER_DIV_1024;
      TIMER1_CR &= ~TIMER_ENABLE; TIMER1_DATA = 0; TIMER1_CR = TIMER_CASCADE;
   }
   
   // Current time in seconds (1 second = 1.f)
   float getTime() const {
      return static_cast<float>((TIMER1_DATA << 16) + TIMER0_DATA) / 32768;
   }

   static NdsTimer& the() {
      static NdsTimer globalNdsTimer;
      return globalNdsTimer;
   }
};