Skip to navigation

Aviator on the BBC Micro

Flight model: ApplyFlightModel (Part 7 of 7)

Name: ApplyFlightModel (Part 7 of 7) [Show more] Type: Subroutine Category: Flight model Summary: Calculate fuel usage Deep dive: The flight model
Context: See this subroutine in context in the source code References: No direct references to this subroutine in this source file

This routine calculates the following: * Calculate fuel usage, depending on the current thrust, and decrease the fuel level as required.
LDA fuelLevel \ If the fuel tank is empty, jump to fmod19 to turn the BEQ fmod19 \ engine off, as we just ran out of fuel LDA engineStatus \ If the engine is off, jump to fmod20 to return from BEQ fmod20 \ the subroutine \ Otherwise the engine is on and we are not yet out of \ fuel, so we need to calculate the correct consumption \ rate and deplete the fuel supplies LDA thrustHi \ Set (R A) = (thrustHi thrustLo) STA R \ = thrust LDA thrustLo LDX #3 \ We now shift (R A) right by four places, so set a \ shift counter in X .fmod18 LSR R \ Set (R A) = (R A) / 2 ROR A \ = thrust / 2 DEX \ Decrement the shift counter BPL fmod18 \ Loop back until we have shifted (R A) four times \ By now we have: \ \ (R A) = thrust / 16 \ \ and because the maximum value of thrust is 1280, we \ know R must be 0, so: \ \ A = thrust / 16 \ \ We use this value of A as the amount of fuel used in \ this iteration of the main loop, so the higher the \ thrust, the more fuel is used \ \ That said, we don't just take this off the fuel level \ in fuelLevel - instead, we keep track of the fuel \ used in two bytes, fuelUsedLo and fuelUsedHi \ \ We add the fuel used in A to fuelUsedLo, and when \ we've used 256 units and fuelLevelLo wraps around \ from 255 to 0, we then add 4 to fuelUsedHi, and when \ we've filled up fuelUsedHi and it wraps from 255 to 0, \ that's when we take one unit of fuel off the main \ fuel level in fuelLevel \ \ In summary, to use up one unit of fuelLevel, we have \ to use up 256 * 64 = 16384 units from the above \ calculation CLC \ Set fuelUsedLo = fuelUsedLo + A ADC fuelUsedLo STA fuelUsedLo BCC fmod20 \ If the addition didn't overflow, jump to fmod20 \ to return from the subroutine LDA #4 \ The addition overflowed, so set: CLC \ ADC fuelUsedHi \ fuelUsedHi = fuelUsedHi + 4 STA fuelUsedHi BCC fmod20 \ If the addition didn't overflow, jump to fmod20 \ to return from the subroutine \ The addition overflowed, so it's time to reduce our \ fuel level LDA fuelLevel \ If the fuel tank is already empty, jump to fmod19 to BEQ fmod19 \ turn the engine off, as we just ran out of fuel DEC fuelLevel \ Otherwise decrement the fuel level by 1 BNE fmod20 \ If the fuel tank is still not empty, jump to fmod20 to \ return from the subroutine .fmod19 LDA #0 \ The fuel tank is empty, so turn the engine off JSR SetEngine .fmod20 RTS \ Return from the subroutine