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 > Meaning of Volatile

#17884 - xadeu - Tue Mar 16, 2004 4:17 pm

What is the meaning of volatile and how does it effect the GBA Hardware?
Basically why is it needed in GBA Programming...I haven't ever used it before in my programs.

#17885 - Gopher - Tue Mar 16, 2004 4:22 pm

volatile means that the variable's value might be changed outside your code. Specifying a variable or expression as Volatile prevents the compiler from trying to optimize your code by pre-evaluating it once. I.e., if you didn't define the joypad input register to volatile and you did a loop watching for a button press, the optimizer can tell that your code doesn't change that register, and it might try to test once before your loop instead of repeating the test inside the loop.
_________________
"Only two things are infinite: the universe, and human stupidity. The first is debatable." -Albert Einstein

#17886 - jma - Tue Mar 16, 2004 4:24 pm

Volatile is a keyword telling the compiler that an area of RAM is hardware controlled. This is a descriptive term meaning: it can be altered from outside the software.

If you did not use the volatile keyword, the C compiler would attempt to put the value of a GBA register in a hardware register for faster use, instead of loading it again from RAM each time.

Jeff
_________________
massung@gmail.com
http://www.retrobyte.org

#17945 - johnny_north - Wed Mar 17, 2004 9:58 pm

I case you'd be wondering some of the places to use it, I most frequently see it used in conjunction with the register that logs the vcount, although our forum moderator suggests that you should use volatile in conjunction with any register declaration. Check out pineight's headder:
http://www.pineight.com/gba/#progs

If you set the optimization level in gcc to O1 or greater, variables changed within an interrupt won't actually change unless you declare them volatile.