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 /* "core" 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 (sd, opt, arg, is_command)
192 case OPTION_MEMORY_DELETE:
193 if (strcasecmp (arg, "all") == 0)
195 while (STATE_MEMOPT (sd) != NULL)
196 do_memopt_delete (sd,
197 STATE_MEMOPT (sd)->level,
198 STATE_MEMOPT (sd)->space,
199 STATE_MEMOPT (sd)->addr);
206 address_word addr = 0;
207 parse_addr (arg, &level, &space, &addr);
208 return do_memopt_delete (sd, level, space, addr);
211 case OPTION_MEMORY_REGION:
216 address_word addr = 0;
217 address_word nr_bytes = 0;
219 /* parse the arguments */
220 chp = parse_addr (chp, &level, &space, &addr);
223 sim_io_eprintf (sd, "Missing size for memory-region\n");
226 chp = parse_size (chp + 1, &nr_bytes, &modulo);
229 modulo = strtoul (chp + 1, &chp, 0);
230 /* try to attach/insert it */
231 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
232 &STATE_MEMOPT (sd), NULL);
236 case OPTION_MEMORY_ALIAS:
241 address_word addr = 0;
242 address_word nr_bytes = 0;
245 /* parse the arguments */
246 chp = parse_addr (chp, &level, &space, &addr);
249 sim_io_eprintf (sd, "Missing size for memory-region\n");
252 chp = parse_size (chp + 1, &nr_bytes, &modulo);
253 /* try to attach/insert the main record */
254 entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
255 &STATE_MEMOPT (sd), zalloc (nr_bytes));
256 /* now attach all the aliases */
261 address_word a_addr = addr;
262 chp = parse_addr (chp, &a_level, &a_space, &a_addr);
263 do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
264 &entry->alias, entry->buffer);
269 case OPTION_MEMORY_SIZE:
273 address_word addr = 0;
274 address_word nr_bytes = 0;
276 /* parse the arguments */
277 parse_size (arg, &nr_bytes, &modulo);
278 /* try to attach/insert it */
279 do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
280 &STATE_MEMOPT (sd), NULL);
284 case OPTION_MEMORY_CLEAR:
287 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
290 for (alias = entry; alias != NULL; alias = alias->next)
293 address_word nr_bytes;
294 if (alias->modulo != 0)
295 nr_bytes = alias->modulo;
297 nr_bytes = alias->nr_bytes;
298 sim_core_write_buffer (sd, NULL, sim_core_write_map,
300 alias->addr + nr_bytes,
309 case OPTION_MEMORY_INFO:
312 sim_io_printf (sd, "Memory maps:\n");
313 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
316 sim_io_printf (sd, " memory");
317 if (entry->alias == NULL)
318 sim_io_printf (sd, " region ");
320 sim_io_printf (sd, " alias ");
321 if (entry->space != 0)
322 sim_io_printf (sd, "0x%lx:", (long) entry->space);
323 sim_io_printf (sd, "0x%08lx",
325 if (entry->level != 0)
326 sim_io_printf (sd, "@0x%lx", (long) entry->level);
327 sim_io_printf (sd, ",0x%lx",
328 (long) entry->nr_bytes);
329 if (entry->modulo != 0)
330 sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
331 for (alias = entry->alias;
335 if (alias->space != 0)
336 sim_io_printf (sd, "0x%lx:", (long) alias->space);
337 sim_io_printf (sd, ",0x%08lx", alias->addr);
338 if (alias->level != 0)
339 sim_io_printf (sd, "@0x%lx", (long) alias->level);
341 sim_io_printf (sd, "\n");
348 sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
357 /* "memory" module install handler.
359 This is called via sim_module_install to install the "memory" subsystem
360 into the simulator. */
362 static MODULE_INIT_FN sim_memory_init;
363 static MODULE_UNINSTALL_FN sim_memory_uninstall;
366 sim_memopt_install (SIM_DESC sd)
368 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
369 sim_add_option_table (sd, memory_options);
370 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
371 sim_module_add_init_fn (sd, sim_memory_init);
376 /* Uninstall the "memory" subsystem from the simulator. */
379 sim_memory_uninstall (SIM_DESC sd)
381 /* FIXME: free buffers, etc. */
386 sim_memory_init (SIM_DESC sd)
388 /* FIXME: anything needed? */