1 /* Simulator memory option handling.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "sim-assert.h"
23 #include "sim-options.h"
36 /* Memory command line options. */
39 OPTION_MEMORY_DELETE = OPTION_START,
47 static DECLARE_OPTION_HANDLER (memory_option_handler);
49 static const OPTION memory_options[] =
51 { {"memory-delete", required_argument, NULL, OPTION_MEMORY_DELETE },
52 '\0', "ADDRESS|all", "Delete memory at ADDRESS (all addresses)",
53 memory_option_handler },
54 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
55 '\0', "ADDRESS", NULL,
56 memory_option_handler },
58 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
59 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
60 memory_option_handler },
62 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
63 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
64 memory_option_handler },
66 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
67 '\0', "SIZE", "Add memory at address zero",
68 memory_option_handler },
70 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
71 '\0', NULL, "Clear all memory regions",
72 memory_option_handler },
74 { {"memory-info", no_argument, NULL, OPTION_MEMORY_INFO },
75 '\0', NULL, "List configurable memory regions",
76 memory_option_handler },
77 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
79 memory_option_handler },
81 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
86 do_memopt_add (SIM_DESC sd,
90 address_word nr_bytes,
95 sim_core_attach (sd, NULL,
96 level, access_read_write_exec, space,
97 addr, nr_bytes, modulo, NULL, buffer);
98 while ((*entry) != NULL)
99 entry = &(*entry)->next;
100 (*entry) = ZALLOC (sim_memopt);
101 (*entry)->level = level;
102 (*entry)->space = space;
103 (*entry)->addr = addr;
104 (*entry)->nr_bytes = nr_bytes;
105 (*entry)->modulo = modulo;
106 (*entry)->buffer = buffer;
111 do_memopt_delete (SIM_DESC sd,
116 sim_memopt **entry = &STATE_MEMOPT (sd);
118 while ((*entry) != NULL
119 && ((*entry)->level != level
120 || (*entry)->space != space
121 || (*entry)->addr != addr))
122 entry = &(*entry)->next;
123 if ((*entry) == NULL)
125 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
129 /* delete any buffer */
130 if ((*entry)->buffer != NULL)
131 zfree ((*entry)->buffer);
132 /* delete it and its aliases */
134 *entry = (*entry)->next;
135 while (alias != NULL)
137 sim_memopt *dead = alias;
138 alias = alias->alias;
139 sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
147 parse_size (char *chp,
148 address_word *nr_bytes,
151 /* <nr_bytes> [ "%" <modulo> ] */
152 *nr_bytes = strtoul (chp, &chp, 0);
155 *modulo = strtoul (chp + 1, &chp, 0);
162 parse_addr (char *chp,
167 /* [ <space> ": " ] <addr> [ "@" <level> ] */
168 *addr = strtoul (chp, &chp, 0);
172 *addr = strtoul (chp + 1, &chp, 0);
176 *level = strtoul (chp + 1, &chp, 0);
183 memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
184 char *arg, int is_command)
189 case OPTION_MEMORY_DELETE:
190 if (strcasecmp (arg, "all") == 0)
192 while (STATE_MEMOPT (sd) != NULL)
193 do_memopt_delete (sd,
194 STATE_MEMOPT (sd)->level,
195 STATE_MEMOPT (sd)->space,
196 STATE_MEMOPT (sd)->addr);
203 address_word addr = 0;
204 parse_addr (arg, &level, &space, &addr);
205 return do_memopt_delete (sd, level, space, addr);
208 case OPTION_MEMORY_REGION:
213 address_word addr = 0;
214 address_word nr_bytes = 0;
216 /* parse the arguments */
217 chp = parse_addr (chp, &level, &space, &addr);
220 sim_io_eprintf (sd, "Missing size for memory-region\n");
223 chp = parse_size (chp + 1, &nr_bytes, &modulo);
226 modulo = strtoul (chp + 1, &chp, 0);
227 /* try to attach/insert it */
228 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
229 &STATE_MEMOPT (sd), NULL);
233 case OPTION_MEMORY_ALIAS:
238 address_word addr = 0;
239 address_word nr_bytes = 0;
242 /* parse the arguments */
243 chp = parse_addr (chp, &level, &space, &addr);
246 sim_io_eprintf (sd, "Missing size for memory-region\n");
249 chp = parse_size (chp + 1, &nr_bytes, &modulo);
250 /* try to attach/insert the main record */
251 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
253 zalloc (modulo ? modulo : nr_bytes));
254 /* now attach all the aliases */
259 address_word a_addr = addr;
260 chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
261 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
262 &entry->alias, entry->buffer);
267 case OPTION_MEMORY_SIZE:
271 address_word addr = 0;
272 address_word nr_bytes = 0;
274 /* parse the arguments */
275 parse_size (arg, &nr_bytes, &modulo);
276 /* try to attach/insert it */
277 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
278 &STATE_MEMOPT (sd), NULL);
282 case OPTION_MEMORY_CLEAR:
285 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
288 for (alias = entry; alias != NULL; alias = alias->next)
291 address_word nr_bytes;
292 if (alias->modulo != 0)
293 nr_bytes = alias->modulo;
295 nr_bytes = alias->nr_bytes;
296 sim_core_write_buffer (sd, NULL, sim_core_write_map,
298 alias->addr + nr_bytes,
307 case OPTION_MEMORY_INFO:
310 sim_io_printf (sd, "Memory maps:\n");
311 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
314 sim_io_printf (sd, " memory");
315 if (entry->alias == NULL)
316 sim_io_printf (sd, " region ");
318 sim_io_printf (sd, " alias ");
319 if (entry->space != 0)
320 sim_io_printf (sd, "0x%lx:", (long) entry->space);
321 sim_io_printf (sd, "0x%08lx", (long) entry->addr);
322 if (entry->level != 0)
323 sim_io_printf (sd, "@0x%lx", (long) entry->level);
324 sim_io_printf (sd, ",0x%lx",
325 (long) entry->nr_bytes);
326 if (entry->modulo != 0)
327 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
328 for (alias = entry->alias;
332 if (alias->space != 0)
333 sim_io_printf (sd, "0x%lx:", (long) alias->space);
334 sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
335 if (alias->level != 0)
336 sim_io_printf (sd, "@0x%lx", (long) alias->level);
338 sim_io_printf (sd, "\n");
345 sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
354 /* "memory" module install handler.
356 This is called via sim_module_install to install the "memory" subsystem
357 into the simulator. */
359 static MODULE_INIT_FN sim_memory_init;
360 static MODULE_UNINSTALL_FN sim_memory_uninstall;
363 sim_memopt_install (SIM_DESC sd)
365 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
366 sim_add_option_table (sd, NULL, memory_options);
367 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
368 sim_module_add_init_fn (sd, sim_memory_init);
373 /* Uninstall the "memory" subsystem from the simulator. */
376 sim_memory_uninstall (SIM_DESC sd)
378 /* FIXME: free buffers, etc. */
383 sim_memory_init (SIM_DESC sd)
385 /* FIXME: anything needed? */