Commit | Line | Data |
---|---|---|
87326c78 DD |
1 | /* main.c --- main function for stand-alone RL78 simulator. |
2 | ||
3 | Copyright (C) 2011 | |
4 | Free Software Foundation, Inc. | |
5 | Contributed by Red Hat, Inc. | |
6 | ||
7 | This file is part of the GNU simulators. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
23 | ||
24 | #include "config.h" | |
25 | #include <stdio.h> | |
26 | #include <string.h> | |
27 | #include <stdlib.h> | |
28 | #ifdef HAVE_UNISTD_H | |
29 | #include <unistd.h> | |
30 | #endif | |
31 | #include <assert.h> | |
32 | #include <setjmp.h> | |
33 | #include <signal.h> | |
34 | #ifdef HAVE_GETOPT_H | |
35 | #include <getopt.h> | |
36 | #endif | |
37 | ||
38 | #include "libiberty.h" | |
39 | #include "bfd.h" | |
40 | ||
41 | #include "cpu.h" | |
42 | #include "mem.h" | |
43 | #include "load.h" | |
44 | #include "trace.h" | |
45 | ||
46 | static int disassemble = 0; | |
47 | static const char * dump_counts_filename = NULL; | |
48 | ||
49 | static void | |
50 | done (int exit_code) | |
51 | { | |
52 | if (verbose) | |
53 | { | |
54 | printf ("Exit code: %d\n", exit_code); | |
55 | printf ("total clocks: %lld\n", total_clocks); | |
56 | } | |
57 | if (dump_counts_filename) | |
58 | dump_counts_per_insn (dump_counts_filename); | |
59 | exit (exit_code); | |
60 | } | |
61 | ||
62 | int | |
63 | main (int argc, char **argv) | |
64 | { | |
65 | int o; | |
66 | int save_trace; | |
67 | bfd *prog; | |
68 | int rc; | |
69 | ||
70 | xmalloc_set_program_name (argv[0]); | |
71 | ||
72 | while ((o = getopt (argc, argv, "tvdr:D:")) != -1) | |
73 | { | |
74 | switch (o) | |
75 | { | |
76 | case 't': | |
77 | trace ++; | |
78 | break; | |
79 | case 'v': | |
80 | verbose ++; | |
81 | break; | |
82 | case 'd': | |
83 | disassemble ++; | |
84 | break; | |
85 | case 'r': | |
86 | mem_ram_size (atoi (optarg)); | |
87 | break; | |
88 | case 'D': | |
89 | dump_counts_filename = optarg; | |
90 | break; | |
91 | case '?': | |
92 | { | |
93 | fprintf (stderr, | |
94 | "usage: run [options] program [arguments]\n"); | |
95 | fprintf (stderr, | |
96 | "\t-v\t\t- increase verbosity.\n" | |
97 | "\t-t\t\t- trace.\n" | |
98 | "\t-d\t\t- disassemble.\n" | |
99 | "\t-r <bytes>\t- ram size.\n" | |
100 | "\t-D <filename>\t- dump cycle count histogram\n"); | |
101 | exit (1); | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | prog = bfd_openr (argv[optind], 0); | |
107 | if (!prog) | |
108 | { | |
109 | fprintf (stderr, "Can't read %s\n", argv[optind]); | |
110 | exit (1); | |
111 | } | |
112 | ||
113 | if (!bfd_check_format (prog, bfd_object)) | |
114 | { | |
115 | fprintf (stderr, "%s not a rl78 program\n", argv[optind]); | |
116 | exit (1); | |
117 | } | |
118 | ||
119 | init_cpu (); | |
120 | ||
121 | rl78_in_gdb = 0; | |
122 | save_trace = trace; | |
123 | trace = 0; | |
124 | rl78_load (prog, 0, argv[0]); | |
125 | trace = save_trace; | |
126 | ||
127 | sim_disasm_init (prog); | |
128 | ||
129 | rc = setjmp (decode_jmp_buf); | |
130 | ||
131 | if (rc == 0) | |
132 | { | |
133 | if (!trace && !disassemble) | |
134 | { | |
135 | /* This will longjmp to the above if an exception | |
136 | happens. */ | |
137 | for (;;) | |
138 | decode_opcode (); | |
139 | } | |
140 | else | |
141 | while (1) | |
142 | { | |
143 | ||
144 | if (trace) | |
145 | printf ("\n"); | |
146 | ||
147 | if (disassemble) | |
148 | sim_disasm_one (); | |
149 | ||
150 | rc = decode_opcode (); | |
151 | ||
152 | if (trace) | |
153 | trace_register_changes (); | |
154 | } | |
155 | } | |
156 | ||
157 | if (RL78_HIT_BREAK (rc)) | |
158 | done (1); | |
159 | else if (RL78_EXITED (rc)) | |
160 | done (RL78_EXIT_STATUS (rc)); | |
161 | else if (RL78_STOPPED (rc)) | |
162 | { | |
163 | if (verbose) | |
164 | printf ("Stopped on signal %d\n", RL78_STOP_SIG (rc)); | |
165 | exit (1); | |
166 | } | |
167 | done (0); | |
168 | exit (0); | |
169 | } |