]>
Commit | Line | Data |
---|---|---|
87326c78 DD |
1 | /* main.c --- main function for stand-alone RL78 simulator. |
2 | ||
3666a048 | 3 | Copyright (C) 2011-2021 Free Software Foundation, Inc. |
87326c78 DD |
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 | ||
23 | #include "config.h" | |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <stdlib.h> | |
27 | #ifdef HAVE_UNISTD_H | |
28 | #include <unistd.h> | |
29 | #endif | |
30 | #include <assert.h> | |
31 | #include <setjmp.h> | |
32 | #include <signal.h> | |
87326c78 | 33 | #include <getopt.h> |
87326c78 DD |
34 | |
35 | #include "libiberty.h" | |
36 | #include "bfd.h" | |
37 | ||
38 | #include "cpu.h" | |
39 | #include "mem.h" | |
40 | #include "load.h" | |
41 | #include "trace.h" | |
42 | ||
43 | static int disassemble = 0; | |
44 | static const char * dump_counts_filename = NULL; | |
45 | ||
46 | static void | |
47 | done (int exit_code) | |
48 | { | |
49 | if (verbose) | |
50 | { | |
51 | printf ("Exit code: %d\n", exit_code); | |
52 | printf ("total clocks: %lld\n", total_clocks); | |
53 | } | |
54 | if (dump_counts_filename) | |
55 | dump_counts_per_insn (dump_counts_filename); | |
56 | exit (exit_code); | |
57 | } | |
58 | ||
59 | int | |
60 | main (int argc, char **argv) | |
61 | { | |
62 | int o; | |
63 | int save_trace; | |
64 | bfd *prog; | |
65 | int rc; | |
66 | ||
67 | xmalloc_set_program_name (argv[0]); | |
68 | ||
0952813b | 69 | while ((o = getopt (argc, argv, "tvdr:D:M:")) != -1) |
87326c78 DD |
70 | { |
71 | switch (o) | |
72 | { | |
73 | case 't': | |
74 | trace ++; | |
75 | break; | |
76 | case 'v': | |
77 | verbose ++; | |
78 | break; | |
79 | case 'd': | |
80 | disassemble ++; | |
81 | break; | |
82 | case 'r': | |
83 | mem_ram_size (atoi (optarg)); | |
84 | break; | |
85 | case 'D': | |
86 | dump_counts_filename = optarg; | |
87 | break; | |
0952813b DD |
88 | case 'M': |
89 | if (strcmp (optarg, "g10") == 0) | |
90 | { | |
91 | rl78_g10_mode = 1; | |
92 | g13_multiply = 0; | |
93 | g14_multiply = 0; | |
94 | mem_set_mirror (0, 0xf8000, 4096); | |
95 | } | |
96 | if (strcmp (optarg, "g13") == 0) | |
97 | { | |
98 | rl78_g10_mode = 0; | |
99 | g13_multiply = 1; | |
100 | g14_multiply = 0; | |
101 | } | |
102 | if (strcmp (optarg, "g14") == 0) | |
103 | { | |
104 | rl78_g10_mode = 0; | |
105 | g13_multiply = 0; | |
106 | g14_multiply = 1; | |
107 | } | |
108 | break; | |
87326c78 DD |
109 | case '?': |
110 | { | |
111 | fprintf (stderr, | |
112 | "usage: run [options] program [arguments]\n"); | |
113 | fprintf (stderr, | |
114 | "\t-v\t\t- increase verbosity.\n" | |
115 | "\t-t\t\t- trace.\n" | |
116 | "\t-d\t\t- disassemble.\n" | |
117 | "\t-r <bytes>\t- ram size.\n" | |
0952813b | 118 | "\t-M <mcu>\t- mcu type, default none, allowed: g10,g13,g14\n" |
87326c78 DD |
119 | "\t-D <filename>\t- dump cycle count histogram\n"); |
120 | exit (1); | |
121 | } | |
122 | } | |
123 | } | |
124 | ||
125 | prog = bfd_openr (argv[optind], 0); | |
126 | if (!prog) | |
127 | { | |
128 | fprintf (stderr, "Can't read %s\n", argv[optind]); | |
129 | exit (1); | |
130 | } | |
131 | ||
132 | if (!bfd_check_format (prog, bfd_object)) | |
133 | { | |
134 | fprintf (stderr, "%s not a rl78 program\n", argv[optind]); | |
135 | exit (1); | |
136 | } | |
137 | ||
138 | init_cpu (); | |
139 | ||
140 | rl78_in_gdb = 0; | |
141 | save_trace = trace; | |
142 | trace = 0; | |
143 | rl78_load (prog, 0, argv[0]); | |
144 | trace = save_trace; | |
145 | ||
146 | sim_disasm_init (prog); | |
147 | ||
148 | rc = setjmp (decode_jmp_buf); | |
149 | ||
150 | if (rc == 0) | |
151 | { | |
152 | if (!trace && !disassemble) | |
153 | { | |
154 | /* This will longjmp to the above if an exception | |
155 | happens. */ | |
156 | for (;;) | |
157 | decode_opcode (); | |
158 | } | |
159 | else | |
160 | while (1) | |
161 | { | |
162 | ||
163 | if (trace) | |
164 | printf ("\n"); | |
165 | ||
166 | if (disassemble) | |
167 | sim_disasm_one (); | |
168 | ||
169 | rc = decode_opcode (); | |
170 | ||
171 | if (trace) | |
172 | trace_register_changes (); | |
173 | } | |
174 | } | |
175 | ||
176 | if (RL78_HIT_BREAK (rc)) | |
177 | done (1); | |
178 | else if (RL78_EXITED (rc)) | |
179 | done (RL78_EXIT_STATUS (rc)); | |
180 | else if (RL78_STOPPED (rc)) | |
181 | { | |
182 | if (verbose) | |
183 | printf ("Stopped on signal %d\n", RL78_STOP_SIG (rc)); | |
184 | exit (1); | |
185 | } | |
186 | done (0); | |
187 | exit (0); | |
188 | } |