* Make-common.in (CGEN_INCLUDE_DEPS): Add cgen-defs.h, cgen-engine.h.
[external/binutils.git] / sim / common / sim-model.c
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_DESC, 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
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'", 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
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
79 model_set (SIM_DESC sd, 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
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 (sd, STATE_CPU (sd, c), model);
100     }
101   else
102     {
103       model_set (sd, 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 *
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 *
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 /* Initialize model support.  */
144
145 static SIM_RC
146 sim_model_init (SIM_DESC sd)
147 {
148   SIM_CPU *cpu;
149
150   /* If both cpu model and state architecture are set, ensure they're
151      compatible.  If only one is set, set the other.  If neither are set,
152      use the default model.  STATE_ARCHITECTURE is the bfd_arch_info data
153      for the selected "mach" (bfd terminology).  */
154
155   /* Only check cpu 0.  STATE_ARCHITECTURE is for that one only.  */
156   /* ??? At present this only supports homogeneous multiprocessors.  */
157   cpu = STATE_CPU (sd, 0);
158
159   if (! STATE_ARCHITECTURE (sd)
160       && ! CPU_MACH (cpu))
161     {
162       /* Set the default model.  */
163       const MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
164       sim_model_set (sd, NULL, model);
165     }
166
167   if (STATE_ARCHITECTURE (sd)
168       && CPU_MACH (cpu))
169     {
170       if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
171                   MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
172         {
173           sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
174                           MODEL_NAME (CPU_MODEL (cpu)),
175                           STATE_ARCHITECTURE (sd)->printable_name);
176           return SIM_RC_FAIL;
177         }
178     }
179   else if (STATE_ARCHITECTURE (sd))
180     {
181       /* Use the default model for the selected machine.
182          The default model is the first one in the list.  */
183       const MACH *mach = sim_mach_lookup (STATE_ARCHITECTURE (sd)->printable_name);
184       sim_model_set (sd, NULL, MACH_MODELS (mach));
185     }
186   else
187     {
188       STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
189     }
190
191   return SIM_RC_OK;
192 }