OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [sim/] [or32/] [wrapper.c] - Blame information for rev 841

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* GDB Simulator wrapper for Or1ksim, the OpenRISC architectural simulator
2
 
3
   Copyright 1988-2008, Free Software Foundation, Inc.
4
   Copyright (C) 2010 Embecosm Limited
5
 
6
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7
 
8
   This file is part of GDB.
9
 
10
   This program is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published by the Free
12
   Software Foundation; either version 3 of the License, or (at your option)
13
   any later version.
14
 
15
   This program is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18
   more details.
19
 
20
   You should have received a copy of the GNU General Public License along
21
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
 
23
/*---------------------------------------------------------------------------*/
24
/* This is a wrapper for Or1ksim, suitable for use as a GDB simulator.
25
 
26
   The code tries to follow the GDB coding style.
27
 
28
   Commenting is Doxygen compatible.                                         */
29
/*---------------------------------------------------------------------------*/
30
 
31
#include <errno.h>
32
#include <stdlib.h>
33
#include <stdio.h>
34
#include <signal.h>
35
#include <string.h>
36
#include <sys/socket.h>
37
#include <sys/types.h>
38
#include <sys/un.h>
39
#include <unistd.h>
40
 
41
#include "ansidecl.h"
42
#include "gdb/callback.h"
43
#include "gdb/remote-sim.h"
44
#include "sim-utils.h"
45
#include "targ-vals.h"
46
 
47
#include "or1ksim.h"
48
#include "or32sim.h"
49
 
50
/* Define this to turn on debug messages */
51
/* #define OR32_SIM_DEBUG */
52
 
53
 
54
/* ------------------------------------------------------------------------- */
55
/*!Create a fully initialized simulator instance.
56
 
57
   This function is called when the simulator is selected from the gdb command
58
   line.
59
 
60
   While the simulator configuration can be parameterized by (in decreasing
61
   precedence) argv's SIM-OPTION, argv's TARGET-PROGRAM and the abfd argument,
62
   the successful creation of the simulator shall not dependent on the
63
   presence of any of these arguments/options.
64
 
65
   For a hardware simulator the created simulator shall be sufficiently
66
   initialized to handle, without restrictions any client requests (including
67
   memory reads/writes, register fetch/stores and a resume).
68
 
69
   For a process simulator, the process is not created until a call to
70
   sim_create_inferior.
71
 
72
   We do the following on a first call.
73
   - parse the options
74
   -
75
   @todo Eventually we should use the option parser built into the GDB
76
         simulator (see common/sim-options.h). However since this is minimally
77
         documented, and we have only the one option, for now we do it
78
         ourselves.
79
 
80
   @note We seem to capable of being called twice. We use the static
81
         "global_sd" variable to keep track of this. Second and subsequent
82
         calls do nothing, but return the previously opened simulator
83
         description.
84
 
85
   @param[in] kind  Specifies how the simulator shall be used.  Currently
86
                    there are only two kinds: stand-alone and debug.
87
 
88
   @param[in] callback  Specifies a standard host callback (defined in
89
                        callback.h).
90
 
91
   @param[in] abfd      When non NULL, designates a target program.  The
92
                        program is not loaded.
93
 
94
   @param[in] argv      A standard ARGV pointer such as that passed from the
95
                        command line.  The syntax of the argument list is is
96
                        assumed to be ``SIM-PROG { SIM-OPTION } [
97
                        TARGET-PROGRAM { TARGET-OPTION } ]''.
98
 
99
                        The trailing TARGET-PROGRAM and args are only valid
100
                        for a stand-alone simulator.
101
 
102
                        The argument list is null terminated!
103
 
104
   @return On success, the result is a non NULL descriptor that shall be
105
           passed to the other sim_foo functions.                            */
106
/* ------------------------------------------------------------------------- */
107
SIM_DESC
108
sim_open (SIM_OPEN_KIND                kind,
109
          struct host_callback_struct *callback,
110
          struct bfd                  *abfd,
111
          char                        *argv[])
112
{
113
  /*!A global record of the simulator description */
114
  static SIM_DESC  static_sd = NULL;
115
 
116
#ifdef OR32_SIM_DEBUG
117 399 jeremybenn
  printf ("sim_open called\n", (int) kind);
118 330 jeremybenn
#endif
119
 
120
  /* If static_sd is not yet allocated, we allocate it and mark the simulator
121
     as not yet open. This is the only time we can process any custom
122
     arguments and only time we initialize the simulator. */
123
  if (NULL == static_sd)
124
    {
125
      int    local_argc;                /* Our local argv with extra args */
126
      char **local_argv;
127
 
128
      int    argc;                      /* How many args originally */
129
      int    i;                         /* For local argv */
130
      int    mem_defined_p = 0;          /* Have we requested a memory size? */
131
 
132
      int    res;                       /* Result of initialization */
133
 
134
      static_sd = (SIM_DESC) malloc (sizeof (*static_sd));
135
      static_sd->sim_open = 0;
136
 
137
      /* Count the number of arguments and see if we have specified either a
138
         config file or a memory size. */
139
      for (argc = 1; NULL != argv[argc]; argc++)
140
        {
141
#ifdef OR32_SIM_DEBUG
142
          printf ("argv[%d] = %s\n", argc, argv[argc]);
143
#endif
144
          if ((0 == strcmp (argv[argc], "-f"))    ||
145
              (0 == strcmp (argv[argc], "-file")) ||
146
              (0 == strcmp (argv[argc], "-m"))    ||
147
              (0 == strcmp (argv[argc], "-memory")))
148
            {
149
              mem_defined_p = 1;
150
            }
151
        }
152
 
153
      /* If we have no memory defined, we give it a default 8MB. We also always
154
         run quiet. So we must define our own argument vector */
155
      local_argc = mem_defined_p ? argc + 1 : argc + 3;
156
      local_argv = malloc ((local_argc + 1) * sizeof (char *));
157
 
158
      for (i = 0 ; i < argc; i++)
159
        {
160
          local_argv[i] = argv[i];
161
        }
162
 
163
      local_argv[i++] = "--quiet";
164
 
165
      if (!mem_defined_p)
166
        {
167
          local_argv[i++] = "--memory";
168
          local_argv[i++] = "8M";
169
        }
170
 
171
      local_argv[i] = NULL;
172
 
173
      /* Try to initialize, then we can free the local argument vector. If we
174
         fail to initialize return NULL to indicate that failure. */
175
      res = or1ksim_init (local_argc, local_argv, NULL, NULL, NULL);
176
      free (local_argv);
177
 
178
      if (res)
179
        {
180
          return  NULL;                 /* Failure */
181
        }
182
    }
183
 
184
  /* We have either initialized a new simulator, or already have an intialized
185
     simulator. Populate the descriptor and stall the processor, the return
186
     the descriptor. */
187
  static_sd->callback    = callback;
188
  static_sd->is_debug    = (kind == SIM_OPEN_DEBUG);
189
  static_sd->myname      = (char *)xstrdup (argv[0]);
190
  static_sd->sim_open    = 1;
191
  static_sd->last_reason = sim_running;
192
  static_sd->last_rc     = TARGET_SIGNAL_NONE;
193
  static_sd->entry_point = OR32_RESET_EXCEPTION;
194
  static_sd->resume_npc  = OR32_RESET_EXCEPTION;
195
 
196
  or1ksim_set_stall_state (0);
197
 
198
  return  static_sd;
199
 
200
}       /* sim_open () */
201
 
202
 
203
/* ------------------------------------------------------------------------- */
204
/*!Destroy a simulator instance.
205
 
206
   We never actually close the simulator, because we have no way to
207
   reinitialize it. Instead we just stall the processor and mark it closed.
208
 
209
   @param[in] sd        Simulation descriptor from sim_open ().
210
   @param[in] quitting  Non-zero if we cannot hang on errors.                */
211
/* ------------------------------------------------------------------------- */
212
void
213
sim_close (SIM_DESC  sd,
214
           int       quitting)
215
{
216
#ifdef OR32_SIM_DEBUG
217
  printf ("sim_close called\n");
218
#endif
219
 
220
  if (NULL == sd)
221
    {
222
      fprintf (stderr,
223
               "Warning: Attempt to close non-open simulation: ignored.\n");
224
    }
225
  else
226
    {
227
      free (sd->myname);
228
      sd->sim_open = 0;
229
      or1ksim_set_stall_state (0);
230
    }
231
}       /* sim_close () */
232
 
233
 
234
/* ------------------------------------------------------------------------- */
235
/*!Load program PROG into the simulators memory.
236
 
237
   Hardware simulator: Normally, each program section is written into
238
   memory according to that sections LMA using physical (direct)
239
   addressing.  The exception being systems, such as PPC/CHRP, which
240
   support more complicated program loaders.  A call to this function
241
   should not effect the state of the processor registers.  Multiple
242
   calls to this function are permitted and have an accumulative
243
   effect.
244
 
245
   Process simulator: Calls to this function may be ignored.
246
 
247
   @todo Most hardware simulators load the image at the VMA using
248
         virtual addressing.
249
 
250
   @todo For some hardware targets, before a loaded program can be executed,
251
         it requires the manipulation of VM registers and tables.  Such
252
         manipulation should probably (?) occure in sim_create_inferior ().
253
 
254
   @param[in] sd        Simulation descriptor from sim_open ().
255
   @param[in] prog      The name of the program
256
   @param[in] abfd      If non-NULL, the BFD for the file has already been
257
                        opened.
258
   @param[in] from_tty  Not sure what this does. Probably indicates this is a
259
                        command line load? Anyway we don't use it.
260
 
261
   @return  A return code indicating success.                                */
262
/* ------------------------------------------------------------------------- */
263
SIM_RC
264
sim_load (SIM_DESC    sd,
265
          char       *prog,
266
          struct bfd *abfd,
267
          int         from_tty)
268
{
269
  bfd *prog_bfd;
270
 
271
#ifdef OR32_SIM_DEBUG
272
  printf ("sim_load called\n");
273
#endif
274
 
275
  /* Use the built in loader, which will in turn use our write function. */
276
  prog_bfd = sim_load_file (sd, sd->myname, sd->callback, prog, abfd,
277
                            sd->is_debug, 0, sim_write);
278
 
279
  if (NULL == prog_bfd)
280
    {
281
      return SIM_RC_FAIL;
282
    }
283
 
284
  /* If the BFD was not already open, then close the loaded program. */
285
  if (NULL == abfd)
286
    {
287
      bfd_close (prog_bfd);
288
    }
289
 
290
  return  SIM_RC_OK;
291
 
292
}       /* sim_load () */
293
 
294
 
295
/* ------------------------------------------------------------------------- */
296
/*!Prepare to run the simulated program.
297
 
298
   Hardware simulator: This function shall initialize the processor
299
   registers to a known value.  The program counter and possibly stack
300
   pointer shall be set using information obtained from ABFD (or
301
   hardware reset defaults).  ARGV and ENV, dependant on the target
302
   ABI, may be written to memory.
303
 
304
   Process simulator: After a call to this function, a new process
305
   instance shall exist. The TEXT, DATA, BSS and stack regions shall
306
   all be initialized, ARGV and ENV shall be written to process
307
   address space (according to the applicable ABI) and the program
308
   counter and stack pointer set accordingly.
309
 
310
   ABFD, if not NULL, provides initial processor state information.
311
   ARGV and ENV, if non NULL, are NULL terminated lists of pointers.
312
 
313
   We perform the following steps:
314
   - stall the processor
315
   - set the entry point to the entry point in the BFD, or the reset
316
     vector if the BFD is not available.
317
   - set the resumption NPC to the reset vector. We always do this, to ensure
318
     the library is initialized.
319
 
320
   @param[in] sd    Simulation descriptor from sim_open ().
321
   @param[in] abfd  If non-NULL provides initial processor state information.
322
   @param[in] argv  Vector of arguments to the program. We don't use this
323
   @param[in] env   Vector of environment data. We don't use this.
324
 
325
   @return  A return code indicating success.                                */
326
/* ------------------------------------------------------------------------- */
327
SIM_RC
328
sim_create_inferior (SIM_DESC     sd,
329
                     struct bfd  *abfd,
330
                     char       **argv  ATTRIBUTE_UNUSED,
331
                     char       **env   ATTRIBUTE_UNUSED)
332
{
333
#ifdef OR32_SIM_DEBUG
334
  printf ("sim_create_inferior called\n");
335
#endif
336
 
337
  or1ksim_set_stall_state (1);
338
  sd->entry_point = (NULL == abfd) ? OR32_RESET_EXCEPTION :
339
                                    bfd_get_start_address (abfd);
340
  sd->resume_npc  = OR32_RESET_EXCEPTION;
341
 
342
  return  SIM_RC_OK;
343
 
344
}       /* sim_create_inferior () */
345
 
346
 
347
/* ------------------------------------------------------------------------- */
348
/*!Fetch bytes from the simulated program's memory.
349
 
350
   @param[in]  sd   Simulation descriptor from sim_open (). We don't use
351
                    this.
352
   @param[in]  mem  The address in memory to fetch from.
353
   @param[out] buf  Where to put the read data
354
   @param[in]  len  Number of bytes to fetch
355
 
356
   @return  Number of bytes read, or zero if error.                          */
357
/* ------------------------------------------------------------------------- */
358
int
359
sim_read (SIM_DESC       sd  ATTRIBUTE_UNUSED,
360
          SIM_ADDR       mem,
361
          unsigned char *buf,
362
          int            len)
363
{
364
  int res = or1ksim_read_mem (mem, buf, len);
365
 
366
#ifdef OR32_SIM_DEBUG
367 565 jeremybenn
  printf ("Reading %d bytes from %08p\n", len, (void *) mem);
368 330 jeremybenn
#endif
369
 
370
  return  res;
371
 
372
}      /* sim_read () */
373
 
374
 
375
/* ------------------------------------------------------------------------- */
376
/*!Store bytes to the simulated program's memory.
377
 
378
   @param[in] sd   Simulation descriptor from sim_open (). We don't use
379
                   this.
380
   @param[in] mem  The address in memory to write to.
381
   @param[in] buf  The data to write
382
   @param[in] len  Number of bytes to write
383
 
384
   @return  Number of byte written, or zero if error.                        */
385
/* ------------------------------------------------------------------------- */
386
int
387
sim_write (SIM_DESC             sd  ATTRIBUTE_UNUSED,
388
           SIM_ADDR             mem,
389
           const unsigned char *buf,
390
           int                  len)
391
{
392
#ifdef OR32_SIM_DEBUG
393 565 jeremybenn
  printf ("Writing %d bytes to %08p\n", len, (void *) mem);
394 330 jeremybenn
#endif
395
 
396
  return  or1ksim_write_mem ((unsigned int) mem, buf, len);
397
 
398
}       /* sim_write () */
399
 
400
 
401
/* ------------------------------------------------------------------------- */
402
/*!Fetch a register from the simulation
403
 
404
   We get the register back as a 32-bit value. However we must convert it to a
405
   character array <em>in target endian order</em>.
406
 
407
   The exception is if the register is the NPC, which is only written just
408
   before resumption, to avoid pipeline confusion. It is fetched from the SD.
409
 
410
   @param[in]  sd     Simulation descriptor from sim_open (). We don't use
411
                      this.
412
   @param[in]  regno  The register to fetch
413
   @param[out] buf    Buffer of length bytes to store the result. Data is
414
                      only transferred if length matches the register length
415
                      (the actual register size is still returned).
416
   @param[in]  len    Size of buf, which should match the size of the
417
                      register.
418
 
419
   @return  The actual size of the register, or zero if regno is not
420
            applicable. Legacy implementations return -1.
421
/* ------------------------------------------------------------------------- */
422
int
423
sim_fetch_register (SIM_DESC       sd,
424
                    int            regno,
425
                    unsigned char *buf,
426
                    int            len)
427
{
428
  unsigned long int  regval;
429
  int                res;
430
 
431
#ifdef OR32_SIM_DEBUG
432 565 jeremybenn
  printf ("sim_fetch_register (regno=%d) called\n", regno);
433 330 jeremybenn
#endif
434
  if (4 != len)
435
    {
436 399 jeremybenn
      fprintf (stderr, "Invalid register length %d\n", len);
437 330 jeremybenn
      return  0;
438
    }
439
 
440
  if (OR32_NPC_REGNUM == regno)
441
    {
442
      regval = sd->resume_npc;
443
      res    = 4;
444
    }
445
  else
446
    {
447
      int res = or1ksim_read_reg (regno, &regval) ? 4 : 0;
448
    }
449
 
450
  /* Convert to target (big) endian */
451
  if (res)
452
    {
453
      buf[0] = (regval >> 24) & 0xff;
454
      buf[1] = (regval >> 16) & 0xff;
455
      buf[2] = (regval >>  8) & 0xff;
456
      buf[3] =  regval        & 0xff;
457
 
458
      return  4;                        /* Success */
459
    }
460
  else
461
    {
462
      return  0;                 /* Failure */
463
    }
464
}       /* sim_fetch_register () */
465
 
466
 
467
/* ------------------------------------------------------------------------- */
468
/*!Store a register to the simulation
469
 
470
   We write the register back as a 32-bit value. However we must convert it from
471
   a character array <em>in target endian order</em>.
472
 
473
   The exception is if the register is the NPC, which is only written just
474
   before resumption, to avoid pipeline confusion. It is saved in the SD.
475
 
476
   @param[in] sd     Simulation descriptor from sim_open (). We don't use
477
                     this.
478
   @param[in] regno  The register to store
479
   @param[in] buf    Buffer of length bytes with the data to store. Data is
480
                     only transferred if length matches the register length
481
                     (the actual register size is still returned).
482
   @param[in] len    Size of buf, which should match the size of the
483
                     register.
484
 
485
   @return  The actual size of the register, or zero if regno is not
486
            applicable. Legacy implementations return -1.
487
/* ------------------------------------------------------------------------- */
488
int
489
sim_store_register (SIM_DESC       sd,
490
                    int            regno,
491
                    unsigned char *buf,
492
                    int            len)
493
{
494
  unsigned int  regval;
495
 
496
#ifdef OR32_SIM_DEBUG
497
  printf ("sim_store_register (regno=%d\n) called\n", regno);
498
#endif
499
 
500
  if (4 != len)
501
    {
502 399 jeremybenn
      fprintf (stderr, "Invalid register length %d\n", len);
503 330 jeremybenn
      return  0;
504
    }
505
 
506
  /* Convert from target (big) endian */
507
  regval = (((unsigned int) buf[0]) << 24) |
508
           (((unsigned int) buf[1]) << 16) |
509
           (((unsigned int) buf[2]) <<  8) |
510
           (((unsigned int) buf[3])      );
511
 
512
#ifdef OR32_SIM_DEBUG
513
  printf ("Writing register 0x%02x, value 0x%08x\n", regno, regval);
514
#endif
515
 
516
  if (OR32_NPC_REGNUM == regno)
517
    {
518
      sd->resume_npc = regval;
519
      return  4;                        /* Reg length in bytes */
520
    }
521
  else
522
    {
523
      return  or1ksim_write_reg (regno, regval) ? 4 : 0;
524
    }
525
}       /* sim_store_register () */
526
 
527
 
528
/* ------------------------------------------------------------------------- */
529
/* Print whatever statistics the simulator has collected.
530
 
531
   @param[in] sd       Simulation descriptor from sim_open (). We don't use
532
                       this.
533
   @param[in] verbose  Currently unused, and should always be zero.          */
534
/* ------------------------------------------------------------------------- */
535
void
536
sim_info (SIM_DESC  sd       ATTRIBUTE_UNUSED,
537
          int       verbose  ATTRIBUTE_UNUSED)
538
{
539
}       /* sim_info () */
540
 
541
 
542
/* ------------------------------------------------------------------------- */
543
/*!Run (or resume) the simulated program.
544
 
545
   Hardware simulator: If the SIGRC value returned by
546
   sim_stop_reason() is passed back to the simulator via siggnal then
547
   the hardware simulator shall correctly deliver the hardware event
548
   indicated by that signal.  If a value of zero is passed in then the
549
   simulation will continue as if there were no outstanding signal.
550
   The effect of any other siggnal value is is implementation
551
   dependant.
552
 
553
   Process simulator: If SIGRC is non-zero then the corresponding
554
   signal is delivered to the simulated program and execution is then
555
   continued.  A zero SIGRC value indicates that the program should
556
   continue as normal.
557
 
558
   We carry out the following
559
   - Clear the debug reason register
560
   - Clear watchpoing break generation in debug mode register 2
561
   - Set the debug unit to handle TRAP exceptions
562
   - If stepping, set the single step trigger in debug mode register 1
563
   - Write the resume_npc if it differs from the actual NPC.
564
   - Unstall the processor
565
   - Run the processor.
566
 
567
   On execution completion, we determine the reason for the halt. If it is a
568
   breakpoint, we mark the resumption NPC to be the PPC (so we redo the NPC
569
   location).
570
 
571
   @param[in] sd       Simulation descriptor from sim_open ().
572
   @param[in] step     When non-zero indicates that only a single simulator
573
                       cycle should be emulated.
574
   @param[in] siggnal  If non-zero is a (HOST) SIGRC value indicating the type
575
                       of event (hardware interrupt, signal) to be delivered
576
                       to the simulated program.                             */
577
/* ------------------------------------------------------------------------- */
578
void
579
sim_resume (SIM_DESC  sd,
580
            int       step,
581
            int       siggnal)
582
{
583
  unsigned long int  npc;               /* Next Program Counter */
584
  unsigned long int  drr;               /* Debug Reason Register */
585
  unsigned long int  dsr;               /* Debug Stop Register */
586
  unsigned long int  dmr1;              /* Debug Mode Register 1*/
587
  unsigned long int  dmr2;              /* Debug Mode Register 2*/
588
 
589
  unsigned long int  retval;            /* Return value on Or1ksim exit */
590
 
591 399 jeremybenn
  unsigned long int  cycles;            /* Length of run in cycles */
592 330 jeremybenn
  int                res;               /* Result of a run. */
593
 
594
#ifdef OR32_SIM_DEBUG
595
  printf ("sim_resume called\n");
596
#endif
597
 
598
  /* Clear Debug Reason Register and watchpoint break generation in Debug Mode
599
     Register 2 */
600
  (void) or1ksim_write_spr (OR32_SPR_DRR, 0);
601
 
602
  (void) or1ksim_read_spr (OR32_SPR_DMR2, &dmr2);
603
  dmr2 &= ~OR32_SPR_DMR2_WGB;
604
  (void) or1ksim_write_spr (OR32_SPR_DMR2, dmr2);
605
 
606
  /* Set debug unit to handle TRAP exceptions */
607
  (void) or1ksim_read_spr (OR32_SPR_DSR, &dsr);
608
  dsr |= OR32_SPR_DSR_TE;
609
  (void) or1ksim_write_spr (OR32_SPR_DSR, dsr);
610
 
611
  /* Set the single step trigger in Debug Mode Register 1 if we are
612
     stepping. Otherwise clear it! */
613
  if (step)
614
    {
615
      (void) or1ksim_read_spr (OR32_SPR_DMR1, &dmr1);
616
      dmr1 |= OR32_SPR_DMR1_ST;
617
      (void) or1ksim_write_spr (OR32_SPR_DMR1, dmr1);
618
    }
619
  else
620
    {
621
      (void) or1ksim_read_spr (OR32_SPR_DMR1, &dmr1);
622
      dmr1 &= ~OR32_SPR_DMR1_ST;
623
      (void) or1ksim_write_spr (OR32_SPR_DMR1, dmr1);
624
    }
625
 
626
  /* Set the NPC if it has changed */
627
  (void) or1ksim_read_reg (OR32_NPC_REGNUM, &npc);
628
 
629
#ifdef OR32_SIM_DEBUG
630
  printf ("  npc = 0x%08lx, resume_npc = 0x%08lx\n", npc, sd->resume_npc);
631
#endif
632
 
633
  if (npc != sd->resume_npc)
634
    {
635
      (void) or1ksim_write_reg (OR32_NPC_REGNUM, sd->resume_npc);
636
    }
637
 
638 399 jeremybenn
  /* Set a time point */
639
  or1ksim_set_time_point ();
640
 
641 330 jeremybenn
  /* Unstall and run */
642
  or1ksim_set_stall_state (0);
643
  res = or1ksim_run (-1.0);
644
 
645
  /* Determine the reason for stopping. If we hit a breakpoint, then the
646
     resumption NPC must be set to the PPC to allow re-execution of the
647
     trapped instruction. */
648
  switch (res)
649
    {
650
    case OR1KSIM_RC_HALTED:
651 565 jeremybenn
 
652
#ifdef OR32_SIM_DEBUG
653
      (void) or1ksim_read_reg (OR32_NPC_REGNUM, &npc);
654
      printf ("  execution halted at 0x%08lx.\n", npc);
655
#endif
656
 
657 330 jeremybenn
      sd->last_reason = sim_exited;
658
      (void) or1ksim_read_reg (OR32_FIRST_ARG_REGNUM, &retval);
659
      sd->last_rc     = (unsigned int) retval;
660
      sd->resume_npc  = OR32_RESET_EXCEPTION;
661 399 jeremybenn
      cycles = (long int) (or1ksim_get_time_period ()
662
                           * (double) or1ksim_clock_rate());
663 330 jeremybenn
      break;
664
 
665
    case OR1KSIM_RC_BRKPT:
666 565 jeremybenn
 
667
#ifdef OR32_SIM_DEBUG
668
      printf ("  execution hit breakpoint.\n");
669
#endif
670
 
671 330 jeremybenn
      sd->last_reason = sim_stopped;
672
      sd->last_rc     = TARGET_SIGNAL_TRAP;
673
 
674
      /* This could have been a breakpoint or single step. */
675
      if (step)
676
        {
677
          (void) or1ksim_read_reg (OR32_NPC_REGNUM, &(sd->resume_npc));
678
        }
679
      else
680
        {
681
          (void) or1ksim_read_reg (OR32_PPC_REGNUM, &(sd->resume_npc));
682
        }
683
 
684
      break;
685
 
686
    case OR1KSIM_RC_OK:
687
      /* Should not happen */
688
      fprintf (stderr, "Ooops. Didn't expect OK return from Or1ksim.\n");
689
 
690
      sd->last_reason = sim_running;            /* Should trigger an error! */
691
      sd->last_rc     = TARGET_SIGNAL_NONE;
692
      (void) or1ksim_read_reg (OR32_NPC_REGNUM, &(sd->resume_npc));
693
      break;
694
    }
695
}       /* sim_resume () */
696
 
697
 
698
/* ------------------------------------------------------------------------- */
699
/*!Asynchronous request to stop the simulation.
700
 
701
   @param[in] sd  Simulation descriptor from sim_open (). We don't use this.
702
 
703
   @return  Non-zero indicates that the simulator is able to handle the
704
            request.                                                         */
705
/* ------------------------------------------------------------------------- */
706
int sim_stop (SIM_DESC  sd  ATTRIBUTE_UNUSED)
707
{
708
#ifdef OR32_SIM_DEBUG
709
  printf ("sim_stop called\n");
710
#endif
711
 
712
  return  0;                     /* We don't support this */
713
 
714
}       /* sim_stop () */
715
 
716
 
717
/* ------------------------------------------------------------------------- */
718
/*!Fetch the REASON why the program stopped.
719
 
720
   The reason enumeration indicates why:
721
 
722
   - sim_exited:    The program has terminated. sigrc indicates the target
723
                    dependant exit status.
724
 
725
   - sim_stopped:   The program has stopped.  sigrc uses the host's signal
726
                    numbering as a way of identifying the reaon: program
727
                    interrupted by user via a sim_stop request (SIGINT); a
728
                    breakpoint instruction (SIGTRAP); a completed single step
729
                    (SIGTRAP); an internal error condition (SIGABRT); an
730
                    illegal instruction (SIGILL); Access to an undefined
731
                    memory region (SIGSEGV); Mis-aligned memory access
732
                    (SIGBUS).
733
 
734
                    For some signals information in addition to the signal
735
                    number may be retained by the simulator (e.g. offending
736
                    address), that information is not directly accessable via
737
                    this interface.
738
 
739
   - sim_signalled: The program has been terminated by a signal. The
740
                    simulator has encountered target code that causes the the
741
                    program to exit with signal sigrc.
742
 
743
   - sim_running:
744
   - sim_polling:   The return of one of these values indicates a problem
745
                    internal to the simulator.
746
 
747
   @param[in]  sd      Simulation descriptor from sim_open ().
748
   @param[out] reason  The reason for stopping
749
   @param[out] sigrc   Supplementary information for some values of reason.  */
750
/* ------------------------------------------------------------------------- */
751
void
752
sim_stop_reason (SIM_DESC       sd,
753
                 enum sim_stop *reason,
754
                 int           *sigrc)
755
 {
756
   *reason = sd->last_reason;
757
   *sigrc  = sd->last_rc;
758
 
759
}       /* sim_stop_reason () */
760
 
761
 
762
/* ------------------------------------------------------------------------- */
763
/* Passthru for other commands that the simulator might support.
764
 
765
   Simulators should be prepared to deal with any combination of NULL
766
   or empty command.
767
 
768
   This implementation currently does nothing.
769
 
770
   @param[in] sd  Simulation descriptor from sim_open (). We don't use this.
771
   @param[in] cmd The command to pass through.                               */
772
/* ------------------------------------------------------------------------- */
773
void
774
sim_do_command (SIM_DESC  sd   ATTRIBUTE_UNUSED,
775
                char     *cmd  ATTRIBUTE_UNUSED)
776
{
777 399 jeremybenn
#ifdef OR32_SIM_DEBUG
778
  printf ("sim_do_command called\n");
779
#endif
780
 
781 330 jeremybenn
}       /* sim_do_command () */
782
 
783
 
784
/* ------------------------------------------------------------------------- */
785
/* Set the default host_callback_struct
786
 
787
   @note Deprecated, but implemented, since it is still required for linking.
788
 
789
   This implementation currently does nothing.
790
 
791
   @param[in] ptr  The host_callback_struct pointer. Unused here.            */
792
/* ------------------------------------------------------------------------- */
793
void
794
sim_set_callbacks (struct host_callback_struct *ptr ATTRIBUTE_UNUSED)
795
{
796 399 jeremybenn
#ifdef OR32_SIM_DEBUG
797
  printf ("sim_set_callbacks called\n");
798
#endif
799
 
800 330 jeremybenn
}       /* sim_set_callbacks () */
801
 
802
 
803
/* ------------------------------------------------------------------------- */
804
/* Set the size of the simulator memory array.
805
 
806
   @note Deprecated, but implemented, since it is still required for linking.
807
 
808
   This implementation currently does nothing.
809
 
810
   @param[in] size  The memory size to use. Unused here.                     */
811
/* ------------------------------------------------------------------------- */
812
void
813
sim_size (int  size  ATTRIBUTE_UNUSED)
814
{
815 399 jeremybenn
#ifdef OR32_SIM_DEBUG
816
  printf ("sim_size called\n");
817
#endif
818
 
819 330 jeremybenn
}       /* sim_size () */
820
 
821
 
822
/* ------------------------------------------------------------------------- */
823
/* Single step the simulator with tracing enabled.
824
 
825
   @note Deprecated, but implemented, since it is still required for linking.
826
 
827
   This implementation currently does nothing.
828
 
829
   @param[in] sd  The simulator description struct. Unused here.             */
830
/* ------------------------------------------------------------------------- */
831
void
832
sim_trace (SIM_DESC  sd  ATTRIBUTE_UNUSED)
833
{
834 399 jeremybenn
#ifdef OR32_SIM_DEBUG
835
  printf ("sim_trace called\n");
836
#endif
837
 
838 330 jeremybenn
}       /* sim_trace () */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.