]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/riscv/interp.c
sim: riscv: new port
[thirdparty/binutils-gdb.git] / sim / riscv / interp.c
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
21 #include "config.h"
22
23 #include "sim-main.h"
24 #include "sim-options.h"
25 \f
26 void
27 sim_engine_run (SIM_DESC sd,
28 int next_cpu_nr, /* ignore */
29 int nr_cpus, /* ignore */
30 int siggnal) /* ignore */
31 {
32 SIM_CPU *cpu;
33
34 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
35
36 cpu = STATE_CPU (sd, 0);
37
38 while (1)
39 {
40 step_once (cpu);
41 if (sim_events_tick (sd))
42 sim_events_process (sd);
43 }
44 }
45 \f
46 static void
47 free_state (SIM_DESC sd)
48 {
49 if (STATE_MODULES (sd) != NULL)
50 sim_module_uninstall (sd);
51 sim_cpu_free_all (sd);
52 sim_state_free (sd);
53 }
54
55 SIM_DESC
56 sim_open (SIM_OPEN_KIND kind, host_callback *callback,
57 struct bfd *abfd, char * const *argv)
58 {
59 char c;
60 int i;
61 SIM_DESC sd = sim_state_alloc (kind, callback);
62
63 /* The cpu data is kept in a separately allocated chunk of memory. */
64 if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
65 {
66 free_state (sd);
67 return 0;
68 }
69
70 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
71 {
72 free_state (sd);
73 return 0;
74 }
75
76 /* XXX: Default to the Virtual environment. */
77 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
78 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
79
80 /* The parser will print an error message for us, so we silently return. */
81 if (sim_parse_args (sd, argv) != SIM_RC_OK)
82 {
83 free_state (sd);
84 return 0;
85 }
86
87 /* Check for/establish the a reference program image. */
88 if (sim_analyze_program (sd,
89 (STATE_PROG_ARGV (sd) != NULL
90 ? *STATE_PROG_ARGV (sd)
91 : NULL), abfd) != SIM_RC_OK)
92 {
93 free_state (sd);
94 return 0;
95 }
96
97 /* Establish any remaining configuration options. */
98 if (sim_config (sd) != SIM_RC_OK)
99 {
100 free_state (sd);
101 return 0;
102 }
103
104 if (sim_post_argv_init (sd) != SIM_RC_OK)
105 {
106 free_state (sd);
107 return 0;
108 }
109
110 /* CPU specific initialization. */
111 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
112 {
113 SIM_CPU *cpu = STATE_CPU (sd, i);
114
115 initialize_cpu (sd, cpu, i);
116 }
117
118 /* Allocate external memory if none specified by user.
119 Use address 4 here in case the user wanted address 0 unmapped. */
120 if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
121 sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE);
122
123 return sd;
124 }
125 \f
126 SIM_RC
127 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
128 char * const *argv, char * const *env)
129 {
130 SIM_CPU *cpu = STATE_CPU (sd, 0);
131 SIM_ADDR addr;
132
133 /* Set the PC. */
134 if (abfd != NULL)
135 addr = bfd_get_start_address (abfd);
136 else
137 addr = 0;
138 sim_pc_set (cpu, addr);
139
140 /* Standalone mode (i.e. `run`) will take care of the argv for us in
141 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
142 with `gdb`), we need to handle it because the user can change the
143 argv on the fly via gdb's 'run'. */
144 if (STATE_PROG_ARGV (sd) != argv)
145 {
146 freeargv (STATE_PROG_ARGV (sd));
147 STATE_PROG_ARGV (sd) = dupargv (argv);
148 }
149
150 initialize_env (sd, (void *)argv, (void *)env);
151
152 return SIM_RC_OK;
153 }