]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/cris/rvdummy.c
1 /* Test-driver for the remote-virtual-component simulator framework
2 for GDB, the GNU Debugger.
4 Copyright 2006-2021 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Avoid any problems whatsoever building this program if we're not
22 also building hardware support. */
26 main (int argc
, char *argv
[])
37 #include "libiberty.h"
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
50 #ifdef HAVE_SYS_TIME_H
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
60 /* Not guarded in dv-sockser.c, so why here. */
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
64 #include <sys/socket.h>
72 RV_MBOX_HANDLE_CMD
= 5,
77 enum opts
{ OPT_PORT
= 1, OPT_TIMEOUT
, OPT_VERBOSE
};
79 struct option longopts
[] =
81 {"port", required_argument
, NULL
, OPT_PORT
},
82 {"timeout", required_argument
, NULL
, OPT_TIMEOUT
},
83 {"verbose", no_argument
, NULL
, OPT_VERBOSE
},
88 time_t timeout
= 30000;
89 char *progname
= "(unknown)";
92 /* Required forward-declarations. */
93 static void handle_input_file (int, char *);
95 /* Set up a "server" listening to the port in PORT for a raw TCP
96 connection. Return a file descriptor for the connection or -1 on
99 static int setupsocket (void)
104 struct sockaddr_in sa_in
;
105 struct sockaddr_in from
;
108 memset (&from
, 0, len
);
109 memset (&sa_in
, 0, sizeof (sa_in
));
111 s
= socket (AF_INET
, SOCK_STREAM
, 0);
115 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof reuse
) != 0)
118 sa_in
.sin_port
= htons (port
);
119 sa_in
.sin_family
= AF_INET
;
121 if (bind (s
, (struct sockaddr
*) & sa_in
, sizeof sa_in
) < 0)
124 if (listen (s
, 1) < 0)
127 return accept (s
, (struct sockaddr
*) &from
, &len
);
130 /* Basic host-to-little-endian 32-bit value. Could use the BFD
131 machinery, but let's avoid it for this only dependency. */
134 h2le32 (unsigned char *dest
, unsigned int val
)
137 dest
[1] = (val
>> 8) & 255;
138 dest
[2] = (val
>> 16) & 255;
139 dest
[3] = (val
>> 24) & 255;
142 /* Send a blob of data. */
145 send_output (int fd
, unsigned char *buf
, int nbytes
)
149 ssize_t written
= write (fd
, buf
, nbytes
);
152 fprintf (stderr
, "%s: write to socket failed: %s\n",
153 progname
, strerror (errno
));
160 /* Receive a blob of data, NBYTES large. Compare to the first NCOMP
161 bytes of BUF; if not a match, write error message to stderr and
162 exit (2). Else put it in buf. */
165 expect_input (int fd
, unsigned char *buf
, int nbytes
, int ncomp
)
170 for (i
= 0; i
< nbytes
; i
++)
177 r
= read (fd
, &byt
, 1);
179 while (r
<= 0 && (r
== 0 || errno
== EAGAIN
));
183 fprintf (stderr
, "%s: read from socket failed: %s",
184 progname
, strerror (errno
));
188 if (i
< ncomp
&& byt
!= buf
[i
])
191 fprintf (stderr
, "%s: unexpected input,\n ", progname
);
193 fprintf (stderr
, "nothing,");
195 for (j
= 0; j
< i
; j
++)
196 fprintf (stderr
, "%02x", buf
[j
]);
197 fprintf (stderr
, "\nthen %02x instead of %02x\n", byt
, buf
[i
]);
205 /* Handle everything about a nil-terminated line of input.
206 Call exit (2) on error with error text on stderr. */
209 handle_input (int fd
, char *buf
, char *fname
, int lineno
)
215 static unsigned char bytes
[1024];
218 memset (bytes
, 0, sizeof bytes
);
226 /* Comment characters and empty lines. */
227 case 0: case '!': case '#':
230 /* Include another file. */
232 handle_input_file (fd
, s
);
235 /* Raw input (to be expected). */
240 sscanf (s
, "%02x%n", &data
, &n
);
243 bytes
[nbytes
++] = data
;
246 expect_input (fd
, bytes
, nbytes
, nbytes
);
250 for (i
= 0; i
< nbytes
; i
++)
251 printf ("%02x", bytes
[i
]);
256 /* Raw output (to be written). */
261 sscanf (s
, "%02x%n", &data
, &n
);
265 bytes
[nbytes
++] = data
;
271 send_output (fd
, bytes
, nbytes
);
275 for (i
= 0; i
< nbytes
; i
++)
276 printf ("%02x", bytes
[i
]);
281 /* Read a register. */
285 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
286 if (n
< 0 || s
[n
] != 0)
290 bytes
[2] = RV_READ_CMD
;
291 h2le32 (bytes
+ 3, addr
);
292 expect_input (fd
, bytes
, 11, 7);
293 h2le32 (bytes
+ 7, data
);
294 send_output (fd
, bytes
, 11);
296 printf ("r,%x,%x\n", addr
, data
);
300 /* Write a register. */
304 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
305 if (n
< 0 || s
[n
] != 0)
309 bytes
[2] = RV_WRITE_CMD
;
310 h2le32 (bytes
+ 3, addr
);
311 h2le32 (bytes
+ 7, data
);
312 expect_input (fd
, bytes
, 11, 11);
313 send_output (fd
, bytes
, 11);
315 printf ("w,%x,%x\n", addr
, data
);
319 /* Wait for some milliseconds. */
324 sscanf (s
, "%d%n", &del
, &n
);
325 if (n
< 0 || s
[n
] != 0 || del
== 0)
328 to
.tv_sec
= del
/ 1000;
329 to
.tv_usec
= (del
% 1000) * 1000;
331 if (select (0, NULL
, NULL
, NULL
, &to
) != 0)
333 fprintf (stderr
, "%s: problem waiting for %d ms:\n %s\n",
334 progname
, del
, strerror (errno
));
338 printf ("t,%d\n", del
);
342 /* Expect a watchdog command. */
348 bytes
[2] = RV_WATCHDOG_CMD
;
349 expect_input (fd
, bytes
, 3, 3);
354 /* Send an IRQ notification. */
356 sscanf (s
, "%x%n", &data
, &n
);
357 if (n
< 0 || s
[n
] != 0)
361 bytes
[2] = RV_IRQ_CMD
;
362 h2le32 (bytes
+ 3, data
);
363 send_output (fd
, bytes
, 7);
365 printf ("I,%x\n", data
);
368 /* DMA store (to CPU). */
372 sscanf (s
, "%x,%n", &addr
, &n
);
374 if (n
< 0 || s
[n
] == 0)
380 sscanf (s
, "%02x%n", &data
, &n
);
384 bytes
[11 + nbytes
++] = data
;
391 h2le32 (bytes
, nbytes
+ 11);
392 bytes
[2] = RV_MEM_WR_CMD
;
393 h2le32 (bytes
+ 3, addr
);
394 h2le32 (bytes
+ 7, nbytes
);
395 send_output (fd
, bytes
, nbytes
+ 11);
398 printf ("s,%x,", addr
);
399 for (i
= 0; i
< nbytes
; i
++)
400 printf ("%02x", bytes
[i
]);
406 /* DMA load (from CPU). */
410 sscanf (s
, "%x,%n", &addr
, &n
);
412 if (n
< 0 || s
[n
] == 0)
418 sscanf (s
, "%02x%n", &data
, &n
);
422 bytes
[11 + nbytes
++] = data
;
429 h2le32 (bytes
, nbytes
+ 11);
432 bytes
[2] = RV_MEM_RD_CMD
;
433 h2le32 (bytes
+ 3, addr
);
434 h2le32 (bytes
+ 7, nbytes
);
435 send_output (fd
, bytes
, 11);
436 bytes
[0] = (nbytes
+ 11) & 255;
437 bytes
[1] = ((nbytes
+ 11) >> 8) & 255;
438 expect_input (fd
, bytes
, nbytes
+ 11, nbytes
+ 11);
441 printf ("l,%x,", addr
);
442 for (i
= 0; i
< nbytes
; i
++)
443 printf ("%02x", bytes
[i
]);
451 fprintf (stderr
, "%s: invalid command line in %s:%d:\n %s",
452 progname
, fname
, lineno
, strerror (errno
));
457 /* Loop over the contents of FNAME, using handle_input to parse each line.
458 Errors to stderr, exit (2). */
461 handle_input_file (int fd
, char *fname
)
463 static char buf
[2048] = {0};
465 FILE *f
= fopen (fname
, "r");
469 fprintf (stderr
, "%s: problem opening %s: %s\n",
470 progname
, fname
, strerror (errno
));
474 /* Let's cut the buffer short, so we always get a newline. */
475 while (fgets (buf
, sizeof (buf
) - 1, f
) != NULL
)
477 buf
[strlen (buf
) - 1] = 0;
479 handle_input (fd
, buf
, fname
, lineno
);
486 main (int argc
, char *argv
[])
494 while ((optc
= getopt_long (argc
, argv
, "", longopts
, NULL
)) != -1)
498 port
= atoi (optarg
);
502 timeout
= (time_t) atoi (optarg
);
513 fprintf (stderr
, "%s: problem setting up the connection: %s\n",
514 progname
, strerror (errno
));
518 for (i
= optind
; i
< argc
; i
++)
519 handle_input_file (fd
, argv
[i
]);
521 /* FIXME: option-controlled test for remaining input? */