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.

Coding > Sharing vars between the game loop and the interrup. handler

#58539 - Nacho - Mon Oct 24, 2005 2:13 pm

Hi! I?m trying to set my game?s frequency to 20 FPS. Until now, I?ve been using the scanline counter register but I?ve been suggested to use interruptions instead so I looked into that. My plan of attack is the following one: keep a static variable inside the InterruptHandler and increment it everytime a VBlank interruption occurs. Whenever that variable reaches 3, set a global variable to 1 so that the main loop can flip pages. Here?s the code:

Code:


// Interruptions.c

int g_iOK = 0;

CODE_IN_IWRAM void
InterruptHandler(void)
{
    static int iNumPasses = 0;
    unsigned short usFlags;

    REG_IME = 0x00;
    usFlags = REG_IF;

    if((REG_IF & INT_VBLANK)==INT_VBLANK) {
        if(++iNumPasses==3) {
            iNumPasses = 0;
            g_iOK = 1;
        } /* End if */
        else {
            g_iOK = 0;
        } /* End else */
    } /* End if */

    REG_IF = usFlags;
    REG_IME = 0x01;
    return;
} /* End InterruptHandler() */


Code:


// Main.c

int
main(void)
{
    // Code

    while(iFlag) {
        GameMain(&RendCont);      
        while(!g_iOK);
        FlipPages();
        FillScreen();
    }
    // More code.
}


Unfortunately, the program crashes, although it works ok whenever I comment the
Code:
while(!g_iOK);
statement. I believe this has something to do with the fact that InterruptHandler() is located in IWRAM whereas main() is not but I don?t know how to solve it. Can you help with this? Thanks in advance!

--Nacho

#58543 - strager - Mon Oct 24, 2005 2:19 pm

Try defining your variable as volatile.

As such:
Code:
volatile int g_iOK = 0;

The compiler might assume, since g_iOK is zero on startup (and the interrupt handler isn't called directly from your code), it optimizes the while loop to while(!(0));. This, obviously, is a problem, and the volatile should fix it.

#58551 - Nacho - Mon Oct 24, 2005 4:09 pm

Thanks, that fixed it! I thought that the volatile keyword was meant to be used only for register variables. Another thing to add to my troubleshooting repertoire =P Thanks again strager!

One more question: why does Visual Boy Advance keeps displaying that my game is running at 60 fps? What does it take as a parameter to determine that value? I though it was the amount of page flips performed in one second but, apparently, I?m wrong.

--Nacho

#58553 - NoMis - Mon Oct 24, 2005 4:26 pm

The GBA Hardware runs the display at a constant 60fps. That is a constant that can't be changed. You are only updating the screen every 3 frames and therefor get a theoretical framerate of 20fps.

NoMis
_________________
www.gamedev.at - The austrian gamedev site
hde.gamedev.at - The Handheld Dev Env plugins for Eclipse

#58554 - strager - Mon Oct 24, 2005 4:27 pm

Nacho wrote:
Thanks, that fixed it! I thought that the volatile keyword was meant to be used only for register variables. Another thing to add to my troubleshooting repertoire =P Thanks again strager!

One more question: why does Visual Boy Advance keeps displaying that my game is running at 60 fps? What does it take as a parameter to determine that value? I though it was the amount of page flips performed in one second but, apparently, I?m wrong.

--Nacho


You're welcome. The 'register' keyword was an extention by the Borland compilers, I think...

VisualBoy Advance says your game is running at 60FPS because that is the number of frames the emulator is rendering per second, not the number of frames the game is rendering per second. The closer the emulator is to 60FPS, the more like the real hardware it is (somewhat).

Edit:
NoMis wrote:
The GBA Hardware runs the display at a constant 60fps. That is a constant that can't be changed. You are only updating the screen every 3 frames and therefor get a theoretical framerate of 20fps.

NoMis

What he said. :)

#58601 - tepples - Mon Oct 24, 2005 9:16 pm

But if you set frameskip to 2 (display one, skip two), VBA will run the game at 20fps and it'll still look correct if your engine is synchronized to 20fps.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#58997 - Nacho - Thu Oct 27, 2005 11:04 pm

Thanks for all your explanations, I learned a lot in this thread.

--Nacho