Skip to navigation

Aviator on the BBC Micro

Flight model: UpdateFlightModel (Part 1 of 4)

Name: UpdateFlightModel (Part 1 of 4) [Show more] Type: Subroutine Category: Flight model Summary: Apply any axis control key presses to the current axis values Deep dive: The key logger
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * MainLoop (Part 1 of 15) calls UpdateFlightModel * NewGame calls UpdateFlightModel
.UpdateFlightModel LDX #2 \ We start with the aileron, rudder and elevator key \ pairs, whose values are stored in these key logger \ offsets in (keyLoggerHi keyLoggerLo): \ \ * 2 = aileron \ * 1 = rudder \ * 0 = elevator \ \ So we set a counter in X to count down through these \ index values, from 2 to 1 to 0 .umod1 CLC \ Clear the C flag for the addition below LDA keyLoggerLo,X \ Fetch the low byte for this key pair, which will be 1 \ if a key is being pressed, or 0 if no key is pressed BEQ umod4 \ If A = 0 then neither key in this key pair is being \ pressed, so jump down to umod4 ADC axisKeyUsage,X \ Add the low value to the corresponding variable for STA axisKeyUsage,X \ this key pair in axisKeyUsage, so we increment the \ value every time a key from this pair is used LDA keyLoggerHi,X \ Fetch the high byte for this key pair, which will be \ +1 or -1 if a key is being pressed, or 0 if no key is \ pressed (so it contains the sign of the key logger \ value) STA P \ Store the value in P so we can check its sign below ADC elevatorPosition,X \ Set A = A + one of the following axis values: \ \ * aileronPosition if this is the aileron key pair \ * rudderPosition if this is the rudder key pair \ * elevatorPosition if this is the elevator key pair \ \ so the relevant value is increased or decreased by 1 \ according to the key press LDY axisChangeRate,X \ If this key pair's axisChangeRate value is already BEQ umod2 \ zero, skip the following instruction DEC axisChangeRate,X \ Decrement this key pair's axisChangeRate value .umod2 BNE umod3 \ If this key pair's axisChangeRate value is non-zero, \ skip the following \ If we get here, then this key pair's axisChangeRate \ value has been reduced down to zero by repeated calls \ to UpdateFlightModel with the key being held down, so \ the relevant control is now fully engaged and we bump \ up the rate of change by another 3 in the relevant \ direction CLC \ Set A = A + 3 ADC #3 BIT P \ If P is positive (so we are increasing the relevant BPL umod3 \ axis value), skip the following instruction ADC #250 \ P is negative (so we are decreasing the relevant \ axis value), so set A = A - 6, giving a net change of \ setting A = A - 3 .umod3 TAY \ If the adjusted axis value is positive, jump down to BPL umod5 \ umod5 to check the maximum allowed value \ If we get here then the adjusted axis value is \ negative, so now we check against the minimum allowed \ value CMP #140 \ If A >= -116, the value is within limits, so jump to BCS umod6 \ umod6 to store it LDA #140 \ Set A = -116 as the minimum allowed value for the axis BNE umod6 \ value and jump to umod6 to store it .umod4 \ If we get here then neither key was pressed from this \ key pair LDA #6 \ Set the axisChangeRate value for this key pair to 6, STA axisChangeRate,X \ so the rate of change goes back to 1 until we fully \ engage the control once again BNE umod7 \ Jump down to umod7 to move on to the next key pair \ (this BNE is effectively a JMP as A is never zero) .umod5 \ If we get here then the adjusted axis value is \ positive, so now we check against the maximum allowed \ value CMP #119 \ If A < 119, the value is within limits, so jump to BCC umod6 \ umod6 to store it LDA #118 \ Set A = 118 as the maximum allowed value for the axis \ value .umod6 STA elevatorPosition,X \ Store the adjusted axis value in the relevant axis \ variable: \ \ * aileronPosition if this is the aileron key pair \ * rudderPosition if this is the rudder key pair \ * elevatorPosition if this is the elevator key pair .umod7 DEX \ Decrement the key pair index BPL umod1 \ Loop back to process the next key pair until we have \ done all three axes of movement (aileron roll, rudder \ yaw and elevator pitch) JSR ReadJoystick \ Read the joystick axes and fire button and update the \ aileron, elevator and fire key values accordingly