o Add modulo argument to sim_core_attach
[binutils-gdb.git] / sim / common / sim-memopt.c
1 /* Simulator memory option handling.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
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)
10 any later version.
11
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.
16
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. */
20
21 #include "sim-main.h"
22 #include "sim-assert.h"
23 #include "sim-options.h"
24
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #else
28 #ifdef HAVE_STRINGS_H
29 #include <strings.h>
30 #endif
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 /* "core" command line options. */
37
38 enum {
39 OPTION_MEMORY_DELETE = OPTION_START,
40 OPTION_MEMORY_REGION,
41 OPTION_MEMORY_SIZE,
42 OPTION_MEMORY_INFO,
43 OPTION_MEMORY_ALIAS,
44 OPTION_MEMORY_CLEAR,
45 };
46
47 static DECLARE_OPTION_HANDLER (memory_option_handler);
48
49 static const OPTION memory_options[] =
50 {
51 { {"delete-memory", required_argument, NULL, OPTION_MEMORY_DELETE },
52 '\0', "ADDRESS", "Delete memory at ADDRESS",
53 memory_option_handler },
54
55 { {"memory-region", required_argument, NULL, OPTION_MEMORY_REGION },
56 '\0', "ADDRESS,SIZE[,MODULO]", "Add a memory region",
57 memory_option_handler },
58
59 { {"memory-alias", required_argument, NULL, OPTION_MEMORY_ALIAS },
60 '\0', "ADDRESS,SIZE{,ADDRESS}", "Add memory shadow",
61 memory_option_handler },
62
63 { {"memory-size", required_argument, NULL, OPTION_MEMORY_SIZE },
64 '\0', "SIZE", "Add memory at address zero",
65 memory_option_handler },
66
67 { {"memory-clear", no_argument, NULL, OPTION_MEMORY_CLEAR },
68 '\0', NULL, "Clear all memory regions",
69 memory_option_handler },
70
71 { {"info-memory", no_argument, NULL, OPTION_MEMORY_INFO },
72 '\0', NULL, "Add memory at address zero",
73 memory_option_handler },
74
75 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
76 };
77
78
79 static SIM_RC
80 memory_option_handler (sd, opt, arg, is_command)
81 SIM_DESC sd;
82 int opt;
83 char *arg;
84 int is_command;
85 {
86 switch (opt)
87 {
88
89 case OPTION_MEMORY_DELETE:
90 {
91 address_word addr = strtoul (arg, NULL, 0);
92 sim_memopt **entry = &STATE_MEMOPT (sd);
93 sim_memopt *alias;
94 while ((*entry) != NULL && (*entry)->addr != addr)
95 entry = &(*entry)->next;
96 if ((*entry) == NULL)
97 {
98 sim_io_eprintf (sd, "Memory at 0x%lx not found, not deleted\n",
99 (long) addr);
100 return SIM_RC_FAIL;
101 }
102 /* delete any buffer */
103 if ((*entry)->buf != NULL)
104 zfree ((*entry)->buf);
105 /* delete it and its aliases */
106 alias = *entry;
107 *entry = alias->next;
108 while (alias != NULL)
109 {
110 sim_memopt *dead = alias;
111 alias = alias->alias;
112 sim_core_detach (sd, NULL, attach_raw_memory, 0, dead->addr);
113 zfree (dead);
114 }
115 return SIM_RC_OK;
116 }
117
118 case OPTION_MEMORY_REGION:
119 {
120 char *chp = arg;
121 address_word addr = 0;
122 address_word nr_bytes = 0;
123 unsigned modulo = 0;
124 sim_memopt **entry = &STATE_MEMOPT (sd);
125 /* parse the arguments */
126 addr = strtoul (chp, &chp, 0);
127 if (*chp != ',')
128 {
129 sim_io_eprintf (sd, "Missing size for memory-region\n");
130 return SIM_RC_FAIL;
131 }
132 chp++;
133 nr_bytes = strtoul (chp, &chp, 0);
134 if (*chp == ',')
135 modulo = strtoul (chp + 1, NULL, 0);
136 /* try to attach it */
137 sim_core_attach (sd, NULL,
138 attach_raw_memory, access_read_write_exec, 0,
139 addr, nr_bytes, modulo, NULL, NULL);
140 /* ok, so insert it */
141 while ((*entry) != NULL)
142 entry = &(*entry)->next;
143 (*entry) = ZALLOC (sim_memopt);
144 (*entry)->addr = addr;
145 (*entry)->nr_bytes = nr_bytes;
146 (*entry)->modulo = modulo;
147 return SIM_RC_OK;
148 }
149
150 case OPTION_MEMORY_ALIAS:
151 {
152 sim_io_eprintf (sd, "memory-alias not supported for for this simulator\n");
153 break;
154 }
155
156 case OPTION_MEMORY_SIZE:
157 {
158 sim_io_eprintf (sd, "memory-size not supported for for this simulator\n");
159 return SIM_RC_FAIL;
160 break;
161 }
162
163 case OPTION_MEMORY_CLEAR:
164 {
165 sim_memopt *entry;
166 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
167 {
168 sim_memopt *alias;
169 for (alias = entry; alias != NULL; alias = alias->next)
170 {
171 unsigned8 zero = 0;
172 address_word nr_bytes;
173 if (alias->modulo != 0)
174 nr_bytes = alias->modulo;
175 else
176 nr_bytes = alias->nr_bytes;
177 sim_core_write_buffer (sd, NULL, sim_core_write_map,
178 &zero,
179 alias->addr + nr_bytes,
180 sizeof (zero));
181
182 }
183 }
184 return SIM_RC_OK;
185 break;
186 }
187
188 case OPTION_MEMORY_INFO:
189 {
190 sim_memopt *entry;
191 sim_io_printf (sd, "Memory maps:\n");
192 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
193 {
194 sim_memopt *alias;
195 sim_io_printf (sd, " memory");
196 if (entry->alias == NULL)
197 {
198 sim_io_printf (sd, " region 0x%08lx,0x%lx",
199 (long) entry->addr,
200 (long) entry->nr_bytes);
201 if (entry->modulo != 0)
202 sim_io_printf (sd, ",0x%lx", (long) entry->modulo);
203 }
204 else
205 {
206 sim_io_printf (sd, " alias 0x%08lx,0x%lx",
207 (long) entry->addr,
208 (long) entry->nr_bytes);
209 for (alias = entry->alias; alias != NULL; alias = alias->next)
210 sim_io_printf (sd, ",0x%08lx", entry->addr);
211 }
212 sim_io_printf (sd, "\n");
213 }
214 return SIM_RC_OK;
215 break;
216 }
217
218 default:
219 sim_io_eprintf (sd, "Unknown watch option %d\n", opt);
220 return SIM_RC_FAIL;
221
222 }
223
224 return SIM_RC_FAIL;
225 }
226
227
228 /* "memory" module install handler.
229
230 This is called via sim_module_install to install the "memory" subsystem
231 into the simulator. */
232
233 static MODULE_INIT_FN sim_memory_init;
234 static MODULE_UNINSTALL_FN sim_memory_uninstall;
235
236 SIM_RC
237 sim_memopt_install (SIM_DESC sd)
238 {
239 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
240 sim_add_option_table (sd, memory_options);
241 sim_module_add_uninstall_fn (sd, sim_memory_uninstall);
242 sim_module_add_init_fn (sd, sim_memory_init);
243 return SIM_RC_OK;
244 }
245
246
247 /* Uninstall the "memory" subsystem from the simulator. */
248
249 static void
250 sim_memory_uninstall (SIM_DESC sd)
251 {
252 /* FIXME: free buffers, etc. */
253 }
254
255
256 static SIM_RC
257 sim_memory_init (SIM_DESC sd)
258 {
259 /* FIXME: anything needed? */
260 return SIM_RC_OK;
261 }