Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Ultim809 keyboard handling code, 1/26/11
- ; yes I probably could use direct addressing for almost all of this,
- ; but I don't want to steal the direct page register from a user program;
- ; I'm not sure I want to prevent users from using the DPR at this point
- ; but it would make several ROM routines faster
- ; The scancode buffer is 16 bytes and is aligned on a 16-byte boundary
- ; Even though the head and tail pointers are 2 bytes, we can get away with
- ; manipulating only the low bytes.
- KBD_SAVE .equ 0x58 ;used to save registers in the interrupt handler
- KBD_BITSLEFT .equ 0x5A ;number of bits left to read
- KBD_SCANCODE .equ 0x5B ;scancode being received
- KBD_HEADPTR .equ 0x5C ;pointer to the head of the buffer
- KBD_HEADPTR_L .equ 0x5D
- KBD_TAILPTR .equ 0x5E ;pointer to the tail of the buffer
- KBD_TAILPTR_L .equ 0x5F
- KBD_BUFSTART .equ 0x60 ;start of the buffer
- KBD_BUFEND .equ 0x6F ;end of the buffer
- KBD_BUFMASK .equ 0b11101111
- ;;; keyboard interrupt handler (FIRQ)
- ;;; typical PS/2 clock is 15 kHz (66.67 us period) so this should be fine
- KBD_HANDLER: tst PIA_PRA ;clear interrupt source
- dec KBD_BITSLEFT
- beq kbh_done ;parity bit, ignore
- bmi kbh_stopbit ;last bit, store scancode in buffer
- sta KBD_SAVE ;save A register
- lda PIA_PRA ;read data bit
- lsla
- ror KBD_SCANCODE ;shift into scancode
- lda KBD_SAVE ;restore A register
- kbh_done: rti
- kbh_stopbit: sta KBD_SAVE ;save A register
- lda KBD_SCANCODE ;store scancode
- sta [KBD_HEADPTR]
- lda KBD_HEADPTR_L ;advance head pointer
- inca
- anda #KBD_BUFMASK ;force wraparound
- sta KBD_HEADPTR_L
- lda #10 ;reset bit counter
- sta KBD_BITSLEFT
- lda KBD_SAVE ;restore A register
- rti
- ;;; fetch a scancode from the buffer
- ;;; arguments: none
- ;;; returns: scancode in B or 0 if buffer is empty
- ;;; destroys: A
- KBD_GETCODE:: ldb #10
- 1$: cmpb KBD_BITSLEFT ;wait until a full scancode is processed
- bne 1$
- ldb KBD_HEADPTR_L ;is the buffer empty?
- cmpb KBD_TAILPTR_L ;(i.e. head and tail are the same)
- beq bufempty
- ldb [KBD_TAILPTR] ;buffer not empty, get code
- lda KBD_TAILPTR_L ;advance tail pointer
- inca
- anda #KBD_BUFMASK ;force wraparound
- sta KBD_TAILPTR_L
- tstb ;set flags for return
- getcodedone: rts
- bufempty: clrb ;return 0
- bra getcodedone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement