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 > wait for vblank

#8286 - Dwedit - Mon Jul 07, 2003 3:03 am

How do you halt the processor until a vblank?

I know there is a bios call to do it, how do you do bios calls?
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#8750 - Dwedit - Sat Jul 19, 2003 3:01 am

Right now I'm trying this code to wait for a vblank, but it just fails and ends up restating the program:

Code:
void VBlankIntrWait()
{
  asm volatile("mov r2, #0; swi 0x05" ::: "r0", "r1", "r2", "r3");
  return;
}


What's the proper way to wait for a vblank? (without counting scanlines in a while loop)
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#8751 - tepples - Sat Jul 19, 2003 3:27 am

Dwedit wrote:
Right now I'm trying this code to wait for a vblank, but it just fails and ends up restating the program:

Code:
void VBlankIntrWait()
{
  asm volatile("mov r2, #0; swi 0x05" ::: "r0", "r1", "r2", "r3");
  return;
}

In order to use VBlankIntrWait(), you have to have an ISR installed. It must be compiled in ARM (not Thumb) instructions and should be located in IWRAM. To learn how to write an ISR, see the "interrupts" section of your favorite GBA programming tutorial.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#8782 - yaustar - Sat Jul 19, 2003 8:18 pm

go to the pern project by dovoto, he has a chapter on interrupts

http://www.thepernproject.com
_________________
[Blog] [Portfolio]

#8783 - Dwedit - Sat Jul 19, 2003 8:51 pm

I looked at the pern project's tutorial, it explains how to handle them, but not how to wait for them. I also looked through some of the sample code on the site, all of the wait vblank functions used a while loop and counted scanlines. I know that that can't be good for battery life, so I need a routine that halts the processor until the vblank interrupt occurrs.

I don't know the correct way to call bios functions, and whenever I try to call the bios functions to wait for an interrupt, the program restarts. I am using Devkitadvance with the ISR from crt0.S, using the Interrupt Table option.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#8787 - sgstair - Sat Jul 19, 2003 11:56 pm

To use the bios call VblankIntrWait (5), there are 2 things you need to do...
First, before you even THINK of calling SWI 5, you need to have a vblank interrupt handler in place, and it has to do something like:
Code:
   mov      r0,#0x04000000
   mov      r1,#1
   strh   r1,[r0,#-8]      @ store 1 in 03007ff8

That's just code from my handler, if you plan to use the SWI 4 (To wait for other types of interrupts) you need to OR the IF register with the halfword at 0x03007ff8.

After you have that taken care of, you can call the SWI 5 to wait for vblank and happily conserve your battery power.

Here's some code to call SWI 5 from ARM mode:
Code:
   asm volatile (
      "swi 0x050000 "
      : // output
      : // input
      : "r0","r1","r2","r3","r12","lr"   // Destroy
   );

-- also from my handler :)

Of course you don't have to specify destroy on that many vars, really it doesnt' destroy many at all. I'm just happier being safe than sorry ;)

From nocash gbatek:
Quote:
Incoming parameters are usually passed through registers R0,R1,R2,R3. Outgoing registers R0,R1,R3 are typically containing either garbage, or return value(s). All other registers (R2,R4-R14) are kept unchanged.


Hope this helps :)
-Stephen
_________________
http://blog.akkit.org/ - http://www.akkit.org/dswifi/