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

📄 main_asm.txt

📁 PS/2 设备接口用于许多现代的鼠标和键盘它是由IBM 开发并且最初出现在IBM 技术参考手册里但 是当我知道的时候这篇文件就已经很多年没有印刷了因此关于这个内容现在没有官方的出版物我 无法访问I
💻 TXT
📖 第 1 页 / 共 2 页
字号:
	brsh	pta_no_ck_check
	rcall	read_pc_ck
	breq	pta_Abort
	cpi	bitcnt, 11		; Redo the compare since read_pc_ck destroyed SREG
	brlo	pta_Clock_cycle	; Less than 11 clock cycles done, do one more
	pta_no_ck_check:
	rcall	read_pc_data
	breq	pta_Clock_cycle	; If pc holds data low, keep sending clock pulses

	sbr	Status, 1<<VALID	; There's new data in RxData
	
	rjmp	pta_Done		; Return with successfully received byte in RxByte

	pta_Timeout:
	;debug
;	mov	temp,ConnectedPCs
;	rcall	uartsend
;	mov	temp,LoopPC
;	rcall	uartsend
	
	sbr	temp,(1<<TOV1)		;clear tim1ovfl-flag
	out	TIFR,temp

	com	LoopPC
	and	ConnectedPCs,LoopPC	;clear bit / disable pc
	com	LoopPC

	;debug
;	mov	temp,ConnectedPCs
;	rcall	uartsend
	
	
	pta_Abort:
	rcall	release_pc_data	; Make sure we don't hold data low
	sbr	Status, 1<<ERROR	; Set error bit

	
	pta_Done:
	m_release_keyb_ck		; Allow the keyboard to talk again
ret

;#############################################################
;#              keyb_to_avr                                  #
;#              Recieves one byte from keyboard to RxByte    #
;#############################################################
;sub is not complete? works, but could use better comment and use timeout?
keyb_to_avr:
	cbr	Status, (1<<VALID)|(1<<ERROR)	; Reset valid and error bits
	sbic	PINB,KBD_DATA			;if keyboard sends data
	rjmp	kta_Receive_Done

	ldi	bitcnt,10
	;nu 䲠data l姠och ck troligtivs h?	
	;modda in s堤en droppar om data bli h?nder tiden
	;v䮴a p堣k l姬 om data blir h?nder tiden s堤rop
	kta_loop1:
	;sbic	PINB,KBD_DATA		; if data somehow is released
	;rjmp	kta_Receive_Done		; then exit sub
	sbic	PINB,KBD_CK
	rjmp	kta_loop1
	

	;nu 䲠data l姠och ck l姠(mitt i startbit)
	
	;1: k?n v䮴a-p季k-h?k-l姭loop
	kta_loop2:
	rcall	delay5us
	sbis	PINB,KBD_CK
	rjmp	kta_loop2
	
	kta_loop3:
	rcall	delay5us
	sbic	PINB,KBD_CK
	rjmp	kta_loop3
	
	cpi	bitcnt,3			;if paritybit or stopbit; skip to read in bit
	brlo	kta_noreadin
	
	;2: shifta in data
	lsr	RxByte			;shift in a 0 (lsb first, shift right)
	sbic	PINB,KBD_DATA		;
	sbr	RxByte,0b10000000		;set first bit 1 if KBD_DATA is high
	
	
	;3: g場ill 1, k?姲a g宧er
	kta_noreadin:
	dec	bitcnt
	brne	kta_loop2
	
	kta_loop4:		; Wait until bus idle
	;sbic	PINB,KBD_DATA
	;sbis	PINB,KBD_CK
	;rjmp	kta_loop4
	sbis	PINB,KBD_DATA
	rjmp	kta_loop4

	sbr	Status, 1<<VALID		; There's new data in RxData
	kta_Receive_Done:
ret

;#############################################################
;#              avr_to_keyb                                  #
;#              Transmits the byte in TxByte to the keyboard #
;#############################################################
;sub is not complete, should check if pc starts send data perhaps
avr_to_keyb:
	;mov	TxByte,temp	;(kanske fr宠sram senare)
	
	;INIT DATA SEND FROM HOST
	
	m_clear_keyb_ck		; Bring the clock line low
	rcall	delay100us
	m_clear_keyb_data		; Bring the data line low
	rcall	delay100us
	m_release_keyb_ck		; Release the clock line
	
	rcall	delay5us
	;SEND DATA
	
	ldi	bitcnt,10
	clr	parity_cnt
	;rjmp	atk_loop2		;wait for keyboard to pull clock low
	;ovanst奮de b?nte beh?, clock 䲠p嶤g till h?堬oop1 kommer bara passeras
	
	atk_loop1:
	rcall	delay5us
	sbis	PINB,KBD_CK		;wait for high
	rjmp	atk_loop1
	atk_loop2:
	rcall	delay5us
	sbic	PINB,KBD_CK		;wait for low
	rjmp	atk_loop2
	
	;debug
	;rcall	uartsend
	
	dec 	bitcnt
	cpi	bitcnt,1
	breq	atk_sendparity
	cpi	bitcnt,0
	breq 	atk_makeinput
	
	atk_sendbit:
	;now put first bit on dataline (lsb first)
	lsr	TxByte			;shift out bit to carry
	brcs	atk_Putchar1
	;sbi	DDRB,KBD_DATA		;kan lika g䲮a jobba med PORTB v䬿 (inte om keyb jordar samtidigt)
	m_clear_keyb_data
	rjmp	atk_Putchar2
	atk_Putchar1:
	;cbi	DDRB,KBD_DATA
	m_release_keyb_data
	inc	parity_cnt
	atk_Putchar2:
	rjmp	atk_loop1
	
	atk_sendparity:
	;last bit in parity_cnt is zero if even number of ones in TxByte
	;should send a one if even number of ones in TxByte
	;=> invert parity_cnt
	com	parity_cnt		
	mov	TxByte,parity_cnt	;send the last bit in parity_cnt
	
	rjmp	atk_sendbit
	
	atk_makeinput:
	;cbi	DDRB,KBD_DATA
	m_release_keyb_data
	
	atk_loop3:
	sbic	PINB,KBD_DATA		;wait for low
	rjmp	atk_loop3
	atk_loop4:
	sbic	PINB,KBD_CK			;wait for low
	rjmp	atk_loop4

;jahopp, med detta s堶erkar buggen vara "fixad"
rcall	Delay_halfbit
rcall	Delay_halfbit

	atk_loop5:
	sbis	PINB,KBD_CK			;wait for high
	rjmp	atk_loop5
	
	atk_loop6:
	sbis	PINB,KBD_DATA		;wait for high
	rjmp	atk_loop6


	
;; debug (l䧧er delays ovan ist䬬et)
;; verkar funka med denna i
;; problemet 䲠att denna rutinen avslutar, troligtvis h嬬er keyb klockan l姠f?tt visa att den processar data eller n崍
;; s堫ommer v岠keyb_to_avr att g堩g宧, f?idigt... p堮姯t s䴴
;rcall	delay100us
;rcall	Delay_halfbit
;rcall	Delay_halfbit

ret

process_to_keyb:
	;debug - send received byte to uart
	;mov	temp,RxByte
	;rcall	uartsend

	;debug
	;mov	TxByte,RxByte
	;rcall	avr_to_keyb
	;rjmp	ptk_done
	
	;reset handler
	cpi	RxByte,0xFF
	brne	ptk_nextcomp1
	rcall	delay100us
	ldi	TxByte,KBD_ACK		;send ack
	rcall	avr_to_pc			;to pc
	rcall	delay100us
	;rcall	delay100us
	ldi	TxByte,KBD_BAT_SUCCESS	;send BAT success
	rcall	avr_to_pc			;to pc

	rjmp	ptk_done
	;--
	
	ptk_nextcomp1:
	;id-request handler
	cpi	RxByte,0xF2
	brne	ptk_nextcomp2
	rcall	delay100us
	ldi	TxByte,KBD_ACK		;send ack
	rcall	avr_to_pc			;to pc
	rcall	delay100us
	;rcall	delay100us
	ldi	TxByte,KBD_ID0		;send first byte in ID
	rcall	avr_to_pc			;to pc
	rcall	delay100us
	ldi	TxByte,KBD_ID1		;send second byte in ID
	rcall	avr_to_pc			;to pc
		
	rjmp	ptk_done
	;---
	
	ptk_nextcomp2:
	;resend handler
	;kanske sparar man varje senaste s䮤 non-resend-byte i sram
	;plocka sen ut korrekt h䲠och s䮤
	cpi	RxByte,0xFE
	brne	ptk_nextcomp3
	mov	TxByte,RxByte
	rcall	avr_to_keyb

	rjmp	ptk_done
	;---

	ptk_nextcomp3:
	;echo handler
	cpi	RxByte,0xEE
	brne	ptk_nextcomp4
	rcall	delay100us
	ldi	TxByte,KBD_ECHO		;send echo
	rcall	avr_to_pc		;to pc	
	
	rjmp	ptk_done
	;---
	
	
	ptk_nextcomp4:
	;the rest: just ack to pc
	rcall	delay100us
	ldi	TxByte,KBD_ACK		;send ack
	rcall	avr_to_pc		;to pc	
	
	rjmp	ptk_done
	;---	
	
	
	ptk_done:
ret


;	sbi	PORTD,MOUSE_VCC


process_to_pc:
;l䧧 in s堤en kolla break-code-flaggan direkt, 䲠den satt s堳ka ju datan forwardas?

	;debug, skip filters
	;rjmp	ptp_send
	;rjmp	ptp_clear_break_extend_send
	
	m_clear_keyb_ck				; Send ~CTS to keyboard since we can't listen to it now

	cpi	RxByte,0xF0				; if break-code
	brne	ptp_compnext1
	sbr	STATUS,(1<<BREAK)			; set the statusbit
	rjmp	ptp_send
	
	ptp_compnext1:
	cpi	RxByte,0xE0				; if extend-code
	brne	ptp_compnext2
	sbr	STATUS,(1<<EXTENDED)		; set the statusbit
	rjmp	ptp_clear_break_send
	
	ptp_compnext2:
	;--- snabbt och enkelt filter f?nsert
	cpi	RxByte,0x70
	brne	ptp_compnext3
	sbrc	STATUS,BREAK			;if last was break-code
	rjmp	ptp_clear_break_extend_send	;then send
	sbrc	STATUS,EXTENDED			;if extended
	rjmp	ptp_done				;skip sending
	;--- filter slut h䲍
	
	ptp_compnext3:
	;--- Caps lock
	cpi	RxByte,0x58
	brne	ptp_compnext4
	sbrc	STATUS,BREAK			;if last was break-code
	rjmp	ptp_caps_released		
	sbr	Status,(1<<KB_CAPS)		;set statusbit, caps is pressed
	;there is no exended for 0x58
	;sbrc	STATUS,EXTENDED			;if extended	
	ldi	TxByte,0xED
	rcall	avr_to_keyb				;send command "set leds" to keyboard
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	rcall	delay100us
	;ldi	TxByte,0x02
	cpi	CurrentPC,0b00000001
	brne	ptp_nextled1
	
	;h䲠ska rutinen modifiera ett register som h嬬er reda p堶ilka ledar som lyser
	;sen skicka ut det registret
	;or ledreg,0x02 / 0x04 / 0x01		(sbr ledreg,0x02)
	;s䮤
	;vid release:
	;and ledreg,inv(0x02) / inv(0x04) / inv(0x01)	(cbr ledreg,inv(0x02))
	
	
	ldi	TxByte,NUMLOCKLED
	rjmp	ptp_send_led			;send argument numlockled=1
	ptp_nextled1:
	cpi	CurrentPC,0b00000100
	brne	ptp_nextled2
	ldi	TxByte,CAPSLOCKLED
	rjmp	ptp_send_led			;send argument capslockled=1
	ptp_nextled2:
	cpi	CurrentPC,0b00010000
	brne	ptp_nextled3
	ldi	TxByte,SCROLLOCKLED
	rjmp	ptp_send_led			;send argument scrollockled=1
	ptp_nextled3:
	
	ptp_send_led:
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK

	rjmp	ptp_done			;skip sending
	ptp_caps_released:
	;cbi	PORTD,MOUSE_VCC			;start mouse again
	ldi	TxByte,0xED
	rcall	avr_to_keyb			;send command "set leds" to keyboard
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	rcall	delay100us
	ldi	TxByte,0x00
	rcall	avr_to_keyb			;send argument "set leds" (all leds off) to keyboard
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	
	ldi	RxByte,0x58
	cbr	Status,(1<<KB_CAPS)		;clear statusbit, caps is released
	rjmp	ptp_clear_break_extend_send	;then send	
	;--- filter slut h䲍
	
	ptp_compnext4:
	;--- button 1
	cpi	RxByte,0x16
	brne	ptp_compnext5
	sbrc	STATUS,BREAK			;if last was break-code
	rjmp	ptp_clear_break_extend_send	;then send	
	sbrs	Status,KB_CAPS			;if "function" is pressed
	rjmp	ptp_clear_break_extend_send	;
	ldi	CurrentPC,0b00000100
	;sbi	PORTD,MOUSE_VCC			;kill the mouse, will start when caps is released

	;flash correct led on keyb
	ldi	TxByte,0xED
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	rcall	delay100us
	ldi	TxByte,CAPSLOCKLED
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	
	rcall noMouse
	ldi	temp,MOUSEPORTMASK
	andi	temp,(1<<MOUSEb_E)
	breq	ptp_mouseb_notconnected
	sbi	PORTD,MOUSEb_E
	ptp_mouseb_notconnected:
	
	rjmp	ptp_done
	;--- filter slut h䲍
	
	ptp_compnext5:
	;--- button 2
	cpi	RxByte,0x1E
	brne	ptp_compnext6
	sbrc	STATUS,BREAK			;if last was break-code
	rjmp	ptp_clear_break_extend_send	;then send	
	sbrs	Status,KB_CAPS			;if "function" is pressed
	rjmp	ptp_clear_break_extend_send	;
	ldi	CurrentPC,0b00010000
	;sbi	PORTD,MOUSE_VCC			;kill the mouse, will start when caps is released
	
	;flash correct led on keyb
	ldi	TxByte,0xED
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	rcall	delay100us
	ldi	TxByte,SCROLLOCKLED
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	
	rcall noMouse
	ldi	temp,MOUSEPORTMASK
	andi	temp,(1<<MOUSEc_E)
	breq	ptp_mousec_notconnected
	sbi	PORTD,MOUSEc_E
	ptp_mousec_notconnected:
		
	rjmp	ptp_done
	;--- filter slut h䲉

	ptp_compnext6:
	;--- button ?
	cpi	RxByte,0x0E
	brne	ptp_compnext7
	sbrc	STATUS,BREAK			;if last was break-code
	rjmp	ptp_clear_break_extend_send	;then send	
	sbrs	Status,KB_CAPS			;if "function" is pressed
	rjmp	ptp_clear_break_extend_send	;
	ldi	CurrentPC,0b00000001
	;sbi	PORTD,MOUSE_VCC			;kill the mouse, will start when caps is released
		
	;flash correct led on keyb
	ldi	TxByte,0xED
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	rcall	delay100us
	ldi	TxByte,NUMLOCKLED
	rcall	avr_to_keyb
	rcall	delay100us
	rcall	Wait_keyb_byte
	;cpi	RxByte,KBD_ACK
	
	rcall noMouse
	ldi	temp,MOUSEPORTMASK
	andi	temp,(1<<MOUSEa_E)
	breq	ptp_mousea_notconnected
	sbi	PORTD,MOUSEa_E
	ptp_mousea_notconnected:
	
	rjmp	ptp_done
	;--- filter slut h䲉

	ptp_compnext7:
	
	ptp_clear_break_extend_send:
	;clear this bit when data is'nt BREAK or EXTENDED
	cbr	STATUS,(1<<EXTENDED)	; clear the statusbit
	
	ptp_clear_break_send:
	cbr	STATUS,(1<<BREAK)	; clear the break-statusbit
	
	ptp_send:
	;push	LoopPC
	mov	LoopPC,CurrentPC
	mov	TxByte,RxByte
	ptp_just_send:
	rcall	avr_to_pc
	
	sbrs	Status,ERROR
	rjmp	ptp_send_ok
	;if timer overflowed, quit routine, means that this computer is not ok (disconnected or off)
	in	temp,TIFR
	sbrc	temp,TOV1
	rjmp	ptp_Timeout
	
	rjmp	ptp_just_send
	
	ptp_send_ok:
	;kolla Status,1<<ERROR, om den 䲠satt s堫?gen eller n崬 samt 
	;h嬬 koll p場imer overflow, s堶i inte spenderar f?ycket tid h䲍
	;se ocks場ill s堡tt tangentborde inte kan skicka n姯t under hela denna
	;subben, dvs under process_to_pc
	
	
	;pop	LoopPC
	;; --- debug - send received byte to uart
	;mov	temp,ConnectedPCs
;	mov	temp,RxByte
;	rcall	uartsend
	;; ---
	
	ptp_Timeout:
	ptp_done:
	m_release_keyb_ck	; Allow the keyboard to talk again
ret


noMouse:
;sub clears all mouse_enable_pins, no mouse is selected after sub
	in	temp,PORTD
	cbr	temp,MOUSEPORTMASK
	out	PORTD,temp
ret

Wait_keyb_byte:
	rcall	keyb_to_avr
	sbrs	Status,VALID
	rjmp	Wait_keyb_byte
ret


⌨️ 快捷键说明

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