1 /* Main simulator entry points specific to Lattice Mico32.
2 Contributed by Jon Beniston <jon@beniston.com>
4 Copyright (C) 2009-2016 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "sim-options.h"
23 #include "libiberty.h"
30 /* Cover function of sim_state_free to free the cpu buffers as well. */
33 free_state (SIM_DESC sd
)
35 if (STATE_MODULES (sd
) != NULL
)
36 sim_module_uninstall (sd
);
37 sim_cpu_free_all (sd
);
41 /* Find memory range used by program. */
44 find_base (bfd
*prog_bfd
)
47 unsigned long base
= ~(0UL);
51 for (s
= prog_bfd
->sections
; s
; s
= s
->next
)
53 if ((strcmp (bfd_get_section_name (prog_bfd
, s
), ".boot") == 0)
54 || (strcmp (bfd_get_section_name (prog_bfd
, s
), ".text") == 0)
55 || (strcmp (bfd_get_section_name (prog_bfd
, s
), ".data") == 0)
56 || (strcmp (bfd_get_section_name (prog_bfd
, s
), ".bss") == 0))
60 base
= bfd_get_section_vma (prog_bfd
, s
);
65 bfd_get_section_vma (prog_bfd
,
66 s
) < base
? bfd_get_section_vma (prog_bfd
,
70 return base
& ~(0xffffUL
);
74 find_limit (SIM_DESC sd
)
78 addr
= trace_sym_value (sd
, "_fstack");
82 return (addr
+ 65536) & ~(0xffffUL
);
85 /* Create an instance of the simulator. */
88 sim_open (kind
, callback
, abfd
, argv
)
90 host_callback
*callback
;
94 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
97 unsigned long base
, limit
;
99 /* The cpu data is kept in a separately allocated chunk of memory. */
100 if (sim_cpu_alloc_all (sd
, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK
)
106 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
112 /* The parser will print an error message for us, so we silently return. */
113 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
120 /* Allocate a handler for I/O devices
121 if no memory for that range has been allocated by the user.
122 All are allocated in one chunk to keep things from being
123 unnecessarily complicated. */
124 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, LM32_DEVICE_ADDR
, 1) == 0)
125 sim_core_attach (sd
, NULL
, 0 /*level */ ,
126 access_read_write
, 0 /*space ??? */ ,
127 LM32_DEVICE_ADDR
, LM32_DEVICE_LEN
/*nr_bytes */ ,
129 &lm32_devices
, NULL
/*buffer */ );
132 /* check for/establish the reference program image. */
133 if (sim_analyze_program (sd
,
134 (STATE_PROG_ARGV (sd
) != NULL
135 ? *STATE_PROG_ARGV (sd
)
136 : NULL
), abfd
) != SIM_RC_OK
)
142 /* Check to see if memory exists at programs start address. */
143 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, STATE_START_ADDR (sd
), 1)
146 if (STATE_PROG_BFD (sd
) != NULL
)
148 /* It doesn't, so we should try to allocate enough memory to hold program. */
149 base
= find_base (STATE_PROG_BFD (sd
));
150 limit
= find_limit (sd
);
154 "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
158 /*sim_io_printf (sd, "Allocating memory at 0x%x size 0x%x\n", base, limit); */
159 sim_do_commandf (sd
, "memory region 0x%x,0x%x", base
, limit
);
163 /* Establish any remaining configuration options. */
164 if (sim_config (sd
) != SIM_RC_OK
)
170 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
176 /* Open a copy of the cpu descriptor table. */
179 lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd
)->printable_name
,
181 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
183 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
184 CPU_CPU_DESC (cpu
) = cd
;
185 CPU_DISASSEMBLER (cpu
) = sim_cgen_disassemble_insn
;
187 lm32_cgen_init_dis (cd
);
190 /* Initialize various cgen things not done by common framework.
191 Must be done after lm32_cgen_cpu_open. */
198 sim_create_inferior (sd
, abfd
, argv
, envp
)
204 SIM_CPU
*current_cpu
= STATE_CPU (sd
, 0);
208 addr
= bfd_get_start_address (abfd
);
211 sim_pc_set (current_cpu
, addr
);
213 /* Standalone mode (i.e. `run`) will take care of the argv for us in
214 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
215 with `gdb`), we need to handle it because the user can change the
216 argv on the fly via gdb's 'run'. */
217 if (STATE_PROG_ARGV (sd
) != argv
)
219 freeargv (STATE_PROG_ARGV (sd
));
220 STATE_PROG_ARGV (sd
) = dupargv (argv
);