]>
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
[])
32 /* This must come before any other includes. */
36 #include "libiberty.h"
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
51 #ifdef HAVE_SYS_SELECT_H
52 #include <sys/select.h>
57 /* Not guarded in dv-sockser.c, so why here. */
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
61 #include <sys/socket.h>
69 RV_MBOX_HANDLE_CMD
= 5,
74 enum opts
{ OPT_PORT
= 1, OPT_TIMEOUT
, OPT_VERBOSE
};
76 struct option longopts
[] =
78 {"port", required_argument
, NULL
, OPT_PORT
},
79 {"timeout", required_argument
, NULL
, OPT_TIMEOUT
},
80 {"verbose", no_argument
, NULL
, OPT_VERBOSE
},
85 time_t timeout
= 30000;
86 char *progname
= "(unknown)";
89 /* Required forward-declarations. */
90 static void handle_input_file (int, char *);
92 /* Set up a "server" listening to the port in PORT for a raw TCP
93 connection. Return a file descriptor for the connection or -1 on
96 static int setupsocket (void)
101 struct sockaddr_in sa_in
;
102 struct sockaddr_in from
;
105 memset (&from
, 0, len
);
106 memset (&sa_in
, 0, sizeof (sa_in
));
108 s
= socket (AF_INET
, SOCK_STREAM
, 0);
112 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof reuse
) != 0)
115 sa_in
.sin_port
= htons (port
);
116 sa_in
.sin_family
= AF_INET
;
118 if (bind (s
, (struct sockaddr
*) & sa_in
, sizeof sa_in
) < 0)
121 if (listen (s
, 1) < 0)
124 return accept (s
, (struct sockaddr
*) &from
, &len
);
127 /* Basic host-to-little-endian 32-bit value. Could use the BFD
128 machinery, but let's avoid it for this only dependency. */
131 h2le32 (unsigned char *dest
, unsigned int val
)
134 dest
[1] = (val
>> 8) & 255;
135 dest
[2] = (val
>> 16) & 255;
136 dest
[3] = (val
>> 24) & 255;
139 /* Send a blob of data. */
142 send_output (int fd
, unsigned char *buf
, int nbytes
)
146 ssize_t written
= write (fd
, buf
, nbytes
);
149 fprintf (stderr
, "%s: write to socket failed: %s\n",
150 progname
, strerror (errno
));
157 /* Receive a blob of data, NBYTES large. Compare to the first NCOMP
158 bytes of BUF; if not a match, write error message to stderr and
159 exit (2). Else put it in buf. */
162 expect_input (int fd
, unsigned char *buf
, int nbytes
, int ncomp
)
167 for (i
= 0; i
< nbytes
; i
++)
174 r
= read (fd
, &byt
, 1);
176 while (r
<= 0 && (r
== 0 || errno
== EAGAIN
));
180 fprintf (stderr
, "%s: read from socket failed: %s",
181 progname
, strerror (errno
));
185 if (i
< ncomp
&& byt
!= buf
[i
])
188 fprintf (stderr
, "%s: unexpected input,\n ", progname
);
190 fprintf (stderr
, "nothing,");
192 for (j
= 0; j
< i
; j
++)
193 fprintf (stderr
, "%02x", buf
[j
]);
194 fprintf (stderr
, "\nthen %02x instead of %02x\n", byt
, buf
[i
]);
202 /* Handle everything about a nil-terminated line of input.
203 Call exit (2) on error with error text on stderr. */
206 handle_input (int fd
, char *buf
, char *fname
, int lineno
)
212 static unsigned char bytes
[1024];
215 memset (bytes
, 0, sizeof bytes
);
223 /* Comment characters and empty lines. */
224 case 0: case '!': case '#':
227 /* Include another file. */
229 handle_input_file (fd
, s
);
232 /* Raw input (to be expected). */
237 sscanf (s
, "%02x%n", &data
, &n
);
240 bytes
[nbytes
++] = data
;
243 expect_input (fd
, bytes
, nbytes
, nbytes
);
247 for (i
= 0; i
< nbytes
; i
++)
248 printf ("%02x", bytes
[i
]);
253 /* Raw output (to be written). */
258 sscanf (s
, "%02x%n", &data
, &n
);
262 bytes
[nbytes
++] = data
;
268 send_output (fd
, bytes
, nbytes
);
272 for (i
= 0; i
< nbytes
; i
++)
273 printf ("%02x", bytes
[i
]);
278 /* Read a register. */
282 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
283 if (n
< 0 || s
[n
] != 0)
287 bytes
[2] = RV_READ_CMD
;
288 h2le32 (bytes
+ 3, addr
);
289 expect_input (fd
, bytes
, 11, 7);
290 h2le32 (bytes
+ 7, data
);
291 send_output (fd
, bytes
, 11);
293 printf ("r,%x,%x\n", addr
, data
);
297 /* Write a register. */
301 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
302 if (n
< 0 || s
[n
] != 0)
306 bytes
[2] = RV_WRITE_CMD
;
307 h2le32 (bytes
+ 3, addr
);
308 h2le32 (bytes
+ 7, data
);
309 expect_input (fd
, bytes
, 11, 11);
310 send_output (fd
, bytes
, 11);
312 printf ("w,%x,%x\n", addr
, data
);
316 /* Wait for some milliseconds. */
321 sscanf (s
, "%d%n", &del
, &n
);
322 if (n
< 0 || s
[n
] != 0 || del
== 0)
325 to
.tv_sec
= del
/ 1000;
326 to
.tv_usec
= (del
% 1000) * 1000;
328 if (select (0, NULL
, NULL
, NULL
, &to
) != 0)
330 fprintf (stderr
, "%s: problem waiting for %d ms:\n %s\n",
331 progname
, del
, strerror (errno
));
335 printf ("t,%d\n", del
);
339 /* Expect a watchdog command. */
345 bytes
[2] = RV_WATCHDOG_CMD
;
346 expect_input (fd
, bytes
, 3, 3);
351 /* Send an IRQ notification. */
353 sscanf (s
, "%x%n", &data
, &n
);
354 if (n
< 0 || s
[n
] != 0)
358 bytes
[2] = RV_IRQ_CMD
;
359 h2le32 (bytes
+ 3, data
);
360 send_output (fd
, bytes
, 7);
362 printf ("I,%x\n", data
);
365 /* DMA store (to CPU). */
369 sscanf (s
, "%x,%n", &addr
, &n
);
371 if (n
< 0 || s
[n
] == 0)
377 sscanf (s
, "%02x%n", &data
, &n
);
381 bytes
[11 + nbytes
++] = data
;
388 h2le32 (bytes
, nbytes
+ 11);
389 bytes
[2] = RV_MEM_WR_CMD
;
390 h2le32 (bytes
+ 3, addr
);
391 h2le32 (bytes
+ 7, nbytes
);
392 send_output (fd
, bytes
, nbytes
+ 11);
395 printf ("s,%x,", addr
);
396 for (i
= 0; i
< nbytes
; i
++)
397 printf ("%02x", bytes
[i
]);
403 /* DMA load (from CPU). */
407 sscanf (s
, "%x,%n", &addr
, &n
);
409 if (n
< 0 || s
[n
] == 0)
415 sscanf (s
, "%02x%n", &data
, &n
);
419 bytes
[11 + nbytes
++] = data
;
426 h2le32 (bytes
, nbytes
+ 11);
429 bytes
[2] = RV_MEM_RD_CMD
;
430 h2le32 (bytes
+ 3, addr
);
431 h2le32 (bytes
+ 7, nbytes
);
432 send_output (fd
, bytes
, 11);
433 bytes
[0] = (nbytes
+ 11) & 255;
434 bytes
[1] = ((nbytes
+ 11) >> 8) & 255;
435 expect_input (fd
, bytes
, nbytes
+ 11, nbytes
+ 11);
438 printf ("l,%x,", addr
);
439 for (i
= 0; i
< nbytes
; i
++)
440 printf ("%02x", bytes
[i
]);
448 fprintf (stderr
, "%s: invalid command line in %s:%d:\n %s",
449 progname
, fname
, lineno
, strerror (errno
));
454 /* Loop over the contents of FNAME, using handle_input to parse each line.
455 Errors to stderr, exit (2). */
458 handle_input_file (int fd
, char *fname
)
460 static char buf
[2048] = {0};
462 FILE *f
= fopen (fname
, "r");
466 fprintf (stderr
, "%s: problem opening %s: %s\n",
467 progname
, fname
, strerror (errno
));
471 /* Let's cut the buffer short, so we always get a newline. */
472 while (fgets (buf
, sizeof (buf
) - 1, f
) != NULL
)
474 buf
[strlen (buf
) - 1] = 0;
476 handle_input (fd
, buf
, fname
, lineno
);
483 main (int argc
, char *argv
[])
491 while ((optc
= getopt_long (argc
, argv
, "", longopts
, NULL
)) != -1)
495 port
= atoi (optarg
);
499 timeout
= (time_t) atoi (optarg
);
510 fprintf (stderr
, "%s: problem setting up the connection: %s\n",
511 progname
, strerror (errno
));
515 for (i
= optind
; i
< argc
; i
++)
516 handle_input_file (fd
, argv
[i
]);
518 /* FIXME: option-controlled test for remaining input? */