A large commit.
[pdp8.git] / sw / dumprest / original / comm.c
1
2 #ifdef PC
3 PORT port_handle;
4
5
6 void (interrupt EN_FAR *(old_vect23))(void);
7 void (interrupt EN_FAR *(old_vect1b))(void);
8
9 /* Trap break etc to allow program to terminate on PC */
10 void interrupt EN_FAR en_ctrl_brk()
11 {
12 terminate = 1;
13 }
14 void interrupt EN_FAR en_ctrl_c() {
15 terminate = 1;
16 }
17
18 int ser_read(int fd,char *buf,int bufsize)
19 {
20 int bufcnt = 0;
21 int t;
22
23 t = com_getc(&port_handle);
24 while (t != -1 && bufcnt < bufsize) {
25 *(buf++) = t;
26 t = com_getc(&port_handle);
27 bufcnt++;
28 }
29 return bufcnt;
30 }
31
32 int ser_write(int fd,char *buf,int bufsize)
33 {
34 int bufcnt = bufsize;
35
36 while (bufsize-- > 0) {
37 while (com_putc(*buf,&port_handle) != 0);
38 buf++;
39 }
40 return bufcnt;
41 }
42
43 void cleanup(void)
44 {
45 while (!terminate && port_handle.tx_cnt != 0)
46 ;
47 if (terminate)
48 com_clear_que(RX | TX, &port_handle);
49 suspend(50);
50 printf("Parity err %d framing err %d overrun err %d dropped %d\n",
51 port_handle.parity_errs,port_handle.framing_errs,
52 port_handle.overrun_errs,port_handle.rx_overflow);
53 com_port_destroy(&port_handle);
54 end_clock();
55 setvect(0x23, old_vect23); /* restore ^C, ^Break */
56 setvect(0x1b, old_vect1b);
57 }
58
59 int init_comm(char *cport,long baud,int two_stop)
60 {
61 printf("Port: %s, Baud rate: %i\n", port, baud);
62 int port;
63 int base,irq;
64
65 if (sscanf(cport,"%x,%x",&base,&irq) == 2) {
66 port = COM1;
67 com_config(COM1,base,irq);
68 } else {
69 if (strlen(cport) != 4 || strnicmp(cport,"COM",3) != 0 ||
70 (cport[3] > '4' || cport[3] < '1')) {
71 printf("Illegal port %s, must be com1 to com4\n",cport);
72 exit(1);
73 }
74 switch(cport[3])
75 {
76 case '1': port = COM1; break;
77 case '2': port = COM2; break;
78 case '3': port = COM3; break;
79 case '4': port = COM4; break;
80 default:
81 printf("Illegal port %s, must be com1 to com4\n",cport);
82 exit(1);
83 }
84 }
85 if( com_port_create(port, baud,'N',8,1 + two_stop, 4096, 4096,
86 &port_handle) < 0 )
87 {
88 printf("Cannot create COM port");
89 exit(1);
90 }
91
92 init_clock(0x3333);
93
94 /*--------- take over 0x23 and 0x1B to suppress CTRL-C and Break ---------*/
95 old_vect23 = getvect(0x23);
96 old_vect1b = getvect(0x1b);
97 setvect(0x23, en_ctrl_c);
98 setvect(0x1b, en_ctrl_brk);
99 atexit(cleanup);
100
101 com_clear_que(RX | TX, &port_handle);
102
103 if (port_handle.uart_type == ID_16550) {
104 fifo_enable(&port_handle);
105 com_fifo_trigger(RX_TRIG_4,&port_handle);
106 }
107
108 return 0;
109 }
110
111
112 int getchar_nonblock(void) {
113 if (kbhit()) {
114 return getch();
115 } else{
116 return 0;
117 }
118 }
119
120 #else
121
122 #define ser_read(a,b,c) read(a,b,c)
123 #define ser_write(a,b,c) write(a,b,c)
124
125 #ifdef _STDC_
126 int init_comm(char *, long, int);
127 #endif
128
129 int init_comm(port,baud,two_stop)
130 char *port;
131 long baud;
132 int two_stop;
133
134 {
135 printf("Port: %s, Baud rate: %i\n", port, baud);
136
137 struct termios tios; /* Serial port TERMIO structure */
138 int port_fd;
139
140 port_fd = open(port,O_RDWR,0);
141 if (port_fd < 0) {
142 fprintf (stderr,"init_comm: Open failed on port '%s': ",port);
143 perror("");
144 exit(1);
145 }
146
147 /* Set to 8 bit selected baud no parity with no special processing */
148 if (tcgetattr(port_fd,&tios) < 0) {
149 perror("init_comm: tcgetattr failed");
150 exit(1);
151 }
152 tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL | baud;
153 if (two_stop)
154 tios.c_cflag |= CSTOPB;
155 tios.c_iflag = 0;
156 tios.c_lflag = 0;
157 tios.c_oflag = 0;
158 tios.c_cc[VMIN] = 0;
159 tios.c_cc[VTIME] = 1;
160
161 if (cfsetispeed(&tios,baud) != 0)
162 printf("init_comm: set ispeed failed\n");
163 if (cfsetospeed(&tios,baud) != 0)
164 printf("init_comm: set ospeed failed\n");
165
166 if (tcsetattr(port_fd,TCSANOW,&tios) < 0) {
167 perror("init_comm: tcsetattr failed");
168 exit(1);
169 }
170 tcflush(port_fd,TCIOFLUSH);
171 return(port_fd);
172 }
173
174 struct termios orig_tios; /* Serial port TERMIO structure */
175
176 void restore_termio() {
177 if (tcsetattr(STDIN_FILENO,TCSANOW,&orig_tios) < 0) {
178 perror("restore_termio: tcsetattr failed");
179 exit(1);
180 }
181 printf("Terminal settings restored\n");
182
183 exit(0);
184 }
185
186 void set_input_nonblock() {
187 struct termios tios; /* Serial port TERMIO structure */
188
189 signal(SIGINT, restore_termio);
190 if (tcgetattr(STDIN_FILENO,&orig_tios) < 0) {
191 perror("set_input_nonblock: tcgetattr failed");
192 exit(1);
193 }
194 if (tcgetattr(STDIN_FILENO,&tios) < 0) {
195 perror("tcgetattr failed");
196 exit(1);
197 }
198 tios.c_cc[VMIN] = 0;
199 tios.c_cc[VTIME] = 0;
200 tios.c_lflag &= ~(ICANON | ECHO);
201 if (tcsetattr(STDIN_FILENO,TCSANOW,&tios) < 0) {
202 perror("set_input_nonblock: tcsetattr failed");
203 exit(1);
204 }
205 }
206
207 int getchar_nonblock(void) {
208 static int init = 0;
209 if (!init) {
210 set_input_nonblock();
211 init = 1;
212 }
213
214 return getchar();
215 }
216 #endif