home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / debug / jmon100.lbr / JKBDST.ZZ0 / JKBDST.Z80
Text File  |  1988-02-16  |  6KB  |  296 lines

  1. ;
  2. ;Keyboard status
  3. ;provides label kbdst
  4. ;returns A=ASCII key,NZ else A=0,Z
  5. ;preserves all other registers
  6. ;
  7. ;MicroBee version with keyclick, fast repeat
  8. ;
  9. ;local equates
  10. ;
  11. kbrpdly    equ    28h
  12. kbrpper    equ    04h
  13. kbcldur    equ    01h
  14. kbclper    equ    24h
  15. ;
  16. pioa    equ    00h
  17. piob    equ    02h
  18. romrd    equ    0Bh
  19. crtstat    equ    0Ch
  20. crtaddr    equ    0Ch
  21. crtdata    equ    0Dh
  22. ;
  23. lpenh    equ    16
  24. lpenl    equ    17
  25. updh    equ    18
  26. updl    equ    19
  27. clupd    equ    31
  28. ;
  29. ;
  30. kbdst:    push    bc
  31.     push    de
  32.     push    hl
  33. ;Set ROM READ
  34.     ld    a,1
  35.     out    (romrd),a
  36. ;
  37. ;Test if there was a previous key
  38.     ld    hl,kblkey+poff
  39.     ld    a,(hl)
  40.     cp    0FFh
  41.     jr    z,kbnrpt    ;skip if no previous key
  42. ;
  43. ;Test if previous key is still down
  44.     ld    b,3Ch        ;debounce delay
  45. kbrplp:    call    poff+chkkey
  46.     jr    z,kbrpwt    ;skip if repeating
  47.     djnz    kbrplp        ;loop until sure not rept
  48.     jr    kbscmx        ;skip if not repeating
  49. ;
  50. ;Candidate for a repeating key, is it allowed to do so
  51. kbrpwt:    cp    53h        ;LOCK
  52.     jr    z,kbwkey
  53. ;
  54. ;Same key, so test vertical retrace state
  55. kbsamk:    ld    hl,kblvrb+poff
  56.     in    a,(crtstat)
  57.     and    20h
  58.     cp    (hl)
  59.     jr    z,kbwkey    ;skip if still waiting
  60. ;
  61. ;Change in vertical retrace detected
  62.     ld    (hl),a        ;store current state
  63.     ld    hl,kbrpcnt+poff
  64.     dec    (hl)        ;count down twice for each blank
  65.     jr    nz,kbwkey    ;skip if still waiting
  66. ;
  67. ;Key now repeating
  68.     ld    (hl),kbrpper    ;set up repeat period
  69.     ld    a,(poff+kblkey)    ;A=last key scan code
  70.     jr    kbdcod
  71. ;
  72. ;Test if any key is down
  73. kbnrpt:    in    a,(crtstat)
  74.     and    40h
  75.     jr    z,kbnkey    ;skip if no key
  76. ;
  77. ;Some key is down, scan matrix to find out which
  78. kbscmx:    ld    a,lpenl        ;Clear LPEN strobe
  79.     out    (crtaddr),a
  80.     in    a,(crtdata)
  81.     xor    a        ;start at matrix beginning
  82. ;
  83. ;Scan each location in keyboard matrix
  84. kbtslp:    ld    b,a        ;B=code
  85.     ld    a,updh        ;Set update reg to keyscan location
  86.     out    (crtaddr),a
  87.     ld    a,b
  88.     and    03h
  89.     out    (crtdata),a
  90.     ld    a,updl
  91.     out    (crtaddr),a
  92.     ld    a,b
  93.     and    0F0h
  94.     out    (crtdata),a
  95.     ld    a,clupd        ;Clear Update strobe
  96.     out    (crtaddr),a
  97.     out    (crtdata),a
  98. kbtsl1:    in    a,(crtstat)    ;wait until update
  99.     rlca
  100.     jr    nc,kbtsl1
  101.     in    a,(crtstat)    ;Test if LPEN strobe occurred
  102.     bit    6,a
  103.     ld    a,b        ;A=code
  104.     jr    nz,kbgotk    ;skip if got a key
  105.     add    a,10h
  106.     adc    a,0        ;carry to bit 0
  107.     cp    83h        ;first non-key location
  108.     jp    nz,kbtslp+poff    ;loop for next scan
  109. ;
  110. ;No key, reset last key flag
  111. kbnkey:    ld    (hl),0FFh    
  112. kbwkey:    xor    a        ;A=0,Z
  113. ;
  114. ;Exit keyboard status
  115. kbxt:    push    af
  116.     ld    a,lpenl
  117.     out    (crtaddr),a
  118.     in    a,(crtdata)
  119.     xor    a
  120.     out    (romrd),a
  121.     pop    af
  122.     pop    hl
  123.     pop    de
  124.     pop    bc
  125.     ret
  126. ;
  127. ;LOCK key toggles flag
  128. kblkky:    cp    (hl)
  129.     jr    z,kbwkey
  130.     ld    (hl),a
  131.     ld    hl,kblock+poff
  132.     ld    a,1
  133.     xor    (hl)
  134.     ld    (hl),a        ;keyclick, return no code
  135.     xor    a        ;return A,Z
  136.     jr    kbclk
  137. ;
  138. ;Test if special key
  139. kbgotk:    cp    53h        ;LOCK
  140.     jr    z,kblkky
  141. ;
  142. ;Calculate key scan code
  143.     call    poff+chkshft
  144.     jr    z,kbsh1
  145.     or    08h        ;set bit 3 if shift key up
  146. kbsh1:    call    poff+chkctrl
  147.     jr    z,kbct1
  148.     or    04h        ;set bit 2 if ctrl key up
  149. kbct1:
  150. ;
  151. ;Store last key scan code
  152.     ld    (hl),a
  153.     ld    hl,kbrpcnt+poff
  154.     ld    (hl),kbrpdly
  155. ;
  156. ;Decode scan matrix to ASCII
  157. kbdcod:    rrca
  158.     rrca
  159.     rrca
  160.     rrca
  161.     ld    l,a        ;L=last keyscancode rotr 4
  162.     and    3Fh        ;A=stripped down code
  163.     cp    20h
  164.     jr    c,kbalfa
  165.     cp    30h
  166.     jr    c,kbnum        ;else A in 30..37, GREY key
  167. ;
  168. ;Use lookup table for Grey keys
  169. kbgrey:    ld    c,l        ;C=flags for keys
  170.     ld    hl,kbgtb0+poff
  171.     call    poff+addhla
  172.     ld    a,(hl)
  173. ;
  174. ;Common return endpoint
  175. kbcom:    cp    0FFh        ;cause NZ to indicate key OK
  176. ;
  177. ;Click speaker, return AF
  178. kbclk:    push    af
  179. kbclp0:    in    a,(crtstat)
  180.     and    20h
  181.     jr    z,kbclp0
  182.     ld    h,kbcldur*2
  183. kbclp1:    in    a,(piob)
  184.     xor    40h
  185.     out    (piob),a
  186.     ld    a,kbclper/2
  187. kbclp2:    dec    a
  188.     jr    nz,kbclp2
  189.     dec    h
  190.     jr    nz,kbclp1
  191.     pop    af
  192.     jr    kbxt
  193. ;
  194. ;Handle alphabetic keys
  195. kbalfa:    bit    6,l        ;test if ctrl was pushed
  196.     jr    z,kbcom        ;return ctrl codes if yes
  197.     add    a,60h        ;lower case chars
  198. ;if non alphabetic keys, skip
  199.     cp    'a'
  200.     jr    c,kbnalf
  201.     cp    'z'+1
  202.     jr    nc,kbnalf
  203. ;handle caps lock on alphabetic keys
  204.     ld    h,a
  205.     ld    a,(poff+kblock)
  206.     or    a
  207.     ld    a,h
  208.     jr    z,kbshky    ;skip if no lock
  209. kbswcs:    xor    20h        ;switch case
  210. ;if shift key pressed, switch case
  211. kbshky:    bit    7,l        ;test if shift was pushed
  212.     jr    nz,kbcom    ;exit if no shift
  213.     xor    20h        ;switch case
  214.     jr    kbcom
  215. ;
  216. ;Non alphabetic keys
  217. kbnalf:    cp    7Fh        ;DEL key
  218.     jr    z,kbshky    ;as for normal alpha keys
  219.     jr    kbswcs        ;switch case for [\] keys
  220. ;
  221. ;Handle numeral keys
  222. kbnum:    cp    2Ch        ;don't switch case for ,./ keys
  223.     jr    nc,kbpnc    ;skip for punctuation keys
  224.     xor    10h        ;switch case for numerals
  225. kbpnc:    bit    7,l        ;test if shift was pushed
  226.     jr    nz,kbcom    ;skip if not pushed
  227.     xor    10h        ;switch case if shift pushed
  228.     jr    kbcom
  229. ;
  230. ;Lookup table for grey keys
  231. kbgtb0    equ    $-30h
  232.     db    1Bh        ;ESC
  233.     db    08h        ;BS
  234.     db    09h        ;HT
  235.     db    0Ah        ;LF
  236.     db    0Dh        ;CR
  237.     db    00h        ;LOCK
  238.     db    18h        ;CAN
  239.     db    20h        ;SP
  240. ;
  241. ;Check shift key, return Z if pressed
  242. chkshft:push    hl
  243.     ld    h,0F3h
  244.     jr    chkcom
  245. ;
  246. ;Check ctrl key, return Z if pressed
  247. chkctrl:push    hl
  248.     ld    h,93h
  249.     jr    chkcom
  250. ;
  251. ;Check keyscan code A, returns Z if keypressed
  252. chkkey:    push    hl
  253.     ld    h,a
  254. ;Common code
  255. chkcom:    ld    l,a        ;save A
  256. ;Set update reg to keyscan location
  257.     ld    a,updh
  258.     out    (crtaddr),a
  259.     ld    a,h
  260.     and    03h
  261.     out    (crtdata),a
  262.     ld    a,updl
  263.     out    (crtaddr),a
  264.     ld    a,h
  265.     and    0F0h
  266.     out    (crtdata),a
  267. ;Clear lpen flag
  268.     ld    a,lpenl
  269.     out    (crtaddr),a
  270.     in    a,(crtdata)
  271. ;Clear update flag
  272.     ld    a,clupd
  273.     out    (crtaddr),a
  274.     out    (crtdata),a
  275. ;Wait until update
  276. ckwupd:    in    a,(crtstat)
  277.     rlca
  278.     jr    nc,ckwupd
  279. ;Return NZ if no key
  280.     in    a,(crtstat)
  281.     cpl
  282.     bit    6,a
  283. ;Restore A,HL
  284.     ld    a,l
  285.     pop    hl
  286.     ret
  287. ;
  288. ;Variable space
  289. kblkey:    db    0FFh        ;last key scan code, 0FFh if no key
  290.                 ;bit 2=!ctrl key, bit 3=!shift key
  291. kblock:    db    0        ;0=no lock,1=caps
  292. kblvrb:    db    0        ;last vertical retrace blanking
  293. kbrpcnt:db    1        ;repeat down counter
  294. ;
  295. ;end of include file
  296.