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