Update the copyright notice of some of the files I missed
[platform/upstream/binutils.git] / sim / m32c / main.c
1 /* main.c --- main function for stand-alone M32C simulator.
2
3 Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
5
6 This file is part of the GNU simulators.
7
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.
12
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.
17
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/>.  */
20
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <setjmp.h>
28 #include <signal.h>
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34
35
36 #include "bfd.h"
37
38 #include "cpu.h"
39 #include "mem.h"
40 #include "misc.h"
41 #include "load.h"
42 #include "trace.h"
43 #ifdef TIMER_A
44 #include "int.h"
45 #include "timer_a.h"
46 #endif
47
48 extern int m32c_console_ofd;
49 extern int m32c_console_ifd;
50
51 int m32c_disassemble = 0;
52 static unsigned int cycles = 0;
53
54 static void
55 done (int exit_code)
56 {
57   if (verbose)
58     {
59       stack_heap_stats ();
60       mem_usage_stats ();
61       printf ("insns: %14s\n", comma (cycles));
62     }
63   exit (exit_code);
64 }
65
66 static void
67 setup_tcp_console (char *portname)
68 {
69   int port = atoi (portname);
70   struct sockaddr_in address;
71   int isocket;
72   socklen_t as;
73   unsigned char *a;
74
75   if (port < 1024)
76     {
77       printf ("invalid port number %d\n", port);
78       exit (1);
79     }
80   printf ("waiting for tcp console on port %d\n", port);
81
82   memset (&address, 0, sizeof (address));
83   address.sin_family = AF_INET;
84   address.sin_port = htons (port);
85
86   isocket = socket (AF_INET, SOCK_STREAM, 0);
87   if (isocket < 0)
88     {
89       perror ("socket");
90       exit (1);
91     }
92
93   if (bind (isocket, (struct sockaddr *) &address, sizeof (address)))
94     {
95       perror ("bind");
96       exit (1);
97     }
98   listen (isocket, 2);
99
100   printf ("waiting for connection...\n");
101   as = sizeof (address);
102   m32c_console_ifd = accept (isocket, (struct sockaddr *) &address, &as);
103   if (m32c_console_ifd == -1)
104     {
105       perror ("accept");
106       exit (1);
107     }
108   a = (unsigned char *) (&address.sin_addr.s_addr);
109   printf ("connection from %d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
110   m32c_console_ofd = m32c_console_ifd;
111 }
112
113 int
114 main (int argc, char **argv)
115 {
116   int o;
117   int save_trace;
118   bfd *prog;
119   char *console_port_s = 0;
120
121   setbuf (stdout, 0);
122
123   in_gdb = 0;
124
125   while ((o = getopt (argc, argv, "tc:vdm:C")) != -1)
126     switch (o)
127       {
128       case 't':
129         trace++;
130         break;
131       case 'c':
132         console_port_s = optarg;
133         break;
134       case 'C':
135         m32c_use_raw_console = 1;
136         break;
137       case 'v':
138         verbose++;
139         break;
140       case 'd':
141         m32c_disassemble++;
142         break;
143       case 'm':
144         if (strcmp (optarg, "r8c") == 0 || strcmp (optarg, "m16c") == 0)
145           default_machine = bfd_mach_m16c;
146         else if (strcmp (optarg, "m32cm") == 0
147                  || strcmp (optarg, "m32c") == 0)
148           default_machine = bfd_mach_m32c;
149         else
150           {
151             fprintf (stderr, "Invalid machine: %s\n", optarg);
152             exit (1);
153           }
154         break;
155       case '?':
156         fprintf (stderr,
157                  "usage: run [-v] [-C] [-c port] [-t] [-d] [-m r8c|m16c|m32cm|m32c]"
158                  " program\n");
159         exit (1);
160       }
161
162   prog = bfd_openr (argv[optind], 0);
163   if (!prog)
164     {
165       fprintf (stderr, "Can't read %s\n", argv[optind]);
166       exit (1);
167     }
168
169   if (!bfd_check_format (prog, bfd_object))
170     {
171       fprintf (stderr, "%s not a m32c program\n", argv[optind]);
172       exit (1);
173     }
174
175   save_trace = trace;
176   trace = 0;
177   m32c_load (prog);
178   trace = save_trace;
179
180   if (console_port_s)
181     setup_tcp_console (console_port_s);
182
183   sim_disasm_init (prog);
184
185   while (1)
186     {
187       int rc;
188
189       if (trace)
190         printf ("\n");
191
192       if (m32c_disassemble)
193         sim_disasm_one ();
194
195       enable_counting = verbose;
196       cycles++;
197       rc = decode_opcode ();
198       enable_counting = 0;
199
200       if (M32C_HIT_BREAK (rc))
201         done (1);
202       else if (M32C_EXITED (rc))
203         done (M32C_EXIT_STATUS (rc));
204       else
205         assert (M32C_STEPPED (rc));
206
207       trace_register_changes ();
208
209 #ifdef TIMER_A
210       update_timer_a ();
211 #endif
212     }
213 }