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 > Shouldn't this come out to be around 60 FPS?

#20790 - mr_schmoe - Mon May 17, 2004 10:35 pm

I have an timer interupt that runs at 64 clock pulses. And the overflow calls a function that increments the timer variable.

so shouldn't I be getting somewhere around 60 FPS?

int main()
{
int time1;
int time2;

time1 = 65536 * timer + REG_TM0D
while (REG_VCOUNT < 160);
time2 = 65536 * timer + REG_TM0D

FPS = 262182 / (time2 - time1);

}

Here's the numbers I get:
When I set up my timer rutine at 64 clock pulses then the REG_TM0D register gets incremented every 3.814 nanoseconds.
And when it gets to 65536, my timer variable incresses by one.
Therefore, 65536 * timer + REG_TM0D ought to a count of how long sense the timer interupt was started.
So the frequency of the count should be (1/3.184) MHz or 0.262191924, or 262191.924 Hz.
So FPS should be 262192 / (time2 - time1)

But what do I get for FPS? 0. In fact, time2 - time1 equals 1. I don't get it, and I've spent the last 2 hours trying to figure it out. Anyone with a inkling of an idea?

#20794 - poslundc - Mon May 17, 2004 11:48 pm

I haven't checked your math, but is this the actual game loop you are using? How are you checking the variable's values? Your program could be resetting while still in VBlank and fudging your numbers.

Dan.

#20815 - Cearn - Tue May 18, 2004 8:29 am

As poslundc mentioned, it's difficult to believe this is really your full main function, as it'd exit after one frame. Don't you mean
Code:

int main()
{
    int time1;
    int time2;
    while(1)
    {
        time1 = 65536 * timer + REG_TM0D
        while (REG_VCOUNT < 160);
        time2 = 65536 * timer + REG_TM0D

        FPS = 262182 / (time2 - time1);
    }
}

And in that case, note what happens after VCount == 160. The two lines after it are executed, at which time you jump to the start of the loop. With this little to do it is likely that VCount is still 160, or maybe 161, so you blaze through what you thought was a vsync a couple of times before VCount=0 again, etc, etc.

Try something like this:
Code:

void vid_vsync()
{
    while(REG_VCOUNT >= 160);   // wait till VDraw
    while(REG_VCOUNT < 160);    // wait till VBlank
}

Yes it's a bit wasteful, cyclewise but it works. If you can, try to use a VBlank interrupt, that always works as expected.

(Oh, and use code tags for code, it looks nicer that way)

#20857 - mr_schmoe - Tue May 18, 2004 7:22 pm

Quote:
With this little to do it is likely that VCount is still 160, or maybe 161, so you blaze through what you thought was a vsync a couple of times before VCount=0 again, etc, etc.


That makes perfect sense. I'll try that. But the problem is I was just frustrated with the whole thing not working. I was just trying to get a FPS counter so I can work on optimizing my code for speed. I think what I'll try next is a vblank interupt, I'll have to learn more about it though. Thanks for you help.

#20887 - dagamer34 - Wed May 19, 2004 4:45 am

mr_schmoe wrote:

That makes perfect sense. I'll try that. But the problem is I was just frustrated with the whole thing not working. I was just trying to get a FPS counter so I can work on optimizing my code for speed. I think what I'll try next is a vblank interupt, I'll have to learn more about it though. Thanks for you help.


Don't optimize your code unless you 100% need to. You probably won't use this profiling code for a long time anyway because chances are you aren't doing something time-critical (HBlank tricks, audio mixing, polygon drawing) Like someone's signature on this board(not sure who) says "Premature optimization is the root of all evil".
_________________
Little kids and Playstation 2's don't mix. :(

#20888 - tepples - Wed May 19, 2004 4:56 am

Donald Knuth wrote:
Premature optimization is the root of all evil.


Anyway, my favorite GBA-side profiler is Vcount. Either I read Vcount before and after my loop and subtract, or I change the backdrop color at various stages in a computation to gauge the relative amounts of time that something takes.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#20889 - sajiimori - Wed May 19, 2004 4:57 am

Hey, for all you know he 100% needs to. You say "chances are" this or that, but I think the chances that you've seen his code are 0%. ;)

#20891 - dagamer34 - Wed May 19, 2004 5:02 am

sajiimori wrote:
Hey, for all you know he 100% needs to. You say "chances are" this or that, but I think the chances that you've seen his code are 0%. ;)


His profiler code is more than 0%, but it isn't 100%. So you're right in a way.

If you really want a good profiler, "borrow" ideas from HAM. Since the tools are actually defines, replace the calls by setting the registers yourself.
_________________
Little kids and Playstation 2's don't mix. :(

#20892 - sajiimori - Wed May 19, 2004 7:14 am

I didn't say you've seen 0% of his code, so I'm 100% right. :D

This post was created with 50% recycled bandwidth.

#20920 - dagamer34 - Wed May 19, 2004 11:08 pm

sajiimori wrote:

This post was created with 50% recycled bandwidth.

:)
_________________
Little kids and Playstation 2's don't mix. :(

#21008 - Lupin - Fri May 21, 2004 3:37 pm

:)

*postcount++;*
_________________
Team Pokeme
My blog and PM ASM tutorials

#21014 - Lord Graga - Fri May 21, 2004 4:04 pm

Lupin wrote:
:)

*postcount++;*
You bastard, stop spamming the board... now look kids, I started too! Lupin, it's all your fault that bad light will be shed on this forum! :P

#21040 - dagamer34 - Sat May 22, 2004 2:45 am

Lord Graga wrote:
Lupin wrote:
:)

*postcount++;*
You bastard, stop spamming the board... now look kids, I started too! Lupin, it's all your fault that bad light will be shed on this forum! :P


That's a bit over the top. He's helped people a bit too.

*postcount++;* :)
_________________
Little kids and Playstation 2's don't mix. :(

#21046 - Lord Graga - Sat May 22, 2004 11:41 am

dagamer34 wrote:
Lord Graga wrote:
Lupin wrote:
:)

*postcount++;*
You bastard, stop spamming the board... now look kids, I started too! Lupin, it's all your fault that bad light will be shed on this forum! :P


That's a bit over the top. He's helped people a bit too.

*postcount++;* :)
no, neither did you!


I am starting to have a bad feeling about this :P

#21088 - Lupin - Sun May 23, 2004 8:16 pm

Hey graga, dagamer started first!! I just tried to comment his 2 character-post :P
_________________
Team Pokeme
My blog and PM ASM tutorials

#21101 - sgeos - Sun May 23, 2004 9:59 pm

Code:
*postcount++;*


I can't get it compile. What compiler are you using?

-Brendan