Reputation: 11
So basically I have my game running fine with 1s default delay for the snake to move.
I've seen the int 1Ah
/AH=00
interrupt to make a delay to make it move slower, but how do I make it move faster? Or I'm understanding the interrupt wrong? The idea is to create levels so that when you get to a certain score, the snake moves faster. Say I want a 0,75s delay for the snake to move, then 0,5s, etc.
Upvotes: 1
Views: 1271
Reputation: 37212
I've seen the int 1Ah/AH=00 interrupt to make a delay to make it move slower, but how do I make it move faster? Or I'm understanding the interrupt wrong?
This BIOS function returns a "ticks since midnight" value, where each tick is about 55 ms and there are about 1573040 ticks per day.
To create a delay of N ms; divide N by "about 55" then add the current ticks since midnight to it. This will be your expiry tick. In general you want something like while(current_tick < expiry_tick) { HLT(); }
where the HLT()
is the CPU's HLT
instruction that waits for an IRQ. However, it's not that simple because you have to care about the current tick rolling over at midnight.
To fix that, you actually want something more like:
#define TICKS_PER_DAY 1573040
#define MS_PER_TICK 55
int milliSecondWait(unsigned int milliseconds) {
// Calculate expiry time
(midnightFlag, tick) = get_tick();
expiryTick = tick + milliseconds / MS_PER_TICK;
// Wait for the right day
while(expiryTick > TICKS_PER_DAY) {
(midnightFlag, tick) = get_tick();
if( !midnightFlag) {
HLT();
} else {
expiryTick -= TICKS_PER_DAY;
}
}
// Wait for the right tick
do {
(midnightFlag, tick) = get_tick();
if(midnightFlag) {
break; // Tick rolled over skipping the expiry time
}
} while(tick < expiryTick);
NOTE: This is not valid C (it's pseudo-code that I assume you'll implement in some sort of assembly language), and the (midnightFlag, tick) = get_tick();
is supposed to be a function that returns 2 values (like the BIOS function does).
This can be done a lot more precisely (e.g. floating point maths, more accurate MS_PER_TICK
), possibly including configuring the timer/PIT chip to run at a faster frequency (the default 18.2 Hz frequency is the slowest the chip can run at) and tricking the BIOS into still working correctly (by using a counter to implement a clock divider and still calling the BIOS' IRQ handler at 18.2 Hz); but I'm guessing that you don't need all those complications. ;)
Upvotes: 1