1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/* This file is part of the program psim.
Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <signal.h> /* FIXME - should be machine dependant version */
#include <stdarg.h>
#include <ctype.h>
#include "basics.h"
#include "psim.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#include "../../gdb/defs.h"
#include "devices.h"
#include "../../gdb/remote-sim.h"
#include "../../gdb/callback.h"
/* Structures used by the simulator, for gdb just have static structures */
static psim *simulator;
static char *register_names[] = REGISTER_NAMES;
static int print_info = 0;
void
sim_open (char *args)
{
int i;
/* trace the call */
TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
if (args) {
char **argv = buildargv(args);
int argp = 0;
int argc;
for (argc = 0; argv[argc]; argc++);
while (argp < argc) {
if (*argv[argp] != '-')
error ("Argument is not an option '%s'", argv[argp]);
else {
/* check arguments -- note, main.c also contains argument processing
code for the standalone emulator. */
char *p = argv[argp] + 1;
while (*p != '\0') {
switch (*p) {
default:
printf_filtered("Usage:\n\ttarget sim [ -t <trace-option> ]\n");
trace_usage();
error ("");
break;
case 't':
argp += 1;
if (argv[argp] == NULL)
error("Missing <trace> option for -t\n");
trace_option(argv[argp]); /* better fail if NULL */
break;
case 'I':
print_info = 1;
break;
}
}
}
argp += 1;
}
}
/* do something */
TRACE(trace_tbd, ("sim_open() - TBD - should parse the arguments\n"));
TRACE(trace_tbd, ("sim_open() - TBD - can not create simulator here as do not have description of it\n"));
}
void
sim_close (int quitting)
{
TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
if (print_info)
psim_print_info (simulator, 1);
/* nothing to do */
}
int
sim_load (char *prog, int from_tty)
{
char **argv;
TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
prog, from_tty));
ASSERT(prog != NULL);
/* parse the arguments, assume that the file is argument 0 */
argv = buildargv(prog);
ASSERT(argv != NULL && argv[0] != NULL);
/* create the simulator */
TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
simulator = psim_create(argv[0]);
/* bring in all the data section */
psim_init(simulator);
/* release the arguments */
freeargv(argv);
/* `I did it my way' */
return 0;
}
void
sim_kill (void)
{
TRACE(trace_gdb, ("sim_kill(void) called\n"));
/* do nothing, nothing to do */
}
int
sim_read (SIM_ADDR mem, unsigned char *buf, int length)
{
int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
buf, mem, length);
TRACE(trace_gdb, ("sim_read(mem=0x%x, buf=0x%x, length=%d) = %d\n",
mem, buf, length, result));
return result;
}
int
sim_write (SIM_ADDR mem, unsigned char *buf, int length)
{
int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
buf, mem, length,
1/*violate_ro*/);
TRACE(trace_gdb, ("sim_write(mem=0x%x, buf=0x%x, length=%d) = %d\n",
mem, buf, length, result));
return result;
}
void
sim_fetch_register (int regno, unsigned char *buf)
{
if (simulator == NULL) {
return;
}
TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%x)\n",
regno, register_names[regno], buf));
psim_read_register(simulator, MAX_NR_PROCESSORS,
buf, register_names[regno],
raw_transfer);
}
void
sim_store_register (int regno, unsigned char *buf)
{
if (simulator == NULL)
return;
TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%x)\n",
regno, register_names[regno], buf));
psim_write_register(simulator, MAX_NR_PROCESSORS,
buf, register_names[regno],
raw_transfer);
}
void
sim_info (int verbose)
{
TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
psim_print_info (simulator, verbose);
}
void
sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
{
unsigned_word entry_point = start_address;
TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
start_address));
psim_init(simulator);
psim_stack(simulator, argv, envp);
psim_write_register(simulator, -1 /* all start at same PC */,
&entry_point, "pc", cooked_transfer);
}
static volatile int sim_should_run;
void
sim_stop_reason (enum sim_stop *reason, int *sigrc)
{
psim_status status = psim_get_status(simulator);
switch (CURRENT_ENVIRONMENT) {
case USER_ENVIRONMENT:
case VIRTUAL_ENVIRONMENT:
switch (status.reason) {
case was_continuing:
*reason = sim_stopped;
*sigrc = SIGTRAP;
if (sim_should_run) {
error("sim_stop_reason() unknown reason for halt\n");
}
break;
case was_trap:
*reason = sim_stopped;
*sigrc = SIGTRAP;
break;
case was_exited:
*reason = sim_exited;
*sigrc = 0;
break;
case was_signalled:
*reason = sim_signalled;
*sigrc = status.signal;
break;
}
break;
case OPERATING_ENVIRONMENT:
*reason = sim_stopped;
*sigrc = SIGTRAP;
break;
default:
error("sim_stop_reason() - unknown environment\n");
}
TRACE(trace_gdb, ("sim_stop_reason(reason=0x%x(%d), sigrc=0x%x(%d))\n",
reason, *reason, sigrc, *sigrc));
}
/* Run (or resume) the program. */
static void
sim_ctrl_c()
{
sim_should_run = 0;
}
void
sim_resume (int step, int siggnal)
{
void (*prev) ();
unsigned_word program_counter;
TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
step, siggnal));
prev = signal(SIGINT, sim_ctrl_c);
sim_should_run = 1;
if (step)
psim_step(simulator);
else
psim_run_until_stop(simulator, &sim_should_run);
signal(SIGINT, prev);
}
void
sim_do_command(char *cmd)
{
TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", cmd));
}
void
sim_set_callbacks (host_callback *callback)
{
TRACE(trace_gdb, ("sim_set_callbacks called\n"));
}
/****/
void *
zalloc(long size)
{
void *memory = (void*)xmalloc(size);
if (memory == NULL)
error("xmalloc failed\n");
bzero(memory, size);
return memory;
}
void zfree(void *data)
{
mfree(NULL, data);
}
|