Advertisement
robsoft

first draft keyboard routine

May 28th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                              ; first draft keyboard reading routine to replace some BASIC
  2.                              ;
  3.                              ; Kempston mouse and joystick not implemented yet
  4.                              ; DEBUG CODE - PRESS 0 TO RETURN TO BASIC
  5.                              ; Q,A,O,P move cursor
  6.                              ; SPACE, ENTER, 1 and 0 are detected and indicated
  7.                              ; K and J alter cursor speed
  8.                              ;
  9. org 30000                    ; everyone has to start somewhere
  10.            jp _init          ; for ease of use in BASIC, we're going to keep
  11.                              ; our variables at the start of the routine, so
  12.                              ; jump up out of the way, this is probably sub-optimal
  13.                              ;
  14.            action defb 0     ; we store our 'action' indicator
  15.            xpos defb 128, 0  ; have 2 bytes to make BC printing easy for now
  16.            ypos defb 88, 0   ; have 2 bytes to make BC printing easy for now
  17.            speed defb 1, 0   ; have 2 bytes to make BC printing easy for now
  18.                              ;
  19.                              ;
  20.                              ; screen init code
  21.                              ; ================
  22. _init :    ld a, 71          ; black paper, white ink
  23.            ld (23693), a     ; save attribs
  24.            xor a             ; a = 0
  25.            call 8859         ; clear the screen
  26.            call 3503         ; setup channel 2
  27.                              ;
  28.                              ;
  29.                              ; main code
  30.                              ; =========
  31. _mainloop: xor a             ; a = 0
  32.            ld (action), a    ; reset our action indicator
  33.            ld bc, 57342      ; keyrow for Y-P
  34.            in a,(c)          ; get value
  35.            push af           ; save it so we don't need to re-read in a moment
  36.            cp 190        ; P key pressed?
  37.            call z, moveright ; move cursor right
  38.            pop af            ; get action back
  39.            cp 189            ; O key pressed?
  40.            call z, moveleft  ; move cursor left
  41.            ld bc, 64510      ; keyrow for Q-T
  42.            in a,(c)          ; get value
  43.            cp 190            ; Q key pressed?
  44.            call z, movedown  ; move cursor down (towards origin)
  45.            ld bc, 65022      ; keyrow for A-G
  46.            in a,(c)          ; get value
  47.            cp 190            ; A key pressed?
  48.            call z, moveup    ; move cursor up
  49.                              ; ideally the following keys would be debounced
  50.                              ; or whatever, so there was no auto-repeat going on
  51.                              ; or at least there was a delay between repeats
  52.            ld bc, 32766      ; keyrow for B-SPACE
  53.            in a,(c)          ; get value
  54.            cp 190            ; SPACE key pressed?
  55.            call z, spacehit  ; update action
  56.            ld bc, 63486      ; keyrow for 1-5
  57.            in a,(c)          ; get value
  58.            cp 190            ; 1 key pressed?
  59.            call z, onehit    ; update action
  60.            ld bc, 61438      ; keyrow for 6-0
  61.            in a,(c)          ; get value
  62.            cp 190            ; 0 key pressed?
  63.            call z, zerohit   ; update action
  64.            ld bc, 49150      ; keyrow for H-ENTER
  65.            in a,(c)          ; get value
  66.            push af           ; save it
  67.            cp 190            ; ENTER key pressed?
  68.            call z, enterhit  ; update action
  69.            pop af            ; get it back
  70.            push af           ; and save it again
  71.            cp 187            ; K key pressed? (+) symbol
  72.            call z, speedup   ; speed up cursor
  73.            pop af            ; get action value again
  74.            cp 183            ; J key pressed? (-) symbol
  75.            call z, speeddown ; slow down cursor
  76.                              ;
  77.                              ; start debug code
  78.                              ; ================
  79.                              ; debugging - in final routine, we'd loop around
  80.                              ; until the action code!=0, at which point we'd RET
  81.                              ; to let BASIC handle things. But for now we're just
  82.                              ; blindy plotting, printing to the screen and looping forever
  83.                              ; until 0 key is pressed
  84.                              ;
  85. _plot      ld a, (ypos)      ; crappy hack to easily plot the pixel
  86.            ld b, a           ;
  87.            ld a, (xpos)      ;
  88.            ld c, a           ;
  89.            call 8937         ; ROM PLOT - not the entry point, assumes BC setup
  90.                              ;
  91. _print:    ld a, 22          ; now print at 0, 0
  92.            rst 16            ;
  93.            ld a, 0           ;
  94.            rst 16            ;
  95.            ld a, 0           ;
  96.            rst 16            ;
  97.            ld bc, (xpos)     ; get the xpos value (xpos in C, B is 0)
  98.            call 6683         ; print BC
  99.            ld a, 44          ; and a comma
  100.            rst 16            ;
  101.            ld bc, (ypos)     ; get the ypos value (ypos in C, B is 0)
  102.            call 6683         ; print BC
  103.            ld a, 32          ; and print 2 trailing spaces
  104.            rst 16            ;
  105.            ld a, 32          ;
  106.            rst 16            ;
  107.  
  108.            ld a, 22          ; now, print at 1, 0
  109.            rst 16            ;
  110.            ld a, 1           ;
  111.            rst 16            ;
  112.            ld a, 0           ;
  113.            rst 16            ;
  114.            ld bc, (speed)    ; get the speed value (speed in C, B is 0)
  115.            call 6683         ; print BC
  116.            ld a, 32          ; and print a trailing space
  117.            rst 16            ;
  118.  
  119.            ld a, 22          ; print at 2, 0
  120.            rst 16            ;
  121.            ld a, 2           ;
  122.            rst 16            ;
  123.            ld a, 0           ;
  124.            rst 16            ;
  125.            ld a, (action)    ; get the action value
  126.            add a, 48         ; normalise it (0='0', 1='1' etc)
  127.            rst 16            ; print it
  128.                              ;
  129.            ld a, (action)    ; get the action value
  130.            cp 6              ; if it was 6 (zero pressed)
  131.            ret z             ; then we return to basic
  132.                              ; otherwise
  133.            halt              ; wait for an interrupt
  134.            jp _mainloop      ; go back to the top
  135.                              ;
  136.                              ; end debug code
  137.                              ; ==============
  138.                              ;
  139.                              ;
  140.                              ; subroutines
  141.                              ; ===========
  142.                              ;
  143. speeddown: ld a, (speed)     ; fetch the speed
  144.            dec a             ; subtract 1
  145.            jr nz, savespeed  ; if this is still >0, we're good to save it
  146.            ret
  147.  
  148. speedup:   ld a, (speed)     ; fetch the speec
  149.            inc a             ; add 1
  150.            cp 11             ; we're good as long as its <=10
  151.            jr nz, savespeed  ; so save it
  152.            ret
  153.  
  154. savespeed: ld (speed), a     ; save A back into the speed var
  155.            ld a, 2           ; action code = 2
  156.            ld (action), a    ; save action code
  157.            ret
  158.  
  159. enterhit:  ld a, 3           ; action code = 3
  160.            ld (action), a    ; save action code
  161.            ret
  162.  
  163. spacehit:  ld a, 4           ; action code = 4
  164.            ld (action), a    ; save action code
  165.            ret
  166.  
  167. onehit:    ld a, 5           ; action code = 5
  168.            ld (action), a    ; save action code
  169.            ret
  170.  
  171. zerohit:   ld a, 6           ; action code = 6
  172.            ld (action), a    ; save action code
  173.            ret
  174.  
  175. moveright: ld a, (xpos)      ; fetch the current xpos in A
  176.            ld hl, speed      ; get speed address into HL
  177.            ld c, (hl)        ; load C with speed
  178.            add a, c          ; add C to A
  179.            jr nc, savex      ; if it's not overflowed, we're good to save
  180.            ret               ; ignore event
  181.  
  182. moveleft:  ld a, (xpos)      ; fetch current xpos in A
  183.            ld hl, speed      ; get speed address into HL
  184.            ld c, (hl)        ; load C with speed
  185.            sub c             ; subtract C from A
  186.            jr nc, savex      ; if not underflow, we're good to save
  187.            ret               ; ignore event
  188.  
  189. moveup:    ld a, (ypos)      ; get ypos into A
  190.            ld hl, speed      ; get speed address in HL
  191.            ld c, (hl)        ; get speed in C
  192.            sub c             ; subtract C from A
  193.            jr nc, savey      ; if no underflow, we're good to save
  194.            ret               ; ignore event
  195.  
  196. movedown:  ld a, (ypos)      ; get ypos into A
  197.            ld hl, speed      ; and get speed adddress in HL
  198.            ld c, (hl)        ; get speed into C
  199.            add a, c          ; add C to A
  200.            cp 176            ; compare 176
  201.            jr c, savey       ; if A <176 we can save it
  202.            ret               ; ignore event
  203.  
  204. savey:     ld (ypos), a      ; save A back into ypos
  205.            jr commoncur      ; go and save it etc
  206.  
  207. savex:     ld (xpos), a      ; save A back into xpos
  208.                              ; drop thru into commoncur code
  209.  
  210. commoncur: ld a, 7           ; action code for cursor move = 7
  211.            ld (action), a    ; update action code
  212.            ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement