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