First Commit of my working state
[simh.git] / PDP8 / pdp8_ttx.c
1 /* pdp8_ttx.c: PDP-8 additional terminals simulator
2
3 Copyright (c) 1993-2007, Robert M Supnik
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of Robert M Supnik shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from Robert M Supnik.
25
26 ttix,ttox PT08/KL8JA terminal input/output
27
28 07-Jun-06 RMS Added UNIT_IDLE flag
29 06-Jul-06 RMS Fixed bug in DETACH routine
30 22-Nov-05 RMS Revised for new terminal processing routines
31 29-Jun-05 RMS Added SET TTOXn DISCONNECT
32 Fixed bug in SET LOG/NOLOG
33 21-Jun-05 RMS Fixed bug in SHOW CONN/STATS
34 05-Jan-04 RMS Revised for tmxr library changes
35 09-May-03 RMS Added network device flag
36 25-Apr-03 RMS Revised for extended file support
37 22-Dec-02 RMS Added break support
38 02-Nov-02 RMS Added 7B/8B support
39 04-Oct-02 RMS Added DIB, device number support
40 22-Aug-02 RMS Updated for changes to sim_tmxr.c
41 06-Jan-02 RMS Added device enable/disable support
42 30-Dec-01 RMS Complete rebuild
43 30-Nov-01 RMS Added extended SET/SHOW support
44
45 This module implements four individual serial interfaces similar in function
46 to the console. These interfaces are mapped to Telnet based connections as
47 though they were the four lines of a terminal multiplexor. The connection
48 polling mechanism is superimposed onto the keyboard of the first interface.
49 */
50
51 #include "pdp8_defs.h"
52 #include "sim_sock.h"
53 #include "sim_tmxr.h"
54 #include <ctype.h>
55
56 #define TTX_LINES 4
57 #define TTX_MASK (TTX_LINES - 1)
58
59 #define TTX_GETLN(x) (((x) >> 4) & TTX_MASK)
60
61 extern int32 int_req, int_enable, dev_done, stop_inst;
62
63 uint8 ttix_buf[TTX_LINES] = { 0 }; /* input buffers */
64 uint8 ttox_buf[TTX_LINES] = { 0 }; /* output buffers */
65 int32 ttx_tps = 100; /* polls per second */
66 TMLN ttx_ldsc[TTX_LINES] = { 0 }; /* line descriptors */
67 TMXR ttx_desc = { TTX_LINES, 0, 0, ttx_ldsc }; /* mux descriptor */
68
69 DEVICE ttix_dev, ttox_dev;
70 int32 ttix (int32 IR, int32 AC);
71 int32 ttox (int32 IR, int32 AC);
72 t_stat ttix_svc (UNIT *uptr);
73 t_stat ttix_reset (DEVICE *dptr);
74 t_stat ttox_svc (UNIT *uptr);
75 t_stat ttox_reset (DEVICE *dptr);
76 t_stat ttx_attach (UNIT *uptr, char *cptr);
77 t_stat ttx_detach (UNIT *uptr);
78 t_stat ttx_summ (FILE *st, UNIT *uptr, int32 val, void *desc);
79 t_stat ttx_show (FILE *st, UNIT *uptr, int32 val, void *desc);
80 void ttx_enbdis (int32 dis);
81
82 /* TTIx data structures
83
84 ttix_dev TTIx device descriptor
85 ttix_unit TTIx unit descriptor
86 ttix_reg TTIx register list
87 ttix_mod TTIx modifiers list
88 */
89
90 DIB ttix_dib = { DEV_KJ8, 8,
91 { &ttix, &ttox, &ttix, &ttox, &ttix, &ttox, &ttix, &ttox } };
92
93 UNIT ttix_unit = { UDATA (&ttix_svc, UNIT_IDLE|UNIT_ATTABLE, 0), KBD_POLL_WAIT };
94
95 REG ttix_reg[] = {
96 { BRDATA (BUF, ttix_buf, 8, 8, TTX_LINES) },
97 { GRDATA (DONE, dev_done, 8, TTX_LINES, INT_V_TTI1) },
98 { GRDATA (ENABLE, int_enable, 8, TTX_LINES, INT_V_TTI1) },
99 { GRDATA (INT, int_req, 8, TTX_LINES, INT_V_TTI1) },
100 { DRDATA (TIME, ttix_unit.wait, 24), REG_NZ + PV_LEFT },
101 { DRDATA (TPS, ttx_tps, 10), REG_NZ + PV_LEFT },
102 { ORDATA (DEVNUM, ttix_dib.dev, 6), REG_HRO },
103 { NULL }
104 };
105
106 MTAB ttix_mod[] = {
107 { UNIT_ATT, UNIT_ATT, "summary", NULL, NULL, &ttx_summ },
108 { MTAB_XTD | MTAB_VDV, 1, NULL, "DISCONNECT",
109 &tmxr_dscln, NULL, &ttx_desc },
110 { MTAB_XTD | MTAB_VDV | MTAB_NMO, 1, "CONNECTIONS", NULL,
111 NULL, &ttx_show, NULL },
112 { MTAB_XTD | MTAB_VDV | MTAB_NMO, 0, "STATISTICS", NULL,
113 NULL, &ttx_show, NULL },
114 { MTAB_XTD|MTAB_VDV, 0, "DEVNO", "DEVNO",
115 &set_dev, &show_dev, NULL },
116 { 0 }
117 };
118
119 DEVICE ttix_dev = {
120 "TTIX", &ttix_unit, ttix_reg, ttix_mod,
121 1, 10, 31, 1, 8, 8,
122 &tmxr_ex, &tmxr_dep, &ttix_reset,
123 NULL, &ttx_attach, &ttx_detach,
124 &ttix_dib, DEV_NET | DEV_DISABLE
125 };
126
127 /* TTOx data structures
128
129 ttox_dev TTOx device descriptor
130 ttox_unit TTOx unit descriptor
131 ttox_reg TTOx register list
132 */
133
134 UNIT ttox_unit[] = {
135 { UDATA (&ttox_svc, TT_MODE_UC, 0), SERIAL_OUT_WAIT },
136 { UDATA (&ttox_svc, TT_MODE_UC, 0), SERIAL_OUT_WAIT },
137 { UDATA (&ttox_svc, TT_MODE_UC, 0), SERIAL_OUT_WAIT },
138 { UDATA (&ttox_svc, TT_MODE_UC, 0), SERIAL_OUT_WAIT }
139 };
140
141 REG ttox_reg[] = {
142 { BRDATA (BUF, ttox_buf, 8, 8, TTX_LINES) },
143 { GRDATA (DONE, dev_done, 8, TTX_LINES, INT_V_TTO1) },
144 { GRDATA (ENABLE, int_enable, 8, TTX_LINES, INT_V_TTO1) },
145 { GRDATA (INT, int_req, 8, TTX_LINES, INT_V_TTO1) },
146 { URDATA (TIME, ttox_unit[0].wait, 10, 24, 0,
147 TTX_LINES, PV_LEFT) },
148 { NULL }
149 };
150
151 MTAB ttox_mod[] = {
152 { TT_MODE, TT_MODE_UC, "UC", "UC", NULL },
153 { TT_MODE, TT_MODE_7B, "7b", "7B", NULL },
154 { TT_MODE, TT_MODE_8B, "8b", "8B", NULL },
155 { TT_MODE, TT_MODE_7P, "7p", "7P", NULL },
156 { MTAB_XTD|MTAB_VUN, 0, NULL, "DISCONNECT",
157 &tmxr_dscln, NULL, &ttx_desc },
158 { MTAB_XTD|MTAB_VUN|MTAB_NC, 0, "LOG", "LOG",
159 &tmxr_set_log, &tmxr_show_log, &ttx_desc },
160 { MTAB_XTD|MTAB_VUN|MTAB_NC, 0, NULL, "NOLOG",
161 &tmxr_set_nolog, NULL, &ttx_desc },
162 { 0 }
163 };
164
165 DEVICE ttox_dev = {
166 "TTOX", ttox_unit, ttox_reg, ttox_mod,
167 4, 10, 31, 1, 8, 8,
168 NULL, NULL, &ttox_reset,
169 NULL, NULL, NULL,
170 NULL, DEV_DISABLE
171 };
172
173 /* Terminal input: IOT routine */
174
175 int32 ttix (int32 inst, int32 AC)
176 {
177 int32 pulse = inst & 07; /* IOT pulse */
178 int32 ln = TTX_GETLN (inst); /* line # */
179 int32 itti = (INT_TTI1 << ln); /* rx intr */
180 int32 itto = (INT_TTO1 << ln); /* tx intr */
181
182 switch (pulse) { /* case IR<9:11> */
183
184 case 0: /* KCF */
185 dev_done = dev_done & ~itti; /* clear flag */
186 int_req = int_req & ~itti;
187 break;
188
189 case 1: /* KSF */
190 return (dev_done & itti)? IOT_SKP + AC: AC;
191
192 case 2: /* KCC */
193 dev_done = dev_done & ~itti; /* clear flag */
194 int_req = int_req & ~itti;
195 return 0; /* clear AC */
196
197 case 4: /* KRS */
198 return (AC | ttix_buf[ln]); /* return buf */
199
200 case 5: /* KIE */
201 if (AC & 1) int_enable = int_enable | (itti + itto);
202 else int_enable = int_enable & ~(itti + itto);
203 int_req = INT_UPDATE; /* update intr */
204 break;
205
206 case 6: /* KRB */
207 dev_done = dev_done & ~itti; /* clear flag */
208 int_req = int_req & ~itti;
209 return ttix_buf[ln]; /* return buf */
210
211 default:
212 return (stop_inst << IOT_V_REASON) + AC;
213 } /* end switch */
214
215 return AC;
216 }
217
218 /* Unit service */
219
220 t_stat ttix_svc (UNIT *uptr)
221 {
222 int32 ln, c, temp;
223
224 if ((uptr->flags & UNIT_ATT) == 0) return SCPE_OK; /* attached? */
225 temp = sim_rtcn_calb (ttx_tps, TMR_TTX); /* calibrate */
226 sim_activate (uptr, temp); /* continue poll */
227 ln = tmxr_poll_conn (&ttx_desc); /* look for connect */
228 if (ln >= 0) ttx_ldsc[ln].rcve = 1; /* got one? rcv enb*/
229 tmxr_poll_rx (&ttx_desc); /* poll for input */
230 for (ln = 0; ln < TTX_LINES; ln++) { /* loop thru lines */
231 if (ttx_ldsc[ln].conn) { /* connected? */
232 if (temp = tmxr_getc_ln (&ttx_ldsc[ln])) { /* get char */
233 if (temp & SCPE_BREAK) c = 0; /* break? */
234 else c = sim_tt_inpcvt (temp, TT_GET_MODE (ttox_unit[ln].flags));
235 ttix_buf[ln] = c;
236 dev_done = dev_done | (INT_TTI1 << ln);
237 int_req = INT_UPDATE;
238 }
239 }
240 }
241 return SCPE_OK;
242 }
243
244 /* Reset routine */
245
246 t_stat ttix_reset (DEVICE *dptr)
247 {
248 int32 t, ln, itto;
249
250 ttx_enbdis (dptr->flags & DEV_DIS); /* sync enables */
251 if (ttix_unit.flags & UNIT_ATT) { /* if attached, */
252 if (!sim_is_active (&ttix_unit)) {
253 t = sim_rtcn_init (ttix_unit.wait, TMR_TTX);
254 sim_activate (&ttix_unit, t); /* activate */
255 }
256 }
257 else sim_cancel (&ttix_unit); /* else stop */
258 for (ln = 0; ln < TTX_LINES; ln++) { /* for all lines */
259 ttix_buf[ln] = 0; /* clear buf, */
260 itto = (INT_TTI1 << ln); /* interrupt */
261 dev_done = dev_done & ~itto; /* clr done, int */
262 int_req = int_req & ~itto;
263 int_enable = int_enable | itto; /* set enable */
264 }
265 return SCPE_OK;
266 }
267
268 /* Terminal output: IOT routine */
269
270 int32 ttox (int32 inst, int32 AC)
271 {
272 int32 pulse = inst & 07; /* pulse */
273 int32 ln = TTX_GETLN (inst); /* line # */
274 int32 itti = (INT_TTI1 << ln); /* rx intr */
275 int32 itto = (INT_TTO1 << ln); /* tx intr */
276
277 switch (pulse) { /* case IR<9:11> */
278
279 case 0: /* TLF */
280 dev_done = dev_done | itto; /* set flag */
281 int_req = INT_UPDATE; /* update intr */
282 break;
283
284 case 1: /* TSF */
285 return (dev_done & itto)? IOT_SKP + AC: AC;
286
287 case 2: /* TCF */
288 dev_done = dev_done & ~itto; /* clear flag */
289 int_req = int_req & ~itto; /* clear intr */
290 break;
291
292 case 5: /* SPI */
293 return (int_req & (itti | itto))? IOT_SKP + AC: AC;
294
295 case 6: /* TLS */
296 dev_done = dev_done & ~itto; /* clear flag */
297 int_req = int_req & ~itto; /* clear int req */
298 case 4: /* TPC */
299 sim_activate (&ttox_unit[ln], ttox_unit[ln].wait); /* activate */
300 ttox_buf[ln] = AC & 0377; /* load buffer */
301 break;
302
303 default:
304 return (stop_inst << IOT_V_REASON) + AC;
305 } /* end switch */
306
307 return AC;
308 }
309
310 /* Unit service */
311
312 t_stat ttox_svc (UNIT *uptr)
313 {
314 int32 c, ln = uptr - ttox_unit; /* line # */
315
316 if (ttx_ldsc[ln].conn) { /* connected? */
317 if (ttx_ldsc[ln].xmte) { /* tx enabled? */
318 TMLN *lp = &ttx_ldsc[ln]; /* get line */
319 c = sim_tt_outcvt (ttox_buf[ln], TT_GET_MODE (ttox_unit[ln].flags));
320 if (c >= 0) tmxr_putc_ln (lp, c); /* output char */
321 tmxr_poll_tx (&ttx_desc); /* poll xmt */
322 }
323 else {
324 tmxr_poll_tx (&ttx_desc); /* poll xmt */
325 sim_activate (uptr, ttox_unit[ln].wait); /* wait */
326 return SCPE_OK;
327 }
328 }
329 dev_done = dev_done | (INT_TTO1 << ln); /* set done */
330 int_req = INT_UPDATE; /* update intr */
331 return SCPE_OK;
332 }
333
334 /* Reset routine */
335
336 t_stat ttox_reset (DEVICE *dptr)
337 {
338 int32 ln, itto;
339
340 ttx_enbdis (dptr->flags & DEV_DIS); /* sync enables */
341 for (ln = 0; ln < TTX_LINES; ln++) { /* for all lines */
342 ttox_buf[ln] = 0; /* clear buf */
343 itto = (INT_TTO1 << ln); /* interrupt */
344 dev_done = dev_done & ~itto; /* clr done, int */
345 int_req = int_req & ~itto;
346 int_enable = int_enable | itto; /* set enable */
347 sim_cancel (&ttox_unit[ln]); /* deactivate */
348 }
349 return SCPE_OK;
350 }
351
352 /* Attach master unit */
353
354 t_stat ttx_attach (UNIT *uptr, char *cptr)
355 {
356 int32 t;
357 t_stat r;
358
359 r = tmxr_attach (&ttx_desc, uptr, cptr); /* attach */
360 if (r != SCPE_OK) return r; /* error */
361 t = sim_rtcn_init (ttix_unit.wait, TMR_TTX); /* init calib */
362 sim_activate (uptr, t); /* start poll */
363 return SCPE_OK;
364 }
365
366 /* Detach master unit */
367
368 t_stat ttx_detach (UNIT *uptr)
369 {
370 int32 i;
371 t_stat r;
372
373 r = tmxr_detach (&ttx_desc, uptr); /* detach */
374 for (i = 0; i < TTX_LINES; i++) /* all lines, */
375 ttx_ldsc[i].rcve = 0; /* disable rcv */
376 sim_cancel (uptr); /* stop poll */
377 return r;
378 }
379
380 /* Show summary processor */
381
382 t_stat ttx_summ (FILE *st, UNIT *uptr, int32 val, void *desc)
383 {
384 int32 i, t;
385
386 for (i = t = 0; i < TTX_LINES; i++) t = t + (ttx_ldsc[i].conn != 0);
387 if (t == 1) fprintf (st, "1 connection");
388 else fprintf (st, "%d connections", t);
389 return SCPE_OK;
390 }
391
392 /* SHOW CONN/STAT processor */
393
394 t_stat ttx_show (FILE *st, UNIT *uptr, int32 val, void *desc)
395 {
396 int32 i, t;
397
398 for (i = t = 0; i < TTX_LINES; i++) t = t + (ttx_ldsc[i].conn != 0);
399 if (t) {
400 for (i = 0; i < TTX_LINES; i++) {
401 if (ttx_ldsc[i].conn) {
402 if (val) tmxr_fconns (st, &ttx_ldsc[i], i);
403 else tmxr_fstats (st, &ttx_ldsc[i], i);
404 }
405 }
406 }
407 else fprintf (st, "all disconnected\n");
408 return SCPE_OK;
409 }
410
411 /* Enable/disable device */
412
413 void ttx_enbdis (int32 dis)
414 {
415 if (dis) {
416 ttix_dev.flags = ttox_dev.flags | DEV_DIS;
417 ttox_dev.flags = ttox_dev.flags | DEV_DIS;
418 }
419 else {
420 ttix_dev.flags = ttix_dev.flags & ~DEV_DIS;
421 ttox_dev.flags = ttox_dev.flags & ~DEV_DIS;
422 }
423 return;
424 }