#7922 - hnager - Sat Jun 28, 2003 6:55 pm
I'm curious to see if anybody has a countdown timer writen...so, for example, if each level in a game lasts 30 seconds it will count down and show the remaining time (and then hit you with a 'time up' when it expires).
Do I need to tie in to the interrupts or can it be done other (accurate) ways?
#7931 - tepples - Sat Jun 28, 2003 9:43 pm
It's really quite simple. Set up variables 'minutes', 'seconds', and 'frame' to hold the time of a play period. (For basketball, they'd be 12, 0, and 0.) Every vblank, decrement a 'frame' counter. When 'frame' reaches 0, decrement a 'seconds' counter and reset the 'frame' counter to 60 (the GBA runs at about 60 frames per second). When 'seconds' reaches 0, decrement a 'minutes' counter and reset the 'seconds' counter (there are 60 seconds in a minute). When 'minutes' reaches 0, sound the buzzer; whoever has the most points wins.
If you want to be cute, you can make your levels 3 seconds long instead of 30 and then make your objectives simpler to match.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7934 - hnager - Sat Jun 28, 2003 10:29 pm
3 second levels, eh? sounds familiar ;) For some reason I was having trouble with the timer in general - but have since resolved it and have it working on emu and hardware.
Im actually using a slightly modified version of what'd found int he pern project for a timer (acting more as a wait timer currently) - do most people simplt increment on vblank/?
#7951 - tepples - Sun Jun 29, 2003 6:33 am
hnager wrote: |
do most people simplt increment on vblank/? |
Synchronizing the game to the vblank is what I've seen in pretty much every console game since the NES.
I've seen this question quite a bit. I just added it to the FAQ one minute ago.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7960 - hnager - Sun Jun 29, 2003 2:37 pm
Just read the editted answer int he FAQ - thanks.
#7961 - hnager - Sun Jun 29, 2003 2:49 pm
I just did a hardware test and it seems as if the timer is running double speed(maybe a little more) - when the gba got to '60' my stopwatch read 25 seconds.
int elapsed = 0;
while(1){
WaitForVsync();
if(!(elapsed++%60)) {
sprintf(tracebuffer, "elapsed= %d", elapsed/60);
trace(tracebuffer);
}
}
oh, the trace() function just displays that buffer (tracebuffer) to the gba's screen...any idea why this would be running double fast?
#7962 - hnager - Sun Jun 29, 2003 3:38 pm
Changing the above to WaitVBlank() (defined below) it looks to work properly. I believe that I got the WaitForVsync() from the pern project, is it opening up a full horizonal draw for my timer to increment? That would explain the speedy timer.
void WaitForVsync(){
while((volatile u16)REG_VCOUNT != 160){}
}
void WaitVBlank() {
while(REG_VCOUNT == 160);
while (REG_VCOUNT != 160);
}
#7976 - Quirky - Sun Jun 29, 2003 8:15 pm
I would recommend investigating the bios software interrupt for vblank waiting - it fixes those "is it waiting on the 160th line, or does it skip it?" worries. Plus it saves batteries :)
#7977 - hnager - Sun Jun 29, 2003 8:28 pm
saves batteries?
Thanks for the tip - can you think of any examples flating around?
#7983 - tepples - Sun Jun 29, 2003 9:10 pm
Learn how to write an interrupt service routine (ISR), based on the published tutorials. Then enable the vblank interrupt and use the VblankIntrWait() function in BIOS to wait for a vblank interrupt. This function turns off the GBA CPU's ARM core until the appropriate interrupt happens. This means that during that short time, the ARM core isn't drawing power, and the memory controller isn't drawing power servicing the ARM core's requests.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7988 - hnager - Mon Jun 30, 2003 1:13 am
I'm not finding much which allows me to write the interrupt in C - from what I understand, inorder to do so I need to link crt0.s - this may be a bit beyond me right now - I get multiple compiler errors when trying to link that file (crt0.o that is).
Maybe that's where to start - multiple pern project files include crt0.s, however I can't get them to compile with his makefile.
thanks.
#8522 - toporny - Sat Jul 12, 2003 11:45 pm
tepples wrote: |
This function turns off the GBA CPU's ARM core until the appropriate interrupt happens. This means that during that short time, the ARM core isn't drawing power, and the memory controller isn't drawing power servicing the ARM core's requests. |
Well.. Interesing posibility, but what's happen with my modplay routine when i turn off the core of proccesor ?
#8523 - tepples - Sat Jul 12, 2003 11:56 pm
toporny wrote: |
but what's happen with my modplay routine when i turn off the core of proccesor ? |
When Halt(), Stop(), or IntrWait() puts the processor in halt mode, the interrupt controller turns the ARM core back on after the next interrupt. If your music code and mixer are triggered on vblank or a timer, the interrupt will come through, turn on the ARM core, and trigger the code as you'd expect.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.