2 Copyright (C) 1996-2020 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 SIM_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 SIM_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
:
74 const SIM_MACH
**machp
;
75 const SIM_MODEL
*model
;
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 SIM_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 SIM_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
)
140 const SIM_MACH
**machp
;
141 const SIM_MODEL
*model
;
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
)
160 const SIM_MACH
**machp
;
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
)
176 const SIM_MACH
**machp
;
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
)
196 /* If both cpu model and state architecture are set, ensure they're
197 compatible. If only one is set, set the other. If neither are set,
198 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
199 for the selected "mach" (bfd terminology). */
201 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
202 /* ??? At present this only supports homogeneous multiprocessors. */
203 cpu
= STATE_CPU (sd
, 0);
205 if (! STATE_ARCHITECTURE (sd
)
208 /* Set the default model. */
209 const SIM_MODEL
*model
= sim_model_lookup (WITH_DEFAULT_MODEL
);
210 SIM_ASSERT (model
!= NULL
);
211 sim_model_set (sd
, NULL
, model
);
214 if (STATE_ARCHITECTURE (sd
)
217 if (strcmp (STATE_ARCHITECTURE (sd
)->printable_name
,
218 MACH_BFD_NAME (CPU_MACH (cpu
))) != 0)
220 sim_io_eprintf (sd
, "invalid model `%s' for `%s'\n",
221 MODEL_NAME (CPU_MODEL (cpu
)),
222 STATE_ARCHITECTURE (sd
)->printable_name
);
226 else if (STATE_ARCHITECTURE (sd
))
228 /* Use the default model for the selected machine.
229 The default model is the first one in the list. */
230 const SIM_MACH
*mach
= sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd
)->printable_name
);
234 sim_io_eprintf (sd
, "unsupported machine `%s'\n",
235 STATE_ARCHITECTURE (sd
)->printable_name
);
238 sim_model_set (sd
, NULL
, MACH_MODELS (mach
));
242 STATE_ARCHITECTURE (sd
) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu
)));
249 /* Set up basic model support. This is a stub for ports that do not define
250 models. See sim-model.h for more details. */
251 const SIM_MACH
*sim_machs
[] =