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 > speed too fast

#72854 - naleksiev - Wed Feb 22, 2006 4:09 am

What is wrong with my code or my calculation?

I have this code
Code:
void WaitForVSync() {
   while (*(vu16*)& *(u16*)0x4000006 != 160) {   }
}


Code:
...
int frame = 0;
while (1) {
      WaitForVSync();
      SetPosition(1, 0, frame/60);
      frame++;
   }
...


Because I have only ~60 VBlanks per second I think I need to have different value for frame/60 on ~1 second. It's pretty fast about. About 8 times faster. I tried this on real device GB Micro. It's still fast about 6 times faster.

First do you have any idea why this is so fast?
Second why I have such differense between the real device and the emulator? SetPosition is pretty simple function just one line that is changing the offset ot one of the backgrounds.

#72860 - tepples - Wed Feb 22, 2006 5:09 am

You need to wait for it to not equal 160, and then you need to wait for it to equal 160.

In addition, the syntax is a bit baroque to say the least. Try this:
Code:
void WaitForVSync() {
   while (*(vu16*)0x4000006 != 160) {   }
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#72871 - poslundc - Wed Feb 22, 2006 5:45 am

To expand on the reason why this is happening: your VBlank routine completes before the GBA's current scanline advances past 160, so when your loop executes a second time the wait-until condition is still false.

Also: you should refer to registers by meaningful macro names and not by literal address. You are writing code that will wind up being impossible to read or maintain.

Dan.

#72978 - Ultima2876 - Wed Feb 22, 2006 7:33 pm

>=( now go to bed with no dinner! *shot*

He's right though, code like that quickly gets out of hand. Use some macros, they can't hurt ^_~

#72987 - naleksiev - Wed Feb 22, 2006 8:03 pm

Calm down please.

I'm sorry that I post such code but I made so many changes I take a look at so many versions of the WaitForVSync() that suppose to work but not in my case. I just start testing them one by one to see is there any difference. It's not a code that I wrote. And if you don't see post this in Beginners.

Ultima2976 if you don't want to help just ignore Beginners forum. OK?

And btw thanks for the replies I understand my mistake.