robsoft icon

first draft keyboard routine

robsoft | PRO | 05/28/17 10:09:49 PM UTC | 0 ⭐ | 4606 👁️ | Never ⏰ | []
Z80 Assembler |

0 B

|

None

|

0 👍

/

0 👎

                             ; first draft keyboard reading routine to replace some BASIC
                             ;
                             ; Kempston mouse and joystick not implemented yet
                             ; DEBUG CODE - PRESS 0 TO RETURN TO BASIC
                             ; Q,A,O,P move cursor
                             ; SPACE, ENTER, 1 and 0 are detected and indicated
                             ; K and J alter cursor speed
                             ;
org 30000                    ; everyone has to start somewhere
           jp _init          ; for ease of use in BASIC, we're going to keep
                             ; our variables at the start of the routine, so
                             ; jump up out of the way, this is probably sub-optimal
                             ;
           action defb 0     ; we store our 'action' indicator
           xpos defb 128, 0  ; have 2 bytes to make BC printing easy for now
           ypos defb 88, 0   ; have 2 bytes to make BC printing easy for now
           speed defb 1, 0   ; have 2 bytes to make BC printing easy for now
                             ;
                             ;
                             ; screen init code
                             ; ================
_init :    ld a, 71          ; black paper, white ink
           ld (23693), a     ; save attribs
           xor a             ; a = 0
           call 8859         ; clear the screen
           call 3503         ; setup channel 2
                             ;
                             ;
                             ; main code
                             ; =========
_mainloop: xor a             ; a = 0
           ld (action), a    ; reset our action indicator
           ld bc, 57342      ; keyrow for Y-P
           in a,(c)          ; get value
           push af           ; save it so we don't need to re-read in a moment
           cp 190        ; P key pressed?
           call z, moveright ; move cursor right
           pop af            ; get action back
           cp 189            ; O key pressed?
           call z, moveleft  ; move cursor left
           ld bc, 64510      ; keyrow for Q-T
           in a,(c)          ; get value
           cp 190            ; Q key pressed?
           call z, movedown  ; move cursor down (towards origin)
           ld bc, 65022      ; keyrow for A-G
           in a,(c)          ; get value
           cp 190            ; A key pressed?
           call z, moveup    ; move cursor up
                             ; ideally the following keys would be debounced
                             ; or whatever, so there was no auto-repeat going on
                             ; or at least there was a delay between repeats
           ld bc, 32766      ; keyrow for B-SPACE
           in a,(c)          ; get value
           cp 190            ; SPACE key pressed?
           call z, spacehit  ; update action
           ld bc, 63486      ; keyrow for 1-5
           in a,(c)          ; get value
           cp 190            ; 1 key pressed?
           call z, onehit    ; update action
           ld bc, 61438      ; keyrow for 6-0
           in a,(c)          ; get value
           cp 190            ; 0 key pressed?
           call z, zerohit   ; update action
           ld bc, 49150      ; keyrow for H-ENTER
           in a,(c)          ; get value
           push af           ; save it
           cp 190            ; ENTER key pressed?
           call z, enterhit  ; update action
           pop af            ; get it back
           push af           ; and save it again
           cp 187            ; K key pressed? (+) symbol
           call z, speedup   ; speed up cursor
           pop af            ; get action value again
           cp 183            ; J key pressed? (-) symbol
           call z, speeddown ; slow down cursor
                             ;
                             ; start debug code
                             ; ================
                             ; debugging - in final routine, we'd loop around
                             ; until the action code!=0, at which point we'd RET
                             ; to let BASIC handle things. But for now we're just
                             ; blindy plotting, printing to the screen and looping forever
                             ; until 0 key is pressed
                             ;
_plot      ld a, (ypos)      ; crappy hack to easily plot the pixel
           ld b, a           ;
           ld a, (xpos)      ;
           ld c, a           ;
           call 8937         ; ROM PLOT - not the entry point, assumes BC setup
                             ;
_print:    ld a, 22          ; now print at 0, 0
           rst 16            ;
           ld a, 0           ;
           rst 16            ;
           ld a, 0           ;
           rst 16            ;
           ld bc, (xpos)     ; get the xpos value (xpos in C, B is 0)
           call 6683         ; print BC
           ld a, 44          ; and a comma
           rst 16            ;
           ld bc, (ypos)     ; get the ypos value (ypos in C, B is 0)
           call 6683         ; print BC
           ld a, 32          ; and print 2 trailing spaces
           rst 16            ;
           ld a, 32          ;
           rst 16            ;
 
           ld a, 22          ; now, print at 1, 0
           rst 16            ;
           ld a, 1           ;
           rst 16            ;
           ld a, 0           ;
           rst 16            ;
           ld bc, (speed)    ; get the speed value (speed in C, B is 0)
           call 6683         ; print BC
           ld a, 32          ; and print a trailing space
           rst 16            ;
 
           ld a, 22          ; print at 2, 0
           rst 16            ;
           ld a, 2           ;
           rst 16            ;
           ld a, 0           ;
           rst 16            ;
           ld a, (action)    ; get the action value
           add a, 48         ; normalise it (0='0', 1='1' etc)
           rst 16            ; print it
                             ;
           ld a, (action)    ; get the action value
           cp 6              ; if it was 6 (zero pressed)
           ret z             ; then we return to basic
                             ; otherwise
           halt              ; wait for an interrupt
           jp _mainloop      ; go back to the top
                             ;
                             ; end debug code
                             ; ==============
                             ;
                             ;
                             ; subroutines
                             ; ===========
                             ;
speeddown: ld a, (speed)     ; fetch the speed
           dec a             ; subtract 1
           jr nz, savespeed  ; if this is still >0, we're good to save it
           ret
 
speedup:   ld a, (speed)     ; fetch the speec
           inc a             ; add 1
           cp 11             ; we're good as long as its <=10
           jr nz, savespeed  ; so save it
           ret
 
savespeed: ld (speed), a     ; save A back into the speed var
           ld a, 2           ; action code = 2
           ld (action), a    ; save action code
           ret
 
enterhit:  ld a, 3           ; action code = 3
           ld (action), a    ; save action code
           ret
 
spacehit:  ld a, 4           ; action code = 4
           ld (action), a    ; save action code
           ret
 
onehit:    ld a, 5           ; action code = 5
           ld (action), a    ; save action code
           ret
 
zerohit:   ld a, 6           ; action code = 6
           ld (action), a    ; save action code
           ret
 
moveright: ld a, (xpos)      ; fetch the current xpos in A
           ld hl, speed      ; get speed address into HL
           ld c, (hl)        ; load C with speed
           add a, c          ; add C to A
           jr nc, savex      ; if it's not overflowed, we're good to save
           ret               ; ignore event
 
moveleft:  ld a, (xpos)      ; fetch current xpos in A
           ld hl, speed      ; get speed address into HL
           ld c, (hl)        ; load C with speed
           sub c             ; subtract C from A
           jr nc, savex      ; if not underflow, we're good to save
           ret               ; ignore event
 
moveup:    ld a, (ypos)      ; get ypos into A
           ld hl, speed      ; get speed address in HL
           ld c, (hl)        ; get speed in C
           sub c             ; subtract C from A
           jr nc, savey      ; if no underflow, we're good to save
           ret               ; ignore event
 
movedown:  ld a, (ypos)      ; get ypos into A
           ld hl, speed      ; and get speed adddress in HL
           ld c, (hl)        ; get speed into C
           add a, c          ; add C to A
           cp 176            ; compare 176
           jr c, savey       ; if A <176 we can save it
           ret               ; ignore event
 
savey:     ld (ypos), a      ; save A back into ypos
           jr commoncur      ; go and save it etc
 
savex:     ld (xpos), a      ; save A back into xpos
                             ; drop thru into commoncur code
 
commoncur: ld a, 7           ; action code for cursor move = 7
           ld (action), a    ; update action code
           ret

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎