Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Ultim809 keyboard handling code, 1/26/11
  2. ; yes I probably could use direct addressing for almost all of this,
  3. ; but I don't want to steal the direct page register from a user program;
  4. ; I'm not sure I want to prevent users from using the DPR at this point
  5. ; but it would make several ROM routines faster
  6.  
  7. ; The scancode buffer is 16 bytes and is aligned on a 16-byte boundary
  8. ; Even though the head and tail pointers are 2 bytes, we can get away with
  9. ; manipulating only the low bytes.
  10. KBD_SAVE    .equ    0x58    ;used to save registers in the interrupt handler
  11. KBD_BITSLEFT    .equ    0x5A    ;number of bits left to read
  12. KBD_SCANCODE    .equ    0x5B    ;scancode being received
  13. KBD_HEADPTR .equ    0x5C    ;pointer to the head of the buffer
  14. KBD_HEADPTR_L   .equ    0x5D
  15. KBD_TAILPTR .equ    0x5E    ;pointer to the tail of the buffer
  16. KBD_TAILPTR_L   .equ    0x5F
  17. KBD_BUFSTART    .equ    0x60    ;start of the buffer
  18. KBD_BUFEND  .equ    0x6F    ;end of the buffer
  19. KBD_BUFMASK .equ    0b11101111
  20.  
  21.  
  22. ;;; keyboard interrupt handler (FIRQ)
  23. ;;; typical PS/2 clock is 15 kHz (66.67 us period) so this should be fine
  24. KBD_HANDLER:    tst PIA_PRA     ;clear interrupt source
  25.         dec KBD_BITSLEFT
  26.         beq kbh_done    ;parity bit, ignore
  27.         bmi kbh_stopbit ;last bit, store scancode in buffer
  28.         sta KBD_SAVE    ;save A register
  29.         lda PIA_PRA     ;read data bit
  30.         lsla
  31.         ror KBD_SCANCODE    ;shift into scancode
  32.         lda KBD_SAVE    ;restore A register
  33. kbh_done:   rti
  34. kbh_stopbit:    sta KBD_SAVE    ;save A register
  35.         lda KBD_SCANCODE    ;store scancode
  36.         sta [KBD_HEADPTR]
  37.         lda KBD_HEADPTR_L   ;advance head pointer
  38.         inca
  39.         anda    #KBD_BUFMASK    ;force wraparound
  40.         sta KBD_HEADPTR_L
  41.         lda #10     ;reset bit counter
  42.         sta KBD_BITSLEFT
  43.         lda KBD_SAVE    ;restore A register
  44.         rti
  45.  
  46.  
  47. ;;; fetch a scancode from the buffer
  48. ;;; arguments:  none
  49. ;;; returns:    scancode in B or 0 if buffer is empty
  50. ;;; destroys:   A
  51. KBD_GETCODE::   ldb #10
  52. 1$:     cmpb    KBD_BITSLEFT    ;wait until a full scancode is processed
  53.         bne 1$
  54.         ldb KBD_HEADPTR_L   ;is the buffer empty?
  55.         cmpb    KBD_TAILPTR_L   ;(i.e. head and tail are the same)
  56.         beq bufempty
  57.         ldb [KBD_TAILPTR]   ;buffer not empty, get code
  58.         lda KBD_TAILPTR_L   ;advance tail pointer
  59.         inca
  60.         anda    #KBD_BUFMASK    ;force wraparound
  61.         sta KBD_TAILPTR_L
  62.         tstb            ;set flags for return
  63. getcodedone:    rts
  64. bufempty:   clrb            ;return 0
  65.         bra getcodedone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement