1 /* Model support.
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
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.
11
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.
16
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. */
20
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"
27
28 static void model_set (sim_cpu *, const MODEL *);
29
30 static DECLARE_OPTION_HANDLER (model_option_handler);
31
32 static MODULE_INIT_FN sim_model_init;
33
34 #define OPTION_MODEL (OPTION_START + 0)
35
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 }
41 };
42
43 static SIM_RC
model_option_handler(SIM_DESC sd,sim_cpu * cpu,int opt,char * arg,int is_command)44 model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
45 char *arg, int is_command)
46 {
47 switch (opt)
48 {
49 case OPTION_MODEL :
50 {
51 const MODEL *model = sim_model_lookup (arg);
52 if (! model)
53 {
54 sim_io_eprintf (sd, "unknown model `%s'\n", arg);
55 return SIM_RC_FAIL;
56 }
57 sim_model_set (sd, cpu, model);
58 break;
59 }
60 }
61
62 return SIM_RC_OK;
63 }
64
65 SIM_RC
sim_model_install(SIM_DESC sd)66 sim_model_install (SIM_DESC sd)
67 {
68 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
69
70 sim_add_option_table (sd, NULL, model_options);
71 sim_module_add_init_fn (sd, sim_model_init);
72
73 return SIM_RC_OK;
74 }
75
76 /* Subroutine of sim_model_set to set the model for one cpu. */
77
78 static void
model_set(sim_cpu * cpu,const MODEL * model)79 model_set (sim_cpu *cpu, const MODEL *model)
80 {
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);
85 }
86
87 /* Set the current model of CPU to MODEL.
88 If CPU is NULL, all cpus are set to MODEL. */
89
90 void
sim_model_set(SIM_DESC sd,sim_cpu * cpu,const MODEL * model)91 sim_model_set (SIM_DESC sd, sim_cpu *cpu, const MODEL *model)
92 {
93 if (! cpu)
94 {
95 int c;
96
97 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
98 if (STATE_CPU (sd, c))
99 model_set (STATE_CPU (sd, c), model);
100 }
101 else
102 {
103 model_set (cpu, model);
104 }
105 }
106
107 /* Look up model named NAME.
108 Result is pointer to MODEL entry or NULL if not found. */
109
110 const MODEL *
sim_model_lookup(const char * name)111 sim_model_lookup (const char *name)
112 {
113 const MACH **machp;
114 const MODEL *model;
115
116 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
117 {
118 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
119 {
120 if (strcmp (MODEL_NAME (model), name) == 0)
121 return model;
122 }
123 }
124 return NULL;
125 }
126
127 /* Look up machine named NAME.
128 Result is pointer to MACH entry or NULL if not found. */
129
130 const MACH *
sim_mach_lookup(const char * name)131 sim_mach_lookup (const char *name)
132 {
133 const MACH **machp;
134
135 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
136 {
137 if (strcmp (MACH_NAME (*machp), name) == 0)
138 return *machp;
139 }
140 return NULL;
141 }
142
143 /* Look up a machine via its bfd name.
144 Result is pointer to MACH entry or NULL if not found. */
145
146 const MACH *
sim_mach_lookup_bfd_name(const char * name)147 sim_mach_lookup_bfd_name (const char *name)
148 {
149 const MACH **machp;
150
151 for (machp = & sim_machs[0]; *machp != NULL; ++machp)
152 {
153 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
154 return *machp;
155 }
156 return NULL;
157 }
158
159 /* Initialize model support. */
160
161 static SIM_RC
sim_model_init(SIM_DESC sd)162 sim_model_init (SIM_DESC sd)
163 {
164 SIM_CPU *cpu;
165
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). */
170
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);
174
175 if (! STATE_ARCHITECTURE (sd)
176 && ! CPU_MACH (cpu))
177 {
178 /* Set the default model. */
179 const MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
180 sim_model_set (sd, NULL, model);
181 }
182
183 if (STATE_ARCHITECTURE (sd)
184 && CPU_MACH (cpu))
185 {
186 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
187 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
188 {
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;
193 }
194 }
195 else if (STATE_ARCHITECTURE (sd))
196 {
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);
200
201 if (mach == NULL)
202 {
203 sim_io_eprintf (sd, "unsupported machine `%s'\n",
204 STATE_ARCHITECTURE (sd)->printable_name);
205 return SIM_RC_FAIL;
206 }
207 sim_model_set (sd, NULL, MACH_MODELS (mach));
208 }
209 else
210 {
211 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
212 }
213
214 return SIM_RC_OK;
215 }
216