2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "sim-model.h"
22 #include "libiberty.h"
23 #include "sim-options.h"
25 #include "sim-assert.h"
28 static void model_set (sim_cpu *, const MODEL *);
30 static DECLARE_OPTION_HANDLER (model_option_handler);
32 static MODULE_INIT_FN sim_model_init;
35 OPTION_MODEL = OPTION_START,
39 static const OPTION model_options[] = {
40 { {"model", required_argument, NULL, OPTION_MODEL},
41 '\0', "MODEL", "Specify model to simulate",
42 model_option_handler, NULL },
44 { {"model-info", no_argument, NULL, OPTION_MODEL_INFO},
45 '\0', NULL, "List selectable models",
46 model_option_handler, NULL },
47 { {"info-model", no_argument, NULL, OPTION_MODEL_INFO},
49 model_option_handler, NULL },
51 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
55 model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
56 char *arg, int is_command)
62 const MODEL *model = sim_model_lookup (arg);
65 sim_io_eprintf (sd, "unknown model `%s'\n", arg);
68 sim_model_set (sd, cpu, model);
72 case OPTION_MODEL_INFO :
76 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
78 sim_io_printf (sd, "Models for architecture `%s':\n",
80 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL;
82 sim_io_printf (sd, " %s", MODEL_NAME (model));
83 sim_io_printf (sd, "\n");
93 sim_model_install (SIM_DESC sd)
95 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
97 sim_add_option_table (sd, NULL, model_options);
98 sim_module_add_init_fn (sd, sim_model_init);
103 /* Subroutine of sim_model_set to set the model for one cpu. */
106 model_set (sim_cpu *cpu, const MODEL *model)
108 CPU_MACH (cpu) = MODEL_MACH (model);
109 CPU_MODEL (cpu) = model;
110 (* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
111 (* MODEL_INIT (model)) (cpu);
114 /* Set the current model of CPU to MODEL.
115 If CPU is NULL, all cpus are set to MODEL. */
118 sim_model_set (SIM_DESC sd, sim_cpu *cpu, const MODEL *model)
124 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
125 if (STATE_CPU (sd, c))
126 model_set (STATE_CPU (sd, c), model);
130 model_set (cpu, model);
134 /* Look up model named NAME.
135 Result is pointer to MODEL entry or NULL if not found. */
138 sim_model_lookup (const char *name)
143 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
145 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
147 if (strcmp (MODEL_NAME (model), name) == 0)
154 /* Look up machine named NAME.
155 Result is pointer to MACH entry or NULL if not found. */
158 sim_mach_lookup (const char *name)
162 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
164 if (strcmp (MACH_NAME (*machp), name) == 0)
170 /* Look up a machine via its bfd name.
171 Result is pointer to MACH entry or NULL if not found. */
174 sim_mach_lookup_bfd_name (const char *name)
178 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
180 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
186 /* Initialize model support. */
189 sim_model_init (SIM_DESC sd)
193 /* If both cpu model and state architecture are set, ensure they're
194 compatible. If only one is set, set the other. If neither are set,
195 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
196 for the selected "mach" (bfd terminology). */
198 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
199 /* ??? At present this only supports homogeneous multiprocessors. */
200 cpu = STATE_CPU (sd, 0);
202 if (! STATE_ARCHITECTURE (sd)
205 /* Set the default model. */
206 const MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
207 SIM_ASSERT (model != NULL);
208 sim_model_set (sd, NULL, model);
211 if (STATE_ARCHITECTURE (sd)
214 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
215 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
217 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
218 MODEL_NAME (CPU_MODEL (cpu)),
219 STATE_ARCHITECTURE (sd)->printable_name);
223 else if (STATE_ARCHITECTURE (sd))
225 /* Use the default model for the selected machine.
226 The default model is the first one in the list. */
227 const MACH *mach = sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd)->printable_name);
231 sim_io_eprintf (sd, "unsupported machine `%s'\n",
232 STATE_ARCHITECTURE (sd)->printable_name);
235 sim_model_set (sd, NULL, MACH_MODELS (mach));
239 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));