* ser-go32.c: First cut at adapting to new serial interface.
[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   a1 = getivec(12);
84   a2 = getivec(11);
85   async = 0;
86   if (a1)
87     async = a1;
88   if (a2)
89     async = a2;
90   if (a1 && a2)
91   {
92     if (a1 < a2)
93       async = a1;
94     else
95       async = a2;
96   }
97   if (async == 0)
98   {
99     error("GDB can not connect to asynctsr program, check that it is installed\n\
100 and that serial I/O is not being redirected (perhaps by NFS)\n\n\
101 example configuration:\n\
102 C> mode com2:9600,n,8,1,p\n\
103 C> asynctsr 2\n\
104 C> gdb \n");
105
106   }
107   iov = async->iov;
108   outportb(com_ier, 0x0f);
109   outportb(com_bfr, 0x03);
110   outportb(com_mcr, 0x0b);
111   async->getp = async->putp = async->buffer_start;
112   
113   if (iov > 0x300)
114     return 1;
115   else
116     return 2;
117 }
118
119 static void
120 dos_async_tx(c)
121      const char c;
122 {
123   while (~inportb(com_lsr) & 0x20);
124
125   outportb(com_tb, c);
126 }
127
128 static int
129 dos_async_ready()
130 {
131   return (async->getp != async->putp);
132 }
133
134 static int
135 dos_async_rx()
136 {
137   char rv;
138
139   while (!dos_async_ready())
140     if (kbhit())
141       {
142         printf("abort!\n");
143         return 0;
144       }
145
146   rv = *aptr(async->getp++);
147   if (async->getp >= async->buffer_end)
148     async->getp = async->buffer_start;
149
150   return rv;
151 }
152
153 static int
154 dosasync_read (fd, buf, len, timeout)
155      int fd;
156      char *buf;
157      int len;
158      int timeout;
159 {
160   long now, then;
161   int l = len;
162
163   time (&now);
164   then = now + timeout;
165
166   while (l--)
167     {
168       if (timeout)
169         {
170           while (!dos_async_ready())
171             {
172               time (&now);
173               if (now >= then)
174                 return len - l - 1;
175             }
176         }
177       *buf++ = dos_async_rx();
178     }
179
180   return len;
181 }
182
183 static int
184 dosasync_write(fd, buf, len)
185      int fd;
186      const char *buf;
187      int len;
188 {
189   int l = len;
190
191   while (l--)
192     dos_async_tx (*buf++);
193
194   return len;
195 }
196
197 static int
198 go32_open (scb, name)
199      serial_t scb;
200      const char *name;
201 {
202   scb->fd = dos_async_init();
203   if (scb->fd)
204     return 1;
205
206   return 0;
207 }
208
209 static void
210 go32_raw (scb)
211      serial_t scb;
212 {
213   /* Always in raw mode */
214 }
215
216 static int
217 go32_readchar (scb, timeout)
218      serial_t scb;
219      int timeout;
220 {
221   char buf;
222
223   if (dosasync_read(scb->fd, &buf, 1, timeout))  
224     return buf;
225   else
226     return -2; /* Timeout, I guess */
227 }
228
229 static int
230 go32_setbaudrate (scb, rate)
231      serial_t scb;
232      int rate;
233 {
234   return 0;
235 }
236
237 static int
238 go32_write (scb, str, len)
239      serial_t scb;
240      const char *str;
241      int len;
242 {
243   dosasync_write(scb->fd, str, len);
244 }
245
246 static void
247 go32_close ()
248 {
249 }
250
251 static void
252 go32_restore (scb)
253      serial_t scb;
254 {
255 }
256
257 static struct serial_ops go32_ops =
258 {
259   "hardwire",
260   0,
261   go32_open,
262   go32_close,
263   go32_readchar,
264   go32_write,
265   go32_raw,
266   go32_restore,
267   go32_setbaudrate
268 };
269
270 _initialize_ser_go32 ()
271 {
272   serial_add_interface (&go32_ops);
273 }