]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/example-synacor/interp.c
sim: split program path out of argv vector
[thirdparty/binutils-gdb.git] / sim / example-synacor / interp.c
1 /* Example synacor 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 /* This file contains the main glue logic between the sim core and the target
22 specific simulator. Normally this file will be kept small and the target
23 details will live in other files.
24
25 For more specific details on these functions, see the sim/sim.h header
26 file. */
27
28 /* This must come before any other includes. */
29 #include "defs.h"
30
31 #include "sim-main.h"
32 #include "sim-options.h"
33 \f
34 /* This function is the main loop. It should process ticks and decode+execute
35 a single instruction.
36
37 Usually you do not need to change things here. */
38
39 void
40 sim_engine_run (SIM_DESC sd,
41 int next_cpu_nr, /* ignore */
42 int nr_cpus, /* ignore */
43 int siggnal) /* ignore */
44 {
45 SIM_CPU *cpu;
46
47 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
48
49 cpu = STATE_CPU (sd, 0);
50
51 while (1)
52 {
53 step_once (cpu);
54 if (sim_events_tick (sd))
55 sim_events_process (sd);
56 }
57 }
58 \f
59 /* Initialize the simulator from scratch. This is called once per lifetime of
60 the simulation. Think of it as a processor reset.
61
62 Usually all cpu-specific setup is handled in the initialize_cpu callback.
63 If you want to do cpu-independent stuff, then it should go at the end (see
64 where memory is initialized). */
65
66 #define DEFAULT_MEM_SIZE (16 * 1024 * 1024)
67
68 static void
69 free_state (SIM_DESC sd)
70 {
71 if (STATE_MODULES (sd) != NULL)
72 sim_module_uninstall (sd);
73 sim_cpu_free_all (sd);
74 sim_state_free (sd);
75 }
76
77 SIM_DESC
78 sim_open (SIM_OPEN_KIND kind, host_callback *callback,
79 struct bfd *abfd, char * const *argv)
80 {
81 char c;
82 int i;
83 SIM_DESC sd = sim_state_alloc (kind, callback);
84
85 /* Set default options before parsing user options. */
86 current_alignment = STRICT_ALIGNMENT;
87 current_target_byte_order = BFD_ENDIAN_LITTLE;
88
89 /* The cpu data is kept in a separately allocated chunk of memory. */
90 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
91 {
92 free_state (sd);
93 return 0;
94 }
95
96 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
97 {
98 free_state (sd);
99 return 0;
100 }
101
102 /* XXX: Default to the Virtual environment. */
103 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
104 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
105
106 /* The parser will print an error message for us, so we silently return. */
107 if (sim_parse_args (sd, argv) != SIM_RC_OK)
108 {
109 free_state (sd);
110 return 0;
111 }
112
113 /* Check for/establish the a reference program image. */
114 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
115 {
116 free_state (sd);
117 return 0;
118 }
119
120 /* Establish any remaining configuration options. */
121 if (sim_config (sd) != SIM_RC_OK)
122 {
123 free_state (sd);
124 return 0;
125 }
126
127 if (sim_post_argv_init (sd) != SIM_RC_OK)
128 {
129 free_state (sd);
130 return 0;
131 }
132
133 /* CPU specific initialization. */
134 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
135 {
136 SIM_CPU *cpu = STATE_CPU (sd, i);
137
138 initialize_cpu (sd, cpu);
139 }
140
141 /* Allocate external memory if none specified by user.
142 Use address 4 here in case the user wanted address 0 unmapped. */
143 if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
144 sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE);
145
146 return sd;
147 }
148 \f
149 /* Prepare to run a program that has already been loaded into memory.
150
151 Usually you do not need to change things here. */
152
153 SIM_RC
154 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
155 char * const *argv, char * const *env)
156 {
157 SIM_CPU *cpu = STATE_CPU (sd, 0);
158 sim_cia addr;
159
160 /* Set the PC. */
161 if (abfd != NULL)
162 addr = bfd_get_start_address (abfd);
163 else
164 addr = 0;
165 sim_pc_set (cpu, addr);
166
167 /* Standalone mode (i.e. `run`) will take care of the argv for us in
168 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
169 with `gdb`), we need to handle it because the user can change the
170 argv on the fly via gdb's 'run'. */
171 if (STATE_PROG_ARGV (sd) != argv)
172 {
173 freeargv (STATE_PROG_ARGV (sd));
174 STATE_PROG_ARGV (sd) = dupargv (argv);
175 }
176
177 return SIM_RC_OK;
178 }