The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: Farabi on October 16, 2011, 09:24:38 AM

Title: Keyboard Driver I/O Port Function
Post by: Farabi on October 16, 2011, 09:24:38 AM


WAIT_OUTPUT equ 0
WAIT_WRITE equ 1
WAIT_COMMAND equ 2
WAIT_READ equ 3
WAIT_READ60 equ 4

;Mouse


PortWait proc a_type:dword

;A read from port 64H gives the following status byte:
;
;   Bit     Function
;    7      1 = Parity error
;    6      1 = General Time Out
;    5      1 = Auxiliary output buffer full
;    4      1 = Inhibit switch
;    3      1 = Command/data
;    2      1 = System flag
;    1      1 = Input buffer full
;    0      1 = Output buffer full
cli
.if a_type==WAIT_READ
wait_type_0:
in al,64h
bt ax,6
jc error
bt ax,7
jc error
bt ax,WAIT_READ ; if bit 3 set data is available at 64h
jnc wait_type_0
xor eax,eax
.elseif a_type==WAIT_WRITE
wait_type_1: ; Bit 1 should be zero before we able to send command
in al,64h
bt ax,6
jc error
bt ax,7
jc error
bt ax,WAIT_WRITE
jc wait_type_1
xor eax,eax
.elseif a_type==WAIT_COMMAND
wait_type_2: ; Bit 0 and 1 should be zero before we able to send command
in al,64h
bt ax,6
jc error
bt ax,7
jc error
bt ax,WAIT_COMMAND
jc wait_type_2
xor eax,eax
.elseif a_type==WAIT_READ60
wait_type_4:
in al,64h
bt ax,6
jc error
bt ax,7
jc error
bt ax,WAIT_READ ; if bit 3 not set data is available at 60h
jc wait_type_4
xor eax,eax
.elseif a_type==WAIT_OUTPUT
wait_type_5:
in al,64h
bt ax,6
jc error
bt ax,7
jc error
bt ax,WAIT_OUTPUT ; if bit 3 set data is available at 64h
jnc wait_type_5
xor eax,eax
.endif

error:
sti
ret
PortWait endp

PortRead proc

invoke PortWait,WAIT_READ
in al,60h

ret
PortRead endp

PortWrite proc a_cmd:byte

invoke PortWait,WAIT_WRITE
mov al,a_cmd
out 64h,al

ret
PortWrite endp

KeyBoardRead proc
LOCAL key:byte

invoke PortWrite,0AEh
invoke PortWrite,0D2h
in al,60h
; invoke PortRead
; mov key,al
; invoke PortWrite,0AEh
; mov al,key
; push eax
; push eax
; invoke dw2bin,eax,base_buffer
; invoke Print,base_buffer
; pop eax
; invoke dw2hex,eax,base_buffer
; invoke Print,base_buffer
; invoke PrintRel,addr crg
; pop eax
; invoke PortWrite,0A7h
ret
KeyBoardRead endp


Or if you want a simple thing, have a look at memory address
0040:0017    Keyboard flag bits (byte) 7=ins, 6=caps, 5=num, 4=scrll,
         3=ALT, 2=CTRL, 1=LSHFT, 0=RSHFT (toggle states)
0040:0018    Keyboard flag bits (byte) (depressed states)
0040:0019    Keyboard ALT-Numeric pad number buffer area
0040:001A    Pointer to head of keyboard queue
0040:001C    Pointer to tail of keyboard queue
0040:001E    15 key queue (head=tail, queue empty)

In case my code did not work, just access that address and check everything, it work faster and better. I accessed it from a linier address 0x417h.
Title: Re: Keyboard Driver I/O Port Function
Post by: Tedd on October 16, 2011, 01:34:12 PM
That only works as long as the BIOS keyboard IRQ handler is still running.
Which is also why polling the keyboard port didn't work for you - the handler was already taking the data.
Title: Re: Keyboard Driver I/O Port Function
Post by: dedndave on October 16, 2011, 06:30:59 PM
yah - intercepting the keyboard hardware directly is a nightmare you don't want to get into   :P
Title: Re: Keyboard Driver I/O Port Function
Post by: Farabi on October 16, 2011, 06:42:13 PM
The keyboard is worked, but I needed to poll. Now I got a problem, if my move my mouse too fast my system rebooting. And I had no clue why was that, I guess too musch packet on the queue.
Title: Re: Keyboard Driver I/O Port Function
Post by: Farabi on October 16, 2011, 07:35:42 PM
I dont know about you but I enjoyed programming, just like playing game, you search for the clue so you acomplish things. That is why I can up in front of my computer for hours. Sounds stupid for normal guys, but what can I said, this is my hobby.  :toothy Just like you watching soccer or baseball.
Title: Re: Keyboard Driver I/O Port Function
Post by: Farabi on October 16, 2011, 07:42:07 PM
Here is the complete Mouse and Keyboard driver. Tested and worked on my computers. Fixed the reset issue, buffer full and it reseting, weird.
No illegal thing here, I created it using JWASM and did not even use MASM include files.