]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/lm32/sim-if.c
sim: overhaul & unify endian settings management
[thirdparty/binutils-gdb.git] / sim / lm32 / sim-if.c
CommitLineData
c28c63d8
JB
1/* Main simulator entry points specific to Lattice Mico32.
2 Contributed by Jon Beniston <jon@beniston.com>
3
3666a048 4 Copyright (C) 2009-2021 Free Software Foundation, Inc.
c28c63d8
JB
5
6 This file is part of GDB.
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"
23
c28c63d8
JB
24#include "sim-main.h"
25#include "sim-options.h"
26#include "libiberty.h"
27#include "bfd.h"
28
c28c63d8 29#include <stdlib.h>
c28c63d8
JB
30\f
31/* Cover function of sim_state_free to free the cpu buffers as well. */
32
33static void
34free_state (SIM_DESC sd)
35{
36 if (STATE_MODULES (sd) != NULL)
37 sim_module_uninstall (sd);
38 sim_cpu_free_all (sd);
39 sim_state_free (sd);
40}
41
42/* Find memory range used by program. */
43
44static unsigned long
45find_base (bfd *prog_bfd)
46{
47 int found;
48 unsigned long base = ~(0UL);
49 asection *s;
50
51 found = 0;
52 for (s = prog_bfd->sections; s; s = s->next)
53 {
fd361982
AM
54 if ((strcmp (bfd_section_name (s), ".boot") == 0)
55 || (strcmp (bfd_section_name (s), ".text") == 0)
56 || (strcmp (bfd_section_name (s), ".data") == 0)
57 || (strcmp (bfd_section_name (s), ".bss") == 0))
c28c63d8
JB
58 {
59 if (!found)
60 {
fd361982 61 base = bfd_section_vma (s);
c28c63d8
JB
62 found = 1;
63 }
64 else
fd361982 65 base = bfd_section_vma (s) < base ? bfd_section_vma (s) : base;
c28c63d8
JB
66 }
67 }
68 return base & ~(0xffffUL);
69}
70
71static unsigned long
5357150c 72find_limit (SIM_DESC sd)
c28c63d8 73{
5357150c 74 bfd_vma addr;
c28c63d8 75
5357150c
MF
76 addr = trace_sym_value (sd, "_fstack");
77 if (addr == -1)
c28c63d8
JB
78 return 0;
79
5357150c 80 return (addr + 65536) & ~(0xffffUL);
c28c63d8
JB
81}
82
c28c63d8
JB
83/* Create an instance of the simulator. */
84
85SIM_DESC
81e6e8ae
TT
86sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
87 char * const *argv)
c28c63d8
JB
88{
89 SIM_DESC sd = sim_state_alloc (kind, callback);
90 char c;
91 int i;
92 unsigned long base, limit;
93
ba307cdd
MF
94 /* Set default options before parsing user options. */
95 current_alignment = STRICT_ALIGNMENT;
f9a4d543 96 current_target_byte_order = BFD_ENDIAN_BIG;
ba307cdd 97
c28c63d8 98 /* The cpu data is kept in a separately allocated chunk of memory. */
d5a71b11 99 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
c28c63d8
JB
100 {
101 free_state (sd);
102 return 0;
103 }
104
105 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
106 {
107 free_state (sd);
108 return 0;
109 }
c28c63d8 110
77cf2ef5 111 /* The parser will print an error message for us, so we silently return. */
c28c63d8
JB
112 if (sim_parse_args (sd, argv) != SIM_RC_OK)
113 {
114 free_state (sd);
115 return 0;
116 }
117
118#if 0
119 /* Allocate a handler for I/O devices
120 if no memory for that range has been allocated by the user.
121 All are allocated in one chunk to keep things from being
122 unnecessarily complicated. */
123 if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0)
124 sim_core_attach (sd, NULL, 0 /*level */ ,
125 access_read_write, 0 /*space ??? */ ,
126 LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ ,
127 0 /*modulo */ ,
128 &lm32_devices, NULL /*buffer */ );
129#endif
130
131 /* check for/establish the reference program image. */
132 if (sim_analyze_program (sd,
133 (STATE_PROG_ARGV (sd) != NULL
134 ? *STATE_PROG_ARGV (sd)
135 : NULL), abfd) != SIM_RC_OK)
136 {
137 free_state (sd);
138 return 0;
139 }
140
141 /* Check to see if memory exists at programs start address. */
142 if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1)
143 == 0)
144 {
145 if (STATE_PROG_BFD (sd) != NULL)
146 {
147 /* It doesn't, so we should try to allocate enough memory to hold program. */
148 base = find_base (STATE_PROG_BFD (sd));
5357150c 149 limit = find_limit (sd);
c28c63d8
JB
150 if (limit == 0)
151 {
152 sim_io_eprintf (sd,
153 "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
154 free_state (sd);
155 return 0;
156 }
157 /*sim_io_printf (sd, "Allocating memory at 0x%x size 0x%x\n", base, limit); */
158 sim_do_commandf (sd, "memory region 0x%x,0x%x", base, limit);
159 }
160 }
161
162 /* Establish any remaining configuration options. */
163 if (sim_config (sd) != SIM_RC_OK)
164 {
165 free_state (sd);
166 return 0;
167 }
168
169 if (sim_post_argv_init (sd) != SIM_RC_OK)
170 {
171 free_state (sd);
172 return 0;
173 }
174
175 /* Open a copy of the cpu descriptor table. */
176 {
177 CGEN_CPU_DESC cd =
178 lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
179 CGEN_ENDIAN_BIG);
180 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
181 {
182 SIM_CPU *cpu = STATE_CPU (sd, i);
183 CPU_CPU_DESC (cpu) = cd;
184 CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
185 }
186 lm32_cgen_init_dis (cd);
187 }
188
c28c63d8
JB
189 return sd;
190}
c28c63d8
JB
191\f
192SIM_RC
81e6e8ae
TT
193sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv,
194 char * const *envp)
c28c63d8
JB
195{
196 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
197 SIM_ADDR addr;
198
199 if (abfd != NULL)
200 addr = bfd_get_start_address (abfd);
201 else
202 addr = 0;
203 sim_pc_set (current_cpu, addr);
204
0e967299
MF
205 /* Standalone mode (i.e. `run`) will take care of the argv for us in
206 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
207 with `gdb`), we need to handle it because the user can change the
208 argv on the fly via gdb's 'run'. */
209 if (STATE_PROG_ARGV (sd) != argv)
210 {
211 freeargv (STATE_PROG_ARGV (sd));
212 STATE_PROG_ARGV (sd) = dupargv (argv);
213 }
c28c63d8
JB
214
215 return SIM_RC_OK;
216}