1 /* main.c --- main function for stand-alone RL78 simulator.
2 
3    Copyright (C) 2011-2013 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
done(int exit_code)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
main(int argc,char ** argv)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:")) != -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 '?':
91 	  {
92 	    fprintf (stderr,
93 		     "usage: run [options] program [arguments]\n");
94 	    fprintf (stderr,
95 		     "\t-v\t\t- increase verbosity.\n"
96 		     "\t-t\t\t- trace.\n"
97 		     "\t-d\t\t- disassemble.\n"
98 		     "\t-r <bytes>\t- ram size.\n"
99 		     "\t-D <filename>\t- dump cycle count histogram\n");
100 	    exit (1);
101 	  }
102 	}
103     }
104 
105   prog = bfd_openr (argv[optind], 0);
106   if (!prog)
107     {
108       fprintf (stderr, "Can't read %s\n", argv[optind]);
109       exit (1);
110     }
111 
112   if (!bfd_check_format (prog, bfd_object))
113     {
114       fprintf (stderr, "%s not a rl78 program\n", argv[optind]);
115       exit (1);
116     }
117 
118   init_cpu ();
119 
120   rl78_in_gdb = 0;
121   save_trace = trace;
122   trace = 0;
123   rl78_load (prog, 0, argv[0]);
124   trace = save_trace;
125 
126   sim_disasm_init (prog);
127 
128   rc = setjmp (decode_jmp_buf);
129 
130   if (rc == 0)
131     {
132       if (!trace && !disassemble)
133 	{
134 	  /* This will longjmp to the above if an exception
135 	     happens.  */
136 	  for (;;)
137 	    decode_opcode ();
138 	}
139       else
140 	while (1)
141 	  {
142 
143 	    if (trace)
144 	      printf ("\n");
145 
146 	    if (disassemble)
147 	      sim_disasm_one ();
148 
149 	    rc = decode_opcode ();
150 
151 	    if (trace)
152 	      trace_register_changes ();
153 	  }
154     }
155 
156   if (RL78_HIT_BREAK (rc))
157     done (1);
158   else if (RL78_EXITED (rc))
159     done (RL78_EXIT_STATUS (rc));
160   else if (RL78_STOPPED (rc))
161     {
162       if (verbose)
163 	printf ("Stopped on signal %d\n", RL78_STOP_SIG (rc));
164       exit (1);
165     }
166   done (0);
167   exit (0);
168 }
169