No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / sim / common / sim-model.c
blob3cfdc15c501de71745ce7e056a2e27dc9cd575f7
1 /* Model support.
2 Copyright (C) 1996, 1997, 1998 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 2, or (at your option)
10 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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "sim-main.h"
22 #include "libiberty.h"
23 #include "sim-options.h"
24 #include "sim-io.h"
25 #include "sim-assert.h"
26 #include "bfd.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;
34 #define OPTION_MODEL (OPTION_START + 0)
36 static const OPTION model_options[] = {
37 { {"model", required_argument, NULL, OPTION_MODEL},
38 '\0', "MODEL", "Specify model to simulate",
39 model_option_handler },
40 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
43 static SIM_RC
44 model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
45 char *arg, int is_command)
47 switch (opt)
49 case OPTION_MODEL :
51 const MODEL *model = sim_model_lookup (arg);
52 if (! model)
54 sim_io_eprintf (sd, "unknown model `%s'\n", arg);
55 return SIM_RC_FAIL;
57 sim_model_set (sd, cpu, model);
58 break;
62 return SIM_RC_OK;
65 SIM_RC
66 sim_model_install (SIM_DESC sd)
68 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
70 sim_add_option_table (sd, NULL, model_options);
71 sim_module_add_init_fn (sd, sim_model_init);
73 return SIM_RC_OK;
76 /* Subroutine of sim_model_set to set the model for one cpu. */
78 static void
79 model_set (sim_cpu *cpu, const MODEL *model)
81 CPU_MACH (cpu) = MODEL_MACH (model);
82 CPU_MODEL (cpu) = model;
83 (* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
84 (* MODEL_INIT (model)) (cpu);
87 /* Set the current model of CPU to MODEL.
88 If CPU is NULL, all cpus are set to MODEL. */
90 void
91 sim_model_set (SIM_DESC sd, sim_cpu *cpu, const MODEL *model)
93 if (! cpu)
95 int c;
97 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
98 if (STATE_CPU (sd, c))
99 model_set (STATE_CPU (sd, c), model);
101 else
103 model_set (cpu, model);
107 /* Look up model named NAME.
108 Result is pointer to MODEL entry or NULL if not found. */
110 const MODEL *
111 sim_model_lookup (const char *name)
113 const MACH **machp;
114 const MODEL *model;
116 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
118 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
120 if (strcmp (MODEL_NAME (model), name) == 0)
121 return model;
124 return NULL;
127 /* Look up machine named NAME.
128 Result is pointer to MACH entry or NULL if not found. */
130 const MACH *
131 sim_mach_lookup (const char *name)
133 const MACH **machp;
135 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
137 if (strcmp (MACH_NAME (*machp), name) == 0)
138 return *machp;
140 return NULL;
143 /* Look up a machine via its bfd name.
144 Result is pointer to MACH entry or NULL if not found. */
146 const MACH *
147 sim_mach_lookup_bfd_name (const char *name)
149 const MACH **machp;
151 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
153 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
154 return *machp;
156 return NULL;
159 /* Initialize model support. */
161 static SIM_RC
162 sim_model_init (SIM_DESC sd)
164 SIM_CPU *cpu;
166 /* If both cpu model and state architecture are set, ensure they're
167 compatible. If only one is set, set the other. If neither are set,
168 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
169 for the selected "mach" (bfd terminology). */
171 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
172 /* ??? At present this only supports homogeneous multiprocessors. */
173 cpu = STATE_CPU (sd, 0);
175 if (! STATE_ARCHITECTURE (sd)
176 && ! CPU_MACH (cpu))
178 /* Set the default model. */
179 const MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
180 sim_model_set (sd, NULL, model);
183 if (STATE_ARCHITECTURE (sd)
184 && CPU_MACH (cpu))
186 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
187 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
189 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
190 MODEL_NAME (CPU_MODEL (cpu)),
191 STATE_ARCHITECTURE (sd)->printable_name);
192 return SIM_RC_FAIL;
195 else if (STATE_ARCHITECTURE (sd))
197 /* Use the default model for the selected machine.
198 The default model is the first one in the list. */
199 const MACH *mach = sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd)->printable_name);
201 if (mach == NULL)
203 sim_io_eprintf (sd, "unsupported machine `%s'\n",
204 STATE_ARCHITECTURE (sd)->printable_name);
205 return SIM_RC_FAIL;
207 sim_model_set (sd, NULL, MACH_MODELS (mach));
209 else
211 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
214 return SIM_RC_OK;