sim: h8300: invert sim_state storage
[deliverable/binutils-gdb.git] / sim / riscv / interp.c
CommitLineData
b9249c46
MF
1/* RISC-V simulator.
2
3 Copyright (C) 2005-2021 Free Software Foundation, Inc.
4 Contributed by Mike Frysinger.
5
6 This file is part of simulators.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
6df01ab8
MF
21/* This must come before any other includes. */
22#include "defs.h"
b9249c46
MF
23
24#include "sim-main.h"
25#include "sim-options.h"
26\f
27void
28sim_engine_run (SIM_DESC sd,
29 int next_cpu_nr, /* ignore */
30 int nr_cpus, /* ignore */
31 int siggnal) /* ignore */
32{
33 SIM_CPU *cpu;
34
35 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
36
37 cpu = STATE_CPU (sd, 0);
38
39 while (1)
40 {
41 step_once (cpu);
42 if (sim_events_tick (sd))
43 sim_events_process (sd);
44 }
45}
46\f
47static void
48free_state (SIM_DESC sd)
49{
50 if (STATE_MODULES (sd) != NULL)
51 sim_module_uninstall (sd);
52 sim_cpu_free_all (sd);
53 sim_state_free (sd);
54}
55
56SIM_DESC
57sim_open (SIM_OPEN_KIND kind, host_callback *callback,
58 struct bfd *abfd, char * const *argv)
59{
60 char c;
61 int i;
62 SIM_DESC sd = sim_state_alloc (kind, callback);
63
64 /* The cpu data is kept in a separately allocated chunk of memory. */
d5a71b11 65 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
b9249c46
MF
66 {
67 free_state (sd);
68 return 0;
69 }
70
71 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
72 {
73 free_state (sd);
74 return 0;
75 }
76
77 /* XXX: Default to the Virtual environment. */
78 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
79 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
80
81 /* The parser will print an error message for us, so we silently return. */
82 if (sim_parse_args (sd, argv) != SIM_RC_OK)
83 {
84 free_state (sd);
85 return 0;
86 }
87
88 /* Check for/establish the a reference program image. */
89 if (sim_analyze_program (sd,
90 (STATE_PROG_ARGV (sd) != NULL
91 ? *STATE_PROG_ARGV (sd)
92 : NULL), abfd) != SIM_RC_OK)
93 {
94 free_state (sd);
95 return 0;
96 }
97
98 /* Establish any remaining configuration options. */
99 if (sim_config (sd) != SIM_RC_OK)
100 {
101 free_state (sd);
102 return 0;
103 }
104
105 if (sim_post_argv_init (sd) != SIM_RC_OK)
106 {
107 free_state (sd);
108 return 0;
109 }
110
111 /* CPU specific initialization. */
112 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
113 {
114 SIM_CPU *cpu = STATE_CPU (sd, i);
115
116 initialize_cpu (sd, cpu, i);
117 }
118
119 /* Allocate external memory if none specified by user.
120 Use address 4 here in case the user wanted address 0 unmapped. */
121 if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
122 sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE);
123
124 return sd;
125}
126\f
127SIM_RC
128sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
129 char * const *argv, char * const *env)
130{
131 SIM_CPU *cpu = STATE_CPU (sd, 0);
132 SIM_ADDR addr;
133
134 /* Set the PC. */
135 if (abfd != NULL)
136 addr = bfd_get_start_address (abfd);
137 else
138 addr = 0;
139 sim_pc_set (cpu, addr);
140
141 /* Standalone mode (i.e. `run`) will take care of the argv for us in
142 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
143 with `gdb`), we need to handle it because the user can change the
144 argv on the fly via gdb's 'run'. */
145 if (STATE_PROG_ARGV (sd) != argv)
146 {
147 freeargv (STATE_PROG_ARGV (sd));
148 STATE_PROG_ARGV (sd) = dupargv (argv);
149 }
150
151 initialize_env (sd, (void *)argv, (void *)env);
152
153 return SIM_RC_OK;
154}
This page took 0.042504 seconds and 4 git commands to generate.