2 Copyright (C) 1996-2021 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/>. */
20 /* This must come before any other includes. */
24 #include "sim-model.h"
25 #include "libiberty.h"
26 #include "sim-options.h"
28 #include "sim-assert.h"
31 static void model_set (sim_cpu
*, const SIM_MODEL
*);
33 static DECLARE_OPTION_HANDLER (model_option_handler
);
35 static MODULE_INIT_FN sim_model_init
;
38 OPTION_MODEL
= OPTION_START
,
42 static const OPTION model_options
[] = {
43 { {"model", required_argument
, NULL
, OPTION_MODEL
},
44 '\0', "MODEL", "Specify model to simulate",
45 model_option_handler
, NULL
},
47 { {"model-info", no_argument
, NULL
, OPTION_MODEL_INFO
},
48 '\0', NULL
, "List selectable models",
49 model_option_handler
, NULL
},
50 { {"info-model", no_argument
, NULL
, OPTION_MODEL_INFO
},
52 model_option_handler
, NULL
},
54 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
58 model_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
59 char *arg
, int is_command
)
65 const SIM_MODEL
*model
= sim_model_lookup (arg
);
68 sim_io_eprintf (sd
, "unknown model `%s'\n", arg
);
71 sim_model_set (sd
, cpu
, model
);
75 case OPTION_MODEL_INFO
:
77 const SIM_MACH
**machp
;
78 const SIM_MODEL
*model
;
79 for (machp
= & sim_machs
[0]; *machp
!= NULL
; ++machp
)
81 sim_io_printf (sd
, "Models for architecture `%s':\n",
83 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
;
85 sim_io_printf (sd
, " %s", MODEL_NAME (model
));
86 sim_io_printf (sd
, "\n");
96 sim_model_install (SIM_DESC sd
)
98 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
100 sim_add_option_table (sd
, NULL
, model_options
);
101 sim_module_add_init_fn (sd
, sim_model_init
);
106 /* Subroutine of sim_model_set to set the model for one cpu. */
109 model_set (sim_cpu
*cpu
, const SIM_MODEL
*model
)
111 CPU_MACH (cpu
) = MODEL_MACH (model
);
112 CPU_MODEL (cpu
) = model
;
113 (* MACH_INIT_CPU (MODEL_MACH (model
))) (cpu
);
114 (* MODEL_INIT (model
)) (cpu
);
117 /* Set the current model of CPU to MODEL.
118 If CPU is NULL, all cpus are set to MODEL. */
121 sim_model_set (SIM_DESC sd
, sim_cpu
*cpu
, const SIM_MODEL
*model
)
127 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
128 if (STATE_CPU (sd
, c
))
129 model_set (STATE_CPU (sd
, c
), model
);
133 model_set (cpu
, model
);
137 /* Look up model named NAME.
138 Result is pointer to MODEL entry or NULL if not found. */
141 sim_model_lookup (const char *name
)
143 const SIM_MACH
**machp
;
144 const SIM_MODEL
*model
;
146 for (machp
= & sim_machs
[0]; *machp
!= NULL
; ++machp
)
148 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
; ++model
)
150 if (strcmp (MODEL_NAME (model
), name
) == 0)
157 /* Look up machine named NAME.
158 Result is pointer to MACH entry or NULL if not found. */
161 sim_mach_lookup (const char *name
)
163 const SIM_MACH
**machp
;
165 for (machp
= & sim_machs
[0]; *machp
!= NULL
; ++machp
)
167 if (strcmp (MACH_NAME (*machp
), name
) == 0)
173 /* Look up a machine via its bfd name.
174 Result is pointer to MACH entry or NULL if not found. */
177 sim_mach_lookup_bfd_name (const char *name
)
179 const SIM_MACH
**machp
;
181 for (machp
= & sim_machs
[0]; *machp
!= NULL
; ++machp
)
183 if (strcmp (MACH_BFD_NAME (*machp
), name
) == 0)
189 /* Initialize model support. */
192 sim_model_init (SIM_DESC sd
)
199 /* If both cpu model and state architecture are set, ensure they're
200 compatible. If only one is set, set the other. If neither are set,
201 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
202 for the selected "mach" (bfd terminology). */
204 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
205 /* ??? At present this only supports homogeneous multiprocessors. */
206 cpu
= STATE_CPU (sd
, 0);
208 if (! STATE_ARCHITECTURE (sd
)
211 /* Set the default model. */
212 const SIM_MODEL
*model
= sim_model_lookup (WITH_DEFAULT_MODEL
);
213 SIM_ASSERT (model
!= NULL
);
214 sim_model_set (sd
, NULL
, model
);
217 if (STATE_ARCHITECTURE (sd
)
220 if (strcmp (STATE_ARCHITECTURE (sd
)->printable_name
,
221 MACH_BFD_NAME (CPU_MACH (cpu
))) != 0)
223 sim_io_eprintf (sd
, "invalid model `%s' for `%s'\n",
224 MODEL_NAME (CPU_MODEL (cpu
)),
225 STATE_ARCHITECTURE (sd
)->printable_name
);
229 else if (STATE_ARCHITECTURE (sd
))
231 /* Use the default model for the selected machine.
232 The default model is the first one in the list. */
233 const SIM_MACH
*mach
= sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd
)->printable_name
);
237 sim_io_eprintf (sd
, "unsupported machine `%s'\n",
238 STATE_ARCHITECTURE (sd
)->printable_name
);
241 sim_model_set (sd
, NULL
, MACH_MODELS (mach
));
245 STATE_ARCHITECTURE (sd
) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu
)));
252 /* Set up basic model support. This is a stub for ports that do not define
253 models. See sim-model.h for more details. */
254 const SIM_MACH
*sim_machs
[] =