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++ > VisualBoy Advance Blanks

#170583 - nathanpc - Sun Oct 04, 2009 2:47 pm

Hello,
I'm trying to do my first test of C development for GBA, but when i put the ROM in VisualBoy i got only a white screen, remember that the code compiles perfectly. See the code that i'm using:
Code:
/* hello.c - Gameboy Advance Tutorial - Loirak Development */
#define RGB16(r,g,b)  ((r)+(g<<5)+(b<<10))

int main()
{
   char x,y; 
   unsigned short* Screen = (unsigned short*)0x6000000;
   *(unsigned long*)0x4000000 = 0x403; // mode3, bg2 on

   // clear screen, and draw a blue back ground
   for(x = 0; x<240;x++)   //loop through all x
   {
      for(y = 0; y<160; y++)  //loop through all y
      {
         Screen[x+y*240] = RGB16(0,0,31); 
      }
   }

   // draw a white HI on the background
   for(x = 20; x<=60; x+=15)
      for(y = 30; y<50; y++) 
         Screen[x+y*240] = RGB16(31,31,31); 
   for (x = 20; x < 35; x++)
      Screen[x+40*240] = RGB16(31,31,31); 

   while(1){}   //loop forever
}

I'm using DevKitPro lastest version
And here my VisualBoy:
[Images not permitted - Click here to view it]

Best Regards,
Nathan Paulino Campos

#170585 - Cearn - Sun Oct 04, 2009 4:00 pm

You're probably not using the right linker flags. With devkitArm you need to add a -specs option to indicate where the sections should be. Use -specs=gba.specs or -specs=gba_mb.specs for cart- or multiboot-builds, respectively. For more information, see tonc:setup.

Or you could use template projects (devkitPro/examples/gba/template), which have been set-up to take of all these things automatically.


Two other sidenotes:

The RGB16 macro is unsafe. g and b have no parentheses around them, which could produce incorrect results due to operator precedence. For example, something like `RGB16(0, 0, blue&31)', where excess blue bits are cut off, would result in `blue&31<<10' = `blue & (31<<10)', which will usually be 0 instead of the masked blue you might have expected. As a rule, use parentheses around macro parameters.

Secondly, using a char-type for loop variables is a bad idea. Firstly, because they're not the standard integer type (i.e. int), they're a little slower. Secondly, they overflow faster, which can have its own set of problems. A signed char, for example, has a range of -128 to 127, so the first x-loop would never terminate. Then signedness of an unadorned char is compiler specific, so by just using `char', there may or may not be an infinite loop in this code. As it happens, devkitArm considers `char' to be `unsigned char', so you've lucked out here, but it's best to simply use int or unsigned int, or their typedef'ed shorthands, for local variables in all cases.