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.

C/C++ > Simple tutorial on VBlank

#8890 - Geno - Wed Jul 23, 2003 5:57 am

I see people referring to the VBlank all over these forums, but I haven't found a good C++ tutorial on it. Could someone perhaps give a detailed explaination or point me to an appropriate tutorial? Thanks.
_________________
Out of Order.

#8900 - niltsair - Wed Jul 23, 2003 2:57 pm

That would be because it's not C++ specific but hardware specific. If you do a seach on it, on this forum, you'll find plenty of doc about it.

Quickly explained, the GameBoy has 160lines on its screen that need to be refreshed one by one. If you were to change what's displayed on the currently refresh line, you'll get some garbage, because everything below would display the new changes, and above, the old picture.

While the screen has 160lines, it actually 'refresh' 228 lines. This is done to give programmer a period of time (160-227) to safely make changes to the display while the screen isn't refreshed. This period after Line 160 refresh is called VBlank.

#8902 - night_ - Wed Jul 23, 2003 3:43 pm

One of the popular applications in relation to hblank is to enable the hblank interrupt and to change the rotation/scaling/scrolling parameters for
backgrounds each line. This technique can also be part of the famous "mode 7 effect" used in games like Mario Kart and F-Zero.

I recently used the technique to have a "waving" background image. In the interrupt code, I am adding sinus(lineNumber+offset) to the scroll register of the background in mode4. offset is incremented every frame. If you are interested in that simple code snippet, maybe you can convince me to post it;-)

(edited 07-23-03 3:30) replaced "vblank" with "hblank"


Last edited by night_ on Wed Jul 23, 2003 8:33 pm; edited 1 time in total

#8905 - niltsair - Wed Jul 23, 2003 4:20 pm

Another way to do what you said (that I saw Tepple write about i think) is to fill an array of background scroll values and setup a dma transfer that's setted to occurs every line refresh. Haven't tried it, but i thought it was neat and versatile (can be use for rotation too, and you can easily change the parameters value in the array)

#8906 - Lupin - Wed Jul 23, 2003 4:26 pm

I think mode7 is done by using H Blank interrupt

#8915 - night_ - Wed Jul 23, 2003 8:27 pm

Hey guys, I messed it up. Of course I am talking about HBLANK. Sorry for the confusion..

#8917 - Geno - Wed Jul 23, 2003 8:41 pm

Thanks so far, but I already knew that VBlank was part of the GBA; I was talking about how to access/use it with C++.

I'm really a newbie at GBA developement, so any help as far as posting code goes would be appreciated.

HBlank? I'd like to know how to use that too, if you or someone else could help me there.
_________________
Out of Order.

#8919 - night_ - Wed Jul 23, 2003 9:13 pm

Please read ThePernProject Tutorial day 5 on interrupts. http://www.thepernproject.com/index2.htm
Here is some code that could work. It assumes gba.h to be included. This code is partly untested and maybe not very nice.

Enable interrupts:
Code:
REG_IME = 0x1 //enable interrupts
REG_IE = 0x0002; //enable hblank
REG_DISPSTAT=BIT(4); //enable hblank   
*((u32*) 0x3007FFC)=(u32)&InterruptHandler; //register interrupt handler


Interrupt handler, called when any interruppt occurs
Code:
void InterruptHandler()
{
   
if( REG_IF == 0x0002)//check for hblank
{
  //do stuff here
  REG_IF |= 0x0002;//strange register, set it to tell that the interrupt has been handled
}


}

#8921 - niltsair - Wed Jul 23, 2003 9:23 pm

For the VBlank, if you want to quickly access it without using interupt, you can check which line it is currently refreshing by checking the appropriate Video register :

#define VBLANK ((volatile u16*)0x04000006))

//Wait untill a VBlank
while( VBLANK != 160 );

Note : Code untested and wastefull on batteries, it's better to implement VBlank related things using interupts rather than make the CPU spin mindlessly.

#9754 - sajiimori - Sat Aug 16, 2003 5:47 am

I don't know if you got the answer you want, but if not, perhaps it would help if you asked a more specific question. What is it that you actually want to do with the vblank?

Anyway, you can access parts of memory in C or C++ by typing the address as a constant, then casting it to a pointer and dereferencing it.

If you didn't understand the previous sentence, go to amazon.com and order the "C Primer".