Automatic date update in version.in
[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;
10c23a2c
MF
62 SIM_DESC sd = sim_state_alloc_extra (kind, callback,
63 sizeof (struct riscv_sim_state));
b9249c46
MF
64
65 /* The cpu data is kept in a separately allocated chunk of memory. */
d5a71b11 66 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
b9249c46
MF
67 {
68 free_state (sd);
69 return 0;
70 }
71
72 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
73 {
74 free_state (sd);
75 return 0;
76 }
77
78 /* XXX: Default to the Virtual environment. */
79 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
80 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
81
82 /* The parser will print an error message for us, so we silently return. */
83 if (sim_parse_args (sd, argv) != SIM_RC_OK)
84 {
85 free_state (sd);
86 return 0;
87 }
88
89 /* Check for/establish the a reference program image. */
90 if (sim_analyze_program (sd,
91 (STATE_PROG_ARGV (sd) != NULL
92 ? *STATE_PROG_ARGV (sd)
93 : NULL), abfd) != SIM_RC_OK)
94 {
95 free_state (sd);
96 return 0;
97 }
98
99 /* Establish any remaining configuration options. */
100 if (sim_config (sd) != SIM_RC_OK)
101 {
102 free_state (sd);
103 return 0;
104 }
105
106 if (sim_post_argv_init (sd) != SIM_RC_OK)
107 {
108 free_state (sd);
109 return 0;
110 }
111
112 /* CPU specific initialization. */
113 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
114 {
115 SIM_CPU *cpu = STATE_CPU (sd, i);
116
117 initialize_cpu (sd, cpu, i);
118 }
119
120 /* Allocate external memory if none specified by user.
121 Use address 4 here in case the user wanted address 0 unmapped. */
122 if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
123 sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE);
124
125 return sd;
126}
127\f
128SIM_RC
129sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
130 char * const *argv, char * const *env)
131{
132 SIM_CPU *cpu = STATE_CPU (sd, 0);
133 SIM_ADDR addr;
134
135 /* Set the PC. */
136 if (abfd != NULL)
137 addr = bfd_get_start_address (abfd);
138 else
139 addr = 0;
140 sim_pc_set (cpu, addr);
141
142 /* Standalone mode (i.e. `run`) will take care of the argv for us in
143 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
144 with `gdb`), we need to handle it because the user can change the
145 argv on the fly via gdb's 'run'. */
146 if (STATE_PROG_ARGV (sd) != argv)
147 {
148 freeargv (STATE_PROG_ARGV (sd));
149 STATE_PROG_ARGV (sd) = dupargv (argv);
150 }
151
152 initialize_env (sd, (void *)argv, (void *)env);
153
154 return SIM_RC_OK;
155}
This page took 0.05193 seconds and 4 git commands to generate.