-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUFFER.INC
346 lines (301 loc) · 15.8 KB
/
BUFFER.INC
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
{*****************************************************************************}
{* Modified 08/07/93 *}
{* *}
{* Include file BUFFER.INC *}
{* --- I/O buffer management routines *}
{* *}
{* These routines provide mechanisims for examining and changing the state *}
{* of the internal circular buffers used by ASYNC. Buffers can be size- *}
{* checked, tested for empty/full status and cleared. *}
{* *}
{* Procedures & functions declared in this include file: *}
{* ----------------------------------------------------- *}
{* - Procedure ClearCom(ComPort:Byte; Buffer:Char) *}
{* - Function ComBufLeft(ComPort:Byte; Buffer:Char) : Word *}
{* - Function ComBufUsed(ComPort:Byte; Buffer:Char) : Word *}
{* - Function ComBufEmpty(ComPort:Byte; Buffer:Char) : Boolean *}
{* *}
{* ! = Used internally by ASYNC; not callable from user programs *}
{* - = Delcared in INTERFACE section; may be called from user programs *}
{* *}
{* Copyright (C) 1989-1993, Rising Edge Data Serivces *}
{* *}
{*****************************************************************************}
{*****************************************************************************}
{* *}
{* Procedure ClearCom(ComPort:Byte; Buffer:Char) *}
{* -- Clear (reset) an I/O buffer *}
{* *}
{* ClearCom resets the buffer specified by (Buffer) for the port denoted by *}
{* (ComPort). (Buffer) is a single character that specifies the buffer to *}
{* be cleared and may be one of the following: *}
{* *}
{* Character | Action *}
{* ----------|-------------------------------------------------------------- *}
{* I or i | Clear input (receive) buffer *}
{* O or o | Clear output (transmit) buffer *}
{* B or b | Clear both input and output buffers *}
{* *}
{* Possible error returns (in C_Error): *}
{* 0: No error *}
{* 1: Invalid port number *}
{* 2: Port hardware not present *}
{* 3: Port not open *}
{* 5: Unrecognized character in (Buffer) *}
{* *}
{*****************************************************************************}
Procedure ClearCom(ComPort:Byte; Buffer:Char);
Var
P : Word;
X : Byte;
Begin
{Check for error}
If C_ErrorCheck(ComPort,3) Then Exit;
Buffer := UpCase(Buffer);
If Pos(Buffer,'IOB') = 0 Then
Begin
C_Error := C_BadParameter;
Exit;
End;
P := C_PortAddr[ComPort];
{Clear receive buffer, if requested}
If (Buffer = 'I') Or (Buffer = 'B') Then
Begin
IntPush; {Disable interrupts}
C_InHead[ComPort] := 0; {Clear receive buffer}
C_InTail[ComPort] := 0;
X := C_Status[ComPort] And $FD; {Flag buffer as empty}
C_Status[ComPort] := X Or $01;
IntPop; {Enable interrupts}
End;
{Clear transmit buffer, if requested}
If (Buffer = 'O') Or (Buffer = 'B') Then
Begin
IntPush; {Disable interrupts}
C_OutHead[ComPort] := 0; {Clear transmit buffer}
C_OutTail[ComPort] := 0;
X := C_Status[ComPort] And $F7; {Flag buffer as empty}
C_Status[ComPort] := X Or $04;
IntPop; {Enable interrupts}
End;
End;
{*****************************************************************************}
{* *}
{* Function ComBufLeft(ComPort:Byte; Buffer:Char) : Word *}
{* -- Return number of unused bytes in a I/O buffer *}
{* *}
{* ComBufLeft checks the buffer pointers for the buffer specified by *}
{* (ComPort) and (Buffer) and returns a value indicating the number of bytes *}
{* that are currently free. (ComPort) specifies which port's buffer should *}
{* be checked. (Buffer) controls which buffer for the specified port is *}
{* checked, according to the table below: *}
{* *}
{* Character | Action *}
{* ----------|-------------------------------------------------------------- *}
{* I or i | Return free space in input (receive) buffer *}
{* O or o | Return free space in output (transmit) buffer *}
{* *}
{* Possible error returns (in C_Error): *}
{* 0: No error *}
{* 1: Invalid port number *}
{* 2: Port hardware not present *}
{* 3: Port not open *}
{* 5: Unrecognized character in (Buffer) *}
{* *}
{*****************************************************************************}
Function ComBufLeft(ComPort:Byte; Buffer:Char) : Word;
Var
Left : Longint;
Head,Tail,Size : Word;
Flag : Byte;
Begin
{Check for error}
If C_ErrorCheck(ComPort,3) Then Exit;
Buffer := Upcase(Buffer);
If Pos(Buffer,'IO') = 0 Then
Begin
C_Error := C_BadParameter;
Exit;
End;
{Assign computation variables according to requested buffer}
If Buffer = 'I' Then
Begin
IntPush;
Head := C_InHead[ComPort];
Tail := C_InTail[ComPort];
Flag := C_Status[ComPort] And $02;
IntPop;
Size := C_InSize[ComPort];
End;
If Buffer = 'O' Then
Begin
IntPush;
Head := C_OutHead[ComPort];
Tail := C_OutTail[ComPort];
Flag := C_Status[ComPort] And $08;
IntPop;
Size := C_OutSize[ComPort];
End;
{Compute # bytes remaining}
If Flag > 0 Then
Left := 0
Else
Left := Tail - Longint(Head);
If Left < 0 Then Inc(Left,Size);
ComBufLeft := Left;
End;
{*****************************************************************************}
{* *}
{* Function ComBufUsed(ComPort:Byte; Buffer:Char) : Word *}
{* -- Returns spaced used in a I/O buffer *}
{* *}
{* ComBufUsed will return a value indicating the number of bytes that are *}
{* presently in use for the buffer specified by (ComPort) and (Buffer). *}
{* (ComPort) specifies which port's buffer should be checked. (Buffer) *}
{* selects which of the port's used buffer space is returned, according to *}
{* the table below: *}
{* *}
{* Character | Action *}
{* ----------|-------------------------------------------------------------- *}
{* I or i | Return used space in input (receive) buffer *}
{* O or o | Return used space in output (transmit) buffer *}
{* *}
{* Possible error returns (in C_Error): *}
{* 0: No error *}
{* 1: Invalid port number *}
{* 2: Port hardware not present *}
{* 3: Port not open *}
{* 5: Unrecognized character in (Buffer) *}
{* *}
{*****************************************************************************}
Function ComBufUsed(ComPort:Byte; Buffer:Char) : Word;
Var
Used : Longint;
Head,Tail,Size : Word;
Flag : Byte;
Begin
{Check for error}
If C_ErrorCheck(ComPort,3) Then Exit;
Buffer := Upcase(Buffer);
If Pos(Buffer,'IO') = 0 Then
Begin
C_Error := C_BadParameter;
Exit;
End;
{Assign computation variables according to requested buffer}
If Buffer = 'I' Then
Begin
IntPush;
Head := C_InHead[ComPort];
Tail := C_InTail[ComPort];
Flag := C_Status[ComPort] And $02;
IntPop;
Size := C_InSize[ComPort];
End;
If Buffer = 'O' Then
Begin
IntPush;
Head := C_OutHead[ComPort];
Tail := C_OutTail[ComPort];
Flag := C_Status[ComPort] And $08;
IntPop;
Size := C_OutSize[ComPort];
End;
{Compute # bytes used}
If Flag > 0 Then
Used := Size
Else
Used := Head - Longint(Tail);
If Used < 0 Then Inc(Used,Size);
ComBufUsed := Used;
End;
{*****************************************************************************}
{* *}
{* Function ComBufEmpty(ComPort:Byte; Buffer:Char) : Boolean *}
{* -- Determine if a I/O buffer is empty *}
{* *}
{* ComBufEmpty provides a quick and efficient way of determining if a I/O *}
{* buffer is empty. Unlike ComBufLeft or ComBufUsed, ComBufEmpty checks *}
{* the "buffer empty" status bit in the port's status register, resulting in *}
{* quicker execution. ComBufEmpty returns a TRUE result if the buffer *}
{* specified by (ComPort) and (Buffer) is empty. (Buffer) denotes which *}
{* one of the port's I/O buffers is checked according to the table below: *}
{* *}
{* Character | Action *}
{* ----------|-------------------------------------------------------------- *}
{* I or i | Return TRUE if the input (receive) buffer is empty *}
{* O or o | Return TRUE if the output (transmit) buffer is empty *}
{* B or b | Return TRUE if either the input OR output buffer is empty *}
{* *}
{* Possible error returns (in C_Error): *}
{* 0: No error *}
{* 1: Invalid port number *}
{* 2: Port hardware not present *}
{* 3: Port not open *}
{* 5: Unrecognized character in (Buffer) *}
{* *}
{*****************************************************************************}
Function ComBufEmpty(ComPort:Byte; Buffer:Char) : Boolean;
Var
Empty : Boolean;
Begin
{Check for error}
If C_ErrorCheck(ComPort,3) Then Exit;
Buffer := Upcase(Buffer);
If Pos(Buffer,'IOB') = 0 Then
Begin
C_Error := C_BadParameter;
Exit;
End;
{Determine if chosen buffer is empty}
Empty := False;
If (Buffer = 'I') Or (Buffer = 'B') Then
Empty := Empty Or ((C_Status[ComPort] And $01) <> 0);
If (Buffer = 'O') Or (Buffer = 'B') Then
Empty := Empty Or ((C_Status[ComPort] And $04) <> 0);
ComBufEmpty := Empty;
End;
{*****************************************************************************}
{* *}
{* Function ComBufFull(ComPort:Byte; Buffer:Char) : Boolean *}
{* -- Determine if a I/O buffer is full *}
{* *}
{* ComBufFull is the compliment of ComBufEmpty; it returns a TRUE result *}
{* if the buffer specified by (ComPort) and (Buffer) is completely full. *}
{* (ComPort) specifies the port to check and (Buffer) determines which one *}
{* of the port's I/O buffers is interrogated according to the table below: *}
{* *}
{* Character | Action *}
{* ----------|-------------------------------------------------------------- *}
{* I or i | Return TRUE if the input (receive) buffer is full *}
{* O or o | Return TRUE if the output (transmit) buffer is full *}
{* B or b | Return TRUE if either the input OR output buffer is full *}
{* *}
{* Possible error returns (in C_Error): *}
{* 0: No error *}
{* 1: Invalid port number *}
{* 2: Port hardware not present *}
{* 3: Port not open *}
{* 5: Unrecognized character in (Buffer) *}
{* *}
{*****************************************************************************}
Function ComBufFull(ComPort:Byte; Buffer:Char) : Boolean;
Var
Full : Boolean;
Begin
{Check for error}
If C_ErrorCheck(ComPort,3) Then Exit;
Buffer := Upcase(Buffer);
If Pos(Buffer,'IOB') = 0 Then
Begin
C_Error := C_BadParameter;
Exit;
End;
{Determine if chosen buffer is empty}
Full := False;
If (Buffer = 'I') Or (Buffer = 'B') Then
Full := Full Or ((C_Status[ComPort] And $02) <> 0);
If (Buffer = 'O') Or (Buffer = 'B') Then
Full := Full Or ((C_Status[ComPort] And $08) <> 0);
ComBufFull := Full;
End;