/
K8042LIB.PAS
183 lines (154 loc) · 4.5 KB
/
K8042LIB.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2023
@website(https://www.gladir.com/CODER/K8042LIB-TP)
@abstract(Target: Turbo Pascal 7)
}
Unit K8042LIB;
INTERFACE
Const
K8042_STATUS=$64; (* (Ecriture) *)
K8042_COMMAND=$64; (* (Ecriture) *)
K8042_DATA=$60; (* (Lecture/criture) *)
(* Bits d'‚tat *)
K8042_STATUS_OUTPUT_FULL=$01;
K8042_STATUS_INPUT_FULL=$02;
K8042_STATUS_SYSTEM=$04;
K8042_STATUS_DATA_WRITE=$08;
K8042_STATUS_INHIBIT=$10;
K8042_STATUS_XMIT_TIMEOUT=$20;
K8042_STATUS_MOUSE_OUTPUT_FULL=$20;
K8042_STATUS_RECV_TIMEOUT=$40;
K8042_STATUS_PARITY_ERR=$80;
K8042_IRQ=1;
K8042_MOUSE_IRQ=12;
K8042_F_AUX=$01;
Procedure k8042_Disable_Aux;
Procedure k8042_Disable_Keyboard;
Procedure k8042_Drain_Buffer;
Procedure k8042_Enable_Aux;
Procedure k8042_Enable_Keyboard;
Function k8042_Is_Mouse_Output_Ready:Boolean;
Function k8042_Is_Output_Ready:Boolean;
Function k8042_Output_Was_Aux:Boolean;
Function k8042_Probe:Boolean;
Function k8042_Read_Command_Byte:Integer;
Function k8042_Read_Output:Byte;
Function k8042_Read_Output_Wait:Integer;
Function k8042_Wait_For_Input_Buffer:Boolean;
Function k8042_Wait_For_Output:Byte;
Function k8042_Write_Aux(C:Byte):Boolean;
Function k8042_Write_Command(C:Byte):Boolean;
Function k8042_Write_Command_Byte(C:Byte):Boolean;
Function k8042_Write_Data(C:Byte):Boolean;
IMPLEMENTATION
Var
k8042_Flags,k8042_Last_Status:Byte;
Function k8042_Is_Output_Ready:Boolean;Begin
k8042_Last_Status:=Port[K8042_STATUS];
k8042_Is_Output_Ready:=(k8042_Last_Status and K8042_STATUS_OUTPUT_FULL)=
K8042_STATUS_OUTPUT_FULL;
End;
Function k8042_Is_Mouse_Output_Ready:Boolean;Begin
k8042_Last_Status:=Port[K8042_STATUS];
k8042_Is_Mouse_Output_Ready:=(k8042_Last_Status and
(K8042_STATUS_OUTPUT_FULL or K8042_STATUS_MOUSE_OUTPUT_FULL)) =
(K8042_STATUS_OUTPUT_FULL or K8042_STATUS_MOUSE_OUTPUT_FULL);
End;
Function k8042_Output_Was_Aux:Boolean;Begin
k8042_Output_Was_Aux:=((k8042_Flags and K8042_F_AUX) <> 0) and
((k8042_Last_Status and K8042_STATUS_MOUSE_OUTPUT_FULL)<>0);
End;
Function k8042_Read_Output:Byte;Begin
k8042_Read_Output:=Port[K8042_DATA];
End;
Function k8042_Wait_For_Output:Byte;
Var
Patience:Word;
C:Byte;
Begin
Patience:=$FFFF;
Repeat
C:=Port[K8042_STATUS];
Dec(Patience);
Until Not(((C and K8042_STATUS_OUTPUT_FULL)=0)and(Patience<>0));
k8042_Last_Status:=C;
k8042_Wait_For_Output:=(C and K8042_STATUS_OUTPUT_FULL);
End;
Function k8042_Read_Output_Wait:Integer;Begin
If(k8042_Wait_For_Output or
k8042_Wait_For_Output or
k8042_Wait_For_Output<>0)Then
k8042_Read_Output_Wait:=k8042_Read_Output
Else
k8042_Read_Output_Wait:=-1;
End;
Function k8042_Wait_For_Input_Buffer:Boolean;
Var
Patience:Word;
C:Byte;
Begin
Patience:=$FFFF;
Repeat
C:=Port[K8042_STATUS];
Dec(Patience);
Until Not(((C and K8042_STATUS_INPUT_FULL<>0)and(Patience<>0)));
k8042_Last_Status:=C;
k8042_Wait_For_Input_Buffer:=(C and K8042_STATUS_INPUT_FULL)=0;
End;
Function k8042_Probe:Boolean;Begin
If(Port[K8042_STATUS]=$FF)Then k8042_Probe:=False
Else k8042_Probe:=True;
End;
Procedure k8042_Drain_Buffer;
Var
C,X:Byte;
Begin
Repeat
X:=Port[K8042_DATA];
C:=Port[K8042_STATUS];
Until Not((C and (K8042_STATUS_INPUT_FULL or K8042_STATUS_OUTPUT_FULL))<>0);
k8042_Last_Status:=C;
End;
Function k8042_Write_Command(C:Byte):Boolean;
Var
R:Boolean;
Begin
R:=k8042_Wait_For_Input_Buffer;
If(R)Then Port[K8042_COMMAND]:=C;
k8042_Write_Command:=R;
End;
Function k8042_Write_Data(C:Byte):Boolean;
Var
R:Boolean;
Begin
R:=k8042_Wait_For_Input_Buffer;
If(R)Then Port[K8042_DATA]:=C;
k8042_Write_Data:=R;
End;
Function k8042_Read_Command_Byte:Integer;Begin
If(k8042_Write_Command($20))Then k8042_Read_Command_Byte:=k8042_Read_Output_Wait
Else k8042_Read_Command_Byte:=-1;
End;
Function k8042_Write_Command_Byte(C:Byte):Boolean;Begin
If(k8042_Write_Command($60)and k8042_Write_Data(c))Then k8042_Write_Command_Byte:=True
Else k8042_Write_Command_Byte:=False;
End;
Procedure k8042_Disable_Keyboard;Begin
k8042_Write_Command($AD);
End;
Procedure k8042_Enable_Keyboard;Begin
k8042_Write_Command($AE);
End;
Procedure k8042_Disable_Aux;Begin
k8042_Write_Command($A7);
End;
Procedure k8042_Enable_Aux;Begin
k8042_Write_Command($A8);
End;
Function k8042_Write_Aux(C:Byte):Boolean;Begin
k8042_Write_Aux:=k8042_Write_Command($D4) and k8042_Write_Data(C);
End;
BEGIN
k8042_Flags:=0;
k8042_Last_Status:=0;
END.