aboutsummaryrefslogtreecommitdiff
path: root/gdb/ser-go32.c
blob: b1b53318b746f9cdf5304b6f67fa483d55ab41d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* Remote serial interface for local (hardwired) serial ports for GO32.
   Copyright 1992, 1993 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "defs.h"
#include "serial.h"
#include <sys/dos.h>

#define disable() asm("cli")
#define enable() asm("sti")


struct go32_ttystate
  {
    int bogus;
  };
typedef struct
  {
    short jmp_op;
    short signature;
    short version;
    short buffer_start;
    short buffer_end;
    short getp;
    short putp;
    short iov;
    short count;
    short overflow;
    short buffer_size;
    short ovflushes;
  }
ASYNC_STRUCT;

#define AOFF_JMP_OP 		0
#define AOFF_SIGNATURE 		2
#define AOFF_VERSION 		4
#define AOFF_BUFFER_START 	6
#define AOFF_BUFFER_END 	8
#define AOFF_GETP 		10
#define AOFF_PUTP 		12
#define AOFF_IOV 		14
#define AOFF_COUNT 		16
#define AOFF_OVERFLOW 		18
#define AOFF_BUFFER_SIZE 	20
#define AOFF_OVFLUSHES 		22


static ASYNC_STRUCT a;		/* Copy in our mem of the struct */
static long aindex;		/* index into dos space of struct */

static int go32_open PARAMS ((serial_t scb, const char *name));
static void go32_raw PARAMS ((serial_t scb));
static int go32_readchar PARAMS ((serial_t scb, int timeout));
static int go32_setbaudrate PARAMS ((serial_t scb, int rate));
static int go32_write PARAMS ((serial_t scb, const char *str, int len));
static void go32_close PARAMS ((serial_t scb));
static serial_ttystate go32_get_tty_state PARAMS ((serial_t scb));
static int go32_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
static unsigned char aptr PARAMS ((short p));
static unsigned long getivec PARAMS ((int which));
static int dos_async_init PARAMS ((int port));
static void dos_async_tx PARAMS ((const char c));
static int dos_async_rx PARAMS (());
static int dosasync_read PARAMS ((int fd, char *buf, int len, int timeout));
static int dosasync_write PARAMS ((int fd, const char *buf, int len));

#define SIGNATURE 0x4154
#define VERSION 1
#define OFFSET 0x104

char packet[50];
int packet_len;
int packet_idx;

unsigned char bb;
unsigned short sb;
unsigned long sl;

#define SET_BYTE(x,y)   { char _buf = y;dosmemput(&_buf,1, x);}
#define SET_WORD(x,y)   { short _buf = y;dosmemput(&_buf,2, x);}
#define GET_BYTE(x)     ( dosmemget((x),1,&bb), bb)


#define GET_LONG(x)     ( dosmemget((x),4,&sl), sl)

static
unsigned short
GET_WORD (x)
{
  unsigned short sb;
  dosmemget ((x), 2, &sb);
  return sb;
}

static int iov[2];

#define com_rb(n)	iov[n]
#define com_tb(n)	iov[n]
#define com_ier(n)	iov[n]+1
#define com_ifr(n)	iov[n]+2
#define com_bfr(n)	iov[n]+3
#define com_mcr(n)	iov[n]+4
#define com_lsr(n)	iov[n]+5
#define com_msr(n)	iov[n]+6

static unsigned char
aptr (p)
     short p;
{
  return GET_BYTE (aindex - OFFSET + p);
}

static unsigned long
getivec (int which)
{
  long tryaindex;

  if (GET_WORD (which * 4) != OFFSET)
    return 0;

  /* Find out where in memory this lives */
  tryaindex = GET_WORD (which * 4 + 2) * 16 + GET_WORD (which * 4);

  if (GET_WORD (tryaindex + 2) != SIGNATURE)
    return 0;
  if (GET_WORD (tryaindex + 4) != VERSION)
    return 0;
  return tryaindex;
}

static int
dos_async_init (port)
     int port;
{
  switch (port)
    {
    case 1:
      aindex = getivec (12);
      break;
    case 2:
      aindex = getivec (11);
      break;
    default:
      return 0;
    }

  if (!aindex)
    {
      error ("GDB cannot connect to asynctsr program, check that it is installed\n\
and that serial I/O is not being redirected (perhaps by NFS)\n\n\
example configuration:\n\
C> mode com%d:9600,n,8,1,p\n\
C> asynctsr %d\n\
C> gdb \n", port, port);
    }

  iov[0] = GET_WORD (aindex + AOFF_IOV);
  outportb (com_ier (0), 0x0f);
  outportb (com_bfr (0), 0x03);
  outportb (com_mcr (0), 0x0b);
  return 1;
}

static void
dos_async_tx (c)
     const char c;
{
  while (~inportb (com_lsr (0)) & 0x20)
    ;
  outportb (com_tb (0), c);
}

static int
dos_async_ready ()
{
  int ret;

  if (packet_idx < packet_len)
    return 1;

  disable ();
#if RDY_CNT
  ret = GET_WORD (aindex + AOFF_COUNT);
#else
  ret = GET_WORD (aindex + AOFF_GETP) != GET_WORD (aindex + AOFF_PUTP);
#endif
  enable ();
  return ret;


}

static int
dos_async_rx ()
{
  char rv;
  short idx;

  while (1)
    {
      if (packet_idx < packet_len)
	{
	  char x = packet[packet_idx++];
	  return x;
	}
      while (!dos_async_ready ())
	{
	  if (kbhit ())
	    {
	      printf_unfiltered ("abort!\n");
	      return 0;
	    }
	}
      disable ();
      {
	/* Sometimes we can read more than one char at a time
	 from the buffer, which is good, cause it'll mean
	 less time with interrupts turned off, which means
	 less dropped characters */

	/* We only do the simplest case here - not bothering with
 	   wrap around */
	int len;

	int getp = GET_WORD (aindex + AOFF_GETP);
	int putp = GET_WORD (aindex + AOFF_PUTP);
	int endb = GET_WORD (aindex + AOFF_BUFFER_END);
	int startb = GET_WORD (aindex + AOFF_BUFFER_START);

	/* We'd like to grab to the end of the the input,
	 but it may have wrapped, so max is to the end
	 of the buffer */

	if (putp > endb || putp < getp)
	  putp = endb;

	/* Work out the length of the suck */
	len = putp - getp;

	/* But only suck as many as we can hold in one go */
	if (len > sizeof (packet))
	  len = sizeof (packet);

	dosmemget (aindex - OFFSET + getp, len, packet);

	packet_len = len;
	packet_idx = 0;

	if (getp + len >= endb)
	  {
	    getp = startb;
	  }
	else
	  {
	    getp = getp + len;
	  }

	SET_WORD (aindex + AOFF_GETP, getp);
	SET_WORD (aindex + AOFF_COUNT, GET_WORD (aindex + AOFF_COUNT) - len);
      }
      enable ();
    }
}


static int
dosasync_read (fd, buf, len, timeout)
     int fd;
     char *buf;
     int len;
     int timeout;
{
  long now, then;
  int i;
  int its = 0;
  time (&now);
  then = now + timeout;

  for (i = 0; i < len; i++)
    {
      if (timeout)
	{
	  while (!dos_async_ready ())
	    {
	      time (&now);
	      if (now >= then && timeout > 0 && its > 1000)
		return i;
	      its++;
	    }
	}
      *buf++ = dos_async_rx ();
    }
  return len;
}

static int
dosasync_write (fd, buf, len)
     int fd;
     const char *buf;
     int len;
{
  int l;

  for (l = 0; l < len; l++)
    dos_async_tx (*buf++);

  return len;
}

static int
go32_open (scb, name)
     serial_t scb;
     const char *name;
{
  int port;

  if (strncasecmp (name, "com", 3) != 0)
    {
      errno = ENOENT;
      return -1;
    }

  port = name[3] - '0';

  if ((port != 1) && (port != 2))
    {
      errno = ENOENT;
      return -11;
    }

  scb->fd = dos_async_init (port);
  if (!scb->fd)
    return -1;

  return 0;
}

static int
go32_noop (scb)
     serial_t scb;
{
  return 0;
}

static void
go32_raw (scb)
     serial_t scb;
{
  /* Always in raw mode */
}

static int
go32_readchar (scb, timeout)
     serial_t scb;
     int timeout;
{
  char buf;

  /* Shortcut for polling */
  if (timeout == 0)
    {
      if (dos_async_ready ())
	{
	  return dos_async_rx ();
	}
      return SERIAL_TIMEOUT;
    }

  if (dosasync_read (scb->fd, &buf, 1, timeout))
    return buf;
  else
    return SERIAL_TIMEOUT;
}

/* go32_{get set}_tty_state() are both dummys to fill out the function
   vector.  Someday, they may do something real... */

static serial_ttystate
go32_get_tty_state (scb)
     serial_t scb;
{
  struct go32_ttystate *state;

  state = (struct go32_ttystate *) xmalloc (sizeof *state);

  return (serial_ttystate) state;
}

static int
go32_set_tty_state (scb, ttystate)
     serial_t scb;
     serial_ttystate ttystate;
{
  return 0;
}

static int
go32_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
     serial_t scb;
     serial_ttystate new_ttystate;
     serial_ttystate old_ttystate;
{
  return 0;
}

static void
go32_print_tty_state (scb, ttystate)
     serial_t scb;
     serial_ttystate ttystate;
{
  /* Nothing to print.  */
  return;
}

static int
go32_setbaudrate (scb, rate)
     serial_t scb;
     int rate;
{
  return 0;
}

static int
go32_write (scb, str, len)
     serial_t scb;
     const char *str;
     int len;
{
  dosasync_write (scb->fd, str, len);

  return 0;
}

static void
go32_close (scb)
     serial_t scb;
{
}

static struct serial_ops go32_ops =
{
  "hardwire",
  0,
  go32_open,
  go32_close,
  go32_readchar,
  go32_write,
  go32_noop,			/* flush output */
  go32_noop,			/* flush input */
  go32_noop,			/* send break -- currently used only for nindy */
  go32_raw,
  go32_get_tty_state,
  go32_set_tty_state,
  go32_print_tty_state,
  go32_noflush_set_tty_state,
  go32_setbaudrate,
};

void
_initialize_ser_go32 ()
{
  serial_add_interface (&go32_ops);
}