/
ILSERIAL.C
364 lines (323 loc) · 8.23 KB
/
ILSERIAL.C
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* ****************************************** */
/* HP-IL device emulation */
/* (used in Emu41/Emu71/ILper) */
/* */
/* ilserial.c serial port module */
/* low level serial routines (BIOS calls) */
/* */
/* Version 2.5, J-F Garnier 2005-2007: */
/* ****************************************** */
static int comport;
/* HP-IL data and variables */
#define AID 65 /* Accessory ID = interface */
#define DEFADDR 4 /* default address after AAU */
static int status; /* HP-IL status */
static int adr; /* bits 0-5 = HP-IL address, bit 7 = 1 means auto address taken */
static int fetat; /* HP-IL state machine flags:
/* bit 7, bit 6:
0 0 idle
1 0 addressed listen
0 1 addressed talker
1 1 active talker
bit 1: SDA, SDI
bit 0: SST, SDI, SAI
*/
static int ptsdi; /* output pointer for device ID */
static frdy;
/* static char buf[2048]; */
static int ptrin, ptrout;
/* ****************************************** */
/* init_ilserial(d, config) */
/* */
/* init a virtual HPIL serial device */
/* config port p */
/* use BIOS calls (INT 14) */
/* ****************************************** */
static void init_ilserial(int d, int config)
{
status=0;
adr=0;
fetat=0;
ptsdi=0;
comport=d;
if (config!=0) {
_DX = comport;
_AL = config;
_AH = 0;
geninterrupt(0x14);
}
ptrin=0;
ptrout=0;
}
/* ****************************************** */
/* send_car(car) */
/* */
/* send a character to serial port */
/* use BIOS calls (INT 14) */
/* ****************************************** */
static int send_car(char car)
{
_DX = comport;
_AH = 1;
_AL = car;
geninterrupt(0x14);
fserial=1;
return(_AH);
}
/* ****************************************** */
/* read_car() */
/* */
/* read a character from serial port */
/* use BIOS calls (INT 14) */
/* ****************************************** */
static int read_car(void)
{
unsigned int n;
_DX = comport;
_AH = 2;
geninterrupt(0x14);
n=_AX;
if ((n&0x8000)==0) {
/* if (n&0x100) { */
return(n&255);
}
else {
fserial=1;
return(-1);
}
}
/* ****************************************** */
/* status_serial() */
/* */
/* get the serial port status */
/* use BIOS calls (INT 14) */
/* ****************************************** */
static int status_serial(void)
{
_DX = comport;
_AH = 3; /* status */
geninterrupt(0x14);
return(_AH);
}
#if 0
/* serial buffers are not implemented, upto now */
static int send_buffer_serial(void)
{
int st, n;
n=ptrin-ptrout;
if (n<0) n+=2048;
if (n!=0) {
st=status_serial();
if ((st&0x60)==0x60) {
do {
n=ptrin-ptrout;
if (n<0) n+=2048;
if (n==0) break;
st=send_car(buf[ptrout]);
if ((st&0x60)!=0x60) break;
ptrout++;
if (ptrout>=2048) ptrout=0;
delay(1);
} while (1);
}
}
return(n);
}
static void add_buffer(int c)
{
int i, n;
i=0;
do {
send_buffer_serial();
n=ptrin-ptrout;
if (n<0) n += 2048;
if (n<2047) break;
delay(100);
i++;
if (i==10) break;
} while (1);
if (n<2047) {
buf[ptrin]=c;
ptrin++;
if (ptrin==2048) ptrin =0;
}
send_buffer_serial();
}
#endif
static void clr_buffer(void)
{
ptrin=0;
ptrout=0;
}
/* ****************************************** */
/* traite_doe(frame) */
/* */
/* manage the HPIL data frames */
/* returns the returned frame */
/* ****************************************** */
static int traite_doe(int frame)
{
int n;
char c;
if (fetat&0x80) {
/* addressed */
if (fetat&0x40) {
/* talker */
if (fetat&2) {
/* data (SDA) or accessory ID (SDI) */
if (fetat&1) {
/* SDI */
if (ptsdi!=0) {
c=did[ptsdi];
frame=c;
if (c==0) ptsdi=0; else ptsdi++;
}
if (ptsdi==0) {
frame=0x540; /* EOT */
fetat=0x40;
}
}
else {
/* SDA */
/* frame=outserial(); */
do {
n=read_car();
if (n<0)
if (tstkey()) frdy=0;
} while ((n<0)&&frdy);
if (n<0) {
frame=0x540; /* EOT */
fetat=0x40;
}
else
frame=n;
}
}
else {
/* end of SST, SAI */
frame=0x540; /* EOT */
fetat=0x40;
}
}
else {
/* listener */
c=frame & 255;
/* add_buffer(c); */
do {
n=send_car(c);
if (n&0x80)
if (tstkey()) frdy=0;
} while ((n&0x80)&&frdy);
}
}
return(frame);
}
/* ****************************************** */
/* traite_cmd(frame) */
/* */
/* manage the HPIL command frames */
/* returns the returned frame */
/* ****************************************** */
static int traite_cmd(int frame)
{
int n;
n=frame & 255;
switch (n>>5) {
case 0:
switch (n) {
case 4: /*SDC */
if (fetat&0x80) clr_buffer();
break;
case 20: /* DCL */
clr_buffer();
break;
}
break;
case 1: /* LAD */
n &= 31;
if (n==31) {
/* UNL, if not talker then go to idle state */
if ((fetat&0x40)==0) fetat=0;
}
else {
/* else, if MLA go to listen state */
if (n==(adr&31)) {
fetat=0x80;
frdy=1;
}
}
break;
case 2: /* TAD */
n &= 31;
if (n==(adr&31)) {
/* if MTA go to talker state */
fetat=0x40;
frdy=1;
}
else {
/* else if addressed talker, go to idle state */
if ((fetat&0x40)!=0) fetat=0;
}
break;
case 4:
n &= 31;
switch (n) {
case 16: /* IFC */
fetat=0;
break;
case 26: /* AAU */
adr=DEFADDR;
break;
}
}
return(frame);
}
/* ****************************************** */
/* traite_rdy(frame) */
/* */
/* manage the HPIL ready frames */
/* returns the returned frame */
/* ****************************************** */
static int traite_rdy(int frame)
{
int n;
n=frame & 255;
if (n<=127) {
/* sot */
if (fetat&0x40) {
/* if addressed talker */
if (n==66) { /* NRD */
fetat &= 0xfd; /* abort transfer */
}
else if (n==96) { /* SDA */
frame=read_car();
if (frame<0)
frame=0x540; /* EOT */
else
fetat=0xc2; /* active talker (data) */
}
else if (n==97) { /* SST */
status=status_serial();
frame=status;
fetat=0xc1; /* active talker (single byte status) */
}
else if (n==98) { /* SDI */
frame=did[0];
ptsdi=1;
fetat=0xc3; /* active talker (multiple byte status) */
}
else if (n==99) { /* SAI */
frame=AID;
fetat=0xc1; /* active talker (single byte status) */
}
}
}
else if (n<0x9e) {
/* AAD, if not already an assigned address, take it */
if ((adr&0x80)==0) {
adr=n;
frame++;
}
}
return(frame);
}