1 /* Main simulator entry points specific to Lattice Mico32.
2 Contributed by Jon Beniston <jon@beniston.com>
4 Copyright (C) 2009, 2010 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 static void free_state (SIM_DESC
);
31 static void print_lm32_misc_cpu (SIM_CPU
* cpu
, int verbose
);
32 static DECLARE_OPTION_HANDLER (lm32_option_handler
);
36 OPTION_ENDIAN
= OPTION_START
,
39 /* GDB passes -E, even though it's fixed, so we have to handle it here. common code only handles it if SIM_HAVE_BIENDIAN is defined, which it isn't for lm32. */
40 static const OPTION lm32_options
[] = {
41 {{"endian", required_argument
, NULL
, OPTION_ENDIAN
},
42 'E', "big", "Set endianness",
44 {{NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
47 /* Records simulator descriptor so utilities like lm32_dump_regs can be
49 SIM_DESC current_state
;
51 /* Cover function of sim_state_free to free the cpu buffers as well. */
54 free_state (SIM_DESC sd
)
56 if (STATE_MODULES (sd
) != NULL
)
57 sim_module_uninstall (sd
);
58 sim_cpu_free_all (sd
);
62 /* Find memory range used by program. */
65 find_base (bfd
*prog_bfd
)
68 unsigned long base
= ~(0UL);
72 for (s
= prog_bfd
->sections
; s
; s
= s
->next
)
74 if ((strcmp (bfd_get_section_name (prog_bfd
, s
), ".boot") == 0)
75 || (strcmp (bfd_get_section_name (prog_bfd
, s
), ".text") == 0)
76 || (strcmp (bfd_get_section_name (prog_bfd
, s
), ".data") == 0)
77 || (strcmp (bfd_get_section_name (prog_bfd
, s
), ".bss") == 0))
81 base
= bfd_get_section_vma (prog_bfd
, s
);
86 bfd_get_section_vma (prog_bfd
,
87 s
) < base
? bfd_get_section_vma (prog_bfd
,
91 return base
& ~(0xffffUL
);
95 find_limit (bfd
*prog_bfd
)
97 struct bfd_symbol
**asymbols
;
102 symsize
= bfd_get_symtab_upper_bound (prog_bfd
);
105 asymbols
= (asymbol
**) xmalloc (symsize
);
106 symbol_count
= bfd_canonicalize_symtab (prog_bfd
, asymbols
);
107 if (symbol_count
< 0)
110 for (s
= 0; s
< symbol_count
; s
++)
112 if (!strcmp (asymbols
[s
]->name
, "_fstack"))
113 return (asymbols
[s
]->value
+ 65536) & ~(0xffffUL
);
118 /* Handle lm32 specific options. */
121 lm32_option_handler (sd
, cpu
, opt
, arg
, is_command
)
131 /* Create an instance of the simulator. */
134 sim_open (kind
, callback
, abfd
, argv
)
136 host_callback
*callback
;
140 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
143 unsigned long base
, limit
;
145 /* The cpu data is kept in a separately allocated chunk of memory. */
146 if (sim_cpu_alloc_all (sd
, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK
)
152 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
157 sim_add_option_table (sd
, NULL
, lm32_options
);
159 /* getopt will print the error message so we just have to exit if this fails.
160 FIXME: Hmmm... in the case of gdb we need getopt to call
162 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
169 /* Allocate a handler for I/O devices
170 if no memory for that range has been allocated by the user.
171 All are allocated in one chunk to keep things from being
172 unnecessarily complicated. */
173 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, LM32_DEVICE_ADDR
, 1) == 0)
174 sim_core_attach (sd
, NULL
, 0 /*level */ ,
175 access_read_write
, 0 /*space ??? */ ,
176 LM32_DEVICE_ADDR
, LM32_DEVICE_LEN
/*nr_bytes */ ,
178 &lm32_devices
, NULL
/*buffer */ );
181 /* check for/establish the reference program image. */
182 if (sim_analyze_program (sd
,
183 (STATE_PROG_ARGV (sd
) != NULL
184 ? *STATE_PROG_ARGV (sd
)
185 : NULL
), abfd
) != SIM_RC_OK
)
191 /* Check to see if memory exists at programs start address. */
192 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, STATE_START_ADDR (sd
), 1)
195 if (STATE_PROG_BFD (sd
) != NULL
)
197 /* It doesn't, so we should try to allocate enough memory to hold program. */
198 base
= find_base (STATE_PROG_BFD (sd
));
199 limit
= find_limit (STATE_PROG_BFD (sd
));
203 "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
207 /*sim_io_printf (sd, "Allocating memory at 0x%x size 0x%x\n", base, limit); */
208 sim_do_commandf (sd
, "memory region 0x%x,0x%x", base
, limit
);
212 /* Establish any remaining configuration options. */
213 if (sim_config (sd
) != SIM_RC_OK
)
219 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
225 /* Open a copy of the cpu descriptor table. */
228 lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd
)->printable_name
,
230 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
232 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
233 CPU_CPU_DESC (cpu
) = cd
;
234 CPU_DISASSEMBLER (cpu
) = sim_cgen_disassemble_insn
;
236 lm32_cgen_init_dis (cd
);
239 /* Initialize various cgen things not done by common framework.
240 Must be done after lm32_cgen_cpu_open. */
243 /* Store in a global so things like lm32_dump_regs can be invoked
244 from the gdb command line. */
251 sim_close (sd
, quitting
)
255 lm32_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd
, 0)));
256 sim_module_uninstall (sd
);
260 sim_create_inferior (sd
, abfd
, argv
, envp
)
266 SIM_CPU
*current_cpu
= STATE_CPU (sd
, 0);
270 addr
= bfd_get_start_address (abfd
);
273 sim_pc_set (current_cpu
, addr
);
276 STATE_ARGV (sd
) = sim_copy_argv (argv
);
277 STATE_ENVP (sd
) = sim_copy_argv (envp
);
284 sim_do_command (sd
, cmd
)
288 if (sim_args_command (sd
, cmd
) != SIM_RC_OK
)
289 sim_io_eprintf (sd
, "Unknown command `%s'\n", cmd
);