⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kbscan.asm

📁 用cy7c66113开发的HUB
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;FILE:KEYSCAN.ASM
;
;
; contains the key scanning algorithm for the keyboard scan matrix
;
;REVISION HISTORY
;
;8/13/98
; changed sense of usb connect bit in port 3
;========================================================================
;                       KEYBOARD SCAN TASK
; The keyboard is organized as 8 rows by 16 columns.  The I/O ports are
; assigned as:
; P2
; bit7               bit0
; r7 r6 r5 r4 r3 r2 r1 r0
;
; P1                             P0
; bit7       bit4           bit0 bit7                 bit0
; c15 c14 c13 c12 c11 c10 c9 c8  c7 c6 c5 c4 c3 c2 c1 c0
;
; The three LEDs are on port 3 bits[2:0]
; 
; The keycode table is organized as 16 columns with 8 rows per column.
;       ksc_x_index selects a column from 0 to 15
;       ksc_y_index selects a row from 0 to 7
;========================================================================
;------------------------------------------------------------------------
; ram variables to support the scan matrix
;------------------------------------------------------------------------
LEFT_CTRL_BIT:		equ		01h
LEFT_SHIFT_BIT:		equ		02h
LEFT_ALT_BIT:		equ		04h
LEFT_GUI_BIT:		equ		08h
RIGHT_CTRL_BIT:		equ		10h
RIGHT_SHIFT_BIT:	equ		20h
RIGHT_ALT_BIT:		equ		40h
RIGHT_GUI_BIT:		equ		80h
APP_BIT:			equ		01h


;========================================================================
; FUNCTION: ksc_putkey
;
; calls either ps2key_putkey or usbkey_putkey, depending upon the
; keyboard currently being supported.
;
;
;========================================================================

ksc_putkey:
    push    A
    mov     A,[dual_ifc_keyboard]
    cmp     A,PS2_KEYBOARD
    pop     A
    jnz     .usb
IFDEF   PS2KB
    call ps2key_putkey
ENDIF    
    ret
.usb:
IFDEF   USBKB
    call   usbkey_putkey
ENDIF    
    ret

;========================================================================
; FUNCTION: ksc_scan_keys
;
; Keyboard scanning function
;
;========================================================================
ksc_scan_keys:
	call ksc_Debounce_Task          ; task for debounce
	
	mov A, 0                        ; init. phantom key flag to false
	mov [ksc_phantom], A
	mov [ksc_col_hits],A
	mov [ksc_row_hits],A
    mov [ksc_changes],A                
    mov [ksc_key_count],A
    mov [ksc_x_index],A

	mov X, 16                       ; 16 columns
	call ksc_StartScan              ; write initial scan pattern

.column_loop:
;   DELAY 1                         ; 16 x 10us + alpha = less 200us
	iord  PORT2_DATA_REG            ; read input port
	cpl                             ; complement so down switch is '1'
	mov [ksc_mod1], A               ; save the port 2 data
;   DELAY 25	
;	iord  PORT2_DATA_REG            ; read input port
;	cpl                     
;	and [ksc_mod1], A               ; and with last look
	jz	.nokeysdown                 ; if keys are down in this column

    push A
    call    count_ones
    add A,[ksc_key_count]
    mov [ksc_key_count],A
    pop A
    or  [ksc_row_hits],A            ; keep track of all rows with keys down
    inc [ksc_col_hits]              ; and count of columns with a key down
    
.nokeysdown:
    mov A,[ksc_changes]             ; if key changes have already been found in a previous column
    cmp A, 0
    jnz .key_has_changed            ; skip to next column to scan for phantom situation
                                    ; else
	mov A, [X + ksc_image - 1]      ; get previous column data
	xor A, [ksc_mod1]               ; compare with current data
	mov [ksc_changes], A            ; and save positions where a change occurred
	cmp A, 0                        ; test for state change
	jnz .key_has_changed            ; if no key state change
    inc  [ksc_x_index]              ; increment the column
.key_has_changed:
	call ksc_NextPattern            ; write a new scan pattern
	dec X                           ; done yet?
	jnz .column_loop
.exit:

    mov A,[ksc_changes]                
    cmp A,0                         ; did we find any key changes from the above scan?
    jz  .exit1                      ; no, exit now
                                    ; yes, was there a phantom situation during the above scan?
    call check_phantom              ;
    jc  .exit1                      ; yes, exit now, because we cannot determine state of keys
    mov  A,[ksc_x_index]            ; else restore X to the proper column index
    cpl A                           ; based on the value of ksc_x_index
    add A,17
    mov X,A
    call ksc_KeyChanged             ; then call ksc_KeyChanged to record key changes
    CLEARC
.exit1:
	ret

;========================================================================
; initialize all columns to begin a keyboard scan
;       0 in c[0]
;       1 in c[19:1]
;       clear ksc_x_index to indicate column under test
;========================================================================
ksc_StartScan:
	push A                          ; save accumulator on stack
	mov A, feh                      ; write a zero to c[0]
	iowr  PORT0_DATA_REG            ; write ones to c[7:1]
	mov [ksc_p0out], A              ; remember what was written to Port0

	mov A, ffh                      ; write all ones to c[15:8]
	iowr  PORT1_DATA_REG     
	mov [ksc_p1out], A              ; remember what was written to Port1

	pop A                           ; restore accumulator from stack
	ret                             ; return

;========================================================================
; Write the columns with the next pattern in the keyboard scan.
; the pattern is generated as a left shift to walk the columns from zero
; to 15.  Increment the ksc_x_index to track the column under test.
;
;Port 1                         Port 0
;bit7                     bit0  bit7               bit0
;c15 c14 c13 c12 c11 c10 c9 c8  c7 c6 c5 c4 c3 c2 c1 c0
;========================================================================
ksc_NextPattern:
	push A                          ; save accumulator on stack
	mov A, 0ffh                     ; set the carry bit
	rlc     
	mov A, [ksc_p0out]              ; load c[7:0] data
	rlc                             ; rotate left 
	mov [ksc_p0out], A              ; update memory copy
	iowr  PORT0_DATA_REG
	mov A, [ksc_p1out]              ; load c[15:8] data
	rlc                             ; rotate left 
	mov [ksc_p1out], A              ; update memory copy
	iowr PORT1_DATA_REG 

	pop A                           ; restore accumulator from stack
	ret                             ; ret

;========================================================================
; A column had one or more buttons that changed state (up to down or down
; to up).  Scan the rows to find out which one(s) changed.
;========================================================================
ksc_KeyChanged:
	push A
	mov A, 0                        ; zero the row index
	mov [ksc_y_index], A

	mov A, 1                        ; initialize the scan bit
.row_loop:
	push A                          ; save scan bit on stack
	and A, [ksc_changes]            ; test bit position in row data
	jz .next_row                    ; keep testing
	call ksc_FoundKey               ; found a key
.next_row:
	inc [ksc_y_index]               ; increment row
	pop A                           ; restore scan bit
	rlc                             ; rotate left to test next bit
	jnc .row_loop

.done_KeyChanged:
	pop A
	ret                             ; return




;========================================================================
; We have a key identified by an ksc_x_index and a ksc_y_index.  Combine the two
; into a single index and lookup the keycode, check if up or down change.
;========================================================================
ksc_FoundKey:
	push A                              ; save accumulator on stack
	mov A, X
	mov [ksc_matrix_addr], A            ; save current scanned column
	pop A                               ; restore and save accumulator on stack
	push A

	and A, [X + ksc_image - 1]          ; see whether up or down change
	mov A, 00h                          ; key went up
	jnz .calc_matrix             
	mov	A,[dual_ifc_keyboard]			; key went down
	cmp	A,USB_KEYBOARD                  ; if we are a usb interface
	jz	.process                        ; process the key make
	TSTBIT PS2_SCAN_KBD,ps2_flags		; otherwize,if the keyboard is disabled (ps2)
	jz		.done_FoundKey      		; discard all key makes
.process:
	mov A, FFh                          ; process this key make
.calc_matrix:                             ; calculate offset into code matrix
	mov [ksc_down_up], A                ; set flag
	mov X, [ksc_matrix_addr]            ; restore current column
	mov A, [ksc_x_index]                ; load column number
	asl                                 ; multiply by eight
	asl
	asl
	add A, [ksc_y_index]                ; add in the row index
	mov [ksc_matrix_addr], A            ; store

; test debounce array for match
	call ksc_Debounce_test              ; check if debounce in progress on this key
	jnz .update_image			        ; if it is (zero flag set)
	jmp .done_FoundKey                  ; exit, do not process this key

.update_image:                                      
	pop A                               ; retrieve and save test bit
	push A
	xor [X + ksc_image - 1], A          ; update key switch ksc_image
	
	call ksc_Debounce_keys              ; push matrix address on debounce array

.get_code:
 ;   mov A,[ksc_down_up]                 ; all key breaks pass through unmolested
 ;   cmp A,0h
 ;   jnz   .make
 ;   dec    [ksc_key_count]
 ;   jmp     .put
;.make:
;   inc     [ksc_key_count]
.put:
;	mov A, [dual_ifc_option]			; depending on option 
;	and A, KEYBOARD_ID_BIT
	mov A, [ksc_matrix_addr]
;	jz  .kbd1	
;	index  at101_tbl1					; get matrix info for kbd type 2
;	jmp		.continue
;.kbd1:
	index  at101_tbl					; or kbd type 1
.continue:
	call  ksc_putkey



.done_FoundKey:
	pop A                               ; restore accumulator
	ret                                 ; return

;quick and dirty routine to count the number of 1's in a byte.
;the table below gives the number of 1's in the nibble from 0 - 15
XPAGEOFF

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -