Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Ultim809 PS/2 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. ; Note: clock line connected to 6821's CA1 pin, data line connected to PA7
  8.  
  9. ; The scancode buffer is 16 bytes and is aligned on a 16-byte boundary
  10. ; Even though the head and tail pointers are 2 bytes, we can get away with
  11. ; manipulating only the low bytes.
  12. KBD_SAVE    .equ    0x58    ;used to save registers in the interrupt handler
  13. KBD_BITSLEFT    .equ    0x5A    ;number of bits left to read
  14. KBD_SCANCODE    .equ    0x5B    ;scancode being received
  15. KBD_HEADPTR .equ    0x5C    ;pointer to the head of the buffer
  16. KBD_HEADPTR_L   .equ    0x5D
  17. KBD_TAILPTR .equ    0x5E    ;pointer to the tail of the buffer
  18. KBD_TAILPTR_L   .equ    0x5F
  19. KBD_BUFSTART    .equ    0x60    ;start of the buffer
  20. KBD_BUFEND  .equ    0x6F    ;end of the buffer
  21. KBD_BUFMASK .equ    0b11101111
  22.  
  23. ;;; initializes the keyboard system
  24. ;;; arguments:  none
  25. ;;; returns:    none
  26. ;;; destroys:   A,B
  27. KBD_INIT::  lda #10     ;initialize bit counter
  28.         sta KBD_BITSLEFT
  29.         ldd #KBD_BUFSTART   ;initialize buffer pointers
  30.         std KBD_HEADPTR
  31.         std KBD_TAILPTR
  32.         ldd #KBD_HANDLER    ;install interrupt handler
  33.         std FIRQVEC
  34.         lda PIA_CRA     ;access control register
  35.         ora #0b00000101 ;enable CA1 interrupt and PR access
  36.         sta PIA_CRA
  37.         rts
  38.  
  39. ;;; keyboard interrupt handler (FIRQ)
  40. ;;; typical PS/2 clock is 15 kHz (66.67 us period) so this should be fine
  41. KBD_HANDLER:    tst PIA_PRA     ;clear interrupt source
  42.         dec KBD_BITSLEFT
  43.         beq kbh_done    ;parity bit, ignore
  44.         bmi kbh_stopbit ;last bit, store scancode in buffer
  45.         sta KBD_SAVE    ;save A register
  46.         lda PIA_PRA     ;read data bit
  47.         lsla
  48.         ror KBD_SCANCODE    ;shift into scancode
  49.         lda KBD_SAVE    ;restore A register
  50. kbh_done:   rti
  51. kbh_stopbit:    sta KBD_SAVE    ;save A register
  52.         lda KBD_SCANCODE    ;store scancode
  53.         sta [KBD_HEADPTR]
  54.         lda KBD_HEADPTR_L   ;advance head pointer
  55.         inca
  56.         anda    #KBD_BUFMASK    ;force wraparound
  57.         sta KBD_HEADPTR_L
  58.         lda #10     ;reset bit counter
  59.         sta KBD_BITSLEFT
  60.         lda KBD_SAVE    ;restore A register
  61.         rti
  62.  
  63. ;;; fetch a scancode from the buffer
  64. ;;; arguments:  none
  65. ;;; returns:    scancode in B or 0 if buffer is empty
  66. ;;; destroys:   A
  67. KBD_GETCODE::   ldb #10
  68. 1$:     cmpb    KBD_BITSLEFT    ;wait until a full scancode is processed
  69.         bne 1$
  70.         ldb KBD_HEADPTR_L   ;is the buffer empty?
  71.         cmpb    KBD_TAILPTR_L   ;(i.e. head and tail are the same)
  72.         beq bufempty
  73.         ldb [KBD_TAILPTR]   ;buffer not empty, get code
  74.         lda KBD_TAILPTR_L   ;advance tail pointer
  75.         inca
  76.         anda    #KBD_BUFMASK    ;force wraparound
  77.         sta KBD_TAILPTR_L
  78.         tstb            ;set flags for return
  79. getcodedone:    rts
  80. bufempty:   clrb            ;return 0
  81.         bra getcodedone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement