2 Copyright (C) 1996-2024 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 "libiberty.h"
27 #include "sim-model.h"
28 #include "sim-options.h"
30 #include "sim-assert.h"
32 static void model_set (sim_cpu
*, const SIM_MODEL
*);
34 static DECLARE_OPTION_HANDLER (model_option_handler
);
36 static MODULE_INIT_FN sim_model_init
;
39 OPTION_MODEL
= OPTION_START
,
43 static const OPTION model_options
[] = {
44 { {"model", required_argument
, NULL
, OPTION_MODEL
},
45 '\0', "MODEL", "Specify model to simulate",
46 model_option_handler
, NULL
},
48 { {"model-info", no_argument
, NULL
, OPTION_MODEL_INFO
},
49 '\0', NULL
, "List selectable models",
50 model_option_handler
, NULL
},
51 { {"info-model", no_argument
, NULL
, OPTION_MODEL_INFO
},
53 model_option_handler
, NULL
},
55 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
59 model_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
60 char *arg
, int is_command
)
66 const SIM_MODEL
*model
= sim_model_lookup (sd
, arg
);
69 sim_io_eprintf (sd
, "unknown model `%s'\n", arg
);
72 STATE_MODEL_NAME (sd
) = arg
;
73 sim_model_set (sd
, cpu
, model
);
77 case OPTION_MODEL_INFO
:
79 const SIM_MACH
* const *machp
;
80 const SIM_MODEL
*model
;
82 if (STATE_MACHS (sd
) == NULL
)
84 sim_io_printf (sd
, "This target does not support any models\n");
88 for (machp
= STATE_MACHS(sd
); *machp
!= NULL
; ++machp
)
90 sim_io_printf (sd
, "Models for architecture `%s':\n",
92 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
;
94 sim_io_printf (sd
, " %s", MODEL_NAME (model
));
95 sim_io_printf (sd
, "\n");
105 sim_model_install (SIM_DESC sd
)
107 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
109 sim_add_option_table (sd
, NULL
, model_options
);
110 sim_module_add_init_fn (sd
, sim_model_init
);
115 /* Subroutine of sim_model_set to set the model for one cpu. */
118 model_set (sim_cpu
*cpu
, const SIM_MODEL
*model
)
120 CPU_MACH (cpu
) = MODEL_MACH (model
);
121 CPU_MODEL (cpu
) = model
;
122 (* MACH_INIT_CPU (MODEL_MACH (model
))) (cpu
);
123 (* MODEL_INIT (model
)) (cpu
);
126 /* Set the current model of CPU to MODEL.
127 If CPU is NULL, all cpus are set to MODEL. */
130 sim_model_set (SIM_DESC sd
, sim_cpu
*cpu
, const SIM_MODEL
*model
)
136 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
137 if (STATE_CPU (sd
, c
))
138 model_set (STATE_CPU (sd
, c
), model
);
142 model_set (cpu
, model
);
146 /* Look up model named NAME.
147 Result is pointer to MODEL entry or NULL if not found. */
150 sim_model_lookup (SIM_DESC sd
, const char *name
)
152 const SIM_MACH
* const *machp
;
153 const SIM_MODEL
*model
;
155 if (STATE_MACHS (sd
) == NULL
)
158 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
160 for (model
= MACH_MODELS (*machp
); MODEL_NAME (model
) != NULL
; ++model
)
162 if (strcmp (MODEL_NAME (model
), name
) == 0)
169 /* Look up machine named NAME.
170 Result is pointer to MACH entry or NULL if not found. */
173 sim_mach_lookup (SIM_DESC sd
, const char *name
)
175 const SIM_MACH
* const *machp
;
177 if (STATE_MACHS (sd
) == NULL
)
180 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
182 if (strcmp (MACH_NAME (*machp
), name
) == 0)
188 /* Look up a machine via its bfd name.
189 Result is pointer to MACH entry or NULL if not found. */
192 sim_mach_lookup_bfd_name (SIM_DESC sd
, const char *name
)
194 const SIM_MACH
* const *machp
;
196 if (STATE_MACHS (sd
) == NULL
)
199 for (machp
= STATE_MACHS (sd
); *machp
!= NULL
; ++machp
)
201 if (strcmp (MACH_BFD_NAME (*machp
), name
) == 0)
207 /* Initialize model support. */
210 sim_model_init (SIM_DESC sd
)
214 /* If both cpu model and state architecture are set, ensure they're
215 compatible. If only one is set, set the other. If neither are set,
216 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
217 for the selected "mach" (bfd terminology). */
219 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
220 /* ??? At present this only supports homogeneous multiprocessors. */
221 cpu
= STATE_CPU (sd
, 0);
223 if (! STATE_ARCHITECTURE (sd
)
225 && STATE_MODEL_NAME (sd
))
227 /* Set the default model. */
228 const SIM_MODEL
*model
= sim_model_lookup (sd
, STATE_MODEL_NAME (sd
));
229 SIM_ASSERT (model
!= NULL
);
230 sim_model_set (sd
, NULL
, model
);
233 if (STATE_ARCHITECTURE (sd
)
236 if (strcmp (STATE_ARCHITECTURE (sd
)->printable_name
,
237 MACH_BFD_NAME (CPU_MACH (cpu
))) != 0)
239 sim_io_eprintf (sd
, "invalid model `%s' for `%s'\n",
240 MODEL_NAME (CPU_MODEL (cpu
)),
241 STATE_ARCHITECTURE (sd
)->printable_name
);
245 else if (STATE_ARCHITECTURE (sd
) && STATE_MACHS (sd
))
247 /* Use the default model for the selected machine.
248 The default model is the first one in the list. */
249 const SIM_MACH
*mach
=
250 sim_mach_lookup_bfd_name (sd
, STATE_ARCHITECTURE (sd
)->printable_name
);
254 sim_io_eprintf (sd
, "unsupported machine `%s'\n",
255 STATE_ARCHITECTURE (sd
)->printable_name
);
258 sim_model_set (sd
, NULL
, MACH_MODELS (mach
));
260 else if (CPU_MACH (cpu
))
262 STATE_ARCHITECTURE (sd
) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu
)));