#include "sim-main.h"
#include "libiberty.h"
#include "sim-options.h"
#include "sim-io.h"
#include "sim-assert.h"
#include "bfd.h"
static void model_set (sim_cpu *, const MODEL *);
static DECLARE_OPTION_HANDLER (model_option_handler);
static MODULE_INIT_FN sim_model_init;
#define OPTION_MODEL (OPTION_START + 0)
static const OPTION model_options[] = {
{ {"model", required_argument, NULL, OPTION_MODEL},
'\0', "MODEL", "Specify model to simulate",
model_option_handler },
{ {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
};
static SIM_RC
model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
char *arg, int is_command)
{
switch (opt)
{
case OPTION_MODEL :
{
const MODEL *model = sim_model_lookup (arg);
if (! model)
{
sim_io_eprintf (sd, "unknown model `%s'\n", arg);
return SIM_RC_FAIL;
}
sim_model_set (sd, cpu, model);
break;
}
}
return SIM_RC_OK;
}
SIM_RC
sim_model_install (SIM_DESC sd)
{
SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
sim_add_option_table (sd, NULL, model_options);
sim_module_add_init_fn (sd, sim_model_init);
return SIM_RC_OK;
}
static void
model_set (sim_cpu *cpu, const MODEL *model)
{
CPU_MACH (cpu) = MODEL_MACH (model);
CPU_MODEL (cpu) = model;
(* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
(* MODEL_INIT (model)) (cpu);
}
void
sim_model_set (SIM_DESC sd, sim_cpu *cpu, const MODEL *model)
{
if (! cpu)
{
int c;
for (c = 0; c < MAX_NR_PROCESSORS; ++c)
if (STATE_CPU (sd, c))
model_set (STATE_CPU (sd, c), model);
}
else
{
model_set (cpu, model);
}
}
const MODEL *
sim_model_lookup (const char *name)
{
const MACH **machp;
const MODEL *model;
for (machp = & sim_machs[0]; *machp != NULL; ++machp)
{
for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
{
if (strcmp (MODEL_NAME (model), name) == 0)
return model;
}
}
return NULL;
}
const MACH *
sim_mach_lookup (const char *name)
{
const MACH **machp;
for (machp = & sim_machs[0]; *machp != NULL; ++machp)
{
if (strcmp (MACH_NAME (*machp), name) == 0)
return *machp;
}
return NULL;
}
const MACH *
sim_mach_lookup_bfd_name (const char *name)
{
const MACH **machp;
for (machp = & sim_machs[0]; *machp != NULL; ++machp)
{
if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
return *machp;
}
return NULL;
}
static SIM_RC
sim_model_init (SIM_DESC sd)
{
SIM_CPU *cpu;
cpu = STATE_CPU (sd, 0);
if (! STATE_ARCHITECTURE (sd)
&& ! CPU_MACH (cpu))
{
const MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
sim_model_set (sd, NULL, model);
}
if (STATE_ARCHITECTURE (sd)
&& CPU_MACH (cpu))
{
if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
{
sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
MODEL_NAME (CPU_MODEL (cpu)),
STATE_ARCHITECTURE (sd)->printable_name);
return SIM_RC_FAIL;
}
}
else if (STATE_ARCHITECTURE (sd))
{
const MACH *mach = sim_mach_lookup_bfd_name (STATE_ARCHITECTURE (sd)->printable_name);
if (mach == NULL)
{
sim_io_eprintf (sd, "unsupported machine `%s'\n",
STATE_ARCHITECTURE (sd)->printable_name);
return SIM_RC_FAIL;
}
sim_model_set (sd, NULL, MACH_MODELS (mach));
}
else
{
STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
}
return SIM_RC_OK;
}