]>
Commit | Line | Data |
---|---|---|
26da232c MF |
1 | /* Example synacor simulator. |
2 | ||
3 | Copyright (C) 2005-2021 Free Software Foundation, Inc. | |
4 | Contributed by Mike Frysinger. | |
5 | ||
6 | This file is part of 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 | /* This file contains the main glue logic between the sim core and the target | |
22 | specific simulator. Normally this file will be kept small and the target | |
23 | details will live in other files. | |
24 | ||
df68e12b MF |
25 | For more specific details on these functions, see the sim/sim.h header |
26 | file. */ | |
26da232c MF |
27 | |
28 | #include "config.h" | |
29 | ||
30 | #include "sim-main.h" | |
31 | #include "sim-options.h" | |
32 | \f | |
33 | /* This function is the main loop. It should process ticks and decode+execute | |
34 | a single instruction. | |
35 | ||
36 | Usually you do not need to change things here. */ | |
37 | ||
38 | void | |
39 | sim_engine_run (SIM_DESC sd, | |
40 | int next_cpu_nr, /* ignore */ | |
41 | int nr_cpus, /* ignore */ | |
42 | int siggnal) /* ignore */ | |
43 | { | |
44 | SIM_CPU *cpu; | |
45 | ||
46 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
47 | ||
48 | cpu = STATE_CPU (sd, 0); | |
49 | ||
50 | while (1) | |
51 | { | |
52 | step_once (cpu); | |
53 | if (sim_events_tick (sd)) | |
54 | sim_events_process (sd); | |
55 | } | |
56 | } | |
57 | \f | |
58 | /* Initialize the simulator from scratch. This is called once per lifetime of | |
59 | the simulation. Think of it as a processor reset. | |
60 | ||
61 | Usually all cpu-specific setup is handled in the initialize_cpu callback. | |
62 | If you want to do cpu-independent stuff, then it should go at the end (see | |
63 | where memory is initialized). */ | |
64 | ||
65 | #define DEFAULT_MEM_SIZE (16 * 1024 * 1024) | |
66 | ||
67 | static void | |
68 | free_state (SIM_DESC sd) | |
69 | { | |
70 | if (STATE_MODULES (sd) != NULL) | |
71 | sim_module_uninstall (sd); | |
72 | sim_cpu_free_all (sd); | |
73 | sim_state_free (sd); | |
74 | } | |
75 | ||
76 | SIM_DESC | |
77 | sim_open (SIM_OPEN_KIND kind, host_callback *callback, | |
78 | struct bfd *abfd, char * const *argv) | |
79 | { | |
80 | char c; | |
81 | int i; | |
82 | SIM_DESC sd = sim_state_alloc (kind, callback); | |
83 | ||
84 | /* The cpu data is kept in a separately allocated chunk of memory. */ | |
d5a71b11 | 85 | if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK) |
26da232c MF |
86 | { |
87 | free_state (sd); | |
88 | return 0; | |
89 | } | |
90 | ||
91 | if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK) | |
92 | { | |
93 | free_state (sd); | |
94 | return 0; | |
95 | } | |
96 | ||
97 | /* XXX: Default to the Virtual environment. */ | |
98 | if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT) | |
99 | STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT; | |
100 | ||
101 | /* The parser will print an error message for us, so we silently return. */ | |
102 | if (sim_parse_args (sd, argv) != SIM_RC_OK) | |
103 | { | |
104 | free_state (sd); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | /* Check for/establish the a reference program image. */ | |
109 | if (sim_analyze_program (sd, | |
110 | (STATE_PROG_ARGV (sd) != NULL | |
111 | ? *STATE_PROG_ARGV (sd) | |
112 | : NULL), abfd) != SIM_RC_OK) | |
113 | { | |
114 | free_state (sd); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | /* Establish any remaining configuration options. */ | |
119 | if (sim_config (sd) != SIM_RC_OK) | |
120 | { | |
121 | free_state (sd); | |
122 | return 0; | |
123 | } | |
124 | ||
125 | if (sim_post_argv_init (sd) != SIM_RC_OK) | |
126 | { | |
127 | free_state (sd); | |
128 | return 0; | |
129 | } | |
130 | ||
131 | /* CPU specific initialization. */ | |
132 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
133 | { | |
134 | SIM_CPU *cpu = STATE_CPU (sd, i); | |
135 | ||
136 | initialize_cpu (sd, cpu); | |
137 | } | |
138 | ||
139 | /* Allocate external memory if none specified by user. | |
140 | Use address 4 here in case the user wanted address 0 unmapped. */ | |
141 | if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0) | |
142 | sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE); | |
143 | ||
144 | return sd; | |
145 | } | |
146 | \f | |
147 | /* Prepare to run a program that has already been loaded into memory. | |
148 | ||
149 | Usually you do not need to change things here. */ | |
150 | ||
151 | SIM_RC | |
152 | sim_create_inferior (SIM_DESC sd, struct bfd *abfd, | |
153 | char * const *argv, char * const *env) | |
154 | { | |
155 | SIM_CPU *cpu = STATE_CPU (sd, 0); | |
156 | sim_cia addr; | |
157 | ||
158 | /* Set the PC. */ | |
159 | if (abfd != NULL) | |
160 | addr = bfd_get_start_address (abfd); | |
161 | else | |
162 | addr = 0; | |
163 | sim_pc_set (cpu, addr); | |
164 | ||
165 | /* Standalone mode (i.e. `run`) will take care of the argv for us in | |
166 | sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' | |
167 | with `gdb`), we need to handle it because the user can change the | |
168 | argv on the fly via gdb's 'run'. */ | |
169 | if (STATE_PROG_ARGV (sd) != argv) | |
170 | { | |
171 | freeargv (STATE_PROG_ARGV (sd)); | |
172 | STATE_PROG_ARGV (sd) = dupargv (argv); | |
173 | } | |
174 | ||
175 | return SIM_RC_OK; | |
176 | } |