LZ: Input from keyboard


[Prev][Next][Index][Thread]

LZ: Input from keyboard



Someone wanted a routine that let the user input a number from the keyboard.
I've written one, but I though it was better to post it at LZ so more
ppl could use it. It could be made better though. For example, you can't
backspace (or even clear it) because then you would have to count how
many characters you've entered. There is no limit for how many digits
the user press either...

The routine returns with HL = the number entered. If the number is bigger
than 65535, it will be mod 65536.

To test the routine, there is a program that adds two words...

#include ti-85.h

.org 0
.db "Add two numbers",0

 ROM_CALL(CLEARLCD)
 ld de,$0802
 ld ($800C),de
 CALL_(Input)
 push hl
 ld de,$0802
 ld ($800C),de
 ROM_CALL(D_HL_DECI)
 ld de,$0803
 ld ($800C),de
 CALL_(Input)
 push hl
 ld de,$0703
 ld ($800C),de
 ld a,43
 ROM_CALL(TX_CHARPUT)
 ROM_CALL(D_HL_DECI)
 ld de,$0704
 ld ($800C),de
 ld b,6
 ld a,45
RepLine:
 ROM_CALL(TX_CHARPUT)
 djnz RepLine
 pop hl
 pop de
 add hl,de
 ld de,$0805
 ld ($800C),de
 ROM_CALL(D_HL_DECI)
WaitKey:
 call GET_KEY
 or a
 jr z,WaitKey
 ret

; ------------------------------------------------------------------------
;  INPUT
;
;  Read's a decimal number from the keyboard at the current cursor location
;  The number is stored in HL (thus the range 0-65535)
;  If the value is bigger, it will wrap around (65537=1 for example)
;  The A register will be destroyed (and HL of course)
;  Interrupts must be enabled, else the cursor won't blink
;
; ------------------------------------------------------------------------

Input:
 push bc              ; Save most register pairs
 push de
 push ix
 ld ix,0              ; IX = the current number
 set 2,(iy+12)        ; Turn cursor blinking on (interrupts must be enabled)
RepGetKey:
 ld a,32
 ld ($800E),a         ; Set "character under cursor" to space
 call GET_KEY         ; Get a key
 or a
 jr z,RepGetKey       ; If no key pressed, check again
 cp K_ENTER
 jr z,EnterPressed    ; If enter pressed, jump to "EnterPressed"
 ld hl,KeyTable
 ld de,(PROGRAM_ADDR)
 add hl,de            ; Load HL with the absolute address to KeyTable
 ld bc,10             ; There are ten keys in the table
 cpir                 ; Check which key A is
 jr nz,RepGetKey      ; If A wasn't found (not a valid key) check new key
 push ix              ; \
 pop hl               ;  |
 add hl,hl            ;  |
 ld d,h               ;  |
 ld e,l               ;   \ It requires many instructions to do the simple
 add hl,hl            ;   / IX=IX*10+BC...
 add hl,hl            ;  |
 add hl,bc            ;  |
 add hl,de            ;  |
 push hl              ;  |
 pop ix               ; /
 ld a,c
 add a,48             ; Load A with the ascii char of the digit
 ROM_CALL(TX_CHARPUT) ; And show the digit
 jr RepGetKey         ; Get next key
EnterPressed:         ; Enter has been pressed
 res 2,(iy+12)        ; Stopp cursor blinking
 ld a,32
 ROM_CALL(TX_CHARPUT) ; Clear the cursor (by putting a blank char on it)
 push ix
 pop hl               ; The easiest and shortest way to do HL=IX
 pop ix               ; Pop back the pushed registers
 pop de
 pop bc
 ret

KeyTable:
 .db K_9,K_8,K_7,K_6,K_5,K_4,K_3,K_2,K_1,K_0

.end

-- 
Jimmy Mårdell                "I want to stand in the eye of a storm
mailto:mja@algonet.se         I want to get struck by lightning
http://www.algonet.se/~mja    I want our house to be set on fire
IRC: Yarin                    for us to walk without shelter" /Covenant