1 |
26 |
jlechner |
/* SCARTS (16-bit) target-dependent code for the GNU simulator.
|
2 |
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
|
3 |
|
|
Free Software Foundation, Inc.
|
4 |
|
|
Contributed by Martin Walter <mwalter@opencores.org>
|
5 |
|
|
|
6 |
|
|
This file is part of the GNU simulators.
|
7 |
|
|
|
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.
|
12 |
|
|
|
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.
|
17 |
|
|
|
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/>. */
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
#ifndef PARAMS
|
23 |
|
|
#define PARAMS(ARGS) ARGS
|
24 |
|
|
#endif
|
25 |
|
|
|
26 |
|
|
#include <signal.h>
|
27 |
|
|
#include <stdlib.h>
|
28 |
|
|
#include <gdb/callback.h>
|
29 |
|
|
#include <gdb/remote-sim.h>
|
30 |
|
|
|
31 |
|
|
#include "scarts_16-tdep.h"
|
32 |
|
|
#include "scarts_16-sim.h"
|
33 |
|
|
#include "scarts_16-iss.h"
|
34 |
|
|
#include "scarts_16-mad.h"
|
35 |
|
|
|
36 |
|
|
#ifdef HAVE_CONFIG_H
|
37 |
|
|
#include "tconfig.h"
|
38 |
|
|
#endif
|
39 |
|
|
|
40 |
|
|
volatile int async_sim_stop = 0;
|
41 |
|
|
struct sim_state scarts_sim_state = {NULL, sim_stopped, SIGTRAP};
|
42 |
|
|
|
43 |
|
|
static host_callback *scarts_callback = NULL;
|
44 |
|
|
static int _sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length);
|
45 |
|
|
|
46 |
|
|
static int
|
47 |
|
|
_sim_write (SIM_DESC sd,
|
48 |
|
|
SIM_ADDR mem,
|
49 |
|
|
unsigned char *buf,
|
50 |
|
|
int length)
|
51 |
|
|
{
|
52 |
|
|
int i;
|
53 |
|
|
|
54 |
|
|
for (i = 0; i < length; ++i)
|
55 |
|
|
{
|
56 |
|
|
if (scarts_mem_write (mem + i, buf[i]) == 0)
|
57 |
|
|
return i;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
return length;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
/* Destory a simulator instance.
|
64 |
|
|
|
65 |
|
|
QUITTING is non-zero if we cannot hang on errors.
|
66 |
|
|
|
67 |
|
|
This may involve freeing target memory and closing any open files
|
68 |
|
|
and mmap'd areas. You cannot assume sim_kill has already been
|
69 |
|
|
called. */
|
70 |
|
|
void
|
71 |
|
|
sim_close (SIM_DESC sd,
|
72 |
|
|
int quitting)
|
73 |
|
|
{
|
74 |
|
|
return;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
/* Prepare to run the simulated program.
|
78 |
|
|
|
79 |
|
|
ABFD, if not NULL, provides initial processor state information.
|
80 |
|
|
ARGV and ENV, if non NULL, are NULL terminated lists of pointers.
|
81 |
|
|
|
82 |
|
|
Hardware simulator: This function shall initialize the processor
|
83 |
|
|
registers to a known value. The program counter and possibly stack
|
84 |
|
|
pointer shall be set using information obtained from ABFD (or
|
85 |
|
|
hardware reset defaults). ARGV and ENV, dependant on the target
|
86 |
|
|
ABI, may be written to memory.
|
87 |
|
|
|
88 |
|
|
Process simulator: After a call to this function, a new process
|
89 |
|
|
instance shall exist. The TEXT, DATA, BSS and stack regions shall
|
90 |
|
|
all be initialized, ARGV and ENV shall be written to process
|
91 |
|
|
address space (according to the applicable ABI) and the program
|
92 |
|
|
counter and stack pointer set accordingly. */
|
93 |
|
|
SIM_RC
|
94 |
|
|
sim_create_inferior (SIM_DESC sd,
|
95 |
|
|
struct bfd *abfd,
|
96 |
|
|
char **argv,
|
97 |
|
|
char **env)
|
98 |
|
|
{
|
99 |
|
|
return SIM_RC_OK;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/* Passthru for other commands that the simulator might support.
|
103 |
|
|
Simulators should be prepared to deal with any combination of NULL
|
104 |
|
|
or empty CMD. */
|
105 |
|
|
void
|
106 |
|
|
sim_do_command (SIM_DESC sd,
|
107 |
|
|
char *cmd)
|
108 |
|
|
{
|
109 |
|
|
return;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/* Fetch register REGNO storing its raw (target endian) value in the
|
113 |
|
|
LENGTH byte buffer BUF. Return the actual size of the register or
|
114 |
|
|
zero if REGNO is not applicable.
|
115 |
|
|
|
116 |
|
|
If LENGTH does not match the size of REGNO no data is transfered
|
117 |
|
|
(the actual register size is still returned). */
|
118 |
|
|
int
|
119 |
|
|
sim_fetch_register (SIM_DESC sd,
|
120 |
|
|
int regno,
|
121 |
|
|
unsigned char *buf,
|
122 |
|
|
int length)
|
123 |
|
|
{
|
124 |
|
|
if (regno < 0 || regno >= SCARTS_TOTAL_NUM_REGS)
|
125 |
|
|
return 0;
|
126 |
|
|
|
127 |
|
|
/* All registers are SCARTS_WORD_SIZE bytes long. */
|
128 |
|
|
if (length != SCARTS_WORD_SIZE)
|
129 |
|
|
return SCARTS_WORD_SIZE;
|
130 |
|
|
|
131 |
|
|
*((uint16_t *) buf) = scarts_regfile_read (regno);
|
132 |
|
|
return SCARTS_WORD_SIZE;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/* Print whatever statistics the simulator has collected.
|
136 |
|
|
VERBOSE is currently unused and must always be zero. */
|
137 |
|
|
void
|
138 |
|
|
sim_info (SIM_DESC sd,
|
139 |
|
|
int verbose)
|
140 |
|
|
{
|
141 |
|
|
return;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/* Load program PROG into the simulators memory.
|
145 |
|
|
|
146 |
|
|
If ABFD is non-NULL, the bfd for the file has already been opened.
|
147 |
|
|
The result is a return code indicating success.
|
148 |
|
|
|
149 |
|
|
Hardware simulator: Normally, each program section is written into
|
150 |
|
|
memory according to that sections LMA using physical (direct)
|
151 |
|
|
addressing. The exception being systems, such as PPC/CHRP, which
|
152 |
|
|
support more complicated program loaders. A call to this function
|
153 |
|
|
should not effect the state of the processor registers. Multiple
|
154 |
|
|
calls to this function are permitted and have an accumulative
|
155 |
|
|
effect. */
|
156 |
|
|
SIM_RC
|
157 |
|
|
sim_load (SIM_DESC sd,
|
158 |
|
|
char *prog,
|
159 |
|
|
struct bfd *abfd,
|
160 |
|
|
int from_tty)
|
161 |
|
|
{
|
162 |
|
|
#ifndef SIM_HANDLES_LMA
|
163 |
|
|
#define SIM_HANDLES_LMA 0
|
164 |
|
|
#endif
|
165 |
|
|
|
166 |
|
|
sim_load_file (sd, "", scarts_callback, prog, abfd, 1, SIM_HANDLES_LMA, _sim_write);
|
167 |
|
|
return SIM_RC_OK;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
/* Create a fully initialized simulator instance.
|
171 |
|
|
|
172 |
|
|
(This function is called when the simulator is selected from the
|
173 |
|
|
gdb command line.)
|
174 |
|
|
|
175 |
|
|
KIND specifies how the simulator shall be used. Currently there
|
176 |
|
|
are only two kinds: stand-alone and debug.
|
177 |
|
|
|
178 |
|
|
CALLBACK specifies a standard host callback (defined in callback.h).
|
179 |
|
|
|
180 |
|
|
ABFD, when non NULL, designates a target program. The program is
|
181 |
|
|
not loaded.
|
182 |
|
|
|
183 |
|
|
ARGV is a standard ARGV pointer such as that passed from the
|
184 |
|
|
command line. The syntax of the argument list is is assumed to be
|
185 |
|
|
``SIM-PROG { SIM-OPTION } [ TARGET-PROGRAM { TARGET-OPTION } ]''.
|
186 |
|
|
The trailing TARGET-PROGRAM and args are only valid for a
|
187 |
|
|
stand-alone simulator.
|
188 |
|
|
|
189 |
|
|
On success, the result is a non NULL descriptor that shall be
|
190 |
|
|
passed to the other sim_foo functions. While the simulator
|
191 |
|
|
configuration can be parameterized by (in decreasing precedence)
|
192 |
|
|
ARGV's SIM-OPTION, ARGV's TARGET-PROGRAM and the ABFD argument, the
|
193 |
|
|
successful creation of the simulator shall not dependent on the
|
194 |
|
|
presence of any of these arguments/options.
|
195 |
|
|
|
196 |
|
|
Hardware simulator: The created simulator shall be sufficiently
|
197 |
|
|
initialized to handle, with out restrictions any client requests
|
198 |
|
|
(including memory reads/writes, register fetch/stores and a
|
199 |
|
|
resume). */
|
200 |
|
|
SIM_DESC
|
201 |
|
|
sim_open (SIM_OPEN_KIND kind,
|
202 |
|
|
host_callback *callback,
|
203 |
|
|
struct bfd *abfd,
|
204 |
|
|
char **argv)
|
205 |
|
|
{
|
206 |
|
|
scarts_callback = callback;
|
207 |
|
|
|
208 |
|
|
scarts_init ();
|
209 |
|
|
return &scarts_sim_state;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
/* Fetch LENGTH bytes of the simulated program's memory. Start fetch
|
213 |
|
|
at virtual address MEM and store in BUF. Result is number of bytes
|
214 |
|
|
read, or zero if error. */
|
215 |
|
|
int
|
216 |
|
|
sim_read (SIM_DESC sd,
|
217 |
|
|
SIM_ADDR mem,
|
218 |
|
|
unsigned char *buf,
|
219 |
|
|
int length)
|
220 |
|
|
{
|
221 |
|
|
int i;
|
222 |
|
|
uint8_t temp;
|
223 |
|
|
|
224 |
|
|
for (i = 0; i < length; ++i)
|
225 |
|
|
{
|
226 |
|
|
if (scarts_mem_read (mem + i, &temp) == 0)
|
227 |
|
|
return i;
|
228 |
|
|
|
229 |
|
|
buf[i] = temp;
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
return length;
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/* Run (or resume) the simulated program.
|
236 |
|
|
|
237 |
|
|
STEP, when non-zero indicates that only a single simulator cycle
|
238 |
|
|
should be emulated.
|
239 |
|
|
|
240 |
|
|
SIGGNAL, if non-zero is a (HOST) SIGRC value indicating the type of
|
241 |
|
|
event (hardware interrupt, signal) to be delivered to the simulated
|
242 |
|
|
program.
|
243 |
|
|
|
244 |
|
|
Hardware simulator: If the SIGRC value returned by
|
245 |
|
|
sim_stop_reason() is passed back to the simulator via SIGGNAL then
|
246 |
|
|
the hardware simulator shall correctly deliver the hardware event
|
247 |
|
|
indicated by that signal. If a value of zero is passed in then the
|
248 |
|
|
simulation will continue as if there were no outstanding signal.
|
249 |
|
|
The effect of any other SIGGNAL value is is implementation
|
250 |
|
|
dependant.
|
251 |
|
|
|
252 |
|
|
Process simulator: If SIGRC is non-zero then the corresponding
|
253 |
|
|
signal is delivered to the simulated program and execution is then
|
254 |
|
|
continued. A zero SIGRC value indicates that the program should
|
255 |
|
|
continue as normal. */
|
256 |
|
|
void
|
257 |
|
|
sim_resume (SIM_DESC sd,
|
258 |
|
|
int step,
|
259 |
|
|
int siggnal)
|
260 |
|
|
{
|
261 |
|
|
uint16_t addr, insn;
|
262 |
|
|
scarts_codemem_read_fptr_t read_fptr;
|
263 |
|
|
scarts_codemem_write_fptr_t write_fptr;
|
264 |
|
|
|
265 |
|
|
async_sim_stop = 0;
|
266 |
|
|
|
267 |
|
|
while (!async_sim_stop)
|
268 |
|
|
{
|
269 |
|
|
insn = 0;
|
270 |
|
|
|
271 |
|
|
/* Read the current instruction from the PC. */
|
272 |
|
|
scarts_codemem_vma_decode (scarts_regfile_read (SCARTS_PC_REGNUM), &read_fptr, &write_fptr, &addr);
|
273 |
|
|
(void) (*read_fptr) (addr, &insn);
|
274 |
|
|
|
275 |
|
|
/* The SCARTS_ILLOP_INSN is interpreted as a breakpoint. */
|
276 |
|
|
if (insn == SCARTS_ILLOP_INSN)
|
277 |
|
|
{
|
278 |
|
|
sd->sim_stop_reason = sim_stopped;
|
279 |
|
|
sd->sigrc = SIGTRAP;
|
280 |
|
|
return;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
sd->sigrc = scarts_tick();
|
284 |
|
|
|
285 |
|
|
if (step || sd->sigrc != 0)
|
286 |
|
|
/* Execute only a single simulator step. */
|
287 |
|
|
break;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
async_sim_stop = 0;
|
291 |
|
|
|
292 |
|
|
sd->sim_stop_reason = sim_stopped;
|
293 |
|
|
sd->sigrc = (sd->sigrc != 0 ? sd->sigrc : SIGTRAP);
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
void
|
297 |
|
|
sim_set_callbacks (host_callback *callback)
|
298 |
|
|
{
|
299 |
|
|
return;
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
void
|
303 |
|
|
sim_size (int i)
|
304 |
|
|
{
|
305 |
|
|
return;
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
/* Asynchronous request to stop the simulation.
|
309 |
|
|
A nonzero return indicates that the simulator is able to handle
|
310 |
|
|
the request */
|
311 |
|
|
int
|
312 |
|
|
sim_stop (SIM_DESC sd)
|
313 |
|
|
{
|
314 |
|
|
async_sim_stop = 1;
|
315 |
|
|
return sim_stopped;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
/* Fetch the REASON why the program stopped.
|
319 |
|
|
|
320 |
|
|
SIM_EXITED: The program has terminated. SIGRC indicates the target
|
321 |
|
|
dependant exit status.
|
322 |
|
|
|
323 |
|
|
SIM_STOPPED: The program has stopped. SIGRC uses the host's signal
|
324 |
|
|
numbering as a way of identifying the reaon: program interrupted by
|
325 |
|
|
user via a sim_stop request (SIGINT); a breakpoint instruction
|
326 |
|
|
(SIGTRAP); a completed single step (SIGTRAP); an internal error
|
327 |
|
|
condition (SIGABRT); an illegal instruction (SIGILL); Access to an
|
328 |
|
|
undefined memory region (SIGSEGV); Mis-aligned memory access
|
329 |
|
|
(SIGBUS). For some signals information in addition to the signal
|
330 |
|
|
number may be retained by the simulator (e.g. offending address),
|
331 |
|
|
that information is not directly accessable via this interface.
|
332 |
|
|
|
333 |
|
|
SIM_SIGNALLED: The program has been terminated by a signal. The
|
334 |
|
|
simulator has encountered target code that causes the the program
|
335 |
|
|
to exit with signal SIGRC.
|
336 |
|
|
|
337 |
|
|
SIM_RUNNING, SIM_POLLING: The return of one of these values
|
338 |
|
|
indicates a problem internal to the simulator. */
|
339 |
|
|
void
|
340 |
|
|
sim_stop_reason (SIM_DESC sd,
|
341 |
|
|
enum sim_stop *reason,
|
342 |
|
|
int *sigrc)
|
343 |
|
|
{
|
344 |
|
|
*reason = sd->sim_stop_reason;
|
345 |
|
|
*sigrc = sd->sigrc;
|
346 |
|
|
}
|
347 |
|
|
|
348 |
|
|
/* Store register REGNO from the raw (target endian) value in BUF.
|
349 |
|
|
Return the actual size of the register or zero if REGNO is not
|
350 |
|
|
applicable.
|
351 |
|
|
|
352 |
|
|
If LENGTH does not match the size of REGNO no data is transfered
|
353 |
|
|
(the actual register size is still returned). */
|
354 |
|
|
int
|
355 |
|
|
sim_store_register (SIM_DESC sd,
|
356 |
|
|
int regno,
|
357 |
|
|
unsigned char *buf,
|
358 |
|
|
int length)
|
359 |
|
|
{
|
360 |
|
|
if (regno < 0 || regno >= SCARTS_TOTAL_NUM_REGS)
|
361 |
|
|
return 0;
|
362 |
|
|
|
363 |
|
|
/* All registers are SCARTS_WORD_SIZE bytes long. */
|
364 |
|
|
if (length != SCARTS_WORD_SIZE)
|
365 |
|
|
return SCARTS_WORD_SIZE;
|
366 |
|
|
|
367 |
|
|
scarts_regfile_write (regno, *((uint16_t *) buf));
|
368 |
|
|
return SCARTS_WORD_SIZE;
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
int
|
372 |
|
|
sim_trace (SIM_DESC sd)
|
373 |
|
|
{
|
374 |
|
|
return 0;
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
/* Store LENGTH bytes from BUF into the simulated program's
|
378 |
|
|
memory. Store bytes starting at virtual address MEM. Result is
|
379 |
|
|
number of bytes write, or zero if error. */
|
380 |
|
|
int
|
381 |
|
|
sim_write (SIM_DESC sd,
|
382 |
|
|
SIM_ADDR mem,
|
383 |
|
|
unsigned char *buf,
|
384 |
|
|
int length)
|
385 |
|
|
{
|
386 |
|
|
return _sim_write (sd, mem, buf, length);
|
387 |
|
|
}
|
388 |
|
|
|