* h8500-tdep.c (initialize_h8500_tdep, large_command):
[binutils-gdb.git] / gdb / ser-go32.c
1 /* Remote serial interface for local (hardwired) serial ports for GO32.
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include <sys/dos.h>
23
24 /* This is unused for now. We just return a placeholder. */
25 struct go32_ttystate
26 {
27 int bogus;
28 };
29
30 typedef struct
31 {
32 short jmp_op;
33 short signature;
34 short version;
35 short buffer_start;
36 short buffer_end;
37 short getp;
38 short putp;
39 short iov;
40 }
41 ASYNC_STRUCT;
42
43 static int go32_open PARAMS ((serial_t scb, const char *name));
44 static void go32_raw PARAMS ((serial_t scb));
45 static int go32_readchar PARAMS ((serial_t scb, int timeout));
46 static int go32_setbaudrate PARAMS ((serial_t scb, int rate));
47 static int go32_write PARAMS ((serial_t scb, const char *str, int len));
48 static void go32_close PARAMS ((serial_t scb));
49 static serial_ttystate go32_get_tty_state PARAMS ((serial_t scb));
50 static int go32_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
51 static char *aptr PARAMS ((short p));
52 static ASYNC_STRUCT *getivec PARAMS ((int which));
53 static int dos_async_init PARAMS ((int port));
54 static void dos_async_tx PARAMS ((const char c));
55 static int dos_async_rx PARAMS (());
56 static int dosasync_read PARAMS ((int fd, char *buf, int len, int timeout));
57 static int dosasync_write PARAMS ((int fd, const char *buf, int len));
58
59 #define SIGNATURE 0x4154
60 #define VERSION 1
61 #define OFFSET 0x104
62
63 #define peek(a,b) (*(unsigned short *)(0xe0000000 + (a)*16 + (b)))
64
65 static volatile ASYNC_STRUCT *async;
66 static int iov;
67 #define com_rb iov
68 #define com_tb iov
69 #define com_ier iov+1
70 #define com_ifr iov+2
71 #define com_bfr iov+3
72 #define com_mcr iov+4
73 #define com_lsr iov+5
74 #define com_msr iov+6
75
76 static char *
77 aptr (p)
78 short p;
79 {
80 return (char *) ((unsigned) async - OFFSET + p);
81 }
82
83 static ASYNC_STRUCT *
84 getivec (int which)
85 {
86 ASYNC_STRUCT *a;
87
88 if (peek (0, which * 4) != OFFSET)
89 return 0;
90
91 a = (ASYNC_STRUCT *) (0xe0000000 + peek (0, which * 4 + 2) * 16 + peek (0, which * 4));
92
93 if (a->signature != SIGNATURE)
94 return 0;
95
96 if (a->version != VERSION)
97 return 0;
98
99 return a;
100 }
101
102 static int
103 dos_async_init (port)
104 int port;
105 {
106 switch (port)
107 {
108 case 1:
109 async = getivec (12);
110 break;
111 case 2:
112 async = getivec (11);
113 break;
114 default:
115 return 0;
116 }
117
118 if (!async)
119 {
120 error ("GDB cannot connect to asynctsr program, check that it is installed\n\
121 and that serial I/O is not being redirected (perhaps by NFS)\n\n\
122 example configuration:\n\
123 C> mode com%d:9600,n,8,1,p\n\
124 C> asynctsr %d\n\
125 C> gdb \n", port, port);
126 }
127
128 iov = async->iov;
129 outportb (com_ier, 0x0f);
130 outportb (com_bfr, 0x03);
131 outportb (com_mcr, 0x0b);
132 async->getp = async->putp = async->buffer_start;
133
134 return 1;
135 }
136
137 static void
138 dos_async_tx (c)
139 const char c;
140 {
141 while (~inportb (com_lsr) & 0x20)
142 ;
143 outportb (com_tb, c);
144 }
145
146 #define dos_async_ready() (async->getp != async->putp)
147
148 static int
149 dos_async_rx ()
150 {
151 char rv;
152
153 while (!dos_async_ready ())
154 {
155 if (kbhit ())
156 {
157 printf_unfiltered ("abort!\n");
158 return 0;
159 }
160 }
161
162 rv = *aptr (async->getp++);
163
164 if (async->getp >= async->buffer_end)
165 async->getp = async->buffer_start;
166
167 return rv;
168 }
169
170
171 static int
172 dosasync_read (fd, buf, len, timeout)
173 int fd;
174 char *buf;
175 int len;
176 int timeout;
177 {
178 long now, then;
179 int i;
180
181 time (&now);
182 then = now + timeout;
183
184 for (i = 0; i < len; i++)
185 {
186 if (timeout)
187 {
188 while (!dos_async_ready ())
189 {
190 time (&now);
191 if (now >= then && timeout > 0)
192 return i;
193 }
194 }
195 *buf++ = dos_async_rx ();
196 }
197 return len;
198 }
199
200 static int
201 dosasync_write (fd, buf, len)
202 int fd;
203 const char *buf;
204 int len;
205 {
206 int l;
207
208 for (l = 0; l < len; l++)
209 dos_async_tx (*buf++);
210
211 return len;
212 }
213
214 static int
215 go32_open (scb, name)
216 serial_t scb;
217 const char *name;
218 {
219 int port;
220
221 if (strncasecmp (name, "com", 3) != 0)
222 {
223 errno = ENOENT;
224 return -1;
225 }
226
227 port = name[3] - '0';
228
229 if ((port != 1) && (port != 2))
230 {
231 errno = ENOENT;
232 return -11;
233 }
234
235 scb->fd = dos_async_init (port);
236 if (!scb->fd)
237 return -1;
238
239 return 0;
240 }
241
242 static int
243 go32_noop (scb)
244 serial_t scb;
245 {
246 return 0;
247 }
248
249 static void
250 go32_raw (scb)
251 serial_t scb;
252 {
253 /* Always in raw mode */
254 }
255
256 static int
257 go32_readchar (scb, timeout)
258 serial_t scb;
259 int timeout;
260 {
261 char buf;
262
263
264 /* Shortcut for polling */
265 if (timeout == 0)
266 {
267 if (dos_async_ready ())
268 {
269 return dos_async_rx ();
270 }
271 return SERIAL_TIMEOUT;
272 }
273
274 if (dosasync_read (scb->fd, &buf, 1, timeout))
275 return buf;
276 else
277 return SERIAL_TIMEOUT;
278 }
279
280 /* go32_{get set}_tty_state() are both dummys to fill out the function
281 vector. Someday, they may do something real... */
282
283 static serial_ttystate
284 go32_get_tty_state (scb)
285 serial_t scb;
286 {
287 struct go32_ttystate *state;
288
289 state = (struct go32_ttystate *) xmalloc (sizeof *state);
290
291 return (serial_ttystate) state;
292 }
293
294 static int
295 go32_set_tty_state (scb, ttystate)
296 serial_t scb;
297 serial_ttystate ttystate;
298 {
299 return 0;
300 }
301
302 static int
303 go32_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
304 serial_t scb;
305 serial_ttystate new_ttystate;
306 serial_ttystate old_ttystate;
307 {
308 return 0;
309 }
310
311 static void
312 go32_print_tty_state (scb, ttystate)
313 serial_t scb;
314 serial_ttystate ttystate;
315 {
316 /* Nothing to print. */
317 return;
318 }
319
320 static int
321 go32_setbaudrate (scb, rate)
322 serial_t scb;
323 int rate;
324 {
325 return 0;
326 }
327
328 static int
329 go32_write (scb, str, len)
330 serial_t scb;
331 const char *str;
332 int len;
333 {
334 dosasync_write (scb->fd, str, len);
335
336 return 0;
337 }
338
339 static void
340 go32_close (scb)
341 serial_t scb;
342 {
343 }
344
345 static struct serial_ops go32_ops =
346 {
347 "hardwire",
348 0,
349 go32_open,
350 go32_close,
351 go32_readchar,
352 go32_write,
353 go32_noop, /* flush output */
354 go32_noop, /* flush input */
355 go32_noop, /* send break -- currently used only for nindy */
356 go32_raw,
357 go32_get_tty_state,
358 go32_set_tty_state,
359 go32_print_tty_state,
360 go32_noflush_set_tty_state,
361 go32_setbaudrate,
362 };
363
364 void
365 _initialize_ser_go32 ()
366 {
367 serial_add_interface (&go32_ops);
368 }