]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/m32c/main.c
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / sim / m32c / main.c
CommitLineData
d45a4bef
JB
1/* main.c --- main function for stand-alone M32C simulator.
2
8acc9f48 3Copyright (C) 2005-2013 Free Software Foundation, Inc.
d45a4bef
JB
4Contributed by Red Hat, Inc.
5
6This file is part of the GNU simulators.
7
4744ac1b
JB
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3 of the License, or
11(at your option) any later version.
d45a4bef 12
4744ac1b
JB
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
d45a4bef
JB
17
18You should have received a copy of the GNU General Public License
4744ac1b 19along with this program. If not, see <http://www.gnu.org/licenses/>. */
d45a4bef
JB
20
21
340cf1cf 22#include "config.h"
d45a4bef
JB
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <assert.h>
28#include <setjmp.h>
29#include <signal.h>
3877a145 30#include <sys/types.h>
340cf1cf
DD
31
32#ifdef HAVE_SYS_SOCKET_H
33#ifdef HAVE_NETINET_IN_H
34#ifdef HAVE_NETINET_TCP_H
35#define HAVE_networking
36#endif
37#endif
38#endif
39
40#ifdef HAVE_networking
3877a145
DD
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <netinet/tcp.h>
340cf1cf 44#endif
3877a145
DD
45
46
d45a4bef
JB
47#include "bfd.h"
48
49#include "cpu.h"
50#include "mem.h"
51#include "misc.h"
52#include "load.h"
53#include "trace.h"
3877a145
DD
54#ifdef TIMER_A
55#include "int.h"
56#include "timer_a.h"
57#endif
d45a4bef 58
340cf1cf 59#ifdef HAVE_networking
3877a145
DD
60extern int m32c_console_ofd;
61extern int m32c_console_ifd;
340cf1cf 62#endif
3877a145
DD
63
64int m32c_disassemble = 0;
d45a4bef
JB
65static unsigned int cycles = 0;
66
67static void
68done (int exit_code)
69{
70 if (verbose)
71 {
72 stack_heap_stats ();
73 mem_usage_stats ();
74 printf ("insns: %14s\n", comma (cycles));
75 }
76 exit (exit_code);
77}
78
340cf1cf 79#ifdef HAVE_networking
3877a145
DD
80static void
81setup_tcp_console (char *portname)
82{
83 int port = atoi (portname);
84 struct sockaddr_in address;
85 int isocket;
86 socklen_t as;
87 unsigned char *a;
88
89 if (port < 1024)
90 {
91 printf ("invalid port number %d\n", port);
92 exit (1);
93 }
94 printf ("waiting for tcp console on port %d\n", port);
95
96 memset (&address, 0, sizeof (address));
97 address.sin_family = AF_INET;
98 address.sin_port = htons (port);
99
100 isocket = socket (AF_INET, SOCK_STREAM, 0);
363a6e9f 101 if (isocket == -1)
3877a145
DD
102 {
103 perror ("socket");
104 exit (1);
105 }
106
107 if (bind (isocket, (struct sockaddr *) &address, sizeof (address)))
108 {
109 perror ("bind");
110 exit (1);
111 }
112 listen (isocket, 2);
113
114 printf ("waiting for connection...\n");
115 as = sizeof (address);
116 m32c_console_ifd = accept (isocket, (struct sockaddr *) &address, &as);
117 if (m32c_console_ifd == -1)
118 {
119 perror ("accept");
120 exit (1);
121 }
122 a = (unsigned char *) (&address.sin_addr.s_addr);
123 printf ("connection from %d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
124 m32c_console_ofd = m32c_console_ifd;
125}
340cf1cf 126#endif
3877a145 127
d45a4bef
JB
128int
129main (int argc, char **argv)
130{
131 int o;
132 int save_trace;
133 bfd *prog;
340cf1cf 134#ifdef HAVE_networking
3877a145 135 char *console_port_s = 0;
340cf1cf 136#endif
3877a145
DD
137
138 setbuf (stdout, 0);
139
140 in_gdb = 0;
d45a4bef 141
e7ddc197 142 while ((o = getopt (argc, argv, "tc:vdm:C")) != -1)
d45a4bef
JB
143 switch (o)
144 {
145 case 't':
146 trace++;
147 break;
3877a145 148 case 'c':
340cf1cf 149#ifdef HAVE_networking
3877a145 150 console_port_s = optarg;
340cf1cf
DD
151#else
152 fprintf (stderr, "Nework console not available in this build.\n");
153#endif
3877a145 154 break;
e7ddc197 155 case 'C':
340cf1cf 156#ifdef HAVE_TERMIOS_H
e7ddc197 157 m32c_use_raw_console = 1;
340cf1cf
DD
158#else
159 fprintf (stderr, "Raw console not available in this build.\n");
160#endif
e7ddc197 161 break;
d45a4bef
JB
162 case 'v':
163 verbose++;
164 break;
165 case 'd':
3877a145 166 m32c_disassemble++;
d45a4bef
JB
167 break;
168 case 'm':
169 if (strcmp (optarg, "r8c") == 0 || strcmp (optarg, "m16c") == 0)
170 default_machine = bfd_mach_m16c;
171 else if (strcmp (optarg, "m32cm") == 0
172 || strcmp (optarg, "m32c") == 0)
173 default_machine = bfd_mach_m32c;
174 else
175 {
176 fprintf (stderr, "Invalid machine: %s\n", optarg);
177 exit (1);
178 }
179 break;
180 case '?':
181 fprintf (stderr,
e7ddc197 182 "usage: run [-v] [-C] [-c port] [-t] [-d] [-m r8c|m16c|m32cm|m32c]"
3877a145 183 " program\n");
d45a4bef
JB
184 exit (1);
185 }
186
187 prog = bfd_openr (argv[optind], 0);
188 if (!prog)
189 {
190 fprintf (stderr, "Can't read %s\n", argv[optind]);
191 exit (1);
192 }
193
194 if (!bfd_check_format (prog, bfd_object))
195 {
196 fprintf (stderr, "%s not a m32c program\n", argv[optind]);
197 exit (1);
198 }
199
200 save_trace = trace;
201 trace = 0;
202 m32c_load (prog);
203 trace = save_trace;
204
340cf1cf 205#ifdef HAVE_networking
3877a145
DD
206 if (console_port_s)
207 setup_tcp_console (console_port_s);
340cf1cf 208#endif
3877a145
DD
209
210 sim_disasm_init (prog);
d45a4bef
JB
211
212 while (1)
213 {
214 int rc;
215
216 if (trace)
217 printf ("\n");
218
3877a145 219 if (m32c_disassemble)
d45a4bef
JB
220 sim_disasm_one ();
221
222 enable_counting = verbose;
223 cycles++;
224 rc = decode_opcode ();
225 enable_counting = 0;
226
227 if (M32C_HIT_BREAK (rc))
228 done (1);
229 else if (M32C_EXITED (rc))
230 done (M32C_EXIT_STATUS (rc));
231 else
232 assert (M32C_STEPPED (rc));
233
234 trace_register_changes ();
3877a145
DD
235
236#ifdef TIMER_A
237 update_timer_a ();
238#endif
d45a4bef
JB
239 }
240}