User Tools

Site Tools


base:robust_string_input

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. Written with DASM.

;======================================================================
;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               ;Delete
  beq DELETE

  cmp #$0d               ;Return
  beq INPUT_DONE

  ;Check the allowed list of characters.
  ldx #$00
CHECKALLOWED
  lda $FFFF,x           ;Overwritten
  beq INPUT_GET         ;Reached end of list (0)

  cmp LASTCHAR
  beq INPUTOK           ;Match found

  ;Not end or match, keep checking
  inx
  jmp CHECKALLOWED

INPUTOK
  lda LASTCHAR          ;Get the char back
  ldy INPUT_Y
  sta GOTINPUT,y        ;Add it to string
  jsr $ffd2             ;Print it

  inc INPUT_Y           ;Next character

  ;End reached?
  lda INPUT_Y
  cmp MAXCHARS
  beq INPUT_DONE

  ;Not yet.
  jmp INPUT_GET

INPUT_DONE
   ldy INPUT_Y
   lda #$00
   sta GOTINPUT,y   ;Zero-terminate
   rts

; Delete last character.
DELETE
  ;First, check if we're at the beginning.  If so, just exit.
  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 "1234567890.",0

TEXT
  dc.b " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.,-+!#$%&'()*",0

;=================================================
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