summaryrefslogtreecommitdiff
path: root/sim/d30v/sim-calls.c
blob: d319529cc2152b691dcfe2eef10eb6145c04d467 (plain)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*  This file is part of the program psim.

    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
    Copyright (C) 1997, Free Software Foundation

    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 <stdarg.h>
#include <ctype.h>

#include "sim-main.h"
#include "sim-options.h"

#include "bfd.h"
#include "sim-utils.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

static unsigned long extmem_size = 1024*1024*8;	/* 8 meg is the maximum listed in the arch. manual */

static const char * get_insn_name (sim_cpu *, int);

#define SIM_ADDR unsigned


#define OPTION_TRACE_CALL	200
#define OPTION_TRACE_TRAPDUMP	201
#define OPTION_EXTMEM_SIZE	202

static SIM_RC
d30v_option_handler (SIM_DESC sd,
		     sim_cpu *cpu,
		     int opt,
		     char *arg,
		     int command_p)
{
  char *suffix;

  switch (opt)
    {
    default:
      break;

    case OPTION_TRACE_CALL:
      if (arg == NULL || strcmp (arg, "yes") == 0 || strcmp (arg, "on") == 0)
	TRACE_CALL_P = 1;
      else if (strcmp (arg, "no") == 0 || strcmp (arg, "off") == 0)
	TRACE_CALL_P = 0;
      else
	{
	  sim_io_eprintf (sd, "Unreconized --trace-call option `%s'\n", arg);
	  return SIM_RC_FAIL;
	}
      return SIM_RC_OK;

    case OPTION_TRACE_TRAPDUMP:
      if (arg == NULL || strcmp (arg, "yes") == 0 || strcmp (arg, "on") == 0)
	TRACE_TRAP_P = 1;
      else if (strcmp (arg, "no") == 0 || strcmp (arg, "off") == 0)
	TRACE_TRAP_P = 0;
      else
	{
	  sim_io_eprintf (sd, "Unreconized --trace-call option `%s'\n", arg);
	  return SIM_RC_FAIL;
	}
      return SIM_RC_OK;

    case OPTION_EXTMEM_SIZE:
      if (arg == NULL || !isdigit (*arg))
	{
	  sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
	  return SIM_RC_FAIL;
	}

      suffix = arg;
      extmem_size = strtol (arg, &suffix, 0);
      if (*suffix == 'm' || *suffix == 'M')
	extmem_size <<= 20;
      else if (*suffix == 'k' || *suffix == 'K')
	extmem_size <<= 10;
      sim_do_commandf (sd, "memory delete 0x80000000");
      sim_do_commandf (sd, "memory region 0x80000000,0x%lx", extmem_size);

      return SIM_RC_OK;
    }

  sim_io_eprintf (sd, "Unknown option (%d)\n", opt);
  return SIM_RC_FAIL;
}

static const OPTION d30v_options[] =
{
  { {"trace-call", optional_argument, NULL, OPTION_TRACE_CALL},
      '\0', "on|off", "Enable tracing of calls and returns, checking saved registers",
      d30v_option_handler },
  { {"trace-trapdump", optional_argument, NULL, OPTION_TRACE_TRAPDUMP},
      '\0', "on|off",
#if TRAPDUMP
    "Traps 0..30 dump out all of the registers (defaults on)",
#else
    "Traps 0..30 dump out all of the registers",
#endif
      d30v_option_handler },
  { {"extmem-size", required_argument, NULL, OPTION_EXTMEM_SIZE},
    '\0', "size", "Change size of external memory, default 8 meg",
    d30v_option_handler },
  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
};

/* Return name of an insn, used by insn profiling.  */

static const char *
get_insn_name (sim_cpu *cpu, int i)
{
  return itable[i].name;
}

/* Structures used by the simulator, for gdb just have static structures */

SIM_DESC
sim_open (SIM_OPEN_KIND kind,
	  host_callback *callback,
	  struct _bfd *abfd,
	  char **argv)
{
  SIM_DESC sd = sim_state_alloc (kind, callback);

  /* FIXME: watchpoints code shouldn't need this */
  STATE_WATCHPOINTS (sd)->pc = &(PC);
  STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
  STATE_WATCHPOINTS (sd)->interrupt_handler = d30v_interrupt_event;

  /* Initialize the mechanism for doing insn profiling.  */
  CPU_INSN_NAME (STATE_CPU (sd, 0)) = get_insn_name;
  CPU_MAX_INSNS (STATE_CPU (sd, 0)) = nr_itable_entries;

#ifdef TRAPDUMP
  TRACE_TRAP_P = TRAPDUMP;
#endif

  if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
    return 0;
  sim_add_option_table (sd, NULL, d30v_options);

  /* Memory and EEPROM */
  /* internal instruction RAM - fixed */
  sim_do_commandf (sd, "memory region 0,0x10000");
  /* internal data RAM - fixed */
  sim_do_commandf (sd, "memory region 0x20000000,0x8000");
  /* control register dummy area */
  sim_do_commandf (sd, "memory region 0x40000000,0x10000");
  /* external RAM */
  sim_do_commandf (sd, "memory region 0x80000000,0x%lx", extmem_size);
  /* EIT RAM */
  sim_do_commandf (sd, "memory region 0xfffff000,0x1000");

  /* getopt will print the error message so we just have to exit if this fails.
     FIXME: Hmmm...  in the case of gdb we need getopt to call
     print_filtered.  */
  if (sim_parse_args (sd, argv) != SIM_RC_OK)
    {
      /* Uninstall the modules to avoid memory leaks,
	 file descriptor leaks, etc.  */
      sim_module_uninstall (sd);
      return 0;
    }

  /* check for/establish the a reference program image */
  if (sim_analyze_program (sd,
			   (STATE_PROG_ARGV (sd) != NULL
			    ? *STATE_PROG_ARGV (sd)
			    : NULL),
			   abfd) != SIM_RC_OK)
    {
      sim_module_uninstall (sd);
      return 0;
    }

  /* establish any remaining configuration options */
  if (sim_config (sd) != SIM_RC_OK)
    {
      sim_module_uninstall (sd);
      return 0;
    }

  if (sim_post_argv_init (sd) != SIM_RC_OK)
    {
      /* Uninstall the modules to avoid memory leaks,
	 file descriptor leaks, etc.  */
      sim_module_uninstall (sd);
      return 0;
    }

  return sd;
}


void
sim_close (SIM_DESC sd, int quitting)
{
  /* Uninstall the modules to avoid memory leaks,
     file descriptor leaks, etc.  */
  sim_module_uninstall (sd);
}


SIM_RC
sim_create_inferior (SIM_DESC sd,
		     struct _bfd *abfd,
		     char **argv,
		     char **envp)
{
  /* clear all registers */
  memset (&STATE_CPU (sd, 0)->regs, 0, sizeof (STATE_CPU (sd, 0)->regs));
  EIT_VB = EIT_VB_DEFAULT;
  STATE_CPU (sd, 0)->unit = any_unit;
  sim_module_init (sd);
  if (abfd != NULL)
    PC = bfd_get_start_address (abfd);
  else
    PC = 0xfffff000; /* reset value */
  return SIM_RC_OK;
}

void
sim_do_command (SIM_DESC sd, char *cmd)
{
  if (sim_args_command (sd, cmd) != SIM_RC_OK)
    sim_io_printf (sd, "Unknown command `%s'\n", cmd);
}

/* The following register definitions were ripped off from
   gdb/config/tm-d30v.h.  If any of those defs changes, this table needs to
   be updated.  */

#define NUM_REGS 86

#define R0_REGNUM	0
#define FP_REGNUM	11
#define LR_REGNUM 	62
#define SP_REGNUM 	63
#define SPI_REGNUM	64	/* Interrupt stack pointer */
#define SPU_REGNUM	65	/* User stack pointer */
#define CREGS_START	66

#define PSW_REGNUM 	(CREGS_START + 0) /* psw, bpsw, or dpsw??? */
#define    PSW_SM 0x80000000	/* Stack mode: 0 == interrupt (SPI),
					       1 == user (SPU) */
#define BPSW_REGNUM	(CREGS_START + 1) /* Backup PSW (on interrupt) */
#define PC_REGNUM 	(CREGS_START + 2) /* pc, bpc, or dpc??? */
#define BPC_REGNUM 	(CREGS_START + 3) /* Backup PC (on interrupt) */
#define DPSW_REGNUM	(CREGS_START + 4) /* Backup PSW (on debug trap) */
#define DPC_REGNUM 	(CREGS_START + 5) /* Backup PC (on debug trap) */
#define RPT_C_REGNUM	(CREGS_START + 7) /* Loop count */
#define RPT_S_REGNUM	(CREGS_START + 8) /* Loop start address*/
#define RPT_E_REGNUM	(CREGS_START + 9) /* Loop end address */
#define MOD_S_REGNUM	(CREGS_START + 10)
#define MOD_E_REGNUM	(CREGS_START + 11)
#define IBA_REGNUM	(CREGS_START + 14) /* Instruction break address */
#define EIT_VB_REGNUM	(CREGS_START + 15) /* Vector base address */
#define INT_S_REGNUM	(CREGS_START + 16) /* Interrupt status */
#define INT_M_REGNUM	(CREGS_START + 17) /* Interrupt mask */
#define A0_REGNUM 	84
#define A1_REGNUM 	85

int
sim_fetch_register (sd, regno, buf, length)
     SIM_DESC sd;
     int regno;
     unsigned char *buf;
     int length;
{
  if (regno < A0_REGNUM)
    {
      unsigned32 reg;

      if (regno <= R0_REGNUM + 63)
	reg = sd->cpu[0].regs.general_purpose[regno];
      else if (regno <= SPU_REGNUM)
	reg = sd->cpu[0].regs.sp[regno - SPI_REGNUM];
      else
	reg = sd->cpu[0].regs.control[regno - CREGS_START];

      buf[0] = reg >> 24;
      buf[1] = reg >> 16;
      buf[2] = reg >> 8;
      buf[3] = reg;
    }
  else if (regno < NUM_REGS)
    {
      unsigned32 reg;

      reg = sd->cpu[0].regs.accumulator[regno - A0_REGNUM] >> 32;

      buf[0] = reg >> 24;
      buf[1] = reg >> 16;
      buf[2] = reg >> 8;
      buf[3] = reg;

      reg = sd->cpu[0].regs.accumulator[regno - A0_REGNUM];

      buf[4] = reg >> 24;
      buf[5] = reg >> 16;
      buf[6] = reg >> 8;
      buf[7] = reg;
    }
  else
    abort ();
  return -1;
}

int
sim_store_register (sd, regno, buf, length)
     SIM_DESC sd;
     int regno;
     unsigned char *buf;
     int length;
{
  if (regno < A0_REGNUM)
    {
      unsigned32 reg;

      reg = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];

      if (regno <= R0_REGNUM + 63)
	sd->cpu[0].regs.general_purpose[regno] = reg;
      else if (regno <= SPU_REGNUM)
	sd->cpu[0].regs.sp[regno - SPI_REGNUM] = reg;
      else
	sd->cpu[0].regs.control[regno - CREGS_START] = reg;
    }
  else if (regno < NUM_REGS)
    {
      unsigned32 reg;

      reg = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];

      sd->cpu[0].regs.accumulator[regno - A0_REGNUM] = (unsigned64)reg << 32;

      reg = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];

      sd->cpu[0].regs.accumulator[regno - A0_REGNUM] |= reg;
    }
  else
    abort ();
  return -1;
}