Skip to navigation

Elite on the BBC Micro and NES

Keyboard: TT17

[BBC Master version]

Name: TT17 [Show more] Type: Subroutine Category: Keyboard Summary: Scan the keyboard for cursor key or joystick movement
Context: See this subroutine in context in the source code Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * Main game loop (Part 5 of 6) calls TT17 * DJOY calls via TJ1

Scan the keyboard and joystick for cursor key or stick movement, and return the result as deltas (changes) in x- and y-coordinates as follows: * For joystick, X and Y are integers between -2 and +2 depending on how far the stick has moved * For keyboard, X and Y are integers between -1 and +1 depending on which keys are pressed
Returns: A The key pressed, if the arrow keys were used X Change in the x-coordinate according to the cursor keys being pressed or joystick movement, as an integer (see above) Y Change in the y-coordinate according to the cursor keys being pressed or joystick movement, as an integer (see above)
Other entry points: TJ1 Check for cursor key presses and return the combined deltas for the digital joystick and cursor keys (Master Compact only)
.TT17 LDA QQ11 \ If this not the space view, skip the following three BNE TT17afterall \ instructions to move onto the SHIFT key logic JSR DOKEY \ This is the space view, so scan the keyboard for \ flight controls and pause keys, (or the equivalent on \ joystick) and update the key logger, setting KL to the \ key pressed TXA \ Transfer the value of the key pressed from X to A RTS \ Return from the subroutine .TT17afterall JSR DOKEY \ Scan the keyboard for flight controls and pause keys, \ (or the equivalent on joystick) and update the key \ logger, setting KL to the key pressed IF _COMPACT LDX #0 \ Set the initial values for the results, X = Y = 0, LDY #0 \ which we now increase or decrease appropriately ENDIF LDA JSTK \ If the joystick is not configured, jump down to TJ1, BEQ TJ1 \ otherwise we move the cursor with the joystick IF _COMPACT LDA MOS \ If MOS = 0 then this is a Master Compact, so jump to BEQ DJOY \ DJOY to read the digital joystick before rejoining the \ routine below at TJ1 ENDIF LDA JSTY \ Fetch the joystick pitch, ranging from 1 to 255 with \ 128 as the centre point JSR TJS1 \ Call TJS1 just below to set A to a value between -4 \ and +4 depending on the joystick pitch value (moving \ the stick up and down) TAY \ Copy the result into Y LDA JSTX \ Fetch the joystick roll, ranging from 1 to 255 with \ 128 as the centre point EOR #&FF \ Flip the sign so A = -JSTX, because the joystick roll \ works in the opposite way to moving a cursor on-screen \ in terms of left and right JSR TJS1 \ Call TJS1 just below to set A to a value between -4 \ and +4 depending on the joystick roll value (moving \ the stick sideways) TAX \ Copy the value of A into X IF _SNG47 LDA KL \ Set A to the value of KL (the key pressed) RTS \ Return from the subroutine ENDIF .TJ1 LDA KL \ Set A to the value of KL (the key pressed) IF _SNG47 LDX #0 \ Set the results, X = Y = 0 LDY #0 ENDIF CMP #&8C \ If left arrow was pressed, set X = X - 1 BNE P%+3 DEX CMP #&8D \ If right arrow was pressed, set X = X + 1 BNE P%+3 INX CMP #&8E \ If down arrow was pressed, set Y = Y - 1 BNE P%+3 DEY CMP #&8F \ If up arrow was pressed, set Y = Y + 1 BNE P%+3 INY PHX \ Store X (which contains the change in the \ x-coordinate) on the stack so we can retrieve it later IF _SNG47 LDA #0 \ Call DKS5 to check whether the SHIFT key is being JSR DKS5 \ pressed ELIF _COMPACT LDA #0 \ Call DKS4mc to check whether the SHIFT key is being JSR DKS4mc \ pressed ENDIF BMI speedup \ If SHIFT is being pressed, skip the next three \ instructions PLX \ SHIFT is not being pressed, so retrieve the value of X \ we stored above so we can return it LDA KL \ Set A to the value of KL (the key pressed) RTS \ Return from the subroutine .speedup PLA \ Pull the value of X from the stack into A, so A now \ contains the change in the x-coordinate ASL A \ SHIFT is being held down, so quadruple the value of A ASL A \ (i.e. SHIFT moves the cursor at four times the speed \ when using the joystick) TAX \ Put the amended value of A back into X TYA \ Now to do the same with the change in y-coordinate, so \ fetch the value of Y into A ASL A \ SHIFT is being held down, so quadruple the value of A ASL A \ (i.e. SHIFT moves the cursor at four times the speed \ when using the joystick) TAY \ Put the amended value of A back into Y IF _COMPACT STZ KY7 \ Clear the key logger at KY7 to reset the "A" (fire) \ button to "not pressed" ENDIF LDA KL \ Set A to the value of KL (the key pressed) RTS \ Return from the subroutine .TJS1 \ This routine calculates the following: \ \ A = round(A / 16) - 4 \ \ This set A to a value between -4 and +4, given an \ initial value ranging from 1 to 255 with 128 as \ the centre point LSR A \ Set A = A / 16 LSR A \ LSR A \ and C contains the last bit to be shifted out LSR A LSR A ADC #0 \ If that last bit was a 1, this increments A, so \ this effectively implements a rounding function, \ where 0.5 and above get rounded up SBC #3 \ The addition will not overflow, so the C flag is \ clear at this point, so this performs: \ \ A = A - 3 - (1 - C) \ = A - 3 - (1 - 0) \ = A - 3 - 1 \ = A - 4 RTS \ Return from the subroutine