96c0ac45f2bf03e902cf4f07ac969dc62b682c6b
[external/binutils.git] / sim / m32c / main.c
1 /* main.c --- main function for stand-alone M32C simulator.
2
3 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
5
6 This file is part of the GNU simulators.
7
8 The GNU simulators are free software; you can redistribute them and/or
9 modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 The GNU simulators are distributed in the hope that they will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with the GNU simulators; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA  */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include <setjmp.h>
30 #include <signal.h>
31
32 #include "bfd.h"
33
34 #include "cpu.h"
35 #include "mem.h"
36 #include "misc.h"
37 #include "load.h"
38 #include "trace.h"
39
40 static int disassemble = 0;
41 static unsigned int cycles = 0;
42
43 static void
44 done (int exit_code)
45 {
46   if (verbose)
47     {
48       stack_heap_stats ();
49       mem_usage_stats ();
50       printf ("insns: %14s\n", comma (cycles));
51     }
52   exit (exit_code);
53 }
54
55 int
56 main (int argc, char **argv)
57 {
58   int o;
59   int save_trace;
60   bfd *prog;
61
62   while ((o = getopt (argc, argv, "tvdm:")) != -1)
63     switch (o)
64       {
65       case 't':
66         trace++;
67         break;
68       case 'v':
69         verbose++;
70         break;
71       case 'd':
72         disassemble++;
73         break;
74       case 'm':
75         if (strcmp (optarg, "r8c") == 0 || strcmp (optarg, "m16c") == 0)
76           default_machine = bfd_mach_m16c;
77         else if (strcmp (optarg, "m32cm") == 0
78                  || strcmp (optarg, "m32c") == 0)
79           default_machine = bfd_mach_m32c;
80         else
81           {
82             fprintf (stderr, "Invalid machine: %s\n", optarg);
83             exit (1);
84           }
85         break;
86       case '?':
87         fprintf (stderr,
88                  "usage: run [-v] [-t] [-d] [-m r8c|m16c|m32cm|m32c]"
89                  " program\n");
90         exit (1);
91       }
92
93   prog = bfd_openr (argv[optind], 0);
94   if (!prog)
95     {
96       fprintf (stderr, "Can't read %s\n", argv[optind]);
97       exit (1);
98     }
99
100   if (!bfd_check_format (prog, bfd_object))
101     {
102       fprintf (stderr, "%s not a m32c program\n", argv[optind]);
103       exit (1);
104     }
105
106   save_trace = trace;
107   trace = 0;
108   m32c_load (prog);
109   trace = save_trace;
110
111   if (disassemble)
112     sim_disasm_init (prog);
113
114   while (1)
115     {
116       int rc;
117
118       if (trace)
119         printf ("\n");
120
121       if (disassemble)
122         sim_disasm_one ();
123
124       enable_counting = verbose;
125       cycles++;
126       rc = decode_opcode ();
127       enable_counting = 0;
128
129       if (M32C_HIT_BREAK (rc))
130         done (1);
131       else if (M32C_EXITED (rc))
132         done (M32C_EXIT_STATUS (rc));
133       else
134         assert (M32C_STEPPED (rc));
135
136       trace_register_changes ();
137     }
138 }