#include <signal.h>
#include "sysdep.h"
#include "bfd.h"
+#include "callback.h"
#include "remote-sim.h"
#include "d10v_sim.h"
enum _leftright { LEFT_FIRST, RIGHT_FIRST };
+static char *myname;
+static SIM_OPEN_KIND sim_kind;
int d10v_debug;
host_callback *d10v_callback;
unsigned long ins_type_counters[ (int)INS_MAX ];
uint16 OP[4];
+static int init_text_p = 0;
+/* non-zero if we opened prog_bfd */
+static int prog_bfd_was_opened_p;
+bfd *prog_bfd;
+asection *text;
+bfd_vma text_start;
+bfd_vma text_end;
+
static long hash PARAMS ((long insn, int format));
static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int size));
static void get_operands PARAMS ((struct simops *s, uint32 ins));
static void do_2_short PARAMS ((uint16 ins1, uint16 ins2, enum _leftright leftright));
static void do_parallel PARAMS ((uint16 ins1, uint16 ins2));
static char *add_commas PARAMS ((char *buf, int sizeof_buf, unsigned long value));
-extern void sim_size PARAMS ((int power));
static void init_system PARAMS ((void));
-extern int sim_write PARAMS ((SIM_ADDR addr, unsigned char *buffer, int size));
-extern void sim_open PARAMS ((char *args));
-extern void sim_close PARAMS ((int quitting));
extern void sim_set_profile PARAMS ((int n));
extern void sim_set_profile_size PARAMS ((int n));
-extern void sim_resume PARAMS ((int step, int siggnal));
-extern void sim_info PARAMS ((int verbose));
-extern void sim_create_inferior PARAMS ((SIM_ADDR start_address, char **argv, char **env));
-extern void sim_kill PARAMS ((void));
-extern void sim_set_callbacks PARAMS ((host_callback *p));
-extern void sim_stop_reason PARAMS ((enum sim_stop *reason, int *sigrc));
-extern void sim_fetch_register PARAMS ((int rn, unsigned char *memory));
-extern void sim_store_register PARAMS ((int rn, unsigned char *memory));
-extern int sim_read PARAMS ((SIM_ADDR addr, unsigned char *buffer, int size));
-extern void sim_do_command PARAMS ((char *cmd));
#ifndef INLINE
#if defined(__GNUC__) && defined(__OPTIMIZE__)
struct hash_entry *next;
long opcode;
long mask;
+ int size;
struct simops *ops;
};
else
h = &hash_table[(ins & 0x7E00) >> 9];
- while ((ins & h->mask) != h->opcode)
+ while ((ins & h->mask) != h->opcode || h->size != size)
{
if (h->next == NULL)
{
}
}
+bfd_vma
+decode_pc ()
+{
+ asection *s;
+ if (!init_text_p)
+ {
+ init_text_p = 1;
+ for (s = prog_bfd->sections; s; s = s->next)
+ if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
+ {
+ text = s;
+ text_start = bfd_get_section_vma (prog_bfd, s);
+ text_end = text_start + bfd_section_size (prog_bfd, s);
+ break;
+ }
+ }
+
+ return (PC << 2) + text_start;
+}
+
static void
do_long (ins)
uint32 ins;
/* to access data, we use the following mapping */
/* 0x01000000 - 0x0103ffff : instruction memory */
/* 0x02000000 - 0x0200ffff : data memory */
- /* 0x03000000 - 0x03ffffff : unified memory */
+ /* 0x00000000 - 0x00ffffff : unified memory */
- if ( (addr & 0x03000000) == 0x03000000)
+ if ( (addr & 0x03000000) == 0)
{
/* UNIFIED MEMORY */
int segment;
- addr &= ~0x03000000;
segment = addr >> UMEM_SIZE;
addr &= 0x1ffff;
if (!State.umem[segment])
- State.umem[segment] = (uint8 *)calloc(1,1<<UMEM_SIZE);
+ {
+#ifdef DEBUG
+ (*d10v_callback->printf_filtered) (d10v_callback,"Allocating %s bytes unified memory to region %d\n",
+ add_commas (buffer, sizeof (buffer), (1UL<<IMEM_SIZE)), segment);
+#endif
+ State.umem[segment] = (uint8 *)calloc(1,1<<UMEM_SIZE);
+ }
if (!State.umem[segment])
{
(*d10v_callback->printf_filtered) (d10v_callback, "Memory allocation failed.\n");
exit(1);
}
-#ifdef DEBUG
- (*d10v_callback->printf_filtered) (d10v_callback,"Allocated %s bytes unified memory to region %d\n",
- add_commas (buffer, sizeof (buffer), (1UL<<IMEM_SIZE)), segment);
-#endif
/* FIXME: need to check size and read/write multiple segments if necessary */
if (write)
- memcpy (State.umem[segment]+addr, buffer, size);
+ memcpy (State.umem[segment]+addr, buffer, size) ;
else
memcpy (buffer, State.umem[segment]+addr, size);
}
(*d10v_callback->printf_filtered) (d10v_callback, "ERROR: address 0x%x is not in valid range\n",addr);
(*d10v_callback->printf_filtered) (d10v_callback, "Instruction addresses start at 0x01000000\n");
(*d10v_callback->printf_filtered) (d10v_callback, "Data addresses start at 0x02000000\n");
- (*d10v_callback->printf_filtered) (d10v_callback, "Unified addresses start at 0x03000000\n");
+ (*d10v_callback->printf_filtered) (d10v_callback, "Unified addresses start at 0x00000000\n");
exit(1);
}
else
}
+static int
+sim_write_phys (sd, addr, buffer, size)
+ SIM_DESC sd;
+ SIM_ADDR addr;
+ unsigned char *buffer;
+ int size;
+{
+ return xfer_mem( addr, buffer, size, 1);
+}
+
int
-sim_write (addr, buffer, size)
+sim_write (sd, addr, buffer, size)
+ SIM_DESC sd;
SIM_ADDR addr;
unsigned char *buffer;
int size;
{
+ /* FIXME: this should be performing a virtual transfer */
return xfer_mem( addr, buffer, size, 1);
}
int
-sim_read (addr, buffer, size)
+sim_read (sd, addr, buffer, size)
+ SIM_DESC sd;
SIM_ADDR addr;
unsigned char *buffer;
int size;
{
+ /* FIXME: this should be performing a virtual transfer */
return xfer_mem( addr, buffer, size, 0);
}
-void
-sim_open (args)
- char *args;
+SIM_DESC
+sim_open (kind, callback, abfd, argv)
+ SIM_OPEN_KIND kind;
+ host_callback *callback;
+ struct _bfd *abfd;
+ char **argv;
{
struct simops *s;
struct hash_entry *h;
static int init_p = 0;
+ char **p;
- if (args != NULL)
+ sim_kind = kind;
+ d10v_callback = callback;
+ myname = argv[0];
+
+ for (p = argv + 1; *p; ++p)
{
#ifdef DEBUG
- if (strcmp (args, "-t") == 0)
+ if (strcmp (*p, "-t") == 0)
d10v_debug = DEBUG;
else
#endif
- (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unsupported option(s): %s\n",args);
+ (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unsupported option(s): %s\n",*p);
}
/* put all the opcodes in the hash table */
if (h->ops)
{
- h->next = calloc(1,sizeof(struct hash_entry));
+ h->next = (struct hash_entry *) calloc(1,sizeof(struct hash_entry));
+ if (!h->next)
+ perror ("malloc failure");
+
h = h->next;
}
h->ops = s;
h->mask = s->mask;
h->opcode = s->opcode;
+ h->size = s->is_long;
}
}
+
+ /* Fudge our descriptor. */
+ return (SIM_DESC) 1;
}
void
-sim_close (quitting)
+sim_close (sd, quitting)
+ SIM_DESC sd;
int quitting;
{
- /* nothing to do */
+ if (prog_bfd != NULL && prog_bfd_was_opened_p)
+ bfd_close (prog_bfd);
}
void
if (addr > 0xbfff)
{
if ( (addr & 0xfff0) != 0xff00)
- (*d10v_callback->printf_filtered) (d10v_callback, "Data address %x is in I/O space.\n",addr);
+ {
+ (*d10v_callback->printf_filtered) (d10v_callback, "Data address 0x%lx is in I/O space, pc = 0x%lx.\n",
+ (long)addr, (long)decode_pc ());
+ State.exception = SIGBUS;
+ }
+
return State.dmem + addr;
}
/* unified memory */
/* this is ugly because we allocate unified memory in 128K segments and */
/* dmap addresses 16k segments */
- seg = (DMAP & 0x3ff) >> 2;
+ seg = (DMAP & 0x3ff) >> 3;
if (State.umem[seg] == NULL)
{
- (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unified memory region %d unmapped\n", seg);
- exit(1);
+ (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unified memory region %d unmapped, pc = 0x%lx\n",
+ seg, (long)decode_pc ());
+ State.exception = SIGBUS;
}
- return State.umem[seg] + (DMAP & 3) * 0x4000;
+ return State.umem[seg] + (DMAP & 7) * 0x4000;
}
return State.dmem + addr;
if (State.umem[imap & 0xff] == NULL)
{
- (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unified memory region %d unmapped\n", imap & 0xff);
- State.exception = SIGILL;
+ (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: unified memory region %d unmapped, pc = 0x%lx\n",
+ imap & 0xff, (long)PC);
+ State.exception = SIGBUS;
return 0;
}
+ /* Discard upper bit(s) of PC in case IMAP1 selects unified memory. */
+ pc &= (1 << UMEM_SIZE) - 1;
+
return State.umem[imap & 0xff] + pc;
}
+static int stop_simulator;
+
+static void
+sim_ctrl_c()
+{
+ stop_simulator = 1;
+}
+
+
+int
+sim_stop (sd)
+ SIM_DESC sd;
+{
+ stop_simulator = 1;
+ return 1;
+}
+
+
+/* Run (or resume) the program. */
void
-sim_resume (step, siggnal)
+sim_resume (sd, step, siggnal)
+ SIM_DESC sd;
int step, siggnal;
{
+ void (*prev) ();
uint32 inst;
- reg_t oldpc = 0;
/* (*d10v_callback->printf_filtered) (d10v_callback, "sim_resume (%d,%d) PC=0x%x\n",step,siggnal,PC); */
-
State.exception = 0;
+ prev = signal(SIGINT, sim_ctrl_c);
+ stop_simulator = step;
+
do
{
inst = get_longword( pc_addr() );
- oldpc = PC;
+ State.pc_changed = 0;
ins_type_counters[ (int)INS_CYCLES ]++;
switch (inst & 0xC0000000)
{
{
RPT_C -= 1;
if (RPT_C == 0)
- State.RP = 0;
+ {
+ State.RP = 0;
+ PC++;
+ }
else
PC = RPT_S;
}
-
- /* FIXME */
- if (PC == oldpc)
+ else if (!State.pc_changed)
PC++;
-
}
- while ( !State.exception && !step);
+ while ( !State.exception && !stop_simulator);
if (step && !State.exception)
State.exception = SIGTRAP;
+
+ signal(SIGINT, prev);
}
int
-sim_trace ()
+sim_trace (sd)
+ SIM_DESC sd;
{
#ifdef DEBUG
d10v_debug = DEBUG;
#endif
- sim_resume (0, 0);
+ sim_resume (sd, 0, 0);
return 1;
}
void
-sim_info (verbose)
+sim_info (sd, verbose)
+ SIM_DESC sd;
int verbose;
{
char buf1[40];
size, add_commas (buf1, sizeof (buf1), total));
}
-void
-sim_create_inferior (start_address, argv, env)
- SIM_ADDR start_address;
+SIM_RC
+sim_create_inferior (sd, abfd, argv, env)
+ SIM_DESC sd;
+ struct _bfd *abfd;
char **argv;
char **env;
{
-#ifdef DEBUG
- if (d10v_debug)
- (*d10v_callback->printf_filtered) (d10v_callback, "sim_create_inferior: PC=0x%x\n", start_address);
-#endif
+ bfd_vma start_address;
/* reset all state information */
memset (&State.regs, 0, (int)&State.imem - (int)&State.regs[0]);
/* set PC */
+ if (abfd != NULL)
+ start_address = bfd_get_start_address (prog_bfd);
+ else
+ start_address = 0xffc0 << 2;
+#ifdef DEBUG
+ if (d10v_debug)
+ (*d10v_callback->printf_filtered) (d10v_callback, "sim_create_inferior: PC=0x%lx\n", (long) start_address);
+#endif
PC = start_address >> 2;
/* cpu resets imap0 to 0 and imap1 to 0x7f, but D10V-EVA board */
SET_IMAP0(0x1000);
SET_IMAP1(0x1000);
SET_DMAP(0);
-}
-
-void
-sim_kill ()
-{
- /* nothing to do */
+ return SIM_RC_OK;
}
+
void
-sim_set_callbacks(p)
+sim_set_callbacks (p)
host_callback *p;
{
-/* printf ("sim_set_callbacks\n"); */
d10v_callback = p;
}
void
-sim_stop_reason (reason, sigrc)
+sim_stop_reason (sd, reason, sigrc)
+ SIM_DESC sd;
enum sim_stop *reason;
int *sigrc;
{
}
void
-sim_fetch_register (rn, memory)
+sim_fetch_register (sd, rn, memory)
+ SIM_DESC sd;
int rn;
unsigned char *memory;
{
init_system();
if (rn > 34)
- WRITE_64 (memory, State.a[rn-32]);
+ WRITE_64 (memory, State.a[rn-35]);
else if (rn == 32)
WRITE_16 (memory, IMAP0);
else if (rn == 33)
}
void
-sim_store_register (rn, memory)
+sim_store_register (sd, rn, memory)
+ SIM_DESC sd;
int rn;
unsigned char *memory;
{
init_system();
if (rn > 34)
- State.a[rn-32] = READ_64 (memory) & MASK40;
+ State.a[rn-35] = READ_64 (memory) & MASK40;
else if (rn == 34)
SET_DMAP( READ_16(memory) );
else if (rn == 33)
void
-sim_do_command (cmd)
+sim_do_command (sd, cmd)
+ SIM_DESC sd;
char *cmd;
{
(*d10v_callback->printf_filtered) (d10v_callback, "sim_do_command: %s\n",cmd);
}
-int
-sim_load (prog, from_tty)
+SIM_RC
+sim_load (sd, prog, abfd, from_tty)
+ SIM_DESC sd;
char *prog;
+ bfd *abfd;
int from_tty;
{
- /* Return nonzero so GDB will handle it. */
- return 1;
+ extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
+
+ if (prog_bfd != NULL && prog_bfd_was_opened_p)
+ bfd_close (prog_bfd);
+ prog_bfd = sim_load_file (sd, myname, d10v_callback, prog, abfd,
+ sim_kind == SIM_OPEN_DEBUG,
+ 0, sim_write_phys);
+ if (prog_bfd == NULL)
+ return SIM_RC_FAIL;
+ prog_bfd_was_opened_p = abfd == NULL;
+ return SIM_RC_OK;
}