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.

Coding > REG_VCOUNT doesnt change

#11667 - yanos - Wed Oct 15, 2003 4:50 pm

Im trying a random function founded on this board but im having a small problem with the seed. It is based on the REG_VCOUNT register, but mine seems to be stuck at the same value. My set_random_seed(u8 offset) functions looks like this:

Code:

random_seed = REG_VCOUNT + offset;


where random_seed => volatile s32 random_seed;
and REG_VCOUNT => #define REG_VCOUNT *(u16*)0x4000006

I call this function once in a while in my code and random_seed always ended up to be the same. I first tought that was because I was waiting for vblank before calling it, but commenting that part of the code didnt change anything. Also, before calling this function, there is a title screen displayed, waiting for the user to press 'start' before continuing, so set_random_seed() is always called 'randomly'.

#11668 - DekuTree64 - Wed Oct 15, 2003 5:10 pm

You need to define REG_VCOUNT as volatile too.
#define REG_VCOUNT *(volatile u16*)0x4000006
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#11669 - niltsair - Wed Oct 15, 2003 5:14 pm

You must declare the register as volatile, else your compiler assume the value never change and does optimisation accordingly.

#define REG_VCOUNT *(volatile u16*)0x4000006

(edit)
Darn, i was beaten to it ;-)
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#11671 - yanos - Wed Oct 15, 2003 5:45 pm

I didnt know you also have to define the register as volatile too. But it doesnt seem to solve my problem. REG_VCOUNT is still always the same. Any clue?

#11672 - yanos - Wed Oct 15, 2003 6:14 pm

forget it! My REG_VCOUNT is changing. It's just that each time I call my function, it ends up that REG_VCOUNT is always at the same value. Dont really know why thought, maybe it's because the registers for the input is always refreshed at a very precise moment, so no matter how long the user waits before pressing the start button, my program detect it always at the same REG_VCOUNT value... or something like that :)

#11673 - DekuTree64 - Wed Oct 15, 2003 6:18 pm

Hmm, is it stuck at 0, or some other value? I can't think of any way to make it freeze up.
Try doing a while(REG_VCOUNT != 100); (or any number between 0 and 160) just to see if it's moving at all.

edit: nevermind then^_^
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#11674 - niltsair - Wed Oct 15, 2003 6:20 pm

(Edit)
Ok, never mind what i just wrote, do this instead:

Create a timer out of VBlank, use this timer value instead of VCount. There's some posts around about doing a timer out of VBlank. Basis is just enable VBlank interrupt, and in it increase a global variable value everytime. This was the way specified to generate the random number too, in the post about it.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#11677 - yanos - Wed Oct 15, 2003 6:51 pm

I started a timer while the user is waiting to press the start button to start the game. So, when it happens, the value in the timer is enough random for me and I set the seed based on that value. In a way, its very close to the solution you proposed, and its working great. The bombs in my minesweeper game are not always at the same place anymore ;)

thanks for your help!

#11678 - niltsair - Wed Oct 15, 2003 7:16 pm

Glad to help.

I see you're a compatriot from Montreal too. Hope you're car wasn't parked near the yesterday protest ;-)
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#11680 - yanos - Wed Oct 15, 2003 8:18 pm

no, im just a poor student without a car :)