2006-01-24 06:10:41 +08:00
|
|
|
/* main.c --- main function for stand-alone M32C simulator.
|
|
|
|
|
2024-01-12 23:30:44 +08:00
|
|
|
Copyright (C) 2005-2024 Free Software Foundation, Inc.
|
2006-01-24 06:10:41 +08:00
|
|
|
Contributed by Red Hat, Inc.
|
|
|
|
|
|
|
|
This file is part of the GNU simulators.
|
|
|
|
|
2007-08-24 22:30:15 +08:00
|
|
|
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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2006-01-24 06:10:41 +08:00
|
|
|
|
2007-08-24 22:30:15 +08:00
|
|
|
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.
|
2006-01-24 06:10:41 +08:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2007-08-24 22:30:15 +08:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2006-01-24 06:10:41 +08:00
|
|
|
|
2021-05-02 06:05:23 +08:00
|
|
|
/* This must come before any other includes. */
|
|
|
|
#include "defs.h"
|
2006-01-24 06:10:41 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
2008-06-07 03:18:15 +08:00
|
|
|
#include <sys/types.h>
|
sim, sim/{m32c,ppc,rl78}: Use getopt_long
Because of a Libiberty hack, getopt on GNU libc (2.25 or earlier) is
currently unusable on sim, causing a regression on CentOS 7.
This is caused as follows:
1. If HAVE_DECL_GETOPT is defined (getopt declaration with known prototype
is detected while configuration), a declaration of getopt in
"include/getopt.h" is suppressed.
The author started to define HAVE_DECL_GETOPT in sim with the commit
340aa4f6872c ("sim: Check known getopt definition existence").
2. GNU libc (2.25 or earlier)'s <unistd.h> includes <getopt.h> with a
special purpose macro defined to declare only getopt function but due
to include path (not tested while configuration), it causes <unistd.h>
to include Libiberty's "include/getopt.h".
3. If both 1. and 2. are satisfied, despite that <unistd.h> tries to
declare getopt by including <getopt.h>, "include/getopt.h" does not do
so, causing getopt function undeclared.
Getting rid of "include/getopt.h" (e.g. renaming this header file) is the
best solution to avoid hacking but as a short-term solution, this commit
replaces getopt with getopt_long under sim/.
2022-10-17 23:47:23 +08:00
|
|
|
#include <getopt.h>
|
2009-08-14 12:24:30 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#ifdef HAVE_NETINET_TCP_H
|
|
|
|
#define HAVE_networking
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_networking
|
2008-06-07 03:18:15 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2009-08-14 12:24:30 +08:00
|
|
|
#endif
|
2008-06-07 03:18:15 +08:00
|
|
|
|
|
|
|
|
2006-01-24 06:10:41 +08:00
|
|
|
#include "bfd.h"
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "load.h"
|
|
|
|
#include "trace.h"
|
2008-06-07 03:18:15 +08:00
|
|
|
#ifdef TIMER_A
|
|
|
|
#include "int.h"
|
|
|
|
#include "timer_a.h"
|
|
|
|
#endif
|
2006-01-24 06:10:41 +08:00
|
|
|
|
2009-08-14 12:24:30 +08:00
|
|
|
#ifdef HAVE_networking
|
2008-06-07 03:18:15 +08:00
|
|
|
extern int m32c_console_ofd;
|
|
|
|
extern int m32c_console_ifd;
|
2009-08-14 12:24:30 +08:00
|
|
|
#endif
|
2008-06-07 03:18:15 +08:00
|
|
|
|
|
|
|
int m32c_disassemble = 0;
|
2006-01-24 06:10:41 +08:00
|
|
|
static unsigned int cycles = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
done (int exit_code)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
stack_heap_stats ();
|
|
|
|
mem_usage_stats ();
|
|
|
|
printf ("insns: %14s\n", comma (cycles));
|
|
|
|
}
|
|
|
|
exit (exit_code);
|
|
|
|
}
|
|
|
|
|
2009-08-14 12:24:30 +08:00
|
|
|
#ifdef HAVE_networking
|
2008-06-07 03:18:15 +08:00
|
|
|
static void
|
|
|
|
setup_tcp_console (char *portname)
|
|
|
|
{
|
|
|
|
int port = atoi (portname);
|
|
|
|
struct sockaddr_in address;
|
|
|
|
int isocket;
|
|
|
|
socklen_t as;
|
|
|
|
unsigned char *a;
|
|
|
|
|
|
|
|
if (port < 1024)
|
|
|
|
{
|
|
|
|
printf ("invalid port number %d\n", port);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
printf ("waiting for tcp console on port %d\n", port);
|
|
|
|
|
|
|
|
memset (&address, 0, sizeof (address));
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_port = htons (port);
|
|
|
|
|
|
|
|
isocket = socket (AF_INET, SOCK_STREAM, 0);
|
2010-05-27 06:40:24 +08:00
|
|
|
if (isocket == -1)
|
2008-06-07 03:18:15 +08:00
|
|
|
{
|
|
|
|
perror ("socket");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind (isocket, (struct sockaddr *) &address, sizeof (address)))
|
|
|
|
{
|
|
|
|
perror ("bind");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
listen (isocket, 2);
|
|
|
|
|
|
|
|
printf ("waiting for connection...\n");
|
|
|
|
as = sizeof (address);
|
|
|
|
m32c_console_ifd = accept (isocket, (struct sockaddr *) &address, &as);
|
|
|
|
if (m32c_console_ifd == -1)
|
|
|
|
{
|
|
|
|
perror ("accept");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
a = (unsigned char *) (&address.sin_addr.s_addr);
|
|
|
|
printf ("connection from %d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
|
|
|
|
m32c_console_ofd = m32c_console_ifd;
|
|
|
|
}
|
2009-08-14 12:24:30 +08:00
|
|
|
#endif
|
2008-06-07 03:18:15 +08:00
|
|
|
|
2006-01-24 06:10:41 +08:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int o;
|
|
|
|
int save_trace;
|
|
|
|
bfd *prog;
|
2009-08-14 12:24:30 +08:00
|
|
|
#ifdef HAVE_networking
|
2008-06-07 03:18:15 +08:00
|
|
|
char *console_port_s = 0;
|
2009-08-14 12:24:30 +08:00
|
|
|
#endif
|
sim, sim/{m32c,ppc,rl78}: Use getopt_long
Because of a Libiberty hack, getopt on GNU libc (2.25 or earlier) is
currently unusable on sim, causing a regression on CentOS 7.
This is caused as follows:
1. If HAVE_DECL_GETOPT is defined (getopt declaration with known prototype
is detected while configuration), a declaration of getopt in
"include/getopt.h" is suppressed.
The author started to define HAVE_DECL_GETOPT in sim with the commit
340aa4f6872c ("sim: Check known getopt definition existence").
2. GNU libc (2.25 or earlier)'s <unistd.h> includes <getopt.h> with a
special purpose macro defined to declare only getopt function but due
to include path (not tested while configuration), it causes <unistd.h>
to include Libiberty's "include/getopt.h".
3. If both 1. and 2. are satisfied, despite that <unistd.h> tries to
declare getopt by including <getopt.h>, "include/getopt.h" does not do
so, causing getopt function undeclared.
Getting rid of "include/getopt.h" (e.g. renaming this header file) is the
best solution to avoid hacking but as a short-term solution, this commit
replaces getopt with getopt_long under sim/.
2022-10-17 23:47:23 +08:00
|
|
|
static const struct option longopts[] = { { 0 } };
|
2008-06-07 03:18:15 +08:00
|
|
|
|
|
|
|
setbuf (stdout, 0);
|
|
|
|
|
|
|
|
in_gdb = 0;
|
2006-01-24 06:10:41 +08:00
|
|
|
|
sim, sim/{m32c,ppc,rl78}: Use getopt_long
Because of a Libiberty hack, getopt on GNU libc (2.25 or earlier) is
currently unusable on sim, causing a regression on CentOS 7.
This is caused as follows:
1. If HAVE_DECL_GETOPT is defined (getopt declaration with known prototype
is detected while configuration), a declaration of getopt in
"include/getopt.h" is suppressed.
The author started to define HAVE_DECL_GETOPT in sim with the commit
340aa4f6872c ("sim: Check known getopt definition existence").
2. GNU libc (2.25 or earlier)'s <unistd.h> includes <getopt.h> with a
special purpose macro defined to declare only getopt function but due
to include path (not tested while configuration), it causes <unistd.h>
to include Libiberty's "include/getopt.h".
3. If both 1. and 2. are satisfied, despite that <unistd.h> tries to
declare getopt by including <getopt.h>, "include/getopt.h" does not do
so, causing getopt function undeclared.
Getting rid of "include/getopt.h" (e.g. renaming this header file) is the
best solution to avoid hacking but as a short-term solution, this commit
replaces getopt with getopt_long under sim/.
2022-10-17 23:47:23 +08:00
|
|
|
while ((o = getopt_long (argc, argv, "tc:vdm:C", longopts, NULL))
|
|
|
|
!= -1)
|
2006-01-24 06:10:41 +08:00
|
|
|
switch (o)
|
|
|
|
{
|
|
|
|
case 't':
|
|
|
|
trace++;
|
|
|
|
break;
|
2008-06-07 03:18:15 +08:00
|
|
|
case 'c':
|
2009-08-14 12:24:30 +08:00
|
|
|
#ifdef HAVE_networking
|
2008-06-07 03:18:15 +08:00
|
|
|
console_port_s = optarg;
|
2009-08-14 12:24:30 +08:00
|
|
|
#else
|
|
|
|
fprintf (stderr, "Nework console not available in this build.\n");
|
|
|
|
#endif
|
2008-06-07 03:18:15 +08:00
|
|
|
break;
|
2008-06-17 08:34:37 +08:00
|
|
|
case 'C':
|
2009-08-14 12:24:30 +08:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
2008-06-17 08:34:37 +08:00
|
|
|
m32c_use_raw_console = 1;
|
2009-08-14 12:24:30 +08:00
|
|
|
#else
|
|
|
|
fprintf (stderr, "Raw console not available in this build.\n");
|
|
|
|
#endif
|
2008-06-17 08:34:37 +08:00
|
|
|
break;
|
2006-01-24 06:10:41 +08:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2008-06-07 03:18:15 +08:00
|
|
|
m32c_disassemble++;
|
2006-01-24 06:10:41 +08:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (strcmp (optarg, "r8c") == 0 || strcmp (optarg, "m16c") == 0)
|
|
|
|
default_machine = bfd_mach_m16c;
|
|
|
|
else if (strcmp (optarg, "m32cm") == 0
|
|
|
|
|| strcmp (optarg, "m32c") == 0)
|
|
|
|
default_machine = bfd_mach_m32c;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Invalid machine: %s\n", optarg);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
fprintf (stderr,
|
2008-06-17 08:34:37 +08:00
|
|
|
"usage: run [-v] [-C] [-c port] [-t] [-d] [-m r8c|m16c|m32cm|m32c]"
|
2008-06-07 03:18:15 +08:00
|
|
|
" program\n");
|
2006-01-24 06:10:41 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
prog = bfd_openr (argv[optind], 0);
|
|
|
|
if (!prog)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Can't read %s\n", argv[optind]);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bfd_check_format (prog, bfd_object))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s not a m32c program\n", argv[optind]);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
save_trace = trace;
|
|
|
|
trace = 0;
|
|
|
|
m32c_load (prog);
|
|
|
|
trace = save_trace;
|
|
|
|
|
2009-08-14 12:24:30 +08:00
|
|
|
#ifdef HAVE_networking
|
2008-06-07 03:18:15 +08:00
|
|
|
if (console_port_s)
|
|
|
|
setup_tcp_console (console_port_s);
|
2009-08-14 12:24:30 +08:00
|
|
|
#endif
|
2008-06-07 03:18:15 +08:00
|
|
|
|
|
|
|
sim_disasm_init (prog);
|
2006-01-24 06:10:41 +08:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (trace)
|
|
|
|
printf ("\n");
|
|
|
|
|
2008-06-07 03:18:15 +08:00
|
|
|
if (m32c_disassemble)
|
2006-01-24 06:10:41 +08:00
|
|
|
sim_disasm_one ();
|
|
|
|
|
|
|
|
enable_counting = verbose;
|
|
|
|
cycles++;
|
|
|
|
rc = decode_opcode ();
|
|
|
|
enable_counting = 0;
|
|
|
|
|
|
|
|
if (M32C_HIT_BREAK (rc))
|
|
|
|
done (1);
|
|
|
|
else if (M32C_EXITED (rc))
|
|
|
|
done (M32C_EXIT_STATUS (rc));
|
|
|
|
else
|
|
|
|
assert (M32C_STEPPED (rc));
|
|
|
|
|
|
|
|
trace_register_changes ();
|
2008-06-07 03:18:15 +08:00
|
|
|
|
|
|
|
#ifdef TIMER_A
|
|
|
|
update_timer_a ();
|
|
|
|
#endif
|
2006-01-24 06:10:41 +08:00
|
|
|
}
|
|
|
|
}
|