home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Boot_Images / 2.11_on_rl02 / pdpsim.tz / pdpsim / scp_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-29  |  5.2 KB  |  201 lines

  1. /* scp_tty.c: Operating system-dependent terminal I/O routines.
  2.  
  3.    Copyright 1994, Robert M Supnik, Digital Equipment Corporation
  4.    Commercial use prohibited
  5.  
  6.    ttinit    -    called once to get initial terminal state
  7.    ttrunstate    -    called to put terminal into run state
  8.    ttcmdstate    -    called to return terminal to command state
  9.    ttclose    -    called once before the simulator exits
  10.    sim_poll_kbd    -    poll for keyboard input, -1 if no input
  11.    sim_type_tty -    output character to terminal
  12.  
  13.    Versions are included for generic (BSD) UNIX and for VMS.
  14.    The generic UNIX version works with LINUX.
  15. */
  16.  
  17. #include "sim_defs.h"
  18. int sim_int_char = 005;                /* interrupt character */
  19.  
  20. /* VMS routines */
  21.  
  22. #ifdef VMS
  23. #define __TTYROUTINES 0
  24. #include <descrip.h>
  25. #include <ttdef.h>
  26. #include <tt2def.h>
  27. #include <iodef.h>
  28. #include <ssdef.h>
  29. #include <starlet.h>
  30. #define EFN 0
  31. unsigned int tty_chan = 0;
  32. typedef struct {
  33.     unsigned short sense_count;
  34.     unsigned char sense_first_char;
  35.     unsigned char sense_reserved;
  36.     unsigned int stat;
  37.     unsigned int stat2; } SENSE_BUF;
  38. typedef struct {
  39.     unsigned short status;
  40.     unsigned short count;
  41.     unsigned int dev_status; } IOSB;
  42. SENSE_BUF cmd_mode = { 0 };
  43. SENSE_BUF run_mode = { 0 };
  44.  
  45. int ttinit (void)
  46. {
  47. unsigned int status;
  48. IOSB iosb;
  49. $DESCRIPTOR (terminal_device, "tt");
  50.  
  51. status = sys$assign (&terminal_device, &tty_chan, 0, 0);
  52. if (status != SS$_NORMAL) return SCPE_TTIERR;
  53. status = sys$qiow (EFN, tty_chan, IO$_SENSEMODE, &iosb, 0, 0,
  54.     &cmd_mode, sizeof (cmd_mode), 0, 0, 0, 0);
  55. if ((status != SS$_NORMAL) || (iosb.status != SS$_NORMAL)) return SCPE_TTIERR;
  56. run_mode = cmd_mode;
  57. run_mode.stat = cmd_mode.stat | TT$M_NOECHO & ~(TT$M_HOSTSYNC | TT$M_TTSYNC);
  58. run_mode.stat2 = cmd_mode.stat2 | TT2$M_PASTHRU;
  59. return SCPE_OK;
  60. }
  61.  
  62. int ttrunstate (void)
  63. {
  64. unsigned int status;
  65. IOSB iosb;
  66.  
  67. status = sys$qiow (EFN, tty_chan, IO$_SETMODE, &iosb, 0, 0,
  68.     &run_mode, sizeof (run_mode), 0, 0, 0, 0);
  69. if ((status != SS$_NORMAL) || (iosb.status != SS$_NORMAL)) return SCPE_TTIERR;
  70. return SCPE_OK;
  71. }
  72.  
  73. int ttcmdstate (void)
  74. {
  75. unsigned int status;
  76. IOSB iosb;
  77.  
  78. status = sys$qiow (EFN, tty_chan, IO$_SETMODE, &iosb, 0, 0,
  79.     &cmd_mode, sizeof (cmd_mode), 0, 0, 0, 0);
  80. if ((status != SS$_NORMAL) || (iosb.status != SS$_NORMAL)) return SCPE_TTIERR;
  81. return SCPE_OK;
  82. }
  83.  
  84. int ttclose (void)
  85. {
  86. return ttcmdstate ();
  87. }
  88.  
  89. int sim_poll_kbd (void)
  90. {
  91. unsigned int status, term[2];
  92. unsigned char buf[4];
  93. IOSB iosb;
  94. SENSE_BUF sense;
  95.  
  96. term[0] = 0; term[1] = 0;
  97. status = sys$qiow (EFN, tty_chan, IO$_SENSEMODE | IO$M_TYPEAHDCNT, &iosb,
  98.     0, 0, &sense, 8, 0, term, 0, 0);
  99. if ((status != SS$_NORMAL) || (iosb.status != SS$_NORMAL)) return SCPE_TTIERR;
  100. if (sense.sense_count == 0) return SCPE_OK;
  101. term[0] = 0; term[1] = 0;
  102. status = sys$qiow (EFN, tty_chan,
  103.     IO$_READLBLK | IO$M_NOECHO | IO$M_NOFILTR | IO$M_TIMED | IO$M_TRMNOECHO,
  104.     &iosb, 0, 0, buf, 1, 0, term, 0, 0);
  105. if ((status != SS$_NORMAL) || (iosb.status != SS$_NORMAL)) return SCPE_OK;
  106. if (buf[0] == sim_int_char) return SCPE_STOP;
  107. return (buf[0] | SCPE_KFLAG);
  108. }
  109.  
  110. int sim_type_tty (char out)
  111. {
  112. unsigned int status;
  113. IOSB iosb;
  114.  
  115. status = sys$qiow (EFN, tty_chan, IO$_WRITELBLK | IO$M_NOFORMAT,
  116.     &iosb, 0, 0, &out, 1, 0, 0, 0, 0);
  117. if ((status != SS$_NORMAL) || (iosb.status != SS$_NORMAL)) return SCPE_TTOERR;
  118. return SCPE_OK;
  119. }
  120. #endif
  121.  
  122. /* UNIX routines */
  123.  
  124. #ifndef __TTYROUTINES
  125. #ifdef LINUX
  126. #include <bsd/sgtty.h>
  127. #else
  128. #include <sgtty.h>
  129. #endif
  130. #include <fcntl.h>
  131.  
  132. struct sgttyb cmdtty,runtty;            /* V6/V7 stty data */
  133. struct tchars cmdtchars,runtchars;        /* V7 editing */
  134. struct ltchars cmdltchars,runltchars;        /* 4.2 BSD editing */
  135. int cmdfl,runfl;                /* TTY flags */
  136.  
  137. int ttinit (void)
  138. {
  139. cmdfl = fcntl (0, F_GETFL, 0);            /* get old flags  and status */
  140. runfl = cmdfl | FNDELAY;
  141. if (ioctl (0, TIOCGETP, &cmdtty) < 0) return SCPE_TTIERR;
  142. if (ioctl (0, TIOCGETC, &cmdtchars) < 0) return SCPE_TTIERR;
  143. if (ioctl (0, TIOCGLTC, &cmdltchars) < 0) return SCPE_TTIERR;
  144. runtty = cmdtty;                /* initial run state */
  145. runtty.sg_flags = cmdtty.sg_flags & ~(ECHO|CRMOD) | CBREAK;
  146. runtchars.t_intrc = sim_int_char;        /* interrupt */
  147. runtchars.t_quitc = 0xFF;            /* no quit */
  148. runtchars.t_startc = 0xFF;            /* no host sync */
  149. runtchars.t_stopc = 0xFF;
  150. runtchars.t_eofc = 0xFF;
  151. runtchars.t_brkc = 0xFF;
  152. runltchars.t_suspc = 0xFF;            /* no specials of any kind */
  153. runltchars.t_dsuspc = 0xFF;
  154. runltchars.t_rprntc = 0xFF;
  155. runltchars.t_flushc = 0xFF;
  156. runltchars.t_werasc = 0xFF;
  157. runltchars.t_lnextc = 0xFF;
  158. return SCPE_OK;                    /* return success */
  159. }
  160.  
  161. int ttrunstate (void)
  162. {
  163. runtchars.t_intrc = sim_int_char;        /* in case changed */
  164. fcntl (0, F_SETFL, runfl);            /* non-block mode */
  165. if (ioctl (0, TIOCSETP, &runtty) < 0) return SCPE_TTIERR;
  166. if (ioctl (0, TIOCSETC, &runtchars) < 0) return SCPE_TTIERR;
  167. if (ioctl (0, TIOCSLTC, &runltchars) < 0) return SCPE_TTIERR;
  168. return SCPE_OK;
  169. }
  170.  
  171. int ttcmdstate (void)
  172. {
  173. fcntl (0, F_SETFL, cmdfl);            /* block mode */
  174. if (ioctl (0, TIOCSETP, &cmdtty) < 0) return SCPE_TTIERR;
  175. if (ioctl (0, TIOCSETC, &cmdtchars) < 0) return SCPE_TTIERR;
  176. if (ioctl (0, TIOCSLTC, &cmdltchars) < 0) return SCPE_TTIERR;
  177. return SCPE_OK;
  178. }
  179.  
  180. int ttclose (void)
  181. {
  182. return ttcmdstate ();
  183. }
  184.  
  185. int sim_poll_kbd (void)
  186. {
  187. int status;
  188. char buf[1];
  189.  
  190. status = read (0, buf, 1);
  191. if (status != 1) return SCPE_OK;
  192. else return (buf[0] | SCPE_KFLAG);
  193. }
  194.  
  195. int sim_type_tty (char out)
  196. {
  197. write (1, &out, 1);
  198. return SCPE_OK;
  199. }
  200. #endif
  201.