* ser-go32.c: Format. (go32_open): Use proper return value.
[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 SIGNATURE 0x4154
25 #define VERSION 1
26 #define OFFSET 0x104
27
28 #define peek(a,b) (*(unsigned short *)(0xe0000000 + (a)*16 + (b)))
29
30 typedef struct {
31   short jmp_op;
32   short signature;
33   short version;
34   short buffer_start;
35   short buffer_end;
36   short getp;
37   short putp;
38   short iov;
39 } ASYNC_STRUCT;
40
41 static ASYNC_STRUCT *async;
42 static int iov;
43 #define com_rb  iov
44 #define com_tb  iov
45 #define com_ier iov+1
46 #define com_ifr iov+2
47 #define com_bfr iov+3
48 #define com_mcr iov+4
49 #define com_lsr iov+5
50 #define com_msr iov+6
51
52 static int fd;
53
54 static char *
55 aptr(p)
56      short p;
57 {
58   return (char *)((unsigned)async - OFFSET + p);
59 }
60
61 static ASYNC_STRUCT *
62 getivec(int which)
63 {
64   ASYNC_STRUCT *a;
65
66   if (peek(0, which*4) != OFFSET)
67     return 0;
68   a = (ASYNC_STRUCT *)(0xe0000000 + peek(0, which*4+2)*16 + peek(0, which*4));
69
70   if (a->signature != SIGNATURE)
71     return 0;
72   if (a->version != VERSION)
73     return 0;
74   return a;
75 }
76
77 static int
78 dos_async_init()
79 {
80   int i;
81   ASYNC_STRUCT *a1;
82   ASYNC_STRUCT *a2;
83
84   a1 = getivec(12);
85   a2 = getivec(11);
86   async = 0;
87   if (a1)
88     async = a1;
89   if (a2)
90     async = a2;
91
92   if (a1 && a2)
93     {
94       if (a1 < a2)
95         async = a1;
96       else
97         async = a2;
98     }
99
100   if (async == 0)
101     {
102       error("GDB can not connect to asynctsr program, check that it is installed\n\
103 and that serial I/O is not being redirected (perhaps by NFS)\n\n\
104 example configuration:\n\
105 C> mode com2:9600,n,8,1,p\n\
106 C> asynctsr 2\n\
107 C> gdb \n");
108
109     }
110
111   iov = async->iov;
112   outportb(com_ier, 0x0f);
113   outportb(com_bfr, 0x03);
114   outportb(com_mcr, 0x0b);
115   async->getp = async->putp = async->buffer_start;
116   
117   if (iov > 0x300)
118     return 1;
119   else
120     return 2;
121 }
122
123 static void
124 dos_async_tx(c)
125      const char c;
126 {
127   while (~inportb(com_lsr) & 0x20);
128
129   outportb(com_tb, c);
130 }
131
132 static int
133 dos_async_ready()
134 {
135   return (async->getp != async->putp);
136 }
137
138 static int
139 dos_async_rx()
140 {
141   char rv;
142
143   while (!dos_async_ready())
144     if (kbhit())
145       {
146         printf("abort!\n");
147         return 0;
148       }
149
150   rv = *aptr(async->getp++);
151   if (async->getp >= async->buffer_end)
152     async->getp = async->buffer_start;
153
154   return rv;
155 }
156
157 static int
158 dosasync_read (fd, buf, len, timeout)
159      int fd;
160      char *buf;
161      int len;
162      int timeout;
163 {
164   long now, then;
165   int l = len;
166
167   time (&now);
168   then = now + timeout;
169
170   while (l--)
171     {
172       if (timeout)
173         {
174           while (!dos_async_ready())
175             {
176               time (&now);
177               if (now >= then)
178                 return len - l - 1;
179             }
180         }
181       *buf++ = dos_async_rx();
182     }
183
184   return len;
185 }
186
187 static int
188 dosasync_write(fd, buf, len)
189      int fd;
190      const char *buf;
191      int len;
192 {
193   int l = len;
194
195   while (l--)
196     dos_async_tx (*buf++);
197
198   return len;
199 }
200
201 static int
202 go32_open (scb, name)
203      serial_t scb;
204      const char *name;
205 {
206   scb->fd = dos_async_init();
207   if (!scb->fd)
208     return 1;
209
210   return 0;
211 }
212
213 static void
214 go32_raw (scb)
215      serial_t scb;
216 {
217   /* Always in raw mode */
218 }
219
220 static int
221 go32_readchar (scb, timeout)
222      serial_t scb;
223      int timeout;
224 {
225   char buf;
226
227   if (dosasync_read(scb->fd, &buf, 1, timeout))  
228     return buf;
229   else
230     return -2; /* Timeout, I guess */
231 }
232
233 static int
234 go32_setbaudrate (scb, rate)
235      serial_t scb;
236      int rate;
237 {
238   return 0;
239 }
240
241 static int
242 go32_write (scb, str, len)
243      serial_t scb;
244      const char *str;
245      int len;
246 {
247   dosasync_write(scb->fd, str, len);
248 }
249
250 static void
251 go32_close ()
252 {
253 }
254
255 static void
256 go32_restore (scb)
257      serial_t scb;
258 {
259 }
260
261 static struct serial_ops go32_ops =
262 {
263   "hardwire",
264   0,
265   go32_open,
266   go32_close,
267   go32_readchar,
268   go32_write,
269   go32_raw,
270   go32_restore,
271   go32_setbaudrate
272 };
273
274 _initialize_ser_go32 ()
275 {
276   serial_add_interface (&go32_ops);
277 }