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.

Beginners > yet another timer question

#22893 - jhinders - Wed Jun 30, 2004 9:29 pm

ok, i am trying to use a timer and basically i check it in a while loop:

Code:

   while(1)
   {
      CumulateTicks();
   
   }


#define REG_TM2D       ( *(vu16*)0x4000108 )
#define REG_TIM2CNT    ( *(vu16*)0x400010A )
#define FREQUENCY_64 (1)
#define TIMER_ENABLE  (1<<7)
#define TIMER_MS (REG_TM2D / (262144/1000))

static void ResetTimer()
{
   REG_TIM2CNT = 0;
   REG_TM2D = 0;
   REG_TIM2CNT = FREQUENCY_64 | TIMER_ENABLE;
}

static unsigned int Ticks = 0;

static void CumulateTicks()
{
   static DWORD cumulate = 0;
   DWORD ms = TIMER_MS;
   Ticks += ms;
   cumulate += ms;

   ResetTimer();
}


Now i would assume that i would get values for Ticks that would go up at a fairly steady rate, but am not. i will get a difference of 13 between the current Ticks value and the next, and then sometimes get like 34 or something. Anyone know what stupidity i am doing?

thanks.

#22900 - jhinders - Wed Jun 30, 2004 10:25 pm

ok, i'll respond to my own post. turns out i was waiting for vsync so the timer update was not always called at the appropriate time.

#22908 - poslundc - Wed Jun 30, 2004 10:48 pm

The lesson is that the code you post should reflect the code in your program.

My first thought when looking at the description of the problem was that you were probably doing something like synching to VBlank, but your code only showed an infinite loop.

Dan.