-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWECOM.PAS
313 lines (271 loc) · 5.61 KB
/
WECOM.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
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
UNIT WECom;
{$I WEGLOBAL.PAS}
{ -- Asychronous communications routines for WWIVEdit 2.4
-- }
INTERFACE
PROCEDURE AsyncTransmit(ch1:char);
FUNCTION AsyncRecieve:char;
PROCEDURE AsyncFlushComm;
FUNCTION AsyncCharWaiting:BOOLEAN;
FUNCTION AsyncCDetect:boolean;
PROCEDURE AsyncInit(port:byte);
PROCEDURE AsyncShutdown;
IMPLEMENTATION
USES DOS
{$IFDEF USE_EMS}
,WEMemory
{$ENDIF};
CONST
BufferSize = 1024;
NoParity=0;
OddParity=8;
EvenParity=24;
OneStop=0;
TwoStop=4;
SevenBits=2;
EightBits=3;
Space=64;
Break=0;
theParity = EightBits OR OneStop OR NoParity OR Break;
TYPE
QPtr = ^Queue;
Queue = RECORD
Empty : boolean;
Front,Rear : Word;
Info : ARRAY[0..BufferSize-1] OF Byte;
END;
VAR
thePort : word;
Status : word;
busy:boolean;
OldOutput : text;
inp,outp: Qptr;
CONST
Initialized : boolean = FALSE;
CONST
ComInterrupt : ARRAY[1..2] OF BYTE = (12,11);
P8259_0=$20; { Int control register }
P8259_1=$21; { Int mask register }
EndInt =$20; { non-specific EOI }
RDAINT = 1;
THREINT= 2;
RLSINT = 4;
MSINT = 8;
MCRALL = 11; { Modem Control Register, DTR,RTS,Out 2}
MCROFF = 0; { Modem Control Register, turn all off }
IERALL = RDAINT+THREINT+RLSINT+MSINT;
IEROFF = 0;
THREOFF= $FD;
MDMSTATUS = 0;
TXREGEMPTY = 2;
RXDATAREADY = 4;
RLINESTATUS = 6;
VAR
comport:word;
IER, { Interrupt enable register }
IIR, { Interrupt identification }
LCR, { Line control register }
MCR, { Modem control register }
LSR, { Line status register }
MSR : word; { Modem status register }
IDM, { Interrupt disable mask }
IEM : byte; { Interrupt enable mask }
OldComInt : POINTER; { A pointer to the old com interrupt }
IntMask : byte;
ModemStat,LineStat : byte;
PROCEDURE DisableInt; INLINE($FA);
PROCEDURE EnableInt; INLINE($FB);
FUNCTION DeQue(VAR Q:Queue):byte;
BEGIN
WITH Q DO
BEGIN
DeQue:=Info[front];
front:=(front+1) MOD BufferSize;
Empty:=Front=Rear;
END;
END;
PROCEDURE EnQue(VAR Q:Queue; c:byte);
BEGIN
WITH Q DO
BEGIN
Empty:=false;
Info[rear]:=c;
rear:=(rear+1) MOD BufferSize;
END;
END;
FUNCTION QueueEmpty(VAR Q:Queue):boolean;
BEGIN
WITH Q DO
QueueEmpty:=Q.Empty;
END;
PROCEDURE InitQue(VAR Q:Qptr);
BEGIN
new(Q);
Q^.Empty:=TRUE;
Q^.Front:=0;
Q^.Rear:=0
END;
PROCEDURE NewComInt;
INTERRUPT;
VAR
IntID,IERVal : byte;
c:byte;
BEGIN
EnableInt;
WHILE TRUE DO
BEGIN
IntID:=port[IIR] AND 7;
IF (IntID AND 1) = 1 THEN
BEGIN
port[P8259_0]:=EndInt;
exit;
END;
CASE IntID OF
MdmStatus : ModemStat:=port[MSR];
TXREGEmpty : BEGIN
IF NOT Outp^.Empty THEN
Port[ComPort]:=Deque(Outp^)
ELSE
Port[IER]:=Port[IER] AND THREOFF;
END;
RXDataReady : BEGIN
c:=port[comport];
Enque(inp^,c);
END;
RLineStatus : LineStat:=port[LSR];
END;
END;
END;
PROCEDURE SetPort(aport:byte);
VAR
r:registers;
BEGIN
thePort:=aport-1;
comport:=memw[$40:theport*2];
IER:=comport+1;
IIR:=comport+2;
LCR:=comport+3;
MCR:=comport+4;
LSR:=comport+5;
MSR:=comport+6;
IDM:=(32 shr aport);
IEM:=IDM xor $FF;
InitQue(Inp);
InitQue(Outp);
GetIntVec(ComInterrupt[aPort],OldComInt);
SetIntVec(ComInterrupt[aPort],@NewComInt);
port[MCR]:=MCRALL; { Set up modem control register }
port[IER]:=IERALL; { Enable all interrups on serial card }
IntMask:=port[P8259_1] AND IEM; { Read 8259A's interrupt mask and write}
port[P8259_1]:=intmask; { it back after ANDing it with IEM }
END;
PROCEDURE CheckStatus;
BEGIN
IF Initialized THEN
ModemStat:=port[MSR];
END;
FUNCTION AsyncCDetect:boolean;
VAR
i: integer;
carrier : boolean;
BEGIN
IF Initialized THEN
BEGIN
AsyncCDetect:=TRUE;
IF theport=$00ff THEN Exit;
FOR i:=1 TO 100 DO
BEGIN
CheckStatus;
IF ModemStat AND 128 > 0 THEN exit;
END;
AsyncCDetect:=FALSE;
END
ELSE AsyncCdetect:=TRUE;
END;
PROCEDURE AsyncTransmit(ch1:char);
BEGIN
IF Initialized THEN BEGIN
Enque(outp^,ord(ch1));
PORT[IER]:=PORT[IER] OR THREINT;
END;
END;
FUNCTION AsyncRecieve:char;
BEGIN
IF Initialized THEN
AsyncRecieve:=chr(Deque(inp^));
END;
FUNCTION AsyncCharWaiting:BOOLEAN;
VAR
waiting:boolean;
BEGIN
IF Initialized THEN
AsyncCharWaiting:=NOT QueueEmpty(inp^)
ELSE AsyncCharWaiting:=FALSE;
END;
PROCEDURE AsyncFlushComm;
VAR
r:registers;
BEGIN
IF Initialized THEN
WHILE NOT QueueEmpty(outp^) DO ;
END;
{$F+}
FUNCTION DoNothing(VAR f:TextRec):integer;
BEGIN
DoNothing :=0;
END;
FUNCTION OutCh(VAR f:TextRec):integer;
VAR
b:byte;
ch : char;
BEGIN
IF NOT AsyncCdetect THEN
BEGIN
AsyncShutdown;
{$IFDEF USE_EMS}
MemCleanup;
{$ENDIF}
halt(1);
END;
IF NOT Busy THEN
BEGIN
Busy:=TRUE;
b:=0;
WHILE f.BufPos>0 DO
BEGIN
ch:=F.BufPtr^[b];
inc(b);
dec(F.BufPos);
AsyncTransmit(ch);
write(OldOutput,ch);
END;
OutCh := 0;
Busy:=FALSE;
END;
END;
PROCEDURE AsyncInit(port:byte);
BEGIN
IF Port=0 THEN exit;
SetPort(port);
busy:=false;
TextRec(OldOutput) := TextRec(OutPut);
WITH TextRec(Output) DO
BEGIN
Mode:=fmOutput;
InOutFunc := @OutCh;
FlushFunc := @OutCh;
UserData[1]:=0;
END;
Initialized:=True;
END;
PROCEDURE AsyncShutdown;
BEGIN
IF Initialized THEN
BEGIN
initialized:=false;
DisableInt;
SetIntVec(ComInterrupt[thePort+1],OldComInt);
EnableInt;
END;
END;
END.