base:robust_string_input
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:robust_string_input [2015-04-17 04:33] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Robust String Input ====== | ||
+ | Here's a short routine I wrote for the chat function in Artillery Duel. It only allows you to enter the characters specified. | ||
+ | |||
+ | < | ||
+ | ; | ||
+ | ;Input a string and store it in GOTINPUT, terminated with a null byte. | ||
+ | ;x:a is a pointer to the allowed list of characters, null-terminated. | ||
+ | ;max # of chars in y returns num of chars entered in y. | ||
+ | ; | ||
+ | |||
+ | GETIN = $ffe4 | ||
+ | |||
+ | ; Example usage | ||
+ | FILTERED_TEXT | ||
+ | lda #>TEXT | ||
+ | ldx #<TEXT | ||
+ | ldy #38 | ||
+ | ;Drop through | ||
+ | |||
+ | ; Main entry | ||
+ | FILTERED_INPUT | ||
+ | sty MAXCHARS | ||
+ | stx CHECKALLOWED+1 | ||
+ | sta CHECKALLOWED+2 | ||
+ | |||
+ | ;Zero characters received. | ||
+ | lda #$00 | ||
+ | sta INPUT_Y | ||
+ | |||
+ | ;Wait for a character. | ||
+ | INPUT_GET | ||
+ | jsr GETIN | ||
+ | beq INPUT_GET | ||
+ | |||
+ | sta LASTCHAR | ||
+ | |||
+ | cmp #$14 ; | ||
+ | beq DELETE | ||
+ | |||
+ | cmp #$0d ; | ||
+ | beq INPUT_DONE | ||
+ | |||
+ | ;Check the allowed list of characters. | ||
+ | ldx #$00 | ||
+ | CHECKALLOWED | ||
+ | lda $FFFF, | ||
+ | beq INPUT_GET | ||
+ | |||
+ | cmp LASTCHAR | ||
+ | beq INPUTOK | ||
+ | |||
+ | ;Not end or match, keep checking | ||
+ | inx | ||
+ | jmp CHECKALLOWED | ||
+ | |||
+ | INPUTOK | ||
+ | lda LASTCHAR | ||
+ | ldy INPUT_Y | ||
+ | sta GOTINPUT, | ||
+ | jsr $ffd2 ; | ||
+ | |||
+ | inc INPUT_Y | ||
+ | |||
+ | ;End reached? | ||
+ | lda INPUT_Y | ||
+ | cmp MAXCHARS | ||
+ | beq INPUT_DONE | ||
+ | |||
+ | ;Not yet. | ||
+ | jmp INPUT_GET | ||
+ | |||
+ | INPUT_DONE | ||
+ | ldy INPUT_Y | ||
+ | lda #$00 | ||
+ | sta GOTINPUT, | ||
+ | rts | ||
+ | |||
+ | ; Delete last character. | ||
+ | DELETE | ||
+ | ;First, check if we're at the beginning. | ||
+ | lda INPUT_Y | ||
+ | bne DELETE_OK | ||
+ | jmp INPUT_GET | ||
+ | |||
+ | ;At least one character entered. | ||
+ | DELETE_OK | ||
+ | ;Move pointer back. | ||
+ | dec INPUT_Y | ||
+ | |||
+ | ;Store a zero over top of last character, just in case no other characters are entered. | ||
+ | ldy INPUT_Y | ||
+ | lda #$00 | ||
+ | sta GOTINPUT,y | ||
+ | |||
+ | ;Print the delete char | ||
+ | lda #$14 | ||
+ | jsr $ffd2 | ||
+ | |||
+ | ;Wait for next char | ||
+ | jmp INPUT_GET | ||
+ | |||
+ | |||
+ | ; | ||
+ | ;Some example filters | ||
+ | ; | ||
+ | |||
+ | ;IPADDRESS | ||
+ | ; dc.b " | ||
+ | |||
+ | TEXT | ||
+ | dc.b " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890., | ||
+ | |||
+ | ; | ||
+ | MAXCHARS | ||
+ | dc.b $00 | ||
+ | |||
+ | LASTCHAR | ||
+ | dc.b $00 | ||
+ | |||
+ | INPUT_Y | ||
+ | dc.b $00 | ||
+ | |||
+ | GOTINPUT | ||
+ | ds.b #39 | ||
+ | |||
+ | </ |
base/robust_string_input.txt · Last modified: 2015-04-17 04:33 by 127.0.0.1