This source file includes following definitions.
- ta_flush
- ta_push
- ta_peek
- ta_get
- opc_reset
- check_attn_key
- console_init
- console_exit
- opc_autoinput_set
- clear_opc_autoinput
- add_opc_autoinput
- opc_autoinput_show
- console_attn
- console_attn_idx
- newlineOff
- newlineOn
- handleRCP
- sendConsole
- consoleProcessIdx
- consoleProcess
- opc_iom_cmd
- opc_svc
- opc_show_nunits
- opc_set_nunits
- opc_set_config
- opc_show_config
- opc_show_device_name
- opc_set_device_name
- opc_set_console_port
- opc_show_console_port
- opc_set_console_address
- opc_show_console_address
- opc_set_console_pw
- opc_show_console_pw
- console_putstr
- consolePutchar0
- console_putchar
- consoleConnectPrompt
- startRemoteConsole
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 #include <stdio.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #if !defined(__MINGW64__) && !defined(__MINGW32__)
34 # include <termios.h>
35 #endif
36 #include <ctype.h>
37
38 #include "dps8.h"
39 #include "dps8_iom.h"
40 #include "dps8_sys.h"
41 #include "dps8_console.h"
42 #include "dps8_cable.h"
43 #include "dps8_cpu.h"
44 #include "dps8_faults.h"
45 #include "dps8_scu.h"
46 #include "dps8_mt.h"
47 #include "dps8_disk.h"
48 #include "dps8_utils.h"
49 #if defined(LOCKLESS)
50 # include "threadz.h"
51 #endif
52
53 #include "libtelnet.h"
54 #if defined(CONSOLE_FIX)
55 # include "threadz.h"
56 #endif
57
58 #if defined(SIM_NAME)
59 # undef SIM_NAME
60 #endif
61 #define SIM_NAME "DPS8M"
62
63 #define DBG_CTR 1
64 #define ASSUME0 0
65
66 #if defined(FREE)
67 # undef FREE
68 #endif
69 #define FREE(p) do \
70 { \
71 free((p)); \
72 (p) = NULL; \
73 } while(0)
74
75
76
77
78
79
80 static t_stat opc_reset (DEVICE * dptr);
81 static t_stat opc_show_nunits (FILE *st, UNIT *uptr, int val,
82 const void *desc);
83 static t_stat opc_set_nunits (UNIT * uptr, int32 value, const char * cptr,
84 void * desc);
85 static t_stat opc_autoinput_set (UNIT *uptr, int32 val, const char *cptr,
86 void *desc);
87 static t_stat opc_autoinput_show (FILE *st, UNIT *uptr, int val,
88 const void *desc);
89 static t_stat opc_set_config (UNUSED UNIT * uptr, UNUSED int32 value,
90 const char * cptr, UNUSED void * desc);
91 static t_stat opc_show_config (UNUSED FILE * st, UNUSED UNIT * uptr,
92 UNUSED int val, UNUSED const void * desc);
93 static t_stat opc_set_console_port (UNIT * uptr, UNUSED int32 value,
94 const char * cptr, UNUSED void * desc);
95 static t_stat opc_show_console_port (UNUSED FILE * st, UNIT * uptr,
96 UNUSED int val, UNUSED const void * desc);
97 static t_stat opc_set_console_address (UNIT * uptr, UNUSED int32 value,
98 const char * cptr, UNUSED void * desc);
99 static t_stat opc_show_console_address (UNUSED FILE * st, UNIT * uptr,
100 UNUSED int val, UNUSED const void * desc);
101 static t_stat opc_set_console_pw (UNIT * uptr, UNUSED int32 value,
102 const char * cptr, UNUSED void * desc);
103 static t_stat opc_show_console_pw (UNUSED FILE * st, UNIT * uptr,
104 UNUSED int val, UNUSED const void * desc);
105 static t_stat opc_set_device_name (UNIT * uptr, UNUSED int32 value,
106 const char * cptr, UNUSED void * desc);
107 static t_stat opc_show_device_name (UNUSED FILE * st, UNIT * uptr,
108 UNUSED int val, UNUSED const void * desc);
109
110 static MTAB opc_mtab[] =
111 {
112 {
113 MTAB_unit_nouc,
114 0,
115 "AUTOINPUT",
116 "AUTOINPUT",
117 opc_autoinput_set,
118 opc_autoinput_show,
119 NULL,
120 NULL
121 },
122
123 {
124 MTAB_dev_valr,
125 0,
126 "NUNITS",
127 "NUNITS",
128 opc_set_nunits,
129 opc_show_nunits,
130 "Number of OPC units in the system",
131 NULL
132 },
133
134 {
135 MTAB_unit_uc,
136 0,
137 (char *) "CONFIG",
138 (char *) "CONFIG",
139 opc_set_config,
140 opc_show_config,
141 NULL,
142 NULL,
143 },
144 {
145 MTAB_XTD | MTAB_VUN | \
146 MTAB_VALR | MTAB_NC,
147 0,
148 "NAME",
149 "NAME",
150 opc_set_device_name,
151 opc_show_device_name,
152 "Set the device name",
153 NULL
154 },
155
156 {
157 MTAB_unit_valr_nouc,
158 0,
159 "PORT",
160 "PORT",
161 opc_set_console_port,
162 opc_show_console_port,
163 "Set the console port number",
164 NULL
165 },
166
167 {
168 MTAB_unit_valr_nouc,
169 0,
170 "ADDRESS",
171 "ADDRESS",
172 opc_set_console_address,
173 opc_show_console_address,
174 "Set the console IP Address",
175 NULL
176 },
177
178 {
179 MTAB_unit_valr_nouc,
180 0,
181 "PW",
182 "PW",
183 opc_set_console_pw,
184 opc_show_console_pw,
185 "Set the console password",
186 NULL
187 },
188
189 MTAB_eol
190 };
191
192 static DEBTAB opc_dt[] =
193 {
194 { "NOTIFY", DBG_NOTIFY, NULL },
195 { "INFO", DBG_INFO, NULL },
196 { "ERR", DBG_ERR, NULL },
197 { "WARN", DBG_WARN, NULL },
198 { "DEBUG", DBG_DEBUG, NULL },
199 { "ALL", DBG_ALL, NULL },
200 { NULL, 0, NULL }
201 };
202
203
204
205
206
207
208 #define N_OPC_UNITS 1
209 #define OPC_UNIT_IDX(uptr) ((uptr) - opc_unit)
210
211
212
213 #if defined(LOCKLESS)
214
215
216
217 # define ACTIVATE_1SEC 1000
218 #else
219
220
221
222 # define ACTIVATE_1SEC 4000000
223 #endif
224
225 static t_stat opc_svc (UNIT * unitp);
226
227 UNIT opc_unit[N_OPC_UNITS_MAX] = {
228 #if defined(NO_C_ELLIPSIS)
229 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
230 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
231 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
232 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
233 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
234 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
235 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
236 { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
237 #else
238 [0 ... N_OPC_UNITS_MAX - 1] = {
239 UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
240 }
241 #endif
242 };
243
244 DEVICE opc_dev = {
245 "OPC",
246 opc_unit,
247 NULL,
248 opc_mtab,
249 N_OPC_UNITS,
250 10,
251 8,
252 1,
253 8,
254 8,
255 NULL,
256 NULL,
257 opc_reset,
258 NULL,
259 NULL,
260 NULL,
261 NULL,
262 DEV_DEBUG,
263 0,
264 opc_dt,
265 NULL,
266 NULL,
267 NULL,
268 NULL,
269 NULL,
270 NULL,
271 NULL
272 };
273
274 enum console_model { m6001 = 0, m6004 = 1, m6601 = 2 };
275
276
277 typedef struct opc_state_t
278 {
279
280 unsigned char *tailp;
281 unsigned char *readp;
282
283
284 unsigned char *auto_input;
285 unsigned char *autop;
286
287
288 time_t startTime;
289 UNIT * unitp;
290
291
292 uv_access console_access;
293
294 enum console_model model;
295 enum console_mode { opc_no_mode, opc_read_mode, opc_write_mode } io_mode;
296
297
298 uint tally;
299 uint daddr;
300 int chan;
301
302
303
304 int autoaccept;
305
306 int noto;
307
308 int noempty;
309
310 int attn_flush;
311
312 int simh_buffer_cnt;
313
314
315
316 int carrierPosition;
317
318 bool echo;
319
320 bool attn_pressed;
321 bool simh_attn_pressed;
322
323 bool bcd;
324
325
326 bool escapeSequence;
327
328 char device_name [MAX_DEV_NAME_LEN];
329
330
331
332
333 #define bufsize 257
334 unsigned char keyboardLineBuffer[bufsize];
335 bool tabStops [bufsize];
336
337 #define simh_buffer_sz 4096
338 char simh_buffer[simh_buffer_sz];
339 } opc_state_t;
340
341 static opc_state_t console_state[N_OPC_UNITS_MAX];
342
343 static char * bcd_code_page =
344 "01234567"
345 "89[#@;>?"
346 " ABCDEFG"
347 "HI&.](<\\"
348 "^JKLMNOP"
349 "QR-$*);'"
350 "+/STUVWX"
351 "YZ_,%=\"!";
352
353
354
355
356
357 #if !defined(TA_BUFFER_SIZE)
358 # define TA_BUFFER_SIZE 65536
359 #endif
360
361 static int ta_buffer[TA_BUFFER_SIZE];
362 static uint ta_cnt = 0;
363 static uint ta_next = 0;
364 static bool ta_ovf = false;
365
366 static void ta_flush (void)
367 {
368 ta_cnt = ta_next = 0;
369 ta_ovf = false;
370 }
371
372 static void ta_push (int c)
373 {
374
375 if (ta_cnt >= TA_BUFFER_SIZE)
376 {
377 if (! ta_ovf)
378 sim_print ("typeahead buffer overflow");
379 ta_ovf = true;
380 return;
381 }
382 ta_buffer [ta_cnt ++] = c;
383 }
384
385 static int ta_peek (void)
386 {
387 if (ta_next >= ta_cnt)
388 return SCPE_OK;
389 int c = ta_buffer[ta_next];
390 return c;
391 }
392
393 static int ta_get (void)
394 {
395 if (ta_next >= ta_cnt)
396 return SCPE_OK;
397 int c = ta_buffer[ta_next ++];
398 if (ta_next >= ta_cnt)
399 ta_flush ();
400 return c;
401 }
402
403 static t_stat opc_reset (UNUSED DEVICE * dptr)
404 {
405 for (uint i = 0; i < N_OPC_UNITS_MAX; i ++)
406 {
407 console_state[i].io_mode = opc_no_mode;
408 console_state[i].tailp = console_state[i].keyboardLineBuffer;
409 console_state[i].readp = console_state[i].keyboardLineBuffer;
410 console_state[i].carrierPosition = 1;
411 (void)memset (console_state[i].tabStops, 0, sizeof (console_state[i].tabStops));
412 console_state[i].escapeSequence = false;
413 }
414 return SCPE_OK;
415 }
416
417 int check_attn_key (void)
418 {
419 for (uint i = 0; i < opc_dev.numunits; i ++)
420 {
421 opc_state_t * csp = console_state + i;
422 if (csp->attn_pressed)
423 {
424 csp->attn_pressed = false;
425 return (int) i;
426 }
427 }
428 return -1;
429 }
430
431
432
433 void console_init (void)
434 {
435 opc_reset (& opc_dev);
436 for (uint i = 0; i < N_OPC_UNITS_MAX; i ++)
437 {
438 opc_state_t * csp = console_state + i;
439 csp->model = m6001;
440 csp->auto_input = NULL;
441 csp->autop = NULL;
442 csp->attn_pressed = false;
443 csp->simh_attn_pressed = false;
444 csp->simh_buffer_cnt = 0;
445 strcpy (csp->console_access.pw, "MulticsRulez");
446
447 csp->autoaccept = 0;
448 csp->noempty = 0;
449 csp->attn_flush = 1;
450 csp->carrierPosition = 1;
451 csp->escapeSequence = 1;
452 (void)memset (csp->tabStops, 0, sizeof (csp->tabStops));
453 }
454 }
455
456
457
458 void console_exit (void) {
459 for (uint i = 0; i < N_OPC_UNITS_MAX; i ++) {
460 opc_state_t * csp = console_state + i;
461 if (csp->auto_input) {
462 FREE (csp->auto_input);
463 csp->auto_input = NULL;
464 }
465
466
467 accessCloseAll (&csp->console_access);
468 if (csp->console_access.open &&
469 !uv_is_closing ((uv_handle_t *)&csp->console_access.server))
470 uv_close ((uv_handle_t *)&csp->console_access.server, NULL);
471 csp->console_access.open = false;
472 FREE (csp->console_access.inBuffer);
473 csp->console_access.inSize = csp->console_access.inUsed = 0;
474 }
475 }
476
477 static int opc_autoinput_set (UNIT * uptr, UNUSED int32 val,
478 const char * cptr, UNUSED void * desc)
479 {
480 int devUnitIdx = (int) OPC_UNIT_IDX (uptr);
481 opc_state_t * csp = console_state + devUnitIdx;
482
483 if (cptr)
484 {
485 unsigned char * new = (unsigned char *) strdupesc (cptr);
486 if (csp-> auto_input)
487 {
488 size_t nl = strlen ((char *) new);
489 size_t ol = strlen ((char *) csp->auto_input);
490
491 unsigned char * old = realloc (csp->auto_input, nl + ol + 1);
492 if (!old)
493 {
494 (void)fprintf(stderr, "\rFATAL: Out of memory! Aborting at %s[%s:%d]\r\n",
495 __func__, __FILE__, __LINE__);
496 #if defined(USE_BACKTRACE)
497 # if defined(SIGUSR2)
498 (void)raise(SIGUSR2);
499
500 # endif
501 #endif
502 abort();
503 }
504 strcpy ((char *) old + ol, (char *) new);
505 csp->auto_input = old;
506 FREE (new);
507 }
508 else
509 csp->auto_input = new;
510 }
511 else
512 {
513 if (csp->auto_input)
514 FREE (csp->auto_input);
515 csp->auto_input = NULL;
516 }
517 csp->autop = csp->auto_input;
518 return SCPE_OK;
519 }
520
521 int clear_opc_autoinput (int32 flag, UNUSED const char * cptr)
522 {
523 opc_state_t * csp = console_state + flag;
524 if (csp->auto_input)
525 FREE (csp->auto_input);
526 csp->auto_input = NULL;
527 csp->autop = csp->auto_input;
528 return SCPE_OK;
529 }
530
531 int add_opc_autoinput (int32 flag, const char * cptr)
532 {
533 opc_state_t * csp = console_state + flag;
534 unsigned char * new = (unsigned char *) strdupesc (cptr);
535 if (csp->auto_input)
536 {
537 size_t nl = strlen ((char *) new);
538 size_t ol = strlen ((char *) csp->auto_input);
539
540 unsigned char * old = realloc (csp->auto_input, nl + ol + 1);
541 if (!old)
542 {
543 (void)fprintf(stderr, "\rFATAL: Out of memory! Aborting at %s[%s:%d]\r\n",
544 __func__, __FILE__, __LINE__);
545 #if defined(USE_BACKTRACE)
546 # if defined(SIGUSR2)
547 (void)raise(SIGUSR2);
548
549 # endif
550 #endif
551 abort();
552 }
553 strcpy ((char *) old + ol, (char *) new);
554 csp->auto_input = old;
555 FREE (new);
556 }
557 else
558 csp->auto_input = new;
559 csp->autop = csp->auto_input;
560 return SCPE_OK;
561 }
562
563 static int opc_autoinput_show (UNUSED FILE * st, UNIT * uptr,
564 UNUSED int val, UNUSED const void * desc)
565 {
566 int conUnitIdx = (int) OPC_UNIT_IDX (uptr);
567 opc_state_t * csp = console_state + conUnitIdx;
568 if (csp->auto_input)
569 sim_print ("autoinput: '%s'", csp->auto_input);
570 else
571 sim_print ("autoinput: empty");
572 return SCPE_OK;
573 }
574
575 static t_stat console_attn (UNUSED UNIT * uptr);
576
577 static UNIT attn_unit[N_OPC_UNITS_MAX] = {
578 #if defined(NO_C_ELLIPSIS)
579 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
580 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
581 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
582 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
583 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
584 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
585 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
586 { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
587 #else
588 [0 ... N_OPC_UNITS_MAX - 1] = {
589 UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
590 }
591 #endif
592 };
593
594 static t_stat console_attn (UNUSED UNIT * uptr)
595 {
596 uint con_unit_idx = (uint) (uptr - attn_unit);
597 uint ctlr_port_num = 0;
598 uint iom_unit_idx = cables->opc_to_iom[con_unit_idx][ctlr_port_num].iom_unit_idx;
599 uint chan_num = cables->opc_to_iom[con_unit_idx][ctlr_port_num].chan_num;
600 uint dev_code = 0;
601
602 send_special_interrupt (iom_unit_idx, chan_num, dev_code, 0, 0);
603 return SCPE_OK;
604 }
605
606 void console_attn_idx (int conUnitIdx)
607 {
608 console_attn (attn_unit + conUnitIdx);
609 }
610
611 #if !defined(__MINGW64__) && !defined(__MINGW32__)
612 static struct termios ttyTermios;
613 static bool ttyTermiosOk = false;
614
615 static void newlineOff (void)
616 {
617 if (! isatty (0))
618 return;
619 if (! ttyTermiosOk)
620 {
621 int rc = tcgetattr (0, & ttyTermios);
622 if (rc)
623 return;
624 ttyTermiosOk = true;
625 }
626 struct termios runtty;
627 runtty = ttyTermios;
628 runtty.c_oflag &= (unsigned int) ~OPOST;
629 # if defined(__ANDROID__)
630 # define TCSA_TYPE TCSANOW
631 (void)fflush(stdout);
632 (void)fflush(stderr);
633 # else
634 # define TCSA_TYPE TCSAFLUSH
635 # endif
636 tcsetattr (0, TCSA_TYPE, & runtty);
637 }
638
639 static void newlineOn (void)
640 {
641 if (! isatty (0))
642 return;
643 if (! ttyTermiosOk)
644 return;
645 # if defined(__ANDROID__)
646 (void)fflush(stdout);
647 (void)fflush(stderr);
648 # endif
649 tcsetattr (0, TCSA_TYPE, & ttyTermios);
650 }
651 #endif
652
653 static void handleRCP (uint con_unit_idx, char * text)
654 {
655
656
657
658
659
660
661
662
663
664 size_t len = strlen (text);
665 char label [len + 1];
666 char with [len + 1];
667 char drive [len + 1];
668 int rc = sscanf (text, "%*d.%*d RCP: Mount Reel %s %s ring on %s",
669 label, with, drive);
670 if (rc == 3)
671 {
672 bool withring = (strcmp (with, "with") == 0);
673 char labelDotTap[strlen (label) + strlen (".tap") + 1];
674 strcpy (labelDotTap, label);
675 strcat (labelDotTap, ".tap");
676 attachTape (labelDotTap, withring, drive);
677 return;
678 }
679
680 rc = sscanf (text, "%*d.%*d RCP: Remount Reel %s %s ring on %s",
681 label, with, drive);
682 if (rc == 3)
683 {
684 bool withring = (strcmp (with, "with") == 0);
685 char labelDotTap [strlen (label) + strlen (".tap") + 1];
686 strcpy (labelDotTap, label);
687 strcat (labelDotTap, ".tap");
688 attachTape (labelDotTap, withring, drive);
689 return;
690 }
691
692
693
694 if (console_state[con_unit_idx].autoaccept)
695 {
696 rc = sscanf (text, "%*d as dial_ctl_: Channel %s dialed to Initializer",
697 label);
698 if (rc == 1)
699 {
700
701 opc_autoinput_set (opc_unit + con_unit_idx, 0, "accept ", NULL);
702 opc_autoinput_set (opc_unit + con_unit_idx, 0, label, NULL);
703 opc_autoinput_set (opc_unit + con_unit_idx, 0, "\r", NULL);
704
705 if (console_state[con_unit_idx].io_mode != opc_read_mode)
706 console_state[con_unit_idx].attn_pressed = true;
707 return;
708 }
709 }
710 }
711
712
713 static void sendConsole (int conUnitIdx, word12 stati)
714 {
715 opc_state_t * csp = console_state + conUnitIdx;
716 uint tally = csp->tally;
717 uint ctlr_port_num = 0;
718 uint iomUnitIdx = cables->opc_to_iom[conUnitIdx][ctlr_port_num].iom_unit_idx;
719 uint chan_num = cables->opc_to_iom[conUnitIdx][ctlr_port_num].chan_num;
720 iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan_num];
721
722
723 if (csp->io_mode != opc_read_mode)
724 {
725 sim_warn ("%s called with io_mode != opc_read_mode (%d)\r\n",
726 __func__, csp->io_mode);
727 return;
728 }
729
730 uint n_chars = (uint) (csp->tailp - csp->readp);
731 uint n_words;
732 if (csp->bcd)
733
734 n_words = ((n_chars+2) + 5) / 6;
735 else
736 n_words = (n_chars + 3) / 4;
737
738 word36 buf[n_words + 1];
739
740 (void)memset (buf, 0, sizeof (word36) * (n_words + 1));
741 word36 * bufp = buf;
742
743
744
745 if ((!csp->bcd) && csp->noempty && n_chars == 0 && tally)
746 {
747 n_chars = 1;
748 n_words = 1;
749 putbits36_9 (bufp, 0, '@');
750 tally --;
751 }
752 else
753 {
754 int bcd_nl_state = 0;
755
756
757
758 bool bcd_marker = ! tandd_io_mode ();
759 uint bcd_charno = 0;
760 if (csp->bcd)
761 * bufp = 0;
762 while (tally && (csp->bcd ? (csp->readp < csp->tailp ||
763 (bcd_marker && bcd_nl_state < 2))
764 : csp->readp < csp->tailp))
765 {
766 if (csp->bcd)
767 {
768 unsigned char c;
769 if (csp->readp >= csp->tailp)
770 {
771 if (bcd_nl_state == 0)
772 c = '!';
773 else
774 c = '1';
775 bcd_nl_state ++;
776 }
777 else
778 c = (unsigned char) (* csp->readp ++);
779 c = (unsigned char) toupper (c);
780 int i;
781 for (i = 0; i < 64; i ++)
782 if (bcd_code_page[i] == c)
783 break;
784 if (i >= 64)
785 {
786 sim_warn ("Character %o does not map to BCD; replacing with '?'\r\n", c);
787 i = 017;
788 }
789 putbits36_6 (bufp, bcd_charno * 6, (word6) i);
790 if (++ bcd_charno > 5)
791 {
792 bcd_charno = 0;
793 bufp ++;
794 tally --;
795 if (tally)
796 * bufp = 0;
797 }
798 continue;
799 }
800 else
801 {
802 * bufp = 0ul;
803 for (uint charno = 0; charno < 4; ++ charno)
804 {
805 if (csp->readp >= csp->tailp)
806 break;
807 unsigned char c = (unsigned char) (* csp->readp ++);
808 c &= 0177;
809 putbits36_9 (bufp, charno * 9, c);
810 }
811 }
812 bufp ++;
813 tally --;
814 }
815 if (csp->readp < csp->tailp)
816 {
817 sim_warn ("opc_iom_io: discarding %d characters from end of line\r\n",
818 (int) (csp->tailp - csp->readp));
819 }
820 }
821
822 iom_indirect_data_service (iomUnitIdx, chan_num, buf, & n_words, true);
823
824 p->charPos = n_chars % 4;
825 p->stati = (word12) stati;
826
827 csp->readp = csp->keyboardLineBuffer;
828 csp->tailp = csp->keyboardLineBuffer;
829 csp->io_mode = opc_no_mode;
830
831 send_terminate_interrupt (iomUnitIdx, chan_num);
832 }
833
834 static void console_putchar (int conUnitIdx, char ch);
835 static void console_putstr (int conUnitIdx, char * str);
836
837
838 static void consoleProcessIdx (int conUnitIdx)
839 {
840 opc_state_t * csp = console_state + conUnitIdx;
841 int c;
842
843
844
845 for (;;)
846 {
847 c = sim_poll_kbd ();
848 if (c == SCPE_OK)
849 c = accessGetChar (& csp->console_access);
850
851
852
853 if (breakEnable && stop_cpu)
854 {
855 console_putstr (conUnitIdx, " Got <sim stop> \r\n");
856 return;
857 }
858
859
860
861
862
863 if (breakEnable && c == SCPE_STOP)
864 {
865 console_putstr (conUnitIdx, " Got <sim stop> \r\n");
866 stop_cpu = 1;
867 return;
868 }
869
870
871
872 if (breakEnable && c == SCPE_BREAK)
873 {
874 console_putstr (conUnitIdx, " Got <sim stop> \r\n");
875 stop_cpu = 1;
876 return;
877 }
878
879
880
881 if (c == SCPE_OK)
882 break;
883
884
885
886 if (c < SCPE_KFLAG)
887 {
888 sim_printf ("impossible %d %o\r\n", c, c);
889 continue;
890 }
891
892
893
894 int ch = c - SCPE_KFLAG;
895
896
897
898
899 if (csp->io_mode != opc_read_mode)
900 {
901 if (ch == '\033' || ch == '\001')
902 {
903 if (csp->attn_flush)
904 ta_flush ();
905 csp->attn_pressed = true;
906 continue;
907 }
908 }
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933 if (ch == 023)
934 {
935 if (! csp->simh_attn_pressed)
936 {
937 ta_flush ();
938 csp->simh_attn_pressed = true;
939 csp->simh_buffer_cnt = 0;
940 console_putstr (conUnitIdx, "^S\r\n" SIM_NAME "> ");
941 }
942 continue;
943 }
944
945
946
947
948
949
950
951
952
953
954
955 if (ch == 020) {
956 if (csp->io_mode != opc_read_mode) {
957 if (csp->attn_flush)
958 ta_flush ();
959 csp->attn_pressed = true;
960 }
961 continue;
962 }
963
964
965
966 if (ch == 024)
967 {
968 char buf[256];
969 char cms[3] = "?RW";
970
971 (void)sprintf (buf, "^T attn %c %c cnt %d next %d\r\n",
972 console_state[0].attn_pressed+'0',
973 cms[console_state[0].io_mode],
974 ta_cnt, ta_next);
975 console_putstr (conUnitIdx, buf);
976 #if defined(THREADZ) || defined(LOCKLESS)
977 (void) sprintf (buf, "syncClockMode %c syncClockModeMasterIdx %u\r\n",
978 syncClockMode ? 'T' : 'F',
979 syncClockModeMasterIdx);
980 console_putstr (conUnitIdx, buf);
981 for (int i = 0; i < N_CPU_UNITS_MAX; i ++) {
982 (void) sprintf (buf, "CPU %c %s %05o:%06o\r\n"
983 " executing %c syncClockModeMaster %c workAllocation %4ld\r\n"
984 " inMultics %c syncClockModeCache %c isSlave %c\r\n",
985 "ABCDEFGH"[i],
986 cycle_str (cpus[i].cycle),
987 cpus[i].PPR.PSR,
988 cpus[i].PPR.IC,
989 cpus[i].executing ? 'T' : 'F',
990 cpus[i].syncClockModeMaster ? 'T' : 'F',
991 cpus[i].workAllocation,
992 cpus[i].inMultics ? 'T' : 'F',
993 cpus[i].syncClockModeCache ? 'T' : 'F',
994 cpus[i].isSlave ? 'T' : 'F');
995 console_putstr (conUnitIdx, buf);
996 }
997 #endif
998
999 continue;
1000 }
1001
1002
1003
1004 if (csp->simh_attn_pressed)
1005 {
1006 ta_get ();
1007 if (ch == '\177' || ch == '\010')
1008 {
1009 if (csp->simh_buffer_cnt > 0)
1010 {
1011 -- csp->simh_buffer_cnt;
1012 csp->simh_buffer[csp->simh_buffer_cnt] = 0;
1013 console_putstr (conUnitIdx, "\b \b");
1014 }
1015 return;
1016 }
1017
1018
1019
1020 if (ch == '\022')
1021 {
1022 console_putstr (conUnitIdx, "^R\r\n" SIM_NAME "> ");
1023 for (int i = 0; i < csp->simh_buffer_cnt; i ++)
1024 console_putchar (conUnitIdx, (char) (csp->simh_buffer[i]));
1025 return;
1026 }
1027
1028
1029
1030 if (ch == '\025')
1031 {
1032 console_putstr (conUnitIdx, "^U\r\n" SIM_NAME "> ");
1033 csp->simh_buffer_cnt = 0;
1034 return;
1035 }
1036
1037
1038
1039 if (ch == '\012' || ch == '\015')
1040 {
1041 console_putstr (conUnitIdx, "\r\n");
1042 csp->simh_buffer[csp->simh_buffer_cnt] = 0;
1043
1044 char * cptr = csp->simh_buffer;
1045 char gbuf[simh_buffer_sz];
1046 cptr = (char *) get_glyph (cptr, gbuf, 0);
1047 if (strlen (gbuf))
1048 {
1049 CTAB *cmdp;
1050 if ((cmdp = find_cmd (gbuf)))
1051 {
1052 t_stat stat = cmdp->action (cmdp->arg, cptr);
1053
1054 if (stat != SCPE_OK)
1055 {
1056 char buf[4096];
1057 (void)sprintf (buf, "\r%s returned %d '%s'\r\n",
1058 SIM_NAME, stat, sim_error_text (stat));
1059 console_putstr (conUnitIdx, buf);
1060 }
1061 }
1062 else
1063 console_putstr (conUnitIdx,
1064 "\rUnrecognized " SIM_NAME " command.\r\n");
1065 }
1066 csp->simh_buffer_cnt = 0;
1067 csp->simh_buffer[0] = 0;
1068 csp->simh_attn_pressed = false;
1069 return;
1070 }
1071
1072
1073
1074 if (ch == '\033' || ch == '\004' || ch == '\032')
1075 {
1076 console_putstr (conUnitIdx, "\r\n" SIM_NAME " cancel\r\n");
1077
1078 csp->simh_buffer_cnt = 0;
1079 csp->simh_buffer[0] = 0;
1080 csp->simh_attn_pressed = false;
1081 return;
1082 }
1083
1084
1085
1086 if (isprint (ch))
1087 {
1088
1089 if (csp->simh_buffer_cnt + 1 >= simh_buffer_sz)
1090 return;
1091 csp->simh_buffer[csp->simh_buffer_cnt ++] = (char) ch;
1092 console_putchar (conUnitIdx, (char) ch);
1093 return;
1094 }
1095 return;
1096 }
1097
1098
1099
1100 ta_push (c);
1101 }
1102
1103
1104
1105 if (breakEnable && stop_cpu)
1106 {
1107 console_putstr (conUnitIdx, " Got <sim stop> \r\n");
1108 return;
1109 }
1110
1111
1112
1113
1114 if (csp->io_mode == opc_read_mode &&
1115 csp->autop != NULL)
1116 {
1117 int announce = 1;
1118 for (;;)
1119 {
1120 if (csp->tailp >= csp->keyboardLineBuffer + sizeof (csp->keyboardLineBuffer))
1121 {
1122 sim_warn ("getConsoleInput: Buffer full; flushing autoinput.\r\n");
1123 sendConsole (conUnitIdx, 04000);
1124 return;
1125 }
1126 unsigned char c = * (csp->autop);
1127 if (c == 4)
1128 {
1129 FREE (csp->auto_input);
1130 csp->auto_input = NULL;
1131 csp->autop = NULL;
1132
1133 csp->readp = csp->keyboardLineBuffer;
1134 csp->tailp = csp->keyboardLineBuffer;
1135 sendConsole (conUnitIdx, 04310);
1136
1137 console_putstr (conUnitIdx, "CONSOLE: RELEASED\r\n");
1138 return;
1139 }
1140 if (c == 030 || c == 031)
1141 {
1142
1143
1144 return;
1145 }
1146 if (c == 0)
1147 {
1148 FREE (csp->auto_input);
1149 csp->auto_input = NULL;
1150 csp->autop = NULL;
1151 goto eol;
1152 }
1153 if (announce)
1154 {
1155 console_putstr (conUnitIdx, "[auto-input] ");
1156 announce = 0;
1157 }
1158 csp->autop ++;
1159
1160 if (c == '\012' || c == '\015')
1161 {
1162 eol:
1163 if (csp->echo)
1164 console_putstr (conUnitIdx, "\r\n");
1165 sendConsole (conUnitIdx, 04000);
1166 return;
1167 }
1168 else
1169 {
1170 * csp->tailp ++ = c;
1171 if (csp->echo)
1172 console_putchar (conUnitIdx, (char) c);
1173 }
1174 }
1175 }
1176
1177
1178
1179
1180 if (csp->io_mode == opc_read_mode &&
1181 csp->tailp == csp->keyboardLineBuffer)
1182 {
1183 if ((! csp->noto) && csp->startTime + 30 <= time (NULL))
1184 {
1185 console_putstr (conUnitIdx, "CONSOLE: TIMEOUT\r\n");
1186 csp->readp = csp->keyboardLineBuffer;
1187 csp->tailp = csp->keyboardLineBuffer;
1188 sendConsole (conUnitIdx, 04310);
1189
1190 }
1191 }
1192
1193
1194
1195 c = ta_peek ();
1196
1197
1198 if (c == SCPE_OK)
1199 return;
1200
1201
1202 if (c < SCPE_KFLAG)
1203 {
1204 sim_printf ("impossible %d %o\r\n", c, c);
1205 return;
1206 }
1207
1208
1209
1210 int ch = c - SCPE_KFLAG;
1211
1212
1213 if (csp->io_mode != opc_read_mode)
1214 {
1215 if (ch == '\033' || ch == '\001')
1216 {
1217 ta_get ();
1218 csp->attn_pressed = true;
1219 }
1220 return;
1221 }
1222
1223 if (ch == '\177' || ch == '\010')
1224 {
1225 ta_get ();
1226 if (csp->tailp > csp->keyboardLineBuffer)
1227 {
1228 * csp->tailp = 0;
1229 -- csp->tailp;
1230 if (csp->echo)
1231 console_putstr (conUnitIdx, "\b \b");
1232 }
1233 return;
1234 }
1235
1236 if (ch == '\022')
1237 {
1238 ta_get ();
1239 if (csp->echo)
1240 {
1241 console_putstr (conUnitIdx, "^R\r\n");
1242 for (unsigned char * p = csp->keyboardLineBuffer; p < csp->tailp; p ++)
1243 console_putchar (conUnitIdx, (char) (*p));
1244 return;
1245 }
1246 }
1247
1248 if (ch == '\025')
1249 {
1250 ta_get ();
1251 console_putstr (conUnitIdx, "^U\r\n");
1252 csp->tailp = csp->keyboardLineBuffer;
1253 return;
1254 }
1255
1256 if (ch == '\030')
1257 {
1258 ta_get ();
1259 console_putstr (conUnitIdx, "^X\r\n");
1260 csp->tailp = csp->keyboardLineBuffer;
1261 return;
1262 }
1263
1264 if (ch == '\012' || ch == '\015')
1265 {
1266 ta_get ();
1267 if (csp->echo)
1268 console_putstr (conUnitIdx, "\r\n");
1269 sendConsole (conUnitIdx, 04000);
1270 return;
1271 }
1272
1273 if (ch == '\033' || ch == '\004' || ch == '\032')
1274 {
1275 ta_get ();
1276 console_putstr (conUnitIdx, "\r\n");
1277
1278 csp->readp = csp->keyboardLineBuffer;
1279 csp->tailp = csp->keyboardLineBuffer;
1280 sendConsole (conUnitIdx, 04310);
1281
1282 console_putstr (conUnitIdx, "CONSOLE: RELEASED\r\n");
1283 return;
1284 }
1285
1286 if (isprint (ch))
1287 {
1288
1289 ta_get ();
1290 if (csp->tailp >= csp->keyboardLineBuffer + sizeof (csp->keyboardLineBuffer))
1291 return;
1292
1293 * csp->tailp ++ = (unsigned char) ch;
1294 if (csp->echo)
1295 console_putchar (conUnitIdx, (char) ch);
1296 return;
1297 }
1298
1299 ta_get ();
1300 return;
1301 }
1302
1303 void consoleProcess (void)
1304 {
1305 for (int conUnitIdx = 0; conUnitIdx < (int) opc_dev.numunits; conUnitIdx ++)
1306 consoleProcessIdx (conUnitIdx);
1307 }
1308
1309
1310
1311
1312
1313
1314
1315
1316 iom_cmd_rc_t opc_iom_cmd (uint iomUnitIdx, uint chan) {
1317 iom_cmd_rc_t rc = IOM_CMD_PROCEED;
1318 #if defined(TESTING)
1319 cpu_state_t * cpup = _cpup;
1320 #endif
1321
1322 #if defined(LOCKLESS)
1323 lock_libuv ();
1324 #endif
1325
1326 iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan];
1327 uint con_unit_idx = get_ctlr_idx (iomUnitIdx, chan);
1328 UNIT * unitp = & opc_unit[con_unit_idx];
1329 opc_state_t * csp = console_state + con_unit_idx;
1330
1331 p->dev_code = p->IDCW_DEV_CODE;
1332 p->stati = 0;
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342 if (IS_IDCW (p)) {
1343
1344
1345 switch (p->IDCW_DEV_CMD) {
1346 case 000:
1347 sim_debug (DBG_DEBUG, & opc_dev, "%s: Status request\r\n", __func__);
1348 csp->io_mode = opc_no_mode;
1349 p->stati = 04000;
1350 break;
1351
1352 case 003:
1353 sim_debug (DBG_DEBUG, & tape_dev, "%s: Read BCD echoed\r\n", __func__);
1354 csp->io_mode = opc_read_mode;
1355 p->recordResidue --;
1356 csp->echo = true;
1357 csp->bcd = true;
1358 p->stati = 04000;
1359 break;
1360
1361 case 013:
1362 sim_debug (DBG_DEBUG, & opc_dev, "%s: Write BCD\r\n", __func__);
1363 p->isRead = false;
1364 csp->bcd = true;
1365 csp->io_mode = opc_write_mode;
1366 p->recordResidue --;
1367 p->stati = 04000;
1368 break;
1369
1370 case 023:
1371 sim_debug (DBG_DEBUG, & tape_dev, "%s: Read ASCII echoed\r\n", __func__);
1372 csp->io_mode = opc_read_mode;
1373 p->recordResidue --;
1374 csp->echo = true;
1375 csp->bcd = false;
1376 p->stati = 04000;
1377 break;
1378
1379 case 033:
1380 sim_debug (DBG_DEBUG, & opc_dev, "%s: Write ASCII\r\n", __func__);
1381 p->isRead = false;
1382 csp->bcd = false;
1383 csp->io_mode = opc_write_mode;
1384 p->recordResidue --;
1385 p->stati = 04000;
1386 break;
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397 case 040:
1398 sim_debug (DBG_DEBUG, & opc_dev, "%s: Reset\r\n", __func__);
1399 p->stati = 04000;
1400
1401
1402
1403
1404
1405
1406
1407 break;
1408
1409 case 043:
1410 sim_debug (DBG_DEBUG, & tape_dev, "%s: Read ASCII unechoed\r\n", __func__);
1411 csp->io_mode = opc_read_mode;
1412 p->recordResidue --;
1413 csp->echo = false;
1414 csp->bcd = false;
1415 p->stati = 04000;
1416 break;
1417
1418 case 051:
1419 sim_debug (DBG_DEBUG, & opc_dev, "%s: Alert\r\n", __func__);
1420 p->isRead = false;
1421 console_putstr ((int) con_unit_idx, "CONSOLE: ALERT\r\n");
1422 console_putchar ((int) con_unit_idx, '\a');
1423 p->stati = 04000;
1424 if (csp->model == m6001 && p->isPCW) {
1425 rc = IOM_CMD_DISCONNECT;
1426 goto done;
1427 }
1428 break;
1429
1430 case 057:
1431
1432
1433
1434
1435 sim_debug (DBG_DEBUG, & opc_dev, "%s: Read ID\r\n", __func__);
1436 p->stati = 04500;
1437 if (csp->model == m6001 && p->isPCW) {
1438 rc = IOM_CMD_DISCONNECT;
1439 goto done;
1440 }
1441 break;
1442
1443 case 060:
1444 sim_debug (DBG_DEBUG, & opc_dev, "%s: Lock\r\n", __func__);
1445 console_putstr ((int) con_unit_idx, "CONSOLE: LOCK\r\n");
1446 p->stati = 04000;
1447 break;
1448
1449 case 063:
1450 sim_debug (DBG_DEBUG, & opc_dev, "%s: Unlock\r\n", __func__);
1451 console_putstr ((int) con_unit_idx, "CONSOLE: UNLOCK\r\n");
1452 p->stati = 04000;
1453 break;
1454
1455 default:
1456 sim_debug (DBG_DEBUG, & opc_dev, "%s: Unknown command 0%o\r\n", __func__, p->IDCW_DEV_CMD);
1457 p->stati = 04501;
1458 rc = IOM_CMD_ERROR;
1459 goto done;
1460 }
1461 goto done;
1462 }
1463
1464
1465 switch (csp->io_mode) {
1466 case opc_no_mode:
1467
1468
1469 if (! tandd_io_mode ())
1470 sim_warn ("%s: Unexpected IOTx\r\n", __func__);
1471 rc = IOM_CMD_ERROR;
1472 goto done;
1473
1474 case opc_read_mode: {
1475 if (csp->tailp != csp->keyboardLineBuffer) {
1476 sim_warn ("%s: Discarding previously buffered input.\r\n", __func__);
1477 }
1478 uint tally = p->DDCW_TALLY;
1479 uint daddr = p->DDCW_ADDR;
1480
1481 if (tally == 0) {
1482 tally = 4096;
1483 }
1484
1485 csp->tailp = csp->keyboardLineBuffer;
1486 csp->readp = csp->keyboardLineBuffer;
1487 csp->startTime = time (NULL);
1488 csp->tally = tally;
1489 csp->daddr = daddr;
1490 csp->unitp = unitp;
1491 csp->chan = (int) chan;
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502 if (csp->autop && (*csp->autop == 030 || *csp->autop == 031)) {
1503
1504
1505
1506
1507
1508 clear_opc_autoinput (ASSUME0, NULL);
1509 ta_flush ();
1510 sim_printf \
1511 ("\r\nScript wedged and abandoned; autoinput and typeahead buffers flushed\r\n");
1512 }
1513 rc = IOM_CMD_PENDING;
1514 goto done;
1515 }
1516
1517 case opc_write_mode: {
1518 uint tally = p->DDCW_TALLY;
1519
1520
1521
1522
1523 if (tally == 0) {
1524 tally = 4096;
1525 }
1526
1527 word36 buf[tally];
1528 iom_indirect_data_service (iomUnitIdx, chan, buf, & tally, false);
1529 p->initiate = false;
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562 char text[tally * 4 + 1];
1563 char * textp = text;
1564 word36 * bufp = buf;
1565 * textp = 0;
1566 #if !defined(__MINGW64__)
1567 newlineOff ();
1568 #endif
1569
1570
1571
1572 int escape_cnt = 0;
1573
1574 while (tally) {
1575 word36 datum = * bufp ++;
1576 tally --;
1577 if (csp->bcd) {
1578
1579 for (int i = 0; i < 6; i ++) {
1580 word36 narrow_char = datum >> 30;
1581
1582 datum = datum << 6;
1583 narrow_char &= 077;
1584 char ch = bcd_code_page [narrow_char];
1585 if (escape_cnt == 2) {
1586 console_putchar ((int) con_unit_idx, ch);
1587 * textp ++ = ch;
1588 escape_cnt = 0;
1589 } else if (ch == '!') {
1590 escape_cnt ++;
1591 } else if (escape_cnt == 1) {
1592 uint lp = (uint)narrow_char;
1593
1594
1595 if (lp == 060 || lp == 075 ) {
1596 p->stati = 04320;
1597 goto done;
1598 }
1599 if (lp == 0)
1600 lp = 1;
1601 if (lp >= 16) {
1602 console_putstr ((int) con_unit_idx, "\f");
1603
1604 } else {
1605 for (uint i = 0; i < lp; i ++) {
1606 console_putstr ((int) con_unit_idx, "\r\n");
1607
1608
1609 }
1610 }
1611 escape_cnt = 0;
1612 } else if (ch == '?') {
1613 escape_cnt = 0;
1614 } else {
1615 console_putchar ((int) con_unit_idx, ch);
1616 * textp ++ = ch;
1617 escape_cnt = 0;
1618 }
1619 }
1620 } else {
1621 for (int i = 0; i < 4; i ++) {
1622 word36 wide_char = datum >> 27;
1623
1624 datum = datum << 9;
1625 char ch = wide_char & 0x7f;
1626 if (ch != 0177 && ch != 0) {
1627 console_putchar ((int) con_unit_idx, ch);
1628 * textp ++ = ch;
1629 }
1630 }
1631 }
1632 }
1633 * textp ++ = 0;
1634
1635
1636 if (csp->autop && * csp->autop == 030) {
1637
1638
1639
1640 size_t expl = strcspn ((char *) (csp->autop + 1), "\030");
1641
1642 if (strncmp (text, (char *) (csp->autop + 1), expl) == 0) {
1643 csp->autop += expl + 2;
1644 sim_activate (& attn_unit[con_unit_idx], ACTIVATE_1SEC);
1645 }
1646 }
1647
1648 if (csp->autop && * csp->autop == 031) {
1649
1650
1651
1652 size_t expl = strcspn ((char *) (csp->autop + 1), "\031");
1653
1654 char needle [expl + 1];
1655 strncpy (needle, (char *) csp->autop + 1, expl);
1656 needle [expl] = 0;
1657 if (strstr (text, needle)) {
1658 csp->autop += expl + 2;
1659 sim_activate (& attn_unit[con_unit_idx], ACTIVATE_1SEC);
1660 }
1661 }
1662 handleRCP (con_unit_idx, text);
1663 #if !defined(__MINGW64__)
1664 newlineOn ();
1665 #endif
1666 p->stati = 04000;
1667 goto done;
1668 }
1669 }
1670
1671 done:
1672 #if defined(LOCKLESS)
1673 unlock_libuv ();
1674 #endif
1675 return rc;
1676 }
1677
1678 static t_stat opc_svc (UNIT * unitp)
1679 {
1680 int con_unit_idx = (int) OPC_UNIT_IDX (unitp);
1681 uint ctlr_port_num = 0;
1682 uint iom_unit_idx = cables->opc_to_iom[con_unit_idx][ctlr_port_num].iom_unit_idx;
1683 uint chan_num = cables->opc_to_iom[con_unit_idx][ctlr_port_num].chan_num;
1684
1685 opc_iom_cmd (iom_unit_idx, chan_num);
1686 return SCPE_OK;
1687 }
1688
1689 static t_stat opc_show_nunits (UNUSED FILE * st, UNUSED UNIT * uptr,
1690 UNUSED int val, UNUSED const void * desc)
1691 {
1692 sim_print ("%d units\r\n", opc_dev.numunits);
1693 return SCPE_OK;
1694 }
1695
1696 static t_stat opc_set_nunits (UNUSED UNIT * uptr, int32 UNUSED value,
1697 const char * cptr, UNUSED void * desc)
1698 {
1699 if (! cptr)
1700 return SCPE_ARG;
1701 int n = atoi (cptr);
1702 if (n < 1 || n > N_OPC_UNITS_MAX)
1703 return SCPE_ARG;
1704 opc_dev.numunits = (uint32) n;
1705 return SCPE_OK;
1706 }
1707
1708 static config_value_list_t cfg_on_off[] =
1709 {
1710 { "off", 0 },
1711 { "on", 1 },
1712 { "disable", 0 },
1713 { "enable", 1 },
1714 { NULL, 0 }
1715 };
1716
1717 static config_value_list_t cfg_model[] =
1718 {
1719 { "m6001", m6001 },
1720 { "m6004", m6004 },
1721 { "m6601", m6601 },
1722 { NULL, 0 }
1723 };
1724
1725 static config_list_t opc_config_list[] =
1726 {
1727 { "autoaccept", 0, 1, cfg_on_off },
1728 { "noto", 0, 1, cfg_on_off },
1729 { "noempty", 0, 1, cfg_on_off },
1730 { "attn_flush", 0, 1, cfg_on_off },
1731 { "model", 1, 0, cfg_model },
1732 { NULL, 0, 0, NULL }
1733 };
1734
1735 static t_stat opc_set_config (UNUSED UNIT * uptr, UNUSED int32 value,
1736 const char * cptr, UNUSED void * desc)
1737 {
1738 int devUnitIdx = (int) OPC_UNIT_IDX (uptr);
1739 opc_state_t * csp = console_state + devUnitIdx;
1740
1741 config_state_t cfg_state = { NULL, NULL };
1742
1743 for (;;)
1744 {
1745 int64_t v;
1746 int rc = cfg_parse (__func__, cptr, opc_config_list,
1747 & cfg_state, & v);
1748 if (rc == -1)
1749 break;
1750
1751 if (rc == -2)
1752 {
1753 cfg_parse_done (& cfg_state);
1754 return SCPE_ARG;
1755 }
1756 const char * p = opc_config_list[rc].name;
1757
1758 if (strcmp (p, "autoaccept") == 0)
1759 {
1760 csp->autoaccept = (int) v;
1761 continue;
1762 }
1763
1764 if (strcmp (p, "noto") == 0)
1765 {
1766 csp->noto = (int) v;
1767 continue;
1768 }
1769
1770 if (strcmp (p, "noempty") == 0)
1771 {
1772 csp->noempty = (int) v;
1773 continue;
1774 }
1775
1776 if (strcmp (p, "attn_flush") == 0)
1777 {
1778 csp->attn_flush = (int) v;
1779 continue;
1780 }
1781
1782 if (strcmp (p, "model") == 0)
1783 {
1784 csp->model = (enum console_model) v;
1785 continue;
1786 }
1787
1788 sim_warn ("error: opc_set_config: Invalid cfg_parse rc <%d>\r\n",
1789 rc);
1790 cfg_parse_done (& cfg_state);
1791 return SCPE_ARG;
1792 }
1793 cfg_parse_done (& cfg_state);
1794 return SCPE_OK;
1795 }
1796
1797 static t_stat opc_show_config (UNUSED FILE * st, UNUSED UNIT * uptr,
1798 UNUSED int val, UNUSED const void * desc)
1799 {
1800 int devUnitIdx = (int) OPC_UNIT_IDX (uptr);
1801 opc_state_t * csp = console_state + devUnitIdx;
1802 sim_msg ("flags : ");
1803 sim_msg ("autoaccept=%d, ", csp->autoaccept);
1804 sim_msg ("noto=%d, ", csp->noto);
1805 sim_msg ("noempty=%d, ", csp->noempty);
1806 sim_msg ("attn_flush=%d", csp->attn_flush);
1807 return SCPE_OK;
1808 }
1809
1810 static t_stat opc_show_device_name (UNUSED FILE * st, UNIT * uptr,
1811 UNUSED int val, UNUSED const void * desc)
1812 {
1813 int n = (int) OPC_UNIT_IDX (uptr);
1814 if (n < 0 || n >= N_OPC_UNITS_MAX)
1815 return SCPE_ARG;
1816 sim_printf("name : OPC%d", n);
1817 return SCPE_OK;
1818 }
1819
1820 static t_stat opc_set_device_name (UNIT * uptr, UNUSED int32 value,
1821 const char * cptr, UNUSED void * desc)
1822 {
1823 int n = (int) OPC_UNIT_IDX (uptr);
1824 if (n < 0 || n >= N_OPC_UNITS_MAX)
1825 return SCPE_ARG;
1826 if (cptr)
1827 {
1828 strncpy (console_state[n].device_name, cptr, MAX_DEV_NAME_LEN-1);
1829 console_state[n].device_name[MAX_DEV_NAME_LEN-1] = 0;
1830 }
1831 else
1832 console_state[n].device_name[0] = 0;
1833 return SCPE_OK;
1834 }
1835
1836 static t_stat opc_set_console_port (UNIT * uptr, UNUSED int32 value,
1837 const char * cptr, UNUSED void * desc)
1838 {
1839 int dev_idx = (int) OPC_UNIT_IDX (uptr);
1840 if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1841 return SCPE_ARG;
1842
1843 if (cptr)
1844 {
1845 int port = atoi (cptr);
1846 if (port < 0 || port > 65535)
1847 return SCPE_ARG;
1848 console_state[dev_idx].console_access.port = port;
1849 if (port != 0)
1850 sim_msg ("[OPC emulation: TELNET server port set to %d]\r\n", (int)port);
1851 }
1852 else
1853 console_state[dev_idx].console_access.port = 0;
1854 return SCPE_OK;
1855 }
1856
1857 static t_stat opc_show_console_port (UNUSED FILE * st, UNIT * uptr,
1858 UNUSED int val, UNUSED const void * desc)
1859 {
1860 int dev_idx = (int) OPC_UNIT_IDX (uptr);
1861 if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1862 return SCPE_ARG;
1863 if (console_state[dev_idx].console_access.port)
1864 sim_printf("port : %d", console_state[dev_idx].console_access.port);
1865 else
1866 sim_printf("port : disabled");
1867 return SCPE_OK;
1868 }
1869
1870 static t_stat opc_set_console_address (UNIT * uptr, UNUSED int32 value,
1871 const char * cptr, UNUSED void * desc)
1872 {
1873 int dev_idx = (int) OPC_UNIT_IDX (uptr);
1874 if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1875 return SCPE_ARG;
1876
1877 if (console_state[dev_idx].console_access.address)
1878 {
1879 FREE (console_state[dev_idx].console_access.address);
1880 console_state[dev_idx].console_access.address = NULL;
1881 }
1882
1883 if (cptr)
1884 {
1885 console_state[dev_idx].console_access.address = strdup (cptr);
1886 if (!console_state[dev_idx].console_access.address)
1887 {
1888 (void)fprintf (stderr, "\rFATAL: Out of memory! Aborting at %s[%s:%d]\r\n",
1889 __func__, __FILE__, __LINE__);
1890 #if defined(USE_BACKTRACE)
1891 # if defined(SIGUSR2)
1892 (void)raise(SIGUSR2);
1893
1894 # endif
1895 #endif
1896 abort();
1897 }
1898 sim_msg ("\r[OPC emulation: OPC%d server address set to %s]\r\n",
1899 dev_idx, console_state[dev_idx].console_access.address);
1900 }
1901
1902 return SCPE_OK;
1903 }
1904
1905 static t_stat opc_show_console_address (UNUSED FILE * st, UNIT * uptr,
1906 UNUSED int val, UNUSED const void * desc)
1907 {
1908 int dev_idx = (int) OPC_UNIT_IDX (uptr);
1909 if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1910 return SCPE_ARG;
1911 if (console_state[dev_idx].console_access.address)
1912 sim_printf("address : %s", console_state[dev_idx].console_access.address);
1913 else
1914 sim_printf("address : any");
1915 return SCPE_OK;
1916 }
1917
1918 static t_stat opc_set_console_pw (UNIT * uptr, UNUSED int32 value,
1919 const char * cptr, UNUSED void * desc)
1920 {
1921 long dev_idx = (int) OPC_UNIT_IDX (uptr);
1922 if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1923 return SCPE_ARG;
1924
1925 if (cptr && (strlen(cptr) > 0))
1926 {
1927 char token[strlen (cptr)+1];
1928 int rc = sscanf (cptr, "%s", token);
1929 if (rc != 1)
1930 return SCPE_ARG;
1931 if (strlen (token) > PW_SIZE)
1932 return SCPE_ARG;
1933 strcpy (console_state[dev_idx].console_access.pw, token);
1934 }
1935 else
1936 {
1937 sim_msg ("no password\r\n");
1938 console_state[dev_idx].console_access.pw[0] = 0;
1939 }
1940
1941 return SCPE_OK;
1942 }
1943
1944 static t_stat opc_show_console_pw (UNUSED FILE * st, UNIT * uptr,
1945 UNUSED int val, UNUSED const void * desc)
1946 {
1947 int dev_idx = (int) OPC_UNIT_IDX (uptr);
1948 if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1949 return SCPE_ARG;
1950 sim_printf("password : %s", console_state[dev_idx].console_access.pw);
1951 return SCPE_OK;
1952 }
1953
1954 static void console_putstr (int conUnitIdx, char * str)
1955 {
1956 size_t l = strlen (str);
1957 for (size_t i = 0; i < l; i ++)
1958 sim_putchar (str[i]);
1959 opc_state_t * csp = console_state + conUnitIdx;
1960 if (csp->console_access.loggedOn)
1961 accessStartWrite (csp->console_access.client, str,
1962 (ssize_t) l);
1963 }
1964
1965 static void consolePutchar0 (int conUnitIdx, char ch) {
1966 opc_state_t * csp = console_state + conUnitIdx;
1967 sim_putchar (ch);
1968 if (csp->console_access.loggedOn)
1969 accessStartWrite (csp->console_access.client, & ch, 1);
1970 }
1971
1972 static void console_putchar (int conUnitIdx, char ch) {
1973 opc_state_t * csp = console_state + conUnitIdx;
1974 if (csp->escapeSequence) {
1975 csp->escapeSequence = false;
1976 if (ch == '1') {
1977 if (csp->carrierPosition >= 1 && csp->carrierPosition <= 256) {
1978 csp->tabStops[csp->carrierPosition] = true;
1979 }
1980 } else if (ch == '2') {
1981 (void)memset (csp->tabStops, 0, sizeof (csp->tabStops));
1982 } else {
1983 sim_warn ("Unrecognized escape sequence \\033\\%03o\r\n", ch);
1984 }
1985 } else if (isprint (ch)) {
1986 consolePutchar0 (conUnitIdx, ch);
1987 csp->carrierPosition ++;
1988 } else if (ch == '\t') {
1989 while (csp->carrierPosition < bufsize - 1) {
1990 consolePutchar0 (conUnitIdx, ' ');
1991 csp->carrierPosition ++;
1992 if (csp->tabStops[csp->carrierPosition])
1993 break;
1994 }
1995 } else if (ch == '\b') {
1996 consolePutchar0 (conUnitIdx, ch);
1997 csp->carrierPosition --;
1998 } else if (ch == '\f' || ch == '\r') {
1999 consolePutchar0 (conUnitIdx, ch);
2000 csp->carrierPosition = 1;
2001 } else if (ch == '\033') {
2002 csp->escapeSequence = true;
2003 } else {
2004 consolePutchar0 (conUnitIdx, ch);
2005 }
2006 }
2007
2008 static void consoleConnectPrompt (uv_tcp_t * client)
2009 {
2010 accessStartWriteStr (client, "password: \r\n");
2011 }
2012
2013 void startRemoteConsole (void)
2014 {
2015 for (int conUnitIdx = 0; conUnitIdx < N_OPC_UNITS_MAX; conUnitIdx ++)
2016 {
2017 console_state[conUnitIdx].console_access.connectPrompt = consoleConnectPrompt;
2018 console_state[conUnitIdx].console_access.connected = NULL;
2019 console_state[conUnitIdx].console_access.useTelnet = true;
2020 #if defined(CONSOLE_FIX)
2021 # if defined(LOCKLESS)
2022 lock_libuv ();
2023 # endif
2024 #endif
2025 uv_open_access (& console_state[conUnitIdx].console_access);
2026 #if defined(CONSOLE_FIX)
2027 # if defined(LOCKLESS)
2028 unlock_libuv ();
2029 # endif
2030 #endif
2031 }
2032 }