Skip to navigation

Keyboard: ScanForGameKeys

Name: ScanForGameKeys [Show more] Type: Subroutine Category: Keyboard Summary: Scan for game key presses and update the key logger
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * CheckForKeyPresses calls ScanForGameKeys * CheckForSamePanKey calls ScanForGameKeys * IRQHandler calls ScanForGameKeys

Arguments: Y The offset within gameKeys where we start the scan, with the scan working towards the start of gameKeys
Returns: N flag Determines whether a pan key is being pressed: * Bit 7 of A will be set if no pan keys are being pressed (so a BMI branch will be taken) * Bit 7 of A will be clear if at least one pan key is being pressed (so a BPL branch will be taken)
.ScanForGameKeys LDX #3 \ We start by resetting the key logger, so set a loop \ counter in X for resetting all four entries LDA #%10000000 \ Set A = %10000000 to reset all four entries, as the \ set bit 7 indicates an empty entry in the logger .gkey1 STA keyLogger,X \ Reset the X-th entry in the key logger DEX \ Decrement the loop counter BPL gkey1 \ Loop back until we have reset all four entries \ We now work our way backwards through the gameKey \ table, starting at offset Y, and checking to see if \ each key is being pressed and logging the results in \ the key logger .gkey2 LDX gameKeys,Y \ Set X to the internal key number for the Y-th key in \ the gameKey table JSR ScanKeyboard \ Scan the keyboard to see if this key is being pressed BNE gkey3 \ If the key in X is not being pressed, jump to gkey3 to \ move on to the next key in the table LDA keyLoggerConfig,Y \ Set X to the key logger entry where we should store AND #%00000011 \ this key press, which is in bits 0 and 1 of the TAX \ corresponding entry in the keyLoggerConfig table LDA keyLoggerConfig,Y \ Set A to the value to store in the key logger for this LSR A \ key, which is in bits 2 to 7 of the corresponding LSR A \ entry in the keyLoggerConfig table STA keyLogger,X \ Store the configured value in the configured entry \ for this key press .gkey3 DEY \ Decrement the index in Y to move on to the next key \ in the gameKey table BPL gkey2 \ Loop back until we have checked all the keys up to the \ start of the gameKey table LDA keyLogger \ Combine the key logger entry for "S" and "D" (pan left AND keyLogger+2 \ and right) with the key logger entry for "L" and "," \ (pan left and right and set the status flags according \ to the result \ \ Specifically, if bit 7 is set in both entries, then no \ pan keys are being pressed, so a BMI following the \ call to ScanForGameKeys will be taken \ \ If, however, bit 7 is clear in either entry, then at \ least one pan key is being pressed, so a BPL following \ the call to ScanForGameKeys will be taken RTS \ Return from the subroutine