Skip to navigation

Elite on the BBC Micro and NES

Controllers: DOKEY

[NES version, Bank 0]

Name: DOKEY [Show more] Type: Subroutine Category: Controllers Summary: Scan for the seven primary flight controls and apply the docking computer manoeuvring code Deep dive: The key logger The docking computer
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * TT17 calls DOKEY
.DOKEY JSR SetKeyLogger_b6 ; Populate the key logger table with the controller ; button presses and return the button number in X ; if an icon bar button has been chosen LDA auto ; If auto is non-zero, then the docking computer is BNE doky3 ; currently activated, so jump to doky3 to apply the ; docking computer manoeuvring code below .doky1 LDX iconBarKeyPress ; If the icon bar key logger entry in iconBarKeyPress CPX #64 ; is not 64, then jump to doky2 to skip the following BNE doky2 ; (interestingly, iconBarKeyPress can never contain ; this value, as none of the keys in the iconBarButtons ; table have this value, and the value for the Start ; button is 80) JMP PauseGame_b6 ; Pause the game and process choices from the pause menu ; until the game is unpaused by another press of Start, ; returning from the subroutine using a tail call .doky2 RTS ; Return from the subroutine .doky3 LDA SSPR ; If we are inside the space station safe zone, jump to BNE doky4 ; doky4 to run the docking computer manoeuvring code STA auto ; Otherwise set auto to 0 to disable the docking ; computer, so we can't engage it outside of the safe ; zone JSR ResetMusicAfterNMI ; Wait for the next NMI before resetting the current ; tune to 0 (no tune) and stopping the music JMP doky1 ; Loop back to doky1 to check the icon bar key logger ; entry and return from the subroutine .doky4 JSR ZINF ; Call ZINF to reset the INWK ship workspace LDA #96 ; Set nosev_z_hi = 96 STA INWK+14 ORA #%10000000 ; Set sidev_x_hi = -96 STA INWK+22 STA TYPE ; Set the ship type to -96, so the negative value will ; let us check in the DOCKIT routine whether this is our ; ship that is activating its docking computer, rather ; than an NPC ship docking LDA DELTA ; Set the ship speed to DELTA (our speed) STA INWK+27 JSR DOCKIT ; Call DOCKIT to calculate the docking computer's moves ; and update INWK with the results ; We now "press" the relevant flight keys, depending on ; the results from DOCKIT, starting with the pitch keys LDA INWK+27 ; Fetch the updated ship speed from byte #27 into A CMP #22 ; If A < 22, skip the next instruction BCC P%+4 LDA #22 ; Set A = 22, so the maximum speed during docking is 22 STA DELTA ; Update DELTA to the new value in A LDA #$FF ; Set A = $FF, which we can insert into the key logger ; to "fake" the docking computer working the keyboard LDX #0 ; Set X = 0, so we "press" KY1 below (the "slow down" ; key combination of the B and down buttons) LDY INWK+28 ; If the updated acceleration in byte #28 is zero, skip BEQ DK11 ; to DK11 BMI P%+4 ; If the updated acceleration is negative, skip the ; following instruction LDX #1 ; Set X = 1, so we "press" KY+1, i.e. KY2, with the ; next instruction (speed up) STA KL,X ; Store $FF in either KY1 or KY2 to "press" the relevant ; key, depending on whether the updated acceleration is ; negative (in which case we "press" KY1, i.e. the B and ; down buttons, to slow down) or positive (in which case ; we "press" KY2, i.e. the B and up buttons, to speed ; up) .DK11 ; We now "press" the relevant roll keys, depending on ; the results from DOCKIT LDA #128 ; Set A = 128, which indicates no change in roll when ; stored in JSTX (i.e. the centre of the roll indicator) LDX #2 ; Set X = 2, so we "press" KL+2, i.e. KY3 below (left ; button, increase roll) ASL INWK+29 ; Shift ship byte #29 left, which shifts bit 7 of the ; updated roll counter (i.e. the roll direction) into ; the C flag BEQ DK12 ; If the remains of byte #29 is zero, then the updated ; roll counter is zero, so jump to DK12 set JSTX to 128, ; to indicate there's no change in the roll BCC P%+4 ; If the C flag is clear, skip the following instruction LDX #3 ; The C flag is set, i.e. the direction of the updated ; roll counter is negative, so set X to 3 so we ; "press" KY+3. i.e. KY4, below (right button, decrease ; roll) BIT INWK+29 ; We shifted the updated roll counter to the left above, BPL DK14 ; so this tests bit 6 of the original value, and if it ; is clear (i.e. the magnitude is less than 64), jump to ; DK14 to "press" the key and leave JSTX unchanged LDA #64 ; The magnitude of the updated roll is 64 or more, so STA JSTX ; set JSTX to 64 (so the roll decreases at half the ; maximum rate) LDA #0 ; And set A = 0 so we do not "press" any keys (so if the ; docking computer needs to make a serious roll, it does ; so by setting JSTX directly rather than by "pressing" ; a key) .DK14 STA KL,X ; Store A in either KY3 or KY4, depending on whether ; the updated roll rate is increasing (KY3) or ; decreasing (KY4) LDA JSTX ; Fetch A from JSTX so the next instruction has no ; effect .DK12 STA JSTX ; Store A in JSTX to update the current roll rate ; We now "press" the relevant pitch keys, depending on ; the results from DOCKIT LDA #128 ; Set A = 128, which indicates no change in pitch when ; stored in JSTX (i.e. the centre of the pitch ; indicator) LDX #4 ; Set X = 4, so we "press" KY+4, i.e. KY5, below ; (down button, decrease pitch, pulling the nose up) ASL INWK+30 ; Shift ship byte #30 left, which shifts bit 7 of the ; updated pitch counter (i.e. the pitch direction) into ; the C flag BEQ DK13 ; If the remains of byte #30 is zero, then the updated ; pitch counter is zero, so jump to DK13 set JSTY to ; 128, to indicate there's no change in the pitch BCS P%+4 ; If the C flag is set, skip the following instruction LDX #5 ; Set X = 5, so we "press" KY+5, i.e. KY6, with the next ; instruction (up button, increase pitch, so the nose ; dives) STA KL,X ; Store 128 in either KY5 or KY6 to "press" the relevant ; key, depending on whether the pitch direction is ; negative (in which case we "press" KY5, the down ; button, to decrease the pitch, pulling the nose up) or ; positive (in which case we "press" KY6, the up button, ; to increase the pitch, pushing the nose down) LDA JSTY ; Fetch A from JSTY so the next instruction has no ; effect .DK13 STA JSTY ; Store A in JSTY to update the current pitch rate LDX JSTX ; Set X = JSTX, the current roll rate (as shown in the ; RL indicator on the dashboard) LDA #14 ; Set A to 14, which is the amount we want to alter the ; roll rate by if the roll keys are being pressed LDY KY3 ; If the left button is not being pressed, skip the next BEQ P%+5 ; instruction JSR BUMP2 ; The left button is being pressed, so call the BUMP2 ; routine to increase the roll rate in X by A LDY KY4 ; If the right button is not being pressed, skip the BEQ P%+5 ; next instruction JSR REDU2 ; The right button is being pressed, so call the REDU2 ; routine to decrease the roll rate in X by A, taking ; the keyboard auto re-centre setting into account STX JSTX ; Store the updated roll rate in JSTX LDA #14 ; Set A to 15, which is the amount we want to alter the ; roll rate by if the pitch keys are being pressed LDX JSTY ; Set X = JSTY, the current pitch rate (as shown in the ; DC indicator on the dashboard) LDY KY5 ; If the down button is not being pressed, skip the next BEQ P%+5 ; instruction JSR REDU2 ; The down button is being pressed, so call the REDU2 ; routine to decrease the pitch rate in X by A, taking ; the keyboard auto re-centre setting into account LDY KY6 ; If the up button is not being pressed, skip the next BEQ P%+5 ; instruction JSR BUMP2 ; The up button is being pressed, so call the BUMP2 ; routine to increase the pitch rate in X by A STX JSTY ; Store the updated roll rate in JSTY LDA auto ; If auto is non-zero, then the docking computer is BNE doky6 ; currently activated, so jump up to doky1 via doky6 to ; check the icon bar key logger entry and return from ; the subroutine LDX #128 ; Set X = 128, which indicates no change in pitch when ; stored in JSTX (i.e. the centre of the pitch ; indicator) LDA KY3 ; If either of the left or right buttons are being ORA KY4 ; pressed, jump to doky5 to skip the following BNE doky5 ; instruction, so pressing buttons on the controller ; overrides the docking computer STX JSTX ; Store the updated roll rate in JSTX .doky5 LDA KY5 ; If either of the up or down buttons are being ORA KY6 ; pressed, jump to doky6 to skip the following BNE doky6 ; instruction, so pressing buttons on the controller ; overrides the docking computer STX JSTY ; Store the updated roll rate in JSTY .doky6 JMP doky1 ; Loop back to doky1 to check the icon bar key logger ; entry and return from the subroutine