.ReadNumber STA T \ Set T to the maximum number of digits to read JSR EnableKeyboard \ Select the keyboard as the input stream and flush the \ keyboard buffer \ We start by clearing the input buffer by filling it \ with spaces LDY #7 \ The input buffer is eight bytes long, so set a byte \ counter in Y LDA #' ' \ Set A to the space character for use when clearing the \ input buffer .rkey1 STA inputBuffer,Y \ Reset the Y-th byte of the input buffer to contain a \ space character DEY \ Decrement the byte counter BPL rkey1 \ Loop back until we have cleared the whole input buffer JSR PrintInputBuffer \ Print the contents of the keyboard input buffer, which \ will erase any existing text on-screen as we just \ filled the input buffer with spaces .rkey2 LDY #0 \ We now read the specified number of key presses, so \ set Y as a counter for the number of valid characters \ in the input buffer, starting from zero (as the buffer \ is empty at the start) .rkey3 JSR ReadCharacter \ Read a character from the keyboard into A, so A is set \ to the ASCII code of the pressed key CMP #13 \ If RETURN was pressed then jump to rkey9 to return BEQ rkey9 \ from the subroutine, as RETURN terminates the input CMP #'0' \ If the key pressed is less than ACSII "0" then it is a BCC rkey3 \ control code, so jump back to rkey3 to keep listening \ for key presses, as control codes are not valid input CMP #127 \ If the key pressed is less than ASCII 127 then it is BCC rkey5 \ a printable ASCII character, so jump to rkey5 to \ process it BNE rkey3 \ If the key pressed is not the DELETE key then jump \ back to rkey3 to keep listening for key presses, as \ this is not a valid input \ If we get here then DELETE has been pressed, so we \ need to delete the most recently entered character \ \ Because the input buffer is stored as an ascending \ stack, this means we need to delete the character at \ inputBuffer, which is the top of the buffer stack, \ and shuffle the rest of the stack to the left to \ close up the gap (so that's shuffling then down in \ memory) DEY \ Decrement Y to reduce the character count by one, as \ we are about to delete a character from the buffer BMI rkey2 \ If we just decremented Y past zero then the buffer is \ empty, so jump to rkey2 to reset Y to zero and keep \ reading characters, as there is nothing to delete LDX #0 \ Otherwise we want to delete the character from the top \ of the buffer stack at inputBuffer and shuffle the \ rest of the stack along to the left, so set an index \ in X to work through the buffer from left to right .rkey4 LDA inputBuffer+1,X \ Shuffle the character at index X + 1 to the left and STA inputBuffer,X \ into index X INX \ Increment the buffer index to point to the next \ character in the buffer as we work from left to right CPX #7 \ Loop back until we have shuffled all seven characters BNE rkey4 \ to the left LDA #' ' \ Set the last character in the input buffer to a space STA inputBuffer+7 \ as the bottom of the stack at inputBuffer+7 is now \ empty BNE rkey8 \ Jump to rkey8 to print the updated contents of the \ input buffer, so we can see the character being \ deleted, and loop back to listen for more key presses \ (this BNE is effectively a JMP as A is never zero) .rkey5 \ If we get here then the key press in A is a printable \ ASCII character CMP #':' \ If the character in A is ASCII ":" or greater then it BCS rkey3 \ is not a number, so jump to rkey3 to keep listening \ for key presses, as we are only interested in numbers CPY T \ If Y <> T then the buffer does not yet contain the BNE rkey6 \ maximum number of digits allowed, so jump to rkey6 to \ process the number key press LDA #7 \ Otherwise the buffer is already full, so perform a JSR OSWRCH \ VDU 7 command to make a system beep JMP rkey3 \ Jump back to rkey3 to listen for more key presses .rkey6 \ If we get here then the key press in A is a number key \ and the input buffer is not full INY \ Increment Y to increase the character count by one, as \ we are about to add a character to the buffer PHA \ Store the key number in A on the stack, so we can \ retrieve it after the following loop LDX #6 \ We now want to insert the new character into the top \ of the buffer stack at inputBuffer and shuffle the \ stack along to the right (so that's shuffling then up \ in memory), so set an index in X to work through the \ buffer from right to left .rkey7 LDA inputBuffer,X \ Shuffle the character at index X to the right and into STA inputBuffer+1,X \ index X + 1 DEX \ Decrement the buffer index to point to the next \ character in the buffer as we work from right to left BPL rkey7 \ Loop back until we have shuffled all seven characters \ to the right PLA \ Restore the key number that we stored on the stack \ above STA inputBuffer \ Store the key press at the top of the stack, in \ inputBuffer .rkey8 JSR PrintInputBuffer \ Print the contents of the keyboard input buffer so we \ we can see the characters being entered or deleted JMP rkey3 \ Jump back to rkey3 to listen for more key presses .rkey9 RTS \ Return from the subroutineName: ReadNumber [Show more] Type: Subroutine Category: Keyboard Summary: Read a number from the keyboard into the input bufferContext: See this subroutine in context in the source code References: This subroutine is called as follows: * MainTitleLoop calls ReadNumber
Arguments: A The maximum number of digits to read
[X]
Subroutine EnableKeyboard (category: Keyboard)
Select the keyboard as the input stream and flush the keyboard buffer
[X]
Configuration variable OSWRCH = &FFEE
The address for the OSWRCH routine
[X]
Subroutine PrintInputBuffer (category: Text)
Print the contents of the keyboard input buffer
[X]
Subroutine ReadCharacter (category: Keyboard)
Read a character from the currently selected input stream
[X]
Variable inputBuffer in workspace Main variable workspace
The eight-byte keyboard input buffer
[X]
Label rkey1 is local to this routine
[X]
Label rkey2 is local to this routine
[X]
Label rkey3 is local to this routine
[X]
Label rkey4 is local to this routine
[X]
Label rkey5 is local to this routine
[X]
Label rkey6 is local to this routine
[X]
Label rkey7 is local to this routine
[X]
Label rkey8 is local to this routine
[X]
Label rkey9 is local to this routine