Work to reduce the interrupts-off duration when running in DOS.
[external/binutils.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 #define disable() asm("cli")
25 #define enable() asm("sti")
26
27
28 struct go32_ttystate
29   {
30     int bogus;
31   };
32 typedef struct
33   {
34     short jmp_op;
35     short signature;
36     short version;
37     short buffer_start;
38     short buffer_end;
39     short getp;
40     short putp;
41     short iov;
42     short count;
43     short overflow;
44     short buffer_size;
45     short ovflushes;
46   }
47 ASYNC_STRUCT;
48
49 #define AOFF_JMP_OP             0
50 #define AOFF_SIGNATURE          2
51 #define AOFF_VERSION            4
52 #define AOFF_BUFFER_START       6
53 #define AOFF_BUFFER_END         8
54 #define AOFF_GETP               10
55 #define AOFF_PUTP               12
56 #define AOFF_IOV                14
57 #define AOFF_COUNT              16
58 #define AOFF_OVERFLOW           18
59 #define AOFF_BUFFER_SIZE        20
60 #define AOFF_OVFLUSHES          22
61
62
63 static ASYNC_STRUCT a;          /* Copy in our mem of the struct */
64 static long aindex;             /* index into dos space of struct */
65
66 static int go32_open PARAMS ((serial_t scb, const char *name));
67 static void go32_raw PARAMS ((serial_t scb));
68 static int go32_readchar PARAMS ((serial_t scb, int timeout));
69 static int go32_setbaudrate PARAMS ((serial_t scb, int rate));
70 static int go32_write PARAMS ((serial_t scb, const char *str, int len));
71 static void go32_close PARAMS ((serial_t scb));
72 static serial_ttystate go32_get_tty_state PARAMS ((serial_t scb));
73 static int go32_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
74 static unsigned char aptr PARAMS ((short p));
75 static unsigned long getivec PARAMS ((int which));
76 static int dos_async_init PARAMS ((int port));
77 static void dos_async_tx PARAMS ((const char c));
78 static int dos_async_rx PARAMS (());
79 static int dosasync_read PARAMS ((int fd, char *buf, int len, int timeout));
80 static int dosasync_write PARAMS ((int fd, const char *buf, int len));
81
82 #define SIGNATURE 0x4154
83 #define VERSION 1
84 #define OFFSET 0x104
85
86 char packet[50];
87 int packet_len;
88 int packet_idx;
89
90 unsigned char bb;
91 unsigned short sb;
92 unsigned long sl;
93
94 #define SET_BYTE(x,y)   { char _buf = y;dosmemput(&_buf,1, x);}
95 #define SET_WORD(x,y)   { short _buf = y;dosmemput(&_buf,2, x);}
96 #define GET_BYTE(x)     ( dosmemget((x),1,&bb), bb)
97
98
99 #define GET_LONG(x)     ( dosmemget((x),4,&sl), sl)
100
101 static
102 unsigned short
103 GET_WORD (x)
104 {
105   unsigned short sb;
106   dosmemget ((x), 2, &sb);
107   return sb;
108 }
109
110 static int iov[2];
111
112 #define com_rb(n)       iov[n]
113 #define com_tb(n)       iov[n]
114 #define com_ier(n)      iov[n]+1
115 #define com_ifr(n)      iov[n]+2
116 #define com_bfr(n)      iov[n]+3
117 #define com_mcr(n)      iov[n]+4
118 #define com_lsr(n)      iov[n]+5
119 #define com_msr(n)      iov[n]+6
120
121 static unsigned char
122 aptr (p)
123      short p;
124 {
125   return GET_BYTE (aindex - OFFSET + p);
126 }
127
128 static unsigned long
129 getivec (int which)
130 {
131   long tryaindex;
132
133   if (GET_WORD (which * 4) != OFFSET)
134     return 0;
135
136   /* Find out where in memory this lives */
137   tryaindex = GET_WORD (which * 4 + 2) * 16 + GET_WORD (which * 4);
138
139   if (GET_WORD (tryaindex + 2) != SIGNATURE)
140     return 0;
141   if (GET_WORD (tryaindex + 4) != VERSION)
142     return 0;
143   return tryaindex;
144 }
145
146 static int
147 dos_async_init (port)
148      int port;
149 {
150   switch (port)
151     {
152     case 1:
153       aindex = getivec (12);
154       break;
155     case 2:
156       aindex = getivec (11);
157       break;
158     default:
159       return 0;
160     }
161
162   if (!aindex)
163     {
164       error ("GDB cannot connect to asynctsr program, check that it is installed\n\
165 and that serial I/O is not being redirected (perhaps by NFS)\n\n\
166 example configuration:\n\
167 C> mode com%d:9600,n,8,1,p\n\
168 C> asynctsr %d\n\
169 C> gdb \n", port, port);
170     }
171
172   iov[0] = GET_WORD (aindex + AOFF_IOV);
173   outportb (com_ier (0), 0x0f);
174   outportb (com_bfr (0), 0x03);
175   outportb (com_mcr (0), 0x0b);
176   return 1;
177 }
178
179 static void
180 dos_async_tx (c)
181      const char c;
182 {
183   while (~inportb (com_lsr (0)) & 0x20)
184     ;
185   outportb (com_tb (0), c);
186 }
187
188 static int
189 dos_async_ready ()
190 {
191   int ret;
192
193   if (packet_idx < packet_len)
194     return 1;
195
196   disable ();
197 #if RDY_CNT
198   ret = GET_WORD (aindex + AOFF_COUNT);
199 #else
200   ret = GET_WORD (aindex + AOFF_GETP) != GET_WORD (aindex + AOFF_PUTP);
201 #endif
202   enable ();
203   return ret;
204
205
206 }
207
208 static int
209 dos_async_rx ()
210 {
211   char rv;
212   short idx;
213
214   while (1)
215     {
216       if (packet_idx < packet_len)
217         {
218           char x = packet[packet_idx++];
219           return x;
220         }
221       while (!dos_async_ready ())
222         {
223           if (kbhit ())
224             {
225               printf_unfiltered ("abort!\n");
226               return 0;
227             }
228         }
229       disable ();
230       {
231         /* Sometimes we can read more than one char at a time
232          from the buffer, which is good, cause it'll mean
233          less time with interrupts turned off, which means
234          less dropped characters */
235
236         /* We only do the simplest case here - not bothering with
237            wrap around */
238         int len;
239
240         int getp = GET_WORD (aindex + AOFF_GETP);
241         int putp = GET_WORD (aindex + AOFF_PUTP);
242         int endb = GET_WORD (aindex + AOFF_BUFFER_END);
243         int startb = GET_WORD (aindex + AOFF_BUFFER_START);
244
245         /* We'd like to grab to the end of the the input,
246          but it may have wrapped, so max is to the end
247          of the buffer */
248
249         if (putp > endb || putp < getp)
250           putp = endb;
251
252         /* Work out the length of the suck */
253         len = putp - getp;
254
255         /* But only suck as many as we can hold in one go */
256         if (len > sizeof (packet))
257           len = sizeof (packet);
258
259         dosmemget (aindex - OFFSET + getp, len, packet);
260
261         packet_len = len;
262         packet_idx = 0;
263
264         if (getp + len >= endb)
265           {
266             getp = startb;
267           }
268         else
269           {
270             getp = getp + len;
271           }
272
273         SET_WORD (aindex + AOFF_GETP, getp);
274         SET_WORD (aindex + AOFF_COUNT, GET_WORD (aindex + AOFF_COUNT) - len);
275       }
276       enable ();
277     }
278 }
279
280
281 static int
282 dosasync_read (fd, buf, len, timeout)
283      int fd;
284      char *buf;
285      int len;
286      int timeout;
287 {
288   long now, then;
289   int i;
290   int its = 0;
291   time (&now);
292   then = now + timeout;
293
294   for (i = 0; i < len; i++)
295     {
296       if (timeout)
297         {
298           while (!dos_async_ready ())
299             {
300               time (&now);
301               if (now >= then && timeout > 0 && its > 1000)
302                 return i;
303               its++;
304             }
305         }
306       *buf++ = dos_async_rx ();
307     }
308   return len;
309 }
310
311 static int
312 dosasync_write (fd, buf, len)
313      int fd;
314      const char *buf;
315      int len;
316 {
317   int l;
318
319   for (l = 0; l < len; l++)
320     dos_async_tx (*buf++);
321
322   return len;
323 }
324
325 static int
326 go32_open (scb, name)
327      serial_t scb;
328      const char *name;
329 {
330   int port;
331
332   if (strncasecmp (name, "com", 3) != 0)
333     {
334       errno = ENOENT;
335       return -1;
336     }
337
338   port = name[3] - '0';
339
340   if ((port != 1) && (port != 2))
341     {
342       errno = ENOENT;
343       return -11;
344     }
345
346   scb->fd = dos_async_init (port);
347   if (!scb->fd)
348     return -1;
349
350   return 0;
351 }
352
353 static int
354 go32_noop (scb)
355      serial_t scb;
356 {
357   return 0;
358 }
359
360 static void
361 go32_raw (scb)
362      serial_t scb;
363 {
364   /* Always in raw mode */
365 }
366
367 static int
368 go32_readchar (scb, timeout)
369      serial_t scb;
370      int timeout;
371 {
372   char buf;
373
374   /* Shortcut for polling */
375   if (timeout == 0)
376     {
377       if (dos_async_ready ())
378         {
379           return dos_async_rx ();
380         }
381       return SERIAL_TIMEOUT;
382     }
383
384   if (dosasync_read (scb->fd, &buf, 1, timeout))
385     return buf;
386   else
387     return SERIAL_TIMEOUT;
388 }
389
390 /* go32_{get set}_tty_state() are both dummys to fill out the function
391    vector.  Someday, they may do something real... */
392
393 static serial_ttystate
394 go32_get_tty_state (scb)
395      serial_t scb;
396 {
397   struct go32_ttystate *state;
398
399   state = (struct go32_ttystate *) xmalloc (sizeof *state);
400
401   return (serial_ttystate) state;
402 }
403
404 static int
405 go32_set_tty_state (scb, ttystate)
406      serial_t scb;
407      serial_ttystate ttystate;
408 {
409   return 0;
410 }
411
412 static int
413 go32_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
414      serial_t scb;
415      serial_ttystate new_ttystate;
416      serial_ttystate old_ttystate;
417 {
418   return 0;
419 }
420
421 static void
422 go32_print_tty_state (scb, ttystate)
423      serial_t scb;
424      serial_ttystate ttystate;
425 {
426   /* Nothing to print.  */
427   return;
428 }
429
430 static int
431 go32_setbaudrate (scb, rate)
432      serial_t scb;
433      int rate;
434 {
435   return 0;
436 }
437
438 static int
439 go32_write (scb, str, len)
440      serial_t scb;
441      const char *str;
442      int len;
443 {
444   dosasync_write (scb->fd, str, len);
445
446   return 0;
447 }
448
449 static void
450 go32_close (scb)
451      serial_t scb;
452 {
453 }
454
455 static struct serial_ops go32_ops =
456 {
457   "hardwire",
458   0,
459   go32_open,
460   go32_close,
461   go32_readchar,
462   go32_write,
463   go32_noop,                    /* flush output */
464   go32_noop,                    /* flush input */
465   go32_noop,                    /* send break -- currently used only for nindy */
466   go32_raw,
467   go32_get_tty_state,
468   go32_set_tty_state,
469   go32_print_tty_state,
470   go32_noflush_set_tty_state,
471   go32_setbaudrate,
472 };
473
474 void
475 _initialize_ser_go32 ()
476 {
477   serial_add_interface (&go32_ops);
478 }