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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [verilog/] [vpi/] [c/] [gdb.c] - Blame information for rev 46

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 40 julius
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : ORPSoCv2
9
// File Name                      : gdb.c
10
// Prepared By                    : jb, rmd
11
// Project Start                  : 2008-10-01
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// Implements RSP comatible GDB stub
44
//
45
 
46
 
47
/*$$CHANGE HISTORY*/
48
/******************************************************************************/
49
/*                                                                            */
50
/*                         C H A N G E  H I S T O R Y                         */
51
/*                                                                            */
52
/******************************************************************************/
53
 
54
// Date         Version Description
55
//------------------------------------------------------------------------
56
// 081101               Imported code from "jp" project                 jb
57
// 090219               Adapted code from Jeremy Bennett's RSP server
58
//                      for the or1ksim project.                       rmb
59
// 090304               Finished RSP server code import, added extra
60
//                      functions, adding stability when debugging on
61
//                      a remote target.                                jb
62
// 090608               A few hacks for VPI compatibilty added          jb
63 46 julius
// 090827               Fixed endianness, block accesses, byte writes.  jb
64 40 julius
 
65
#ifdef CYGWIN_COMPILE
66
 
67
#else
68
// linux includes                  
69
#include <time.h>
70
#include <sched.h>
71
#endif
72
 
73
#include <stdio.h>
74
#include <ctype.h>
75
#include <string.h>
76
#include <stdlib.h>
77
#include <unistd.h>
78
#include <stdarg.h>
79
 
80
/* Libraries for JTAG proxy server.  */
81
#include <sys/stat.h>
82
#include <sys/types.h>
83
#include <sys/socket.h>
84
#include <netinet/in.h>
85
#include <sys/ioctl.h>
86
#include <sys/select.h>
87
#include <sys/poll.h>
88
#include <fcntl.h>
89
#include <netdb.h>
90
#include <netinet/tcp.h>
91
#include <signal.h>
92
#include <inttypes.h>
93
#include <errno.h>
94
#include <arpa/inet.h>
95
 
96
 
97
#ifndef DEBUG_GDB
98
#define DEBUG_GDB 0
99
#endif
100
 
101
#ifndef DEBUG_GDB_DUMP_DATA
102
#define DEBUG_GDB_DUMP_DATA 0
103
#endif
104
 
105
#ifndef DEBUG_GDB_BLOCK_DATA
106
#define DEBUG_GDB_BLOCK_DATA 0
107
#endif
108
 
109
#ifndef DEBUG_CMDS
110
#define DEBUG_CMDS 0
111
#endif
112
 
113
/*! Name of the Or1ksim RSP service */
114
#define OR1KSIM_RSP_SERVICE  "or1ksim-rsp"
115
 
116
#include "gdb.h" /* partially copied from gdb/config/or1k */
117
#include "rsp-rtl_sim.h"
118
 
119
#define MAX_GPRS    (32)
120
 
121
/* Indices of GDB registers that are not GPRs. Must match GDB settings! */
122
#define PPC_REGNUM  (MAX_GPRS + 0)      /*!< Previous PC */
123
#define NPC_REGNUM  (MAX_GPRS + 1)      /*!< Next PC */
124
#define SR_REGNUM   (MAX_GPRS + 2)      /*!< Supervision Register */
125
#define NUM_REGS    (MAX_GPRS + 3)      /*!< Total GDB registers */
126
 
127
/* OR1k CPU registers address */
128
#define NPC_CPU_REG_ADD         0x10                                                    /* Next PC */
129
#define SR_CPU_REG_ADD          0x11                                                    /* Supervision Register */
130
#define PPC_CPU_REG_ADD         0x12                                                    /* Previous PC */
131
#define DMR1_CPU_REG_ADD        ((6 << 11) + 16)        /* Debug Mode Register 1 (DMR1) 0x3010 */
132
#define DMR2_CPU_REG_ADD        ((6 << 11) + 17)        /* Debug Mode Register 2 (DMR2) 0x3011 */
133
#define DSR_CPU_REG_ADD         ((6 << 11) + 20)        /* Debug Stop Register (DSR) 0x3014 */
134
#define DRR_CPU_REG_ADD         ((6 << 11) + 21)        /* Debug Reason Register (DRR) 0x3015 */
135
 
136
/*! Trap instruction for OR32 */
137
#define OR1K_TRAP_INSTR  0x21000001
138
 
139
/*! The maximum number of characters in inbound/outbound buffers.  The largest
140
    packets are the 'G' packet, which must hold the 'G' and all the registers
141
    with two hex digits per byte and the 'g' reply, which must hold all the
142
    registers, and (in our implementation) an end-of-string (0)
143
    character. Adding the EOS allows us to print out the packet as a
144
    string. So at least NUMREGBYTES*2 + 1 (for the 'G' or the EOS) are needed
145
    for register packets */
146
#define GDB_BUF_MAX  ((NUM_REGS) * 8 + 1)
147
 
148
/*! Size of the matchpoint hash table. Largest prime < 2^10 */
149
#define MP_HASH_SIZE  1021
150
 
151
/* Definition of special-purpose registers (SPRs). */
152
#define MAX_SPRS (0x10000)
153
 
154
#define SPR_DMR1_ST                     0x00400000  /* Single-step trace*/
155
#define SPR_DMR2_WGB            0x003ff000  /* Watchpoints generating breakpoint */
156
#define SPR_DSR_TE                              0x00002000  /* Trap exception */
157
 
158
#define WORDSBIGENDIAN_N
159
 
160
/* Definition of OR1K exceptions */
161
#define EXCEPT_NONE     0x0000
162
#define EXCEPT_RESET    0x0100
163
#define EXCEPT_BUSERR   0x0200
164
#define EXCEPT_DPF      0x0300
165
#define EXCEPT_IPF      0x0400
166
#define EXCEPT_TICK     0x0500
167
#define EXCEPT_ALIGN    0x0600
168
#define EXCEPT_ILLEGAL  0x0700
169
#define EXCEPT_INT      0x0800
170
#define EXCEPT_DTLBMISS 0x0900
171
#define EXCEPT_ITLBMISS 0x0a00
172
#define EXCEPT_RANGE    0x0b00
173
#define EXCEPT_SYSCALL  0x0c00
174
#define EXCEPT_FPE      0x0d00
175
#define EXCEPT_TRAP     0x0e00
176
 
177
// Changed to #defines from static const int's due to compile error
178
// DRR (Debug Reason Register) Bits
179
#define SPR_DRR_RSTE  0x00000001  //!< Reset
180
#define SPR_DRR_BUSEE 0x00000002  //!< Bus error
181
#define SPR_DRR_DPFE  0x00000004  //!< Data page fault
182
#define SPR_DRR_IPFE  0x00000008  //!< Insn page fault
183
#define SPR_DRR_TTE   0x00000010  //!< Tick timer
184
#define SPR_DRR_AE    0x00000020  //!< Alignment
185
#define SPR_DRR_IIE   0x00000040  //!< Illegal instruction
186
#define SPR_DRR_IE    0x00000080  //!< Interrupt
187
#define SPR_DRR_DME   0x00000100  //!< DTLB miss
188
#define SPR_DRR_IME   0x00000200  //!< ITLB miss
189
#define SPR_DRR_RE    0x00000400  //!< Range fault
190
#define SPR_DRR_SCE   0x00000800  //!< System call
191
#define SPR_DRR_FPE   0x00001000  //!< Floating point
192
#define SPR_DRR_TE    0x00002000  //!< Trap
193
 
194
 
195
/*! Definition of GDB target signals. Data taken from the GDB 6.8
196
    source. Only those we use defined here. The exact meaning of
197
    signal number is defined by the header `include/gdb/signals.h'
198
    in the GDB source code. For an explanation of what each signal
199
    means, see target_signal_to_string.*/
200
enum target_signal {
201
  TARGET_SIGNAL_NONE =  0,
202
  TARGET_SIGNAL_INT  =  2,
203
  TARGET_SIGNAL_ILL  =  4,
204
  TARGET_SIGNAL_TRAP =  5,
205
  TARGET_SIGNAL_FPE  =  8,
206
  TARGET_SIGNAL_BUS  = 10,
207
  TARGET_SIGNAL_SEGV = 11,
208
  TARGET_SIGNAL_ALRM = 14,
209
  TARGET_SIGNAL_USR2 = 31,
210
  TARGET_SIGNAL_PWR  = 32
211
};
212
 
213
/*! String to map hex digits to chars */
214
static const char hexchars[]="0123456789abcdef";
215
 
216
 
217
//! Is the NPC cached?
218
 
219
//! Setting the NPC flushes the pipeline, so subsequent reads will return
220
//! zero until the processor has refilled the pipeline. This will not be
221
//! happening if the processor is stalled (as it is when GDB had control),
222
//! so we must cache the NPC. As soon as the processor is unstalled, this
223
//! cached value becomes invalid. So we must track the stall state, and if
224
//! appropriate cache the NPC.
225
enum stallStates {
226
  STALLED,
227
  UNSTALLED,
228
  UNKNOWN
229
} stallState;
230
 
231
int      npcIsCached;           //!< Is the NPC cached - should be bool
232
uint32_t  npcCachedValue;               //!< Cached value of the NPC
233
 
234
static int err = 0;
235
 
236
 
237
/************************
238
   JTAG Server Routines
239
************************/
240
int serverIP = 0;
241
int serverPort = 0;
242
int server_fd = 0;
243
int gdb_fd = 0;
244
 
245
static int tcp_level = 0;
246
 
247
/* global to store what chain the debug unit is currently connected to
248
(not the JTAG TAP, but the onchip debug module has selected) */
249
int gdb_chain = -1;
250
 
251
/*! Data structure for RSP buffers. Can't be null terminated, since it may
252
  include zero bytes */
253
struct rsp_buf
254
{
255
  char  data[GDB_BUF_MAX];
256
  int   len;
257
};
258
 
259
/*! Enumeration of different types of matchpoint. These have explicit values
260
    matching the second digit of 'z' and 'Z' packets. */
261
enum mp_type {
262
  BP_MEMORY   = 0,               // software-breakpoint Z0  break 
263
  BP_HARDWARE = 1,              // hardware-breakpoint Z1  hbreak 
264
  WP_WRITE    = 2,              // write-watchpoint    Z2  watch  
265
  WP_READ     = 3,              // read-watchpoint     Z3  rwatch  
266
  WP_ACCESS   = 4                       // access-watchpoint   Z4  awatch
267
};
268
 
269
/*! Data structure for a matchpoint hash table entry */
270
struct mp_entry
271
{
272
  enum mp_type       type;              /*!< Type of matchpoint */
273
  uint32_t  addr;               /*!< Address with the matchpoint */
274
  uint32_t  instr;              /*!< Substituted instruction */
275
  struct mp_entry   *next;              /*!< Next entry with this hash */
276
};
277
 
278
/*! Central data for the RSP connection */
279
static struct
280
{
281
  int                           client_waiting; /*!< Is client waiting a response? */
282
// Not used  int                proto_num;              /*!< Number of the protocol used */
283
  int                client_fd;         /*!< FD for talking to GDB */
284
  int               sigval;                     /*!< GDB signal for any exception */
285
  uint32_t start_addr;  /*!< Start of last run */
286
  struct mp_entry   *mp_hash[MP_HASH_SIZE];     /*!< Matchpoint hash table */
287
} rsp;
288
 
289
/* Forward declarations of static functions */
290
static char *printTime(void);
291
static int gdb_read(void*, int);
292
static int gdb_write(void*, int);
293
static void ProtocolClean(int, int32_t);
294
static void GDBRequest(void);
295
static void rsp_interrupt();
296
static char rsp_peek();
297
static struct rsp_buf *get_packet (void);
298
static void rsp_init (void);
299
static void set_npc (uint32_t  addr);
300
static uint32_t get_npc();
301
static void rsp_check_for_exception();
302
static int check_for_exception_vector(uint32_t ppc);
303
static void rsp_exception (uint32_t  except);
304
static int get_rsp_char (void);
305
static int hex (int  c);
306
static void rsp_get_client (void);
307
static void rsp_client_request (void);
308
static void rsp_client_close (void);
309
static void client_close (char err);
310
static void     put_str_packet (const char *str);
311
static void rsp_report_exception (void);
312
static void put_packet (struct rsp_buf *p_buf);
313
static void send_rsp_str (unsigned char *data, int len);
314
static void rsp_query (struct rsp_buf *p_buf);
315
static void rsp_vpkt (struct rsp_buf *p_buf);
316
static void     rsp_step (struct rsp_buf *p_buf);
317
static void     rsp_step_with_signal (struct rsp_buf *p_buf);
318
static void     rsp_step_generic (uint32_t  addr, uint32_t  except);
319
static void rsp_continue (struct rsp_buf *p_buf);
320
static void     rsp_continue_with_signal (struct rsp_buf *p_buf);
321
static void     rsp_continue_generic (uint32_t  addr, uint32_t  except);
322
static void rsp_read_all_regs (void);
323
static void rsp_write_all_regs (struct rsp_buf *p_buf);
324
static void rsp_read_mem (struct rsp_buf *p_buf);
325
static void rsp_write_mem (struct rsp_buf *p_buf);
326
static void rsp_write_mem_bin (struct rsp_buf *p_buf);
327
static int rsp_unescape (char *data, int len);
328
static void rsp_read_reg (struct rsp_buf *p_buf);
329
static void rsp_write_reg (struct rsp_buf *p_buf);
330
static void mp_hash_init (void);
331
static void     mp_hash_add (enum mp_type type, uint32_t  addr, uint32_t  instr);
332
static struct mp_entry * mp_hash_lookup (enum mp_type type, uint32_t  addr);
333
static struct mp_entry * mp_hash_delete (enum mp_type type,     uint32_t  addr);
334
static void rsp_remove_matchpoint (struct rsp_buf *p_buf);
335
static void rsp_insert_matchpoint (struct rsp_buf *p_buf);
336
static void rsp_command (struct rsp_buf *p_buf);
337
static void rsp_set (struct rsp_buf *p_buf);
338
static void rsp_restart (void);
339
static void  ascii2hex (char *dest,char *src);
340
static void  hex2ascii (char *dest,     char *src);
341
static uint32_t hex2reg (char *p_buf);
342
static void     reg2hex (uint32_t  val, char *p_buf);
343
static void swap_buf(char* p_buf, int len);
344
static void set_stall_state (int state);
345
static void reset_or1k (void);
346
static void gdb_ensure_or1k_stalled();
347
static int gdb_set_chain(int chain);
348 46 julius
static int gdb_write_byte(uint32_t adr, uint8_t data);
349
static int gdb_write_short(uint32_t adr, uint16_t data);
350 40 julius
static int gdb_write_reg(uint32_t adr, uint32_t data);
351
static int gdb_read_reg(uint32_t adr, uint32_t *data);
352
static int gdb_write_block(uint32_t adr, uint32_t *data, int len);
353
static int gdb_read_block(uint32_t adr, uint32_t *data, int len);
354
 
355
char *printTime(void)
356
{
357
  time_t tid;
358
  struct tm *strtm;
359
  static char timeBuf[20];
360
 
361
  time(&tid);
362
  strtm = localtime(&tid);
363
  sprintf(timeBuf,"[%.02d:%.02d:%.02d] ",strtm->tm_hour,strtm->tm_min,strtm->tm_sec);
364
  return timeBuf;
365
}
366
/*---------------------------------------------------------------------------*/
367
/*!Set the serverPort variable
368
                                                                             */
369
/*---------------------------------------------------------------------------*/
370
 
371
void
372
set_rsp_server_port(int portNum)
373
{
374
  serverPort = portNum;
375
}
376
 
377
/*---------------------------------------------------------------------------*/
378
/*!Initialize the Remote Serial Protocol connection
379
 
380
   Set up the central data structures.                                       */
381
/*---------------------------------------------------------------------------*/
382
void
383
rsp_init (void)
384
{
385
  /* Clear out the central data structure */
386
  rsp.client_waiting =  0;               /* GDB client is not waiting for us */
387
  rsp.client_fd      = -1;              /* i.e. invalid */
388
  rsp.sigval         = 0;                /* No exception */
389
  rsp.start_addr     = EXCEPT_RESET;    /* Default restart point */
390
 
391
  /* Set up the matchpoint hash table */
392
  mp_hash_init ();
393
 
394
  /* RSP always starts stalled as though we have just reset the processor. */
395
  rsp_exception (EXCEPT_TRAP);
396
 
397
  /* Setup the NPC caching variables */
398
  stallState = STALLED;
399
  // Force a caching of the NPC
400
  npcIsCached = 0;
401
  get_npc();
402
 
403
}       /* rsp_init () */
404
 
405
/*---------------------------------------------------------------------------*/
406
/*!Look for action on RSP
407
 
408
   This function is called when the processor has stalled, which, except for
409
   initialization, must be due to an interrupt.
410
 
411
   If we have no RSP client, we get one. We can make no progress until the
412
   client is available.
413
 
414
   Then if the cause is an exception following a step or continue command, and
415
   the exception not been notified to GDB, a packet reporting the cause of the
416
   exception is sent.
417
 
418
   The next client request is then processed.                                */
419
/*---------------------------------------------------------------------------*/
420
void
421
handle_rsp (void)
422
{
423
  uint32_t              temp_uint32;
424
 
425
  rsp_init();
426
 
427
  while (1){
428
    /* If we have no RSP client, wait until we get one. */
429
    while (-1 == rsp.client_fd)
430
      {
431
        rsp_get_client ();
432
        rsp.client_waiting = 0;          /* No longer waiting */
433
      }
434
 
435
    /* If we have an unacknowledged exception tell the GDB client. If this
436
       exception was a trap due to a memory breakpoint, then adjust the NPC. */
437
    if (rsp.client_waiting)
438
      {
439
 
440
        // Check for exception
441
        rsp_check_for_exception();
442
 
443
        if(stallState == STALLED)
444
          // Get the PPC if we're stalled
445
          gdb_read_reg(PPC_CPU_REG_ADD, &temp_uint32);
446
 
447
 
448
        if ((TARGET_SIGNAL_TRAP == rsp.sigval) && (NULL != mp_hash_lookup (BP_MEMORY, temp_uint32)))
449
          {
450
          if (stallState != STALLED)
451
            // This is a quick fix for a strange situation seen in some of the simulators where
452
            // the sw bp would be detected, but the stalled state variable wasn't updated correctly
453
            // indicating that last time it checked, it wasn't set but the processor has now hit the
454
            // breakpoint. So run rsp_check_for_exception() to bring everything up to date.
455
            rsp_check_for_exception();
456
 
457
          if(DEBUG_GDB) printf("Software breakpoint hit at 0x%08x. Rolling back NPC to this instruction\n", temp_uint32);
458
 
459
          set_npc (temp_uint32);
460
 
461
          rsp_report_exception();
462
          rsp.client_waiting = 0;                /* No longer waiting */
463
        }
464
        else if(stallState == STALLED) {
465
          // If we're here, the thing has stalled, but not because of a breakpoint we set
466
          // report back the exception
467
 
468
          rsp_report_exception();
469
          rsp.client_waiting = 0;                /* No longer waiting */
470
 
471
        }
472
      }
473
 
474
    // See if there's any incoming data from the client by peeking at the socket
475
    if (rsp_peek() > 0)
476
      {
477
        if (rsp_peek() == 0x03 && (stallState != STALLED)) // ETX, end of text control char
478
          {
479
            // Got an interrupt command from GDB, this function should
480
            // pull the packet off the socket and stall the processor.
481
            // and then send a stop reply packet with signal TARGET_SIGNAL_NONE
482
            rsp_interrupt();
483
            rsp.client_waiting = 0;
484
          }
485
        else if (rsp.client_waiting == 0)
486
          {
487
            // Default handling of data from the client:
488
            /* Get a RSP client request */
489
            rsp_client_request ();
490
          }
491
      } /* end if (rsp_peek() > 0) */
492
 
493
  }
494
 
495
}   /* handle_rsp () */
496
 
497
 
498
/*
499
  Check if processor is stalled - if it is, read the DRR
500
  and return the target signal code
501
*/
502
static void rsp_check_for_exception()
503
{
504
 
505
  unsigned char stalled;
506
  uint32_t drr;
507
  err = dbg_cpu0_read_ctrl(0, &stalled);                          /* check if we're stalled */
508
 
509
  if (!(stalled & 0x01))
510
    {
511
      // Processor not stalled. Just return;
512
      return;
513
    }
514
 
515
  if (DEBUG_GDB) printf("rsp_check_for_exception() detected processor was stalled\nChecking DRR\n");
516
 
517
  // We're stalled
518
  stallState = STALLED;
519
  npcIsCached = 0;
520
 
521
  gdb_set_chain(SC_RISC_DEBUG);
522
 
523
  // Now read the DRR (Debug Reason Register)
524
  gdb_read_reg(DRR_CPU_REG_ADD, &drr);
525
 
526
  if (DEBUG_GDB) printf("DRR: 0x%08x\n", drr);
527
 
528
  switch ((int)(drr&0xffffffff))
529
    {
530
    case SPR_DRR_RSTE:  rsp.sigval = TARGET_SIGNAL_PWR;  break;
531
    case SPR_DRR_BUSEE: rsp.sigval = TARGET_SIGNAL_BUS;  break;
532
    case SPR_DRR_DPFE:  rsp.sigval = TARGET_SIGNAL_SEGV; break;
533
    case SPR_DRR_IPFE:  rsp.sigval = TARGET_SIGNAL_SEGV; break;
534
    case SPR_DRR_TTE:   rsp.sigval = TARGET_SIGNAL_ALRM; break;
535
    case SPR_DRR_AE:    rsp.sigval = TARGET_SIGNAL_BUS;  break;
536
    case SPR_DRR_IIE:   rsp.sigval = TARGET_SIGNAL_ILL;  break;
537
    case SPR_DRR_IE:    rsp.sigval = TARGET_SIGNAL_INT;  break;
538
    case SPR_DRR_DME:   rsp.sigval = TARGET_SIGNAL_SEGV; break;
539
    case SPR_DRR_IME:   rsp.sigval = TARGET_SIGNAL_SEGV; break;
540
    case SPR_DRR_RE:    rsp.sigval = TARGET_SIGNAL_FPE;  break;
541
    case SPR_DRR_SCE:   rsp.sigval = TARGET_SIGNAL_USR2; break;
542
    case SPR_DRR_FPE:   rsp.sigval = TARGET_SIGNAL_FPE;  break;
543
    case SPR_DRR_TE:    rsp.sigval = TARGET_SIGNAL_TRAP; break;
544
 
545
    default:
546
      // This must be the case of single step (which does not set DRR)
547
      rsp.sigval = TARGET_SIGNAL_TRAP; break;
548
    }
549
 
550
  if (DEBUG_GDB) printf("rsp.sigval: 0x%x\n", rsp.sigval);
551
 
552
  return;
553
}
554
 
555
/*---------------------------------------------------------------------------*/
556
/*!Check if PPC is in an exception vector that halts program flow
557
 
558
Compare the provided PPC with known exception vectors that are fatal
559
to a program's execution. Call rsp_exception(ppc) to set the appropriate
560
sigval and return.
561
 
562
@param[in] ppc  Value of current PPC, as read from debug unit
563
@return: 1 if we set a sigval and should return control to GDB, else 0       */
564
/*---------------------------------------------------------------------------*/
565
static int
566
check_for_exception_vector(uint32_t ppc)
567
{
568
  switch(ppc)
569
    {
570
      // The following should return sigvals to GDB for processing
571
    case EXCEPT_BUSERR:
572
    case EXCEPT_ALIGN:
573
    case EXCEPT_ILLEGAL:
574
    case EXCEPT_TRAP:     if(DEBUG_GDB)
575
                            printf("PPC at exception address\n");
576
                          rsp_exception(ppc);
577
                          return 1;
578
 
579
    default:
580
      return 0;
581
    }
582
  return 1;
583
}
584
 
585
/*---------------------------------------------------------------------------*/
586
/*!Note an exception for future processing
587
 
588
   The simulator has encountered an exception. Record it here, so that a
589
   future call to handle_exception will report it back to the client. The
590
   signal is supplied in Or1ksim form and recorded in GDB form.
591
 
592
   We flag up a warning if an exception is already pending, and ignore the
593
   earlier exception.
594
 
595
   @param[in] except  The exception (Or1ksim form)                           */
596
/*---------------------------------------------------------------------------*/
597
void
598
rsp_exception (uint32_t  except)
599
{
600
  int  sigval;                  /* GDB signal equivalent to exception */
601
 
602
  switch (except)
603
    {
604
    case EXCEPT_RESET:    sigval = TARGET_SIGNAL_PWR;  break;
605
    case EXCEPT_BUSERR:   sigval = TARGET_SIGNAL_BUS;  break;
606
    case EXCEPT_DPF:      sigval = TARGET_SIGNAL_SEGV; break;
607
    case EXCEPT_IPF:      sigval = TARGET_SIGNAL_SEGV; break;
608
    case EXCEPT_TICK:     sigval = TARGET_SIGNAL_ALRM; break;
609
    case EXCEPT_ALIGN:    sigval = TARGET_SIGNAL_BUS;  break;
610
    case EXCEPT_ILLEGAL:  sigval = TARGET_SIGNAL_ILL;  break;
611
    case EXCEPT_INT:      sigval = TARGET_SIGNAL_INT;  break;
612
    case EXCEPT_DTLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
613
    case EXCEPT_ITLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
614
    case EXCEPT_RANGE:    sigval = TARGET_SIGNAL_FPE;  break;
615
    case EXCEPT_SYSCALL:  sigval = TARGET_SIGNAL_USR2; break;
616
    case EXCEPT_FPE:      sigval = TARGET_SIGNAL_FPE;  break;
617
    case EXCEPT_TRAP:     sigval = TARGET_SIGNAL_TRAP; break;
618
 
619
    default:
620
      fprintf (stderr, "Warning: Unknown RSP exception %u: Ignored\n", except);
621
      return;
622
    }
623
 
624
  if ((0 != rsp.sigval) && (sigval != rsp.sigval))
625
    {
626
      fprintf (stderr, "Warning: RSP signal %d received while signal "
627
               "%d pending: Pending exception replaced\n", sigval, rsp.sigval);
628
    }
629
 
630
  rsp.sigval         = sigval;          /* Save the signal value */
631
 
632
}       /* rsp_exception () */
633
 
634
 
635
/*---------------------------------------------------------------------------*/
636
/*!Get a new client connection.
637
 
638
   Blocks until the client connection is available.
639
 
640
   A lot of this code is copied from remote_open in gdbserver remote-utils.c.
641
 
642
   This involves setting up a socket to listen on a socket for attempted
643
   connections from a single GDB instance (we couldn't be talking to multiple
644
   GDBs at once!).
645
 
646
   The service is specified either as a port number in the Or1ksim configuration
647
   (parameter rsp_port in section debug, default 51000) or as a service name
648
   in the constant OR1KSIM_RSP_SERVICE.
649
 
650
   The protocol used for communication is specified in OR1KSIM_RSP_PROTOCOL. */
651
/*---------------------------------------------------------------------------*/
652
static void
653
rsp_get_client (void)
654
{
655
  int                 tmp_fd;           /* Temporary descriptor for socket */
656
  int                 optval;           /* Socket options */
657
  struct sockaddr_in  sock_addr;        /* Socket address */
658
  socklen_t           len;              /* Size of the socket address */
659
 
660
  /* 0 is used as the RSP port number to indicate that we should use the
661
     service name instead. */
662
  if (0 == serverPort)
663
  {
664
    struct servent *service = getservbyname (OR1KSIM_RSP_SERVICE, "tcp");
665
    if (NULL == service)
666
      {
667
        fprintf (stderr, "Warning: RSP unable to find service \"%s\": %s \n",
668
                 OR1KSIM_RSP_SERVICE, strerror (errno));
669
        return;
670
      }
671
    serverPort = ntohs (service->s_port);
672
  }
673
 
674
  /* Open a socket on which we'll listen for clients */
675
  tmp_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
676
  if (tmp_fd < 0)
677
    {
678
      fprintf (stderr, "ERROR: Cannot open RSP socket\n");
679
      exit (0);
680
    }
681
 
682
  /* Allow rapid reuse of the port on this socket */
683
  optval = 1;
684
  setsockopt (tmp_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
685
              sizeof (optval));
686
 
687
  /* Bind the port to the socket */
688
  sock_addr.sin_family      = PF_INET;
689
  sock_addr.sin_port        = htons (serverPort);
690
  sock_addr.sin_addr.s_addr = INADDR_ANY;
691
  if (bind (tmp_fd, (struct sockaddr *) &sock_addr, sizeof (sock_addr)))
692
    {
693
      fprintf (stderr, "ERROR: Cannot bind to RSP socket\n");
694
      exit (0);
695
    }
696
 
697
  /* Listen for (at most one) client */
698
  if (0 != listen (tmp_fd, 1))
699
    {
700
      fprintf (stderr, "ERROR: Cannot listen on RSP socket\n");
701
      exit (0);
702
    }
703
 
704
  printf("Waiting for gdb connection on localhost:%d\n", serverPort);
705
  fflush (stdout);
706
 
707
  printf("Press CTRL+c and type 'finish' to exit.\n");
708
  fflush (stdout);
709
 
710
  /* Accept a client which connects */
711
  len = sizeof (sock_addr);
712
  rsp.client_fd = accept (tmp_fd, (struct sockaddr *)&sock_addr, &len);
713
 
714
  if (-1 == rsp.client_fd)
715
  {
716
    fprintf (stderr, "Warning: Failed to accept RSP client\n");
717
    return;
718
  }
719
 
720
  /* Enable TCP keep alive process */
721
  optval = 1;
722
  setsockopt (rsp.client_fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval,
723
              sizeof (optval));
724
 
725
  int flags;
726
 
727
  /* If they have O_NONBLOCK, use the Posix way to do it */
728
 
729
#if defined(O_NONBLOCK)
730
  /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
731
  if (-1 == (flags = fcntl(rsp.client_fd, F_GETFL, 0)))
732
    flags = 0;
733
 
734
  fcntl(rsp.client_fd, F_SETFL, flags | O_NONBLOCK);
735
#else
736
  /* Otherwise, use the old way of doing it */
737
  flags = 1;
738
  ioctl(fd, FIOBIO, &flags);
739
#endif
740
 
741
 
742
 
743
  /* Set socket to be non-blocking */
744
 
745
  /* We do this because when we're given a continue, or step
746
     instruction,command we set the processor stall off, then instnatly check
747
     if it's stopped. If it hasn't then we drop through and wait for input
748
     from GDB. Obviously this will cause problems when it will stop after we
749
     do the check. So now, rsp_peek() has been implemented to simply check if
750
     there's an incoming command from GDB (only interested in interrupt
751
     commands), otherwise it returns back to and poll the processor's PPC and
752
     stall bit. It can only do this if the socket is non-blocking.
753
 
754
     At first test, simply adding this line appeared to give no problems with
755
     the existing code. No "simulation" of blocking behaviour on the
756
     non-blocking socket was required (in the event that a read/write throws
757
     back a EWOULDBLOCK error, as was looked to be the case in the previous
758
     GDB handling code) -- Julius
759
  */
760
  if (ioctl(rsp.client_fd, FIONBIO, (char *)&optval) > 0 )
761
    {
762
      perror("ioctl() failed");
763
      close(rsp.client_fd);
764
      close(tmp_fd);
765
      exit(0);
766
    }
767
 
768
 
769
  /* Don't delay small packets, for better interactive response (disable
770
     Nagel's algorithm) */
771
  optval = 1;
772
  setsockopt (rsp.client_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
773
              sizeof (optval));
774
 
775
  /* Socket is no longer needed */
776
  close (tmp_fd);                       /* No longer need this */
777
  signal (SIGPIPE, SIG_IGN);            /* So we don't exit if client dies */
778
 
779
  printf ("Remote debugging from host %s\n", inet_ntoa (sock_addr.sin_addr));
780
}       /* rsp_get_client () */
781
 
782
 
783
/*---------------------------------------------------------------------------*/
784
/*!Deal with a request from the GDB client session
785
 
786
   In general, apart from the simplest requests, this function replies on
787
   other functions to implement the functionality.                           */
788
/*---------------------------------------------------------------------------*/
789
static void rsp_client_request (void)
790
{
791
  struct rsp_buf *p_buf = get_packet ();        /* Message sent to us */
792
 
793
  // Null packet means we hit EOF or the link was closed for some other
794
  // reason. Close the client and return
795
  if (NULL == p_buf)
796
    {
797
      rsp_client_close ();
798
      return;
799
    }
800
 
801
  if (DEBUG_GDB){
802
    printf("%s-----------------------------------------------------\n", printTime());
803
    printf ("Packet received %s: %d chars\n", p_buf->data, p_buf->len );
804
    fflush (stdout);
805
  }
806
 
807
  switch (p_buf->data[0])
808
    {
809
    case '!':
810
      /* Request for extended remote mode */
811
      put_str_packet ("OK"); // OK = supports and has enabled extended mode.
812
      return;
813
 
814
    case '?':
815
      /* Return last signal ID */
816
      rsp_report_exception();
817
      return;
818
 
819
    case 'A':
820
      /* Initialization of argv not supported */
821
      fprintf (stderr, "Warning: RSP 'A' packet not supported: ignored\n");
822
      put_str_packet ("E01");
823
      return;
824
 
825
    case 'b':
826
      /* Setting baud rate is deprecated */
827
      fprintf (stderr, "Warning: RSP 'b' packet is deprecated and not "
828
               "supported: ignored\n");
829
      return;
830
 
831
    case 'B':
832
      /* Breakpoints should be set using Z packets */
833
      fprintf (stderr, "Warning: RSP 'B' packet is deprecated (use 'Z'/'z' "
834
               "packets instead): ignored\n");
835
      return;
836
 
837
    case 'c':
838
      /* Continue */
839
      rsp_continue (p_buf);
840
      return;
841
 
842
    case 'C':
843
      /* Continue with signal */
844
      rsp_continue_with_signal (p_buf);
845
      return;
846
 
847
    case 'd':
848
      /* Disable debug using a general query */
849
      fprintf (stderr, "Warning: RSP 'd' packet is deprecated (define a 'Q' "
850
               "packet instead: ignored\n");
851
      return;
852
 
853
    case 'D':
854
      /* Detach GDB. Do this by closing the client. The rules say that
855
         execution should continue. TODO. Is this really then intended
856
         meaning? Or does it just mean that only vAttach will be recognized
857
         after this? */
858
      put_str_packet ("OK");
859
      // In VPI disconnect everyone and exit
860
      rsp_client_close();
861
      client_close('0');
862
      dbg_client_detached();   // Send message to sim that the client detached
863
      exit(0);
864
      //reset_or1k ();
865
      //set_stall_state (0);
866
      return;
867
 
868
    case 'F':
869
      /* File I/O is not currently supported */
870
      fprintf (stderr, "Warning: RSP file I/O not currently supported: 'F' "
871
               "packet ignored\n");
872
      return;
873
 
874
    case 'g':
875
      rsp_read_all_regs ();
876
      return;
877
 
878
    case 'G':
879
      rsp_write_all_regs (p_buf);
880
      return;
881
 
882
    case 'H':
883
      /* Set the thread number of subsequent operations. For now ignore
884
            silently and just reply "OK" */
885
      put_str_packet ("OK");
886
      return;
887
 
888
    case 'i':
889
      /* Single instruction step */
890
      fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
891
               "stopped immediately\n");
892
      rsp.client_waiting = 1;                   /* Stop reply will be sent */
893
      return;
894
 
895
    case 'I':
896
      /* Single instruction step with signal */
897
      fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
898
               "stopped immediately\n");
899
      rsp.client_waiting = 1;                   /* Stop reply will be sent */
900
      return;
901
 
902
    case 'k':
903
      /* Kill request. Do nothing for now. */
904
      return;
905
 
906
    case 'm':
907
      /* Read memory (symbolic) */
908
      rsp_read_mem (p_buf);
909
      return;
910
 
911
    case 'M':
912
      /* Write memory (symbolic) */
913
      rsp_write_mem (p_buf);
914
      return;
915
 
916
    case 'p':
917
      /* Read a register */
918
      rsp_read_reg (p_buf);
919
      return;
920
 
921
    case 'P':
922
      /* Write a register */
923
      rsp_write_reg (p_buf);
924
      return;
925
 
926
    case 'q':
927
      /* Any one of a number of query packets */
928
      rsp_query (p_buf);
929
      return;
930
 
931
    case 'Q':
932
      /* Any one of a number of set packets */
933
      rsp_set (p_buf);
934
      return;
935
 
936
    case 'r':
937
      /* Reset the system. Deprecated (use 'R' instead) */
938
      fprintf (stderr, "Warning: RSP 'r' packet is deprecated (use 'R' "
939
               "packet instead): ignored\n");
940
      return;
941
 
942
    case 'R':
943
      /* Restart the program being debugged. */
944
      rsp_restart ();
945
      return;
946
 
947
    case 's':
948
      /* Single step (one high level instruction). This could be hard without
949
                        DWARF2 info */
950
      rsp_step (p_buf);
951
      return;
952
 
953
    case 'S':
954
      /* Single step (one high level instruction) with signal. This could be
955
                        hard without DWARF2 info */
956
      rsp_step_with_signal (p_buf);
957
      return;
958
 
959
    case 't':
960
      /* Search. This is not well defined in the manual and for now we don't
961
                        support it. No response is defined. */
962
      fprintf (stderr, "Warning: RSP 't' packet not supported: ignored\n");
963
      return;
964
 
965
    case 'T':
966
      /* Is the thread alive. We are bare metal, so don't have a thread
967
                        context. The answer is always "OK". */
968
      put_str_packet ("OK");
969
      return;
970
 
971
    case 'v':
972
      /* Any one of a number of packets to control execution */
973
      rsp_vpkt (p_buf);
974
      return;
975
 
976
    case 'X':
977
      /* Write memory (binary) */
978
      rsp_write_mem_bin (p_buf);
979
      return;
980
 
981
    case 'z':
982
      /* Remove a breakpoint/watchpoint. */
983
      rsp_remove_matchpoint (p_buf);
984
      return;
985
 
986
    case 'Z':
987
      /* Insert a breakpoint/watchpoint. */
988
      rsp_insert_matchpoint (p_buf);
989
      return;
990
 
991
    default:
992
      /* Unknown commands are ignored */
993
      fprintf (stderr, "Warning: Unknown RSP request %s\n", p_buf->data);
994
      return;
995
    }
996
}       /* rsp_client_request () */
997
 
998
 
999
/*---------------------------------------------------------------------------*/
1000
/*!Close the connection to the client if it is open                          */
1001
/*---------------------------------------------------------------------------*/
1002
static void
1003
rsp_client_close (void)
1004
{
1005
  if (-1 != rsp.client_fd)
1006
    {
1007
      close (rsp.client_fd);
1008
      rsp.client_fd = -1;
1009
    }
1010
}       /* rsp_client_close () */
1011
 
1012
 
1013
/*---------------------------------------------------------------------------*/
1014
/*!Send a packet to the GDB client
1015
 
1016
   Modeled on the stub version supplied with GDB. Put out the data preceded by
1017
   a '$', followed by a '#' and a one byte checksum. '$', '#', '*' and '}' are
1018
   escaped by preceding them with '}' and then XORing the character with
1019
   0x20.
1020
 
1021
   @param[in] p_buf  The data to send                                          */
1022
/*---------------------------------------------------------------------------*/
1023
static void
1024
put_packet (struct rsp_buf *p_buf)
1025
{
1026
  unsigned char  data[GDB_BUF_MAX * 2];
1027
  int   len;
1028
  int   ch;                             /* Ack char */
1029
 
1030
  /* Construct $<packet info>#<checksum>. Repeat until the GDB client
1031
     acknowledges satisfactory receipt. */
1032
  do
1033
  {
1034
    unsigned char checksum = 0;  /* Computed checksum */
1035
    int           count    = 0;  /* Index into the buffer */
1036
 
1037
    if (DEBUG_GDB_DUMP_DATA){
1038
      printf ("Putting %s\n\n", p_buf->data);
1039
      fflush (stdout);
1040
    }
1041
 
1042
    len = 0;
1043
    data[len++] =  '$';                 /* Start char */
1044
 
1045
    /* Body of the packet */
1046
    for (count = 0; count < p_buf->len; count++)
1047
                {
1048
                  unsigned char  ch = p_buf->data[count];
1049
 
1050
                  /* Check for escaped chars */
1051
                  if (('$' == ch) || ('#' == ch) || ('*' == ch) || ('}' == ch))
1052
                    {
1053
                      ch       ^= 0x20;
1054
                      checksum += (unsigned char)'}';
1055
                                        data[len++] =  '}';
1056
                    }
1057
 
1058
                  checksum += ch;
1059
                        data[len++] =  ch;
1060
                }
1061
 
1062
                data[len++] =  '#';                     /* End char */
1063
 
1064
    /* Computed checksum */
1065
    data[len++] =       (hexchars[checksum >> 4]);
1066
                data[len++] =   (hexchars[checksum % 16]);
1067
 
1068
                send_rsp_str ((unsigned char *) &data, len);
1069
 
1070
    /* Check for ack of connection failure */
1071
    ch = get_rsp_char ();
1072
    if (0 > ch)
1073
                {
1074
                  return;                       /* Fail the put silently. */
1075
                }
1076
  }
1077
  while ('+' != ch);
1078
 
1079
}       /* put_packet () */
1080
 
1081
 
1082
/*---------------------------------------------------------------------------*/
1083
/*!Convenience to put a constant string packet
1084
 
1085
   param[in] str  The text of the packet                                     */
1086
/*---------------------------------------------------------------------------*/
1087
static void
1088
put_str_packet (const char *str)
1089
{
1090
  struct rsp_buf  buffer;
1091
  int    len = strlen (str);
1092
 
1093
  /* Construct the packet to send, so long as string is not too big,
1094
     otherwise truncate. Add EOS at the end for convenient debug printout */
1095
 
1096
  if (len >= GDB_BUF_MAX)
1097
    {
1098
      fprintf (stderr, "Warning: String %s too large for RSP packet: "
1099
               "truncated\n", str);
1100
      len = GDB_BUF_MAX - 1;
1101
    }
1102
 
1103
  strncpy (buffer.data, str, len);
1104
  buffer.data[len] = 0;
1105
  buffer.len       = len;
1106
 
1107
  put_packet (&buffer);
1108
 
1109
}       /* put_str_packet () */
1110
 
1111
 
1112
/*---------------------------------------------------------------------------*/
1113
/*!Get a packet from the GDB client
1114
 
1115
   Modeled on the stub version supplied with GDB. The data is in a static
1116
   buffer. The data should be copied elsewhere if it is to be preserved across
1117
   a subsequent call to get_packet().
1118
 
1119
   Unlike the reference implementation, we don't deal with sequence
1120
   numbers. GDB has never used them, and this implementation is only intended
1121
   for use with GDB 6.8 or later. Sequence numbers were removed from the RSP
1122
   standard at GDB 5.0.
1123
 
1124
   @return  A pointer to the static buffer containing the data                */
1125
/*---------------------------------------------------------------------------*/
1126
static struct rsp_buf *
1127
get_packet (void)
1128
{
1129
  static struct rsp_buf  buf;           /* Survives the return */
1130
 
1131
  /* Keep getting packets, until one is found with a valid checksum */
1132
  while (1)
1133
        {
1134
                unsigned char checksum;         /* The checksum we have computed */
1135
                int           count;                    /* Index into the buffer */
1136
                int                     ch;                                     /* Current character */
1137
 
1138
    /* Wait around for the start character ('$'). Ignore all other
1139
          characters */
1140
    ch = get_rsp_char ();
1141
 
1142
    while (ch != '$')
1143
                {
1144
                  if (-1 == ch)
1145
                    {
1146
                      return  NULL;             /* Connection failed */
1147
                    }
1148
 
1149
                  ch = get_rsp_char ();
1150
 
1151
                  // Potentially handle an interrupt character (0x03) here                
1152
                }
1153
 
1154
    /* Read until a '#' or end of buffer is found */
1155
    checksum =  0;
1156
    count    =  0;
1157
    while (count < GDB_BUF_MAX - 1)
1158
                {
1159
                  ch = get_rsp_char ();
1160
 
1161
                  if(rsp.client_waiting && DEBUG_GDB)
1162
                    {
1163
                      printf("%x\n",ch);
1164
                    }
1165
 
1166
 
1167
                  /* Check for connection failure */
1168
                  if (0 > ch)
1169
                    {
1170
                      return  NULL;
1171
                    }
1172
 
1173
                  /* If we hit a start of line char begin all over again */
1174
                  if ('$' == ch)
1175
                    {
1176
                      checksum =  0;
1177
                      count    =  0;
1178
 
1179
                      continue;
1180
                    }
1181
 
1182
                  /* Break out if we get the end of line char */
1183
                  if ('#' == ch)
1184
                    {
1185
                      break;
1186
                    }
1187
 
1188
                  /* Update the checksum and add the char to the buffer */
1189
 
1190
                  checksum        = checksum + (unsigned char)ch;
1191
                  buf.data[count] = (char)ch;
1192
                  count           = count + 1;
1193
                }
1194
 
1195
    /* Mark the end of the buffer with EOS - it's convenient for non-binary
1196
          data to be valid strings. */
1197
    buf.data[count] = 0;
1198
    buf.len         = count;
1199
 
1200
    /* If we have a valid end of packet char, validate the checksum */
1201
    if ('#' == ch)
1202
                {
1203
                  unsigned char  xmitcsum;      /* The checksum in the packet */
1204
 
1205
                  ch = get_rsp_char ();
1206
                  if (0 > ch)
1207
                    {
1208
                      return  NULL;             /* Connection failed */
1209
                    }
1210
                  xmitcsum = hex (ch) << 4;
1211
 
1212
                  ch = get_rsp_char ();
1213
                  if (0 > ch)
1214
                    {
1215
                      return  NULL;             /* Connection failed */
1216
                    }
1217
 
1218
                  xmitcsum += hex (ch);
1219
 
1220
                  /* If the checksums don't match print a warning, and put the
1221
                     negative ack back to the client. Otherwise put a positive ack. */
1222
                  if (checksum != xmitcsum)
1223
                    {
1224
                      fprintf (stderr, "Warning: Bad RSP checksum: Computed "
1225
                               "0x%02x, received 0x%02x\n", checksum, xmitcsum);
1226
 
1227
                                        ch = '-';
1228
                      send_rsp_str ((unsigned char *) &ch, 1);  /* Failed checksum */
1229
                    }
1230
                  else
1231
                    {
1232
                                        ch = '+';
1233
                      send_rsp_str ((unsigned char *) &ch, 1);  /* successful transfer */
1234
                      break;
1235
                    }
1236
                }
1237
    else
1238
                {
1239
                  fprintf (stderr, "Warning: RSP packet overran buffer\n");
1240
                }
1241
  }
1242
  return &buf;                          /* Success */
1243
}       /* get_packet () */
1244
 
1245
 
1246
/*---------------------------------------------------------------------------*/
1247
/*!Put a single character out onto the client socket
1248
 
1249
   This should only be called if the client is open, but we check for safety.
1250
 
1251
   @param[in] c  The character to put out                                    */
1252
/*---------------------------------------------------------------------------*/
1253
static void
1254
send_rsp_str (unsigned char *data, int len)
1255
{
1256
  if (-1 == rsp.client_fd)
1257
    {
1258
      fprintf (stderr, "Warning: Attempt to write '%s' to unopened RSP "
1259
               "client: Ignored\n", data);
1260
      return;
1261
    }
1262
 
1263
  /* Write until successful (we retry after interrupts) or catastrophic
1264
     failure. */
1265
  while (1)
1266
    {
1267
      switch (write (rsp.client_fd, data, len))
1268
                        {
1269
                        case -1:
1270
                          /* Error: only allow interrupts or would block */
1271
                          if ((EAGAIN != errno) && (EINTR != errno))
1272
                            {
1273
                              fprintf (stderr, "Warning: Failed to write to RSP client: "
1274
                                       "Closing client connection: %s\n",
1275
                                       strerror (errno));
1276
                              rsp_client_close ();
1277
                              return;
1278
                            }
1279
 
1280
                          break;
1281
 
1282
                        case 0:
1283
                          break;                /* Nothing written! Try again */
1284
 
1285
                        default:
1286
                          return;               /* Success, we can return */
1287
                        }
1288
    }
1289
}       /* send_rsp_str () */
1290
 
1291
 
1292
/*---------------------------------------------------------------------------*/
1293
/*!Get a single character from the client socket
1294
 
1295
   This should only be called if the client is open, but we check for safety.
1296
 
1297
   @return  The character read, or -1 on failure                             */
1298
/*---------------------------------------------------------------------------*/
1299
static int
1300
get_rsp_char ()
1301
{
1302
  if (-1 == rsp.client_fd)
1303
    {
1304
      fprintf (stderr, "Warning: Attempt to read from unopened RSP "
1305
               "client: Ignored\n");
1306
      return  -1;
1307
    }
1308
 
1309
  /* Non-blocking read until successful (we retry after interrupts) or
1310
     catastrophic failure. */
1311
  while (1)
1312
    {
1313
      unsigned char  c;
1314
 
1315
      switch (read (rsp.client_fd, &c, sizeof (c)))
1316
        {
1317
        case -1:
1318
          /* Error: only allow interrupts */
1319
          if ((EAGAIN != errno) && (EINTR != errno))
1320
            {
1321
              fprintf (stderr, "Warning: Failed to read from RSP client: "
1322
                       "Closing client connection: %s\n",
1323
                       strerror (errno));
1324
              rsp_client_close ();
1325
              return  -1;
1326
            }
1327
 
1328
          break;
1329
 
1330
        case 0:
1331
          // EOF
1332
          rsp_client_close ();
1333
          return  -1;
1334
 
1335
        default:
1336
          return  c & 0xff; /* Success, we can return (no sign extend!) */
1337
        }
1338
    }
1339
}       /* get_rsp_char () */
1340
 
1341
/*---------------------------------------------------------------------------*/
1342
/* !Peek at data coming into server from GDB
1343
 
1344
   Useful for polling for ETX (0x3) chars being sent when GDB wants to
1345
   interrupt
1346
 
1347
   @return the char we peeked, 0 otherwise                                   */
1348
/*---------------------------------------------------------------------------*/
1349
static char
1350
rsp_peek()
1351
{
1352
  /*
1353
  if (-1 == rsp.client_fd)
1354
    {
1355
      fprintf (stderr, "Warning: Attempt to read from unopened RSP "
1356
               "client: Ignored\n");
1357
      return  -1;
1358
    }
1359
  */
1360
  char  c;
1361
  int n;
1362
  // Using recv here instead of read becuase we can pass the MSG_PEEK
1363
  // flag, which lets us look at what's on the socket, without actually
1364
  // taking it off
1365
 
1366
  //if (DEBUG_GDB) 
1367
  //  printf("peeking at GDB socket...\n");
1368
 
1369
  n = recv (rsp.client_fd, &c, sizeof (c), MSG_PEEK);
1370
 
1371
  //if (DEBUG_GDB) 
1372
  //  printf("peeked, got n=%d, c=0x%x\n",n, c);
1373
 
1374
  if (n > 0)
1375
    return c;
1376
  else
1377
    return '\0';
1378
 
1379
}
1380
 
1381
/*---------------------------------------------------------------------------*/
1382
/*!Handle an interrupt from GDB
1383
 
1384
 Detect an interrupt from GDB and stall the processor                        */
1385
/*---------------------------------------------------------------------------*/
1386
static void
1387
rsp_interrupt()
1388
{
1389
  unsigned char  c;
1390
 
1391
  if (read (rsp.client_fd, &c, sizeof (c)) <= 0)
1392
    {
1393
      // Had issues, just return
1394
      return;
1395
    }
1396
 
1397
  // Ensure this is a ETX control char (0x3), currently, we only call this
1398
  // function when we've peeked and seen it, otherwise, ignore, return and pray
1399
  // things go back to normal...
1400
  if (c != 0x03)
1401
    {
1402
      printf("Warning: Interrupt character expected but not found on socket.\n");
1403
      return;
1404
    }
1405
 
1406
  // Otherwise, it's an interrupt packet, stall the processor, and upon return
1407
  // to the main handle_rsp() loop, it will inform GDB.
1408
 
1409
  if (DEBUG_GDB) printf("Interrupt received from GDB. Stalling processor.\n");
1410
 
1411
  set_stall_state (1);
1412
 
1413
  // Send a stop reply response, manually set rsp.sigval to TARGET_SIGNAL_NONE
1414
  rsp.sigval = TARGET_SIGNAL_NONE;
1415
  rsp_report_exception();
1416
  rsp.client_waiting = 0;                /* No longer waiting */
1417
 
1418
  return;
1419
 
1420
}
1421
 
1422
 
1423
/*---------------------------------------------------------------------------*/
1424
/*!"Unescape" RSP binary data
1425
 
1426
   '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
1427
 
1428
   This function reverses that, modifying the data in place.
1429
 
1430
   @param[in] data  The array of bytes to convert
1431
   @para[in]  len   The number of bytes to be converted
1432
 
1433
   @return  The number of bytes AFTER conversion                             */
1434
/*---------------------------------------------------------------------------*/
1435
static int
1436
rsp_unescape (char *data,
1437
              int   len)
1438
{
1439
  int  from_off = 0;             /* Offset to source char */
1440
  int  to_off   = 0;             /* Offset to dest char */
1441
 
1442
  while (from_off < len)
1443
    {
1444
      /* Is it escaped */
1445
      if ( '}' == data[from_off])
1446
                        {
1447
                          from_off++;
1448
                          data[to_off] = data[from_off] ^ 0x20;
1449
                        }
1450
                  else
1451
                        {
1452
                          data[to_off] = data[from_off];
1453
                        }
1454
 
1455
      from_off++;
1456
      to_off++;
1457
    }
1458
 
1459
  return  to_off;
1460
 
1461
}       /* rsp_unescape () */
1462
 
1463
 
1464
/*---------------------------------------------------------------------------*/
1465
/*!Initialize the matchpoint hash table
1466
 
1467
   This is an open hash table, so this function clears all the links to
1468
   NULL.                                                                     */
1469
/*---------------------------------------------------------------------------*/
1470
static void
1471
mp_hash_init (void)
1472
{
1473
  int  i;
1474
 
1475
  for (i = 0; i < MP_HASH_SIZE; i++)
1476
    {
1477
      rsp.mp_hash[i] = NULL;
1478
    }
1479
}       /* mp_hash_init () */
1480
 
1481
 
1482
/*---------------------------------------------------------------------------*/
1483
/*!Add an entry to the matchpoint hash table
1484
 
1485
   Add the entry if it wasn't already there. If it was there do nothing. The
1486
   match just be on type and addr. The instr need not match, since if this is
1487
   a duplicate insertion (perhaps due to a lost packet) they will be
1488
   different.
1489
 
1490
   @param[in] type   The type of matchpoint
1491
   @param[in] addr   The address of the matchpoint
1492
   @para[in]  instr  The instruction to associate with the address           */
1493
/*---------------------------------------------------------------------------*/
1494
static void
1495
mp_hash_add (enum mp_type type,
1496
             uint32_t  addr,
1497
             uint32_t  instr)
1498
{
1499
  int              hv    = addr % MP_HASH_SIZE;
1500
  struct mp_entry *curr;
1501
 
1502
  /* See if we already have the entry */
1503
  for(curr = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1504
  {
1505
    if ((type == curr->type) && (addr == curr->addr))
1506
                {
1507
                  return;               /* We already have the entry */
1508
                }
1509
  }
1510
 
1511
  /* Insert the new entry at the head of the chain */
1512
  curr = (struct mp_entry*) malloc (sizeof (*curr));
1513
 
1514
  curr->type  = type;
1515
  curr->addr  = addr;
1516
  curr->instr = instr;
1517
  curr->next  = rsp.mp_hash[hv];
1518
 
1519
  rsp.mp_hash[hv] = curr;
1520
 
1521
}       /* mp_hash_add () */
1522
 
1523
 
1524
/*---------------------------------------------------------------------------*/
1525
/*!Look up an entry in the matchpoint hash table
1526
 
1527
   The match must be on type AND addr.
1528
 
1529
   @param[in] type   The type of matchpoint
1530
   @param[in] addr   The address of the matchpoint
1531
 
1532
   @return  The entry deleted, or NULL if the entry was not found            */
1533
/*---------------------------------------------------------------------------*/
1534
static struct mp_entry * mp_hash_lookup (enum mp_type type,     uint32_t addr)
1535
{
1536
  int    hv = addr % MP_HASH_SIZE;
1537
  struct mp_entry *curr;
1538
 
1539
  /* Search */
1540
  for (curr = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1541
  {
1542
    if ((type == curr->type) && (addr == curr->addr))
1543
                {
1544
                  return  curr;         /* The entry found */
1545
                }
1546
  }
1547
 
1548
  /* Not found */
1549
  return  NULL;
1550
 
1551
}       /* mp_hash_lookup () */
1552
 
1553
 
1554
/*---------------------------------------------------------------------------*/
1555
/*!Delete an entry from the matchpoint hash table
1556
 
1557
   If it is there the entry is deleted from the hash table. If it is not
1558
   there, no action is taken. The match must be on type AND addr.
1559
 
1560
   The usual fun and games tracking the previous entry, so we can delete
1561
   things.
1562
 
1563
   @note  The deletion DOES NOT free the memory associated with the entry,
1564
          since that is returned. The caller should free the memory when they
1565
          have used the information.
1566
 
1567
   @param[in] type   The type of matchpoint
1568
   @param[in] addr   The address of the matchpoint
1569
 
1570
   @return  The entry deleted, or NULL if the entry was not found            */
1571
/*---------------------------------------------------------------------------*/
1572
static struct mp_entry *
1573
mp_hash_delete (enum mp_type       type,
1574
                uint32_t  addr)
1575
{
1576
  int              hv   = addr % MP_HASH_SIZE;
1577
  struct mp_entry *prev = NULL;
1578
  struct mp_entry *curr;
1579
 
1580
  /* Search */
1581
  for (curr  = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1582
    {
1583
      if ((type == curr->type) && (addr == curr->addr))
1584
        {
1585
          /* Found - delete. Method depends on whether we are the head of
1586
             chain. */
1587
          if (NULL == prev)
1588
            {
1589
              rsp.mp_hash[hv] = curr->next;
1590
            }
1591
          else
1592
            {
1593
              prev->next = curr->next;
1594
            }
1595
 
1596
          return  curr;         /* The entry deleted */
1597
        }
1598
 
1599
      prev = curr;
1600
    }
1601
 
1602
  /* Not found */
1603
  return  NULL;
1604
 
1605
}       /* mp_hash_delete () */
1606
 
1607
 
1608
/*---------------------------------------------------------------------------*/
1609
/*!Utility to give the value of a hex char
1610
 
1611
   @param[in] ch  A character representing a hexadecimal digit. Done as -1,
1612
                  for consistency with other character routines, which can use
1613
                  -1 as EOF.
1614
 
1615
   @return  The value of the hex character, or -1 if the character is
1616
            invalid.                                                         */
1617
/*---------------------------------------------------------------------------*/
1618
static int hex (int  c)
1619
{
1620
  return  ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
1621
          ((c >= '0') && (c <= '9')) ? c - '0' :
1622
          ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
1623
 
1624
}       /* hex () */
1625
 
1626
 
1627
/*---------------------------------------------------------------------------*/
1628
/*!Convert a register to a hex digit string
1629
 
1630
   The supplied 32-bit value is converted to an 8 digit hex string according
1631
   the target endianism. It is null terminated for convenient printing.
1632
 
1633
   @param[in]  val  The value to convert
1634
   @param[out] p_buf  The buffer for the text string                           */
1635
/*---------------------------------------------------------------------------*/
1636
static void
1637
reg2hex (uint32_t  val, char *p_buf)
1638
{
1639
  int  n;                       /* Counter for digits */
1640
        int  nyb_shift;
1641
 
1642
  for (n = 0; n < 8; n++)
1643
    {
1644
#ifdef WORDSBIGENDIAN
1645
      if(n%2==0){
1646
        nyb_shift = n * 4 + 4;
1647
                        }
1648
                        else{
1649
                                nyb_shift = n * 4 - 4;
1650
                        }
1651
#else
1652
      nyb_shift = 28 - (n * 4);
1653
#endif
1654
      p_buf[n] = hexchars[(val >> nyb_shift) & 0xf];
1655
    }
1656
 
1657
  p_buf[8] = 0;                  /* Useful to terminate as string */
1658
 
1659
}       /* reg2hex () */
1660
 
1661
 
1662
/*---------------------------------------------------------------------------*/
1663
/*!Convert a hex digit string to a register value
1664
 
1665
   The supplied 8 digit hex string is converted to a 32-bit value according
1666
   the target endianism
1667
 
1668
   @param[in] p_buf  The buffer with the hex string
1669
 
1670
   @return  The value to convert                                             */
1671
/*---------------------------------------------------------------------------*/
1672
static uint32_t
1673
hex2reg (char *p_buf)
1674
{
1675
  int                n;         /* Counter for digits */
1676
  uint32_t  val = 0;     /* The result */
1677
 
1678
  for (n = 0; n < 8; n++)
1679
    {
1680
#ifdef WORDSBIGENDIAN
1681
      int  nyb_shift = n * 4;
1682
#else
1683
      int  nyb_shift = 28 - (n * 4);
1684
#endif
1685
      val |= hex (p_buf[n]) << nyb_shift;
1686
    }
1687
 
1688
  return val;
1689
 
1690
}       /* hex2reg () */
1691
 
1692
 
1693
/*---------------------------------------------------------------------------*/
1694
/*!Convert an ASCII character string to pairs of hex digits
1695
 
1696
   Both source and destination are null terminated.
1697
 
1698
   @param[out] dest  Buffer to store the hex digit pairs (null terminated)
1699
   @param[in]  src   The ASCII string (null terminated)                      */
1700
/*---------------------------------------------------------------------------*/
1701
static void  ascii2hex (char *dest,
1702
                        char *src)
1703
{
1704
  int  i;
1705
 
1706
  /* Step through converting the source string */
1707
  for (i = 0; src[i] != '\0'; i++)
1708
    {
1709
      char  ch = src[i];
1710
 
1711
      dest[i * 2]     = hexchars[ch >> 4 & 0xf];
1712
      dest[i * 2 + 1] = hexchars[ch      & 0xf];
1713
    }
1714
 
1715
  dest[i * 2] = '\0';
1716
 
1717
}       /* ascii2hex () */
1718
 
1719
 
1720
/*---------------------------------------------------------------------------*/
1721
/*!Convert pairs of hex digits to an ASCII character string
1722
 
1723
   Both source and destination are null terminated.
1724
 
1725
   @param[out] dest  The ASCII string (null terminated)
1726
   @param[in]  src   Buffer holding the hex digit pairs (null terminated)    */
1727
/*---------------------------------------------------------------------------*/
1728
static void  hex2ascii (char *dest,
1729
                        char *src)
1730
{
1731
  int  i;
1732
 
1733
  /* Step through convering the source hex digit pairs */
1734
  for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++)
1735
    {
1736
      dest[i] = ((hex (src[i * 2]) & 0xf) << 4) | (hex (src[i * 2 + 1]) & 0xf);
1737
    }
1738
 
1739
  dest[i] = '\0';
1740
 
1741
}       /* hex2ascii () */
1742
 
1743
 
1744
/*---------------------------------------------------------------------------*/
1745
/*!Set the program counter
1746
 
1747
   This sets the value in the NPC SPR. Not completely trivial, since this is
1748
   actually cached in cpu_state.pc. Any reset of the NPC also involves
1749
   clearing the delay state and setting the pcnext global.
1750
 
1751
   Only actually do this if the requested address is different to the current
1752
   NPC (avoids clearing the delay pipe).
1753
 
1754
   @param[in] addr  The address to use                                       */
1755
/*---------------------------------------------------------------------------*/
1756
static void
1757
set_npc (uint32_t  addr)
1758
{
1759
 
1760
  // First set the chain 
1761
  gdb_set_chain(SC_RISC_DEBUG); /* 1 RISC Debug Interface chain */
1762
 
1763
 
1764
  if (addr != get_npc())
1765
  {
1766
 
1767
    gdb_write_reg(NPC_CPU_REG_ADD, addr);
1768
 
1769
    if (STALLED == stallState)
1770
      {
1771
        if (DEBUG_GDB) printf("set_npc(): New NPC value (0x%08x) written and locally cached \n", addr);
1772
        npcCachedValue = addr;
1773
        npcIsCached = 1;
1774
      }
1775
    else
1776
      {
1777
        if (DEBUG_GDB) printf("set_npc(): New NPC value (0x%08x) written \n", addr);
1778
        npcIsCached = 0;
1779
      }
1780
 
1781
 
1782
  }
1783
  else
1784
    return;
1785
 
1786
 
1787
}       /* set_npc () */
1788
 
1789
 
1790
//! Read the value of the Next Program Counter (a SPR)
1791
 
1792
//! Setting the NPC flushes the pipeline, so subsequent reads will return
1793
//! zero until the processor has refilled the pipeline. This will not be
1794
//! happening if the processor is stalled (as it is when GDB had control),
1795
//! so we must cache the NPC. As soon as the processor is unstalled, this
1796
//! cached value becomes invalid.
1797
 
1798
//! If we are stalled and the value has been cached, use it. If we are stalled
1799
//! and the value has not been cached, cache it (for efficiency) and use
1800
//! it. Otherwise read the corresponding SPR.
1801
 
1802
//! @return  The value of the NPC
1803
static uint32_t get_npc ()
1804
{
1805
  uint32_t current_npc;
1806
 
1807
  if (STALLED == stallState)
1808
    {
1809
      if (npcIsCached == 0)
1810
        {
1811
          err = gdb_set_chain(SC_RISC_DEBUG);
1812
          err = gdb_read_reg(NPC_CPU_REG_ADD, &npcCachedValue);
1813
          if(err > 0){
1814
            printf("Error %d reading NPC\n", err);
1815
            rsp_client_close ();
1816
            return 0;
1817
          }
1818
          if (DEBUG_GDB) printf("get_npc(): caching newly read NPC value (0x%08x)\n",npcCachedValue);
1819
 
1820
 
1821
          npcIsCached    = 1;
1822
        }
1823
      else
1824
        if (DEBUG_GDB) printf("get_npc(): reading cached NPC value (0x%08x)\n",npcCachedValue);
1825
 
1826
      return  npcCachedValue;
1827
    }
1828
  else
1829
    {
1830
      err = gdb_read_reg(NPC_CPU_REG_ADD, &current_npc);
1831
      if(err > 0){
1832
        printf("Error %d reading NPC\n", err);
1833
        rsp_client_close ();
1834
        return 0;
1835
      }
1836
      return current_npc;
1837
 
1838
    }
1839
}       // get_npc ()
1840
 
1841
 
1842
 
1843
/*---------------------------------------------------------------------------*/
1844
/*!Send a packet acknowledging an exception has occurred
1845
 
1846
   This is only called if there is a client FD to talk to                    */
1847
/*---------------------------------------------------------------------------*/
1848
static void
1849
rsp_report_exception (void)
1850
{
1851
  struct rsp_buf  buffer;
1852
 
1853
  /* Construct a signal received packet */
1854
  buffer.data[0] = 'S';
1855
  buffer.data[1] = hexchars[rsp.sigval >> 4];
1856
  buffer.data[2] = hexchars[rsp.sigval % 16];
1857
  buffer.data[3] = 0;
1858
  buffer.len     = strlen (buffer.data);
1859
 
1860
  put_packet (&buffer);
1861
 
1862
}       /* rsp_report_exception () */
1863
 
1864
 
1865
/*---------------------------------------------------------------------------*/
1866
/*!Handle a RSP continue request
1867
 
1868
   Parse the command to see if there is an address. Uses the underlying
1869
   generic continue function, with EXCEPT_NONE.
1870
 
1871
   @param[in] p_buf  The full continue packet                                  */
1872
/*---------------------------------------------------------------------------*/
1873
static void
1874
rsp_continue (struct rsp_buf *p_buf)
1875
{
1876
  uint32_t  addr;               /* Address to continue from, if any */
1877
 
1878
  // First set the chain 
1879
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
1880
 
1881
  // Make sure the processor is stalled
1882
  gdb_ensure_or1k_stalled();
1883
 
1884
  if(err > 0){
1885
    printf("Error %d to set RISC Debug Interface chain in the CONTINUE command 'c'\n", err);
1886
    rsp_client_close ();
1887
    return;
1888
  }
1889
 
1890
  if (0 == strcmp ("c", p_buf->data))
1891
  {
1892
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
1893
    /* ---------- NPC ---------- */
1894
    addr = get_npc();
1895
  }
1896
  else if (1 != sscanf (p_buf->data, "c%x", &addr))
1897
  {
1898
    fprintf (stderr,
1899
       "Warning: RSP continue address %s not recognized: ignored\n",
1900
       p_buf->data);
1901
 
1902
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
1903
    /* ---------- NPC ---------- */
1904
    addr = get_npc();
1905
  }
1906
 
1907
  if (DEBUG_GDB) printf("rsp_continue() --> Read NPC = 0x%08x\n", addr);
1908
 
1909
  rsp_continue_generic (addr, EXCEPT_NONE);
1910
 
1911
}       /* rsp_continue () */
1912
 
1913
 
1914
/*---------------------------------------------------------------------------*/
1915
/*!Handle a RSP continue with signal request
1916
 
1917
   Currently null. Will use the underlying generic continue function.
1918
 
1919
   @param[in] p_buf  The full continue with signal packet                      */
1920
/*---------------------------------------------------------------------------*/
1921
static void
1922
rsp_continue_with_signal (struct rsp_buf *p_buf)
1923
{
1924
  printf ("RSP continue with signal '%s' received\n", p_buf->data);
1925
 
1926
}       /* rsp_continue_with_signal () */
1927
 
1928
 
1929
/*---------------------------------------------------------------------------*/
1930
/*!Generic processing of a continue request
1931
 
1932
   The signal may be EXCEPT_NONE if there is no exception to be
1933
   handled. Currently the exception is ignored.
1934
 
1935
   The single step flag is cleared in the debug registers and then the
1936
   processor is unstalled.
1937
 
1938
   @param[in] addr    Address from which to step
1939
   @param[in] except  The exception to use (if any)                          */
1940
/*---------------------------------------------------------------------------*/
1941
static void
1942
rsp_continue_generic (uint32_t  addr,
1943
                      uint32_t  except)
1944
{
1945
  uint32_t              temp_uint32;
1946
 
1947
  /* Set the address as the value of the next program counter */
1948
  set_npc (addr);
1949
 
1950
  /* Clear Debug Reason Register (DRR) 0x3015 */
1951
  // Arc sim --> cpu_state.sprs[SPR_DRR]   = 0;
1952
  if(gdb_write_reg(DRR_CPU_REG_ADD, 0)) printf("Error write to DRR register\n");
1953
 
1954
  /* Clear watchpoint break generation in Debug Mode Register 2 (DMR2) 0x3011 */
1955
  // Arc sim --> cpu_state.sprs[SPR_DMR2] &= ~SPR_DMR2_WGB;
1956
  if(gdb_read_reg(DMR2_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR2 register\n");
1957
  temp_uint32 &= ~SPR_DMR2_WGB;
1958
  if(gdb_write_reg(DMR2_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR2 register\n");
1959
 
1960
  /* Clear the single step trigger in Debug Mode Register 1 (DMR1) Register 0x3010 */
1961
  // Arc sim --> cpu_state.sprs[SPR_DMR1] &= ~SPR_DMR1_ST;
1962
  if(gdb_read_reg(DMR1_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR1 register\n");
1963
  temp_uint32 &= ~SPR_DMR1_ST;
1964
  if(gdb_write_reg(DMR1_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR1 register\n");
1965
 
1966
  /* Set traps to be handled by the debug unit in the Debug Stop Register (DSR) Register 0x3014 */
1967
  // Arc sim --> cpu_state.sprs[SPR_DSR]  |= SPR_DSR_TE;
1968
  if(gdb_read_reg(DSR_CPU_REG_ADD, &temp_uint32)) printf("Error read from DSR register\n");
1969
  temp_uint32 |= SPR_DSR_TE;
1970
  if(gdb_write_reg(DSR_CPU_REG_ADD, temp_uint32)) printf("Error write to DSR register\n");
1971
 
1972
  /* Unstall the processor */
1973
  set_stall_state (0);
1974
 
1975
  /* Note the GDB client is now waiting for a reply. */
1976
  rsp.client_waiting = 1;
1977
 
1978
}       /* rsp_continue_generic () */
1979
 
1980
 
1981
/*---------------------------------------------------------------------------*/
1982
/*!Handle a RSP read all registers request
1983
 
1984
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
1985
   (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
1986
   returned as a sequence of bytes in target endian order.
1987
 
1988
   Each byte is packed as a pair of hex digits.                              */
1989
/*---------------------------------------------------------------------------*/
1990
static void
1991
rsp_read_all_regs (void)
1992
{
1993
  struct rsp_buf  buffer;                       /* Buffer for the reply */
1994
  int             r;                            /* Register index */
1995
  uint32_t   temp_uint32;
1996
 
1997
  // Make sure the processor is stalled
1998
  gdb_ensure_or1k_stalled();
1999
 
2000
  // First set the chain 
2001
  gdb_set_chain(SC_RISC_DEBUG); /* 1 RISC Debug Interface chain */
2002
 
2003
 
2004
  // Read all GPRs
2005
  for (r = 0; r < MAX_GPRS; r++){
2006
 
2007
    err = gdb_read_reg(0x400 + r, &temp_uint32);
2008
    if(err > 0){
2009
      if (DEBUG_GDB) printf("Error %d in gdb_read_reg at reg. %d\n", err, r);
2010
      put_str_packet ("E01");
2011
      return;
2012
    }
2013
    reg2hex (temp_uint32, &(buffer.data[r * 8]));
2014
 
2015
    if (DEBUG_GDB_DUMP_DATA){
2016
      switch(r % 4)
2017
        {
2018
        case 0:
2019
          printf("gpr%02d   0x%08x  ", r, temp_uint32);
2020
          break;
2021
        case 1:
2022
        case 2:
2023
          printf("0x%08x  ", temp_uint32);
2024
          break;
2025
        case 3:
2026
          printf("0x%08x\n", temp_uint32);
2027
          break;
2028
        default:
2029
          break;
2030
        }
2031
    }
2032
 
2033
  }
2034
  /* ---------- PPC ---------- */
2035
  err = gdb_read_reg(PPC_CPU_REG_ADD, &temp_uint32);
2036
  if(err > 0){
2037
        if (DEBUG_GDB) printf("Error %d in gdb_read_reg read --> PPC\n", err);
2038
        put_str_packet ("E01");
2039
        return;
2040
  }
2041
  reg2hex (temp_uint32, &(buffer.data[PPC_REGNUM * 8]));
2042
  if (DEBUG_GDB_DUMP_DATA)      printf("PPC     0x%08x\n", temp_uint32);
2043
  /* ---------- NPC ---------- */
2044
  temp_uint32 = get_npc();
2045
  /*
2046
  err = gdb_read_reg(NPC_CPU_REG_ADD, &temp_uint32);
2047
  if(err > 0){
2048
        if (DEBUG_GDB) printf("Error %d in gdb_read_reg read --> NPC\n", err);
2049
        put_str_packet ("E01");
2050
        return;
2051
  }
2052
  */
2053
  reg2hex (temp_uint32, &(buffer.data[NPC_REGNUM * 8]));
2054
  if (DEBUG_GDB_DUMP_DATA)      printf("NPC     0x%08x\n", temp_uint32);
2055
  /* ---------- SR ---------- */
2056
  err = gdb_read_reg(SR_CPU_REG_ADD, &temp_uint32);
2057
  if(err > 0){
2058
        if (DEBUG_GDB) printf("Error %d in gdb_read_reg read --> SP\n", err);
2059
        put_str_packet ("E01");
2060
        return;
2061
  }
2062
  reg2hex (temp_uint32, &(buffer.data[SR_REGNUM * 8]));
2063
        if (DEBUG_GDB_DUMP_DATA)        printf("SR      0x%08x\n", temp_uint32);
2064
 
2065
  /* Finalize the packet and send it */
2066
  buffer.data[NUM_REGS * 8] = 0;
2067
  buffer.len                = NUM_REGS * 8;
2068
 
2069
  put_packet (&buffer);
2070
        return;
2071
}       /* rsp_read_all_regs () */
2072
 
2073
 
2074
/*---------------------------------------------------------------------------*/
2075
/*!Handle a RSP write all registers request
2076
 
2077
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
2078
   (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
2079
   supplied as a sequence of bytes in target endian order.
2080
 
2081
   Each byte is packed as a pair of hex digits.
2082
 
2083
   @todo There is no error checking at present. Non-hex chars will generate a
2084
         warning message, but there is no other check that the right amount
2085
         of data is present. The result is always "OK".
2086
 
2087
   @param[in] p_buf  The original packet request.                              */
2088
/*---------------------------------------------------------------------------*/
2089
static void
2090
rsp_write_all_regs (struct rsp_buf *p_buf)
2091
{
2092
  uint32_t  regnum;                             /* Register index */
2093
  // char          valstr[9];           /* Allow for EOS on the string */
2094
 
2095
  // /* Check for valid data */
2096
  // if (0 != (strcmp ("G", p_buf->data)) && (GDB_BUF_MAX != strlen(p_buf->data)))
2097
  // {
2098
  //   fprintf (stderr, "Warning: Failed to recognize RSP write register "
2099
  //      "command: %s\n", p_buf->data);
2100
  //   // put_str_packet ("E01");
2101
  //   return;
2102
  // }
2103
 
2104
  // Make sure the processor is stalled
2105
  gdb_ensure_or1k_stalled();
2106
 
2107
  // First set the chain 
2108
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2109
  if(err > 0){
2110
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2111
    put_str_packet ("E01");
2112
        return;
2113
        }
2114
 
2115
  /* ---------- GPRS ---------- */
2116
  for (regnum = 0; regnum < MAX_GPRS; regnum++)
2117
  {
2118
    err = gdb_write_reg(0x400 + regnum, hex2reg (&p_buf->data[regnum * 8 + 1]));
2119
          if(err > 0){
2120
                if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> GPRS\n", err);
2121
            put_str_packet ("E01");
2122
                return;
2123
          }
2124
  }
2125
 
2126
  /* ---------- PPC ---------- */
2127
  err = gdb_write_reg(PPC_CPU_REG_ADD, hex2reg (&p_buf->data[PPC_REGNUM * 8 + 1]));
2128
  if(err > 0){
2129
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> PPC\n", err);
2130
    put_str_packet ("E01");
2131
        return;
2132
  }
2133
  /* ---------- SR ---------- */
2134
  err = gdb_write_reg(SR_CPU_REG_ADD, hex2reg (&p_buf->data[SR_REGNUM * 8 + 1]));
2135
  if(err > 0){
2136
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> SR\n", err);
2137
    put_str_packet ("E01");
2138
        return;
2139
  }
2140
  /* ---------- NPC ---------- */
2141
  set_npc(hex2reg (&p_buf->data[NPC_REGNUM * 8 + 1]));
2142
  /*
2143
  err = gdb_write_reg(NPC_CPU_REG_ADD, hex2reg (&p_buf->data[NPC_REGNUM * 8 + 1]));
2144
  if(err > 0){
2145
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> NPC\n", err);
2146
    put_str_packet ("E01");
2147
        return;
2148
  }
2149
  */
2150
  /* Acknowledge. TODO: We always succeed at present, even if the data was
2151
     defective. */
2152
  put_str_packet ("OK");
2153
}       /* rsp_write_all_regs () */
2154
 
2155
 
2156
/*---------------------------------------------------------------------------*/
2157
/* Handle a RSP read memory (symbolic) request
2158
 
2159
   Syntax is:
2160
 
2161
     m<addr>,<length>:
2162
 
2163
   The response is the bytes, lowest address first, encoded as pairs of hex
2164
   digits.
2165
 
2166
   The length given is the number of bytes to be read.
2167
 
2168
   @note This function reuses p_buf, so trashes the original command.
2169
 
2170
   @param[in] p_buf  The command received                                      */
2171
/*---------------------------------------------------------------------------*/
2172
static void rsp_read_mem (struct rsp_buf *p_buf)
2173
{
2174
  unsigned int    addr;                 /* Where to read the memory */
2175
  int             len;                  /* Number of bytes to read */
2176
  int             off;                  /* Offset into the memory */
2177 46 julius
  uint32_t              temp_uint32 = 0;
2178
  char                                          *rec_buf, *rec_buf_ptr;
2179
  int bytes_per_word = 4; /* Current OR implementation is 4-byte words */
2180
  int i;
2181
  int len_cpy;
2182
  /* Couple of temps we might need when doing aligning/leftover accesses */
2183
  uint32_t tmp_word;
2184
  char *tmp_word_ptr = (char*) &tmp_word;
2185
 
2186 40 julius
 
2187
 
2188
  if (2 != sscanf (p_buf->data, "m%x,%x:", &addr, &len))
2189
  {
2190
    fprintf (stderr, "Warning: Failed to recognize RSP read memory "
2191
       "command: %s\n", p_buf->data);
2192
    put_str_packet ("E01");
2193
    return;
2194
  }
2195
 
2196
  /* Make sure we won't overflow the buffer (2 chars per byte) */
2197
  if ((len * 2) >= GDB_BUF_MAX)
2198
  {
2199
    fprintf (stderr, "Warning: Memory read %s too large for RSP packet: "
2200
       "truncated\n", p_buf->data);
2201
    len = (GDB_BUF_MAX - 1) / 2;
2202
        }
2203
 
2204
  if(!(rec_buf = (char*)malloc(len))) {
2205
    put_str_packet ("E01");
2206
    return;
2207
  }
2208
 
2209
  // Make sure the processor is stalled
2210
  gdb_ensure_or1k_stalled();
2211
 
2212
  // Set chain 5 --> Wishbone Memory chain
2213
  err = gdb_set_chain(SC_WISHBONE);
2214
  if(err){
2215 46 julius
    if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2216
    put_str_packet ("E01");
2217 40 julius
    return;
2218 46 julius
  }
2219
 
2220
  len_cpy = len;
2221
  rec_buf_ptr = rec_buf; // Need to save a copy of pointer
2222
 
2223
  if (addr & 0x3)  // address not aligned at the start
2224
    {
2225
      // Have to read from the word-aligned address first and fetch the bytes
2226
      // we need.
2227
      if (DEBUG_GDB)
2228
        printf("rsp_read_mem: unaligned address read - reading before bytes\n",
2229
               err);
2230
      int num_bytes_to_align = bytes_per_word - (addr & 0x3);
2231
      uint32_t aligned_addr = addr & ~0x3;
2232
 
2233
      if (DEBUG_GDB)
2234
        printf("rsp_read_mem: reading first %d of %d overall, from 0x%.8x\n",
2235
               num_bytes_to_align, len_cpy, aligned_addr);
2236
 
2237
      err = gdb_read_reg(aligned_addr, &tmp_word);
2238
 
2239
      if (DEBUG_GDB) printf("rsp_read_mem: first word 0x%.8x\n", tmp_word);
2240
 
2241
      if(err){
2242
        put_str_packet ("E01");
2243
        return;
2244
      }
2245
 
2246
      // Pack these bytes in first
2247
      if (num_bytes_to_align > len_cpy) num_bytes_to_align = len_cpy;
2248
 
2249
      // A little strange - the OR is big endian, but they wind up
2250
      // in this array in little endian format, so read them out
2251
      // and pack the response array big endian (or, whatever the lowest
2252
      // memory address first is... depends how you print it out I guess.)
2253
      if (DEBUG_GDB_BLOCK_DATA)printf("rsp_read_mem: packing first bytes ");
2254
      i=addr&0x3; int buf_ctr = 0;
2255
      while (buf_ctr < num_bytes_to_align)
2256
        {
2257
          rec_buf_ptr[buf_ctr] = tmp_word_ptr[bytes_per_word-1-i];
2258
          if (DEBUG_GDB_BLOCK_DATA)printf("i=%d=0x%x, ", i,
2259
                                          tmp_word_ptr[bytes_per_word-1-i]);
2260
          i++;
2261
          buf_ctr++;
2262 40 julius
        }
2263 46 julius
 
2264
      if (DEBUG_GDB_BLOCK_DATA)printf("\n");
2265
 
2266
      // Adjust our status
2267
      len_cpy -= num_bytes_to_align; addr += num_bytes_to_align;
2268
      rec_buf_ptr += num_bytes_to_align;
2269
    }
2270
  if (len_cpy/bytes_per_word) // Now perform all full word accesses
2271
    {
2272
      int words_to_read = len_cpy/bytes_per_word; // Full words to read
2273
      if (DEBUG_GDB) printf("rsp_read_mem: reading %d words from 0x%.8x\n",
2274
                            words_to_read, addr);
2275
      // Read full data words from Wishbone Memory chain
2276
      err = gdb_read_block(addr, (uint32_t*)rec_buf_ptr,
2277
                           words_to_read*bytes_per_word);
2278
 
2279
      if(err){
2280
        put_str_packet ("E01");
2281
        return;
2282
      }
2283
      // A little strange, but these words will actually be little endian
2284
      // in the buffer. So swap them around.
2285
      uint32_t* rec_buf_u32_ptr = (uint32_t*)rec_buf_ptr;
2286
      for(i=0;i<words_to_read;i++)
2287
        {
2288
          // htonl() will work.(network byte order is big endian)
2289
          // Note this is a hack, not actually about to send this
2290
          // out onto the network.
2291
          rec_buf_u32_ptr[i] = htonl(rec_buf_u32_ptr[i]);
2292
        }
2293
 
2294 40 julius
 
2295 46 julius
      // Adjust our status
2296
      len_cpy -= (words_to_read*bytes_per_word);
2297
      addr += (words_to_read*bytes_per_word);
2298
      rec_buf_ptr += (words_to_read*bytes_per_word);
2299
    }
2300
  if (len_cpy) // Leftover bytes
2301
    {
2302
      if (DEBUG_GDB)
2303
        printf("rsp_read_mem: reading %d left-over bytes from 0x%.8x\n",
2304
               len_cpy, addr);
2305
 
2306
      err = gdb_read_reg(addr, &tmp_word);
2307
 
2308
      // Big endian - top byte first!
2309
      for(i=0;i<len_cpy;i++)
2310
        rec_buf_ptr[i] = tmp_word_ptr[bytes_per_word - 1 - i];
2311
 
2312
    }
2313
 
2314
  if (DEBUG_GDB)
2315
    printf("rsp_read_mem: err: %d\n",err);
2316
 
2317
 
2318 40 julius
  if(err){
2319 46 julius
    put_str_packet ("E01");
2320
    return;
2321
  }
2322 40 julius
 
2323
  /* Refill the buffer with the reply */
2324
  for( off = 0 ; off < len ; off ++ ) {
2325 46 julius
    ;
2326
    p_buf->data[(2*off)] = hexchars[((rec_buf[off]&0xf0)>>4)];
2327
    p_buf->data[(2*off)+1] = hexchars[(rec_buf[off]&0x0f)];
2328 40 julius
  }
2329 46 julius
 
2330 40 julius
  if (DEBUG_GDB && (err > 0)) printf("\nError %x\n", err);fflush (stdout);
2331
        free(rec_buf);
2332
  p_buf->data[off * 2] = 0;                      /* End of string */
2333
  p_buf->len           = strlen (p_buf->data);
2334 46 julius
  if (DEBUG_GDB_BLOCK_DATA){
2335
    printf("rsp_read_mem: adr 0x%.8x data: ", addr);
2336
    for(i=0;i<len*2;i++)
2337
      printf("%c",p_buf->data[i]);
2338
    printf("\n");
2339
  }
2340
 
2341 40 julius
  put_packet (p_buf);
2342
}       /* rsp_read_mem () */
2343
 
2344
 
2345
/*---------------------------------------------------------------------------*/
2346
/*!Handle a RSP write memory (symbolic) request  ("M")
2347
 
2348
   Syntax is:
2349
 
2350
     M<addr>,<length>:<data>
2351
 
2352
        Example: M4015cc,2:c320#
2353
          (Write the value 0xc320 to address 0x4015cc.)
2354
 
2355
                An example target response:
2356
                + $OK#
2357
 
2358
   The data is the bytes, lowest address first, encoded as pairs of hex
2359
   digits.
2360
 
2361
   The length given is the number of bytes to be written.
2362
 
2363
   @note This function reuses p_buf, so trashes the original command.
2364
 
2365
   @param[in] p_buf  The command received                                      */
2366
/*---------------------------------------------------------------------------*/
2367
static void
2368
rsp_write_mem (struct rsp_buf *p_buf)
2369
{
2370
  unsigned int    addr;                 /* Where to write the memory */
2371
  int             len;                  /* Number of bytes to write */
2372
  char           *symdat;               /* Pointer to the symboli data */
2373
  int             datlen;               /* Number of digits in symbolic data */
2374
  int             off;                  /* Offset into the memory */
2375
  int             nibc;                 /* Nibbel counter */
2376
        uint32_t   val;
2377
 
2378
  if (2 != sscanf (p_buf->data, "M%x,%x:", &addr, &len))
2379
  {
2380
    fprintf (stderr, "Warning: Failed to recognize RSP write memory "
2381
       "command: %s\n", p_buf->data);
2382
    put_str_packet ("E01");
2383
    return;
2384
  }
2385
 
2386
  /* Find the start of the data and check there is the amount we expect. */
2387
  symdat = (char*) memchr ((const void *)p_buf->data, ':', GDB_BUF_MAX) + 1;
2388
  datlen = p_buf->len - (symdat - p_buf->data);
2389
 
2390
  /* Sanity check */
2391
  if (len * 2 != datlen)
2392
  {
2393
    fprintf (stderr, "Warning: Write of %d digits requested, but %d digits "
2394
       "supplied: packet ignored\n", len * 2, datlen );
2395
    put_str_packet ("E01");
2396
    return;
2397
  }
2398
 
2399
  // Make sure the processor is stalled
2400
  gdb_ensure_or1k_stalled();
2401
 
2402
 
2403
  // Set chain 5 --> Wishbone Memory chain
2404
  err = gdb_set_chain(SC_WISHBONE);
2405
        if(err){
2406
    put_str_packet ("E01");
2407
    return;
2408
        }
2409
 
2410
        val = 0;
2411
        off = 0;
2412
  /* Write the bytes to memory */
2413
  for (nibc = 0; nibc < datlen; nibc++)
2414
  {
2415
                val |= 0x0000000f & hex (symdat[nibc]);
2416
                if(nibc % 8 == 7){
2417
                        err = gdb_write_block(addr + off, &val, 4);
2418
                        if (DEBUG_GDB) printf("Error %x\n", err);fflush (stdout);
2419
                        if(err){
2420
                                put_str_packet ("E01");
2421
                                return;
2422
                        }
2423
                  val = 0;
2424
                        off += 4;
2425
                }
2426
          val <<= 4;
2427
  }
2428
  put_str_packet ("OK");
2429
}       /* rsp_write_mem () */
2430
 
2431
 
2432
/*---------------------------------------------------------------------------*/
2433
/*!Read a single register
2434
 
2435
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
2436
   (i.e. SPR NPC) and SR (i.e. SPR SR). The register is returned as a
2437
   sequence of bytes in target endian order.
2438
 
2439
   Each byte is packed as a pair of hex digits.
2440
 
2441
   @param[in] p_buf  The original packet request. Reused for the reply.        */
2442
/*---------------------------------------------------------------------------*/
2443
static void
2444
rsp_read_reg (struct rsp_buf *p_buf)
2445
{
2446
  unsigned int          regnum;
2447
        uint32_t        temp_uint32;
2448
 
2449
  /* Break out the fields from the data */
2450
  if (1 != sscanf (p_buf->data, "p%x", &regnum))
2451
    {
2452
      fprintf (stderr, "Warning: Failed to recognize RSP read register "
2453
               "command: %s\n", p_buf->data);
2454
      put_str_packet ("E01");
2455
      return;
2456
    }
2457
 
2458
  // Make sure the processor is stalled
2459
  gdb_ensure_or1k_stalled();
2460
 
2461
  // First set the chain 
2462
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2463
  if(err > 0){
2464
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2465
    put_str_packet ("E01");
2466
        return;
2467
        }
2468
 
2469
  /* Get the relevant register */
2470
  if (regnum < MAX_GPRS)
2471
    {
2472
                  err = gdb_read_reg(0x400 + regnum, &temp_uint32);
2473
                  if(err > 0){
2474
                        if (DEBUG_GDB) printf("Error %d in rsp_read_reg at reg. %d \n", err, regnum);
2475
                    put_str_packet ("E01");
2476
                        return;
2477
                  }
2478
                  reg2hex (temp_uint32, p_buf->data);
2479
    }
2480
  else if (PPC_REGNUM == regnum)        /* ---------- PPC ---------- */
2481
    {
2482
                  err = gdb_read_reg(PPC_CPU_REG_ADD, &temp_uint32);
2483
                  if(err > 0){
2484
                        if (DEBUG_GDB) printf("Error %d in rsp_read_reg read --> PPC\n", err);
2485
                    put_str_packet ("E01");
2486
                        return;
2487
                  }
2488
                  reg2hex (temp_uint32, p_buf->data);
2489
    }
2490
  else if (NPC_REGNUM == regnum)        /* ---------- NPC ---------- */
2491
    {
2492
      temp_uint32 = get_npc();
2493
      /*
2494
      err = gdb_read_reg(NPC_CPU_REG_ADD, &temp_uint32);
2495
      if(err > 0){
2496
        if (DEBUG_GDB) printf("Error %d in rsp_read_reg read --> PPC\n", err);
2497
        put_str_packet ("E01");
2498
        return;
2499
      }
2500
      */
2501
      reg2hex (temp_uint32, p_buf->data);
2502
    }
2503
  else if (SR_REGNUM == regnum)         /* ---------- SR ---------- */
2504
    {
2505
      err = gdb_read_reg(SR_CPU_REG_ADD, &temp_uint32);
2506
      if(err > 0){
2507
        if (DEBUG_GDB) printf("Error %d in rsp_read_reg read --> PPC\n", err);
2508
        put_str_packet ("E01");
2509
        return;
2510
      }
2511
      reg2hex (temp_uint32, p_buf->data);
2512
    }
2513
  else
2514
    {
2515
      /* Error response if we don't know the register */
2516
      fprintf (stderr, "Warning: Attempt to read unknown register 0x%x: "
2517
               "ignored\n", regnum);
2518
      put_str_packet ("E01");
2519
      return;
2520
    }
2521
 
2522
  p_buf->len = strlen (p_buf->data);
2523
  put_packet (p_buf);
2524
 
2525
}       /* rsp_read_reg () */
2526
 
2527
 
2528
/*---------------------------------------------------------------------------*/
2529
/*!Write a single register
2530
 
2531
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
2532
   (i.e. SPR NPC) and SR (i.e. SPR SR). The register is specified as a
2533
   sequence of bytes in target endian order.
2534
 
2535
   Each byte is packed as a pair of hex digits.
2536
 
2537
   @param[in] p_buf  The original packet request.                              */
2538
/*---------------------------------------------------------------------------*/
2539
static void
2540
rsp_write_reg (struct rsp_buf *p_buf)
2541
{
2542
  unsigned int  regnum;
2543
  char          valstr[9];              /* Allow for EOS on the string */
2544
        // int           err = 0;
2545
 
2546
  /* Break out the fields from the data */
2547
  if (2 != sscanf (p_buf->data, "P%x=%8s", &regnum, valstr))
2548
    {
2549
      fprintf (stderr, "Warning: Failed to recognize RSP write register "
2550
               "command: %s\n", p_buf->data);
2551
      put_str_packet ("E01");
2552
      return;
2553
    }
2554
 
2555
  // Make sure the processor is stalled
2556
  gdb_ensure_or1k_stalled();
2557
 
2558
  // First set the chain 
2559
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2560
  if(err > 0){
2561
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2562
    put_str_packet ("E01");
2563
        return;
2564
        }
2565
 
2566
  /* Set the relevant register */
2567
  if (regnum < MAX_GPRS)                                 /* ---------- GPRS ---------- */
2568
                {
2569
      err = gdb_write_reg(0x400 + regnum, hex2reg (valstr));
2570
                  if(err > 0){
2571
                        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> GPRS\n", err);
2572
                    put_str_packet ("E01");
2573
                        return;
2574
                  }
2575
    }
2576
  else if (PPC_REGNUM == regnum) /* ---------- PPC ---------- */
2577
    {
2578
      err = gdb_write_reg(PPC_CPU_REG_ADD, hex2reg (valstr));
2579
                  if(err > 0){
2580
                        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> PPC\n", err);
2581
                    put_str_packet ("E01");
2582
                        return;
2583
                  }
2584
    }
2585
  else if (NPC_REGNUM == regnum) /* ---------- NPC ---------- */
2586
    {
2587
      set_npc(hex2reg (valstr));
2588
      /*
2589
      err = gdb_write_reg(NPC_CPU_REG_ADD, hex2reg (valstr));
2590
      if(err > 0){
2591
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> NPC\n", err);
2592
        put_str_packet ("E01");
2593
        return;
2594
      }
2595
      */
2596
    }
2597
  else if (SR_REGNUM == regnum)  /* ---------- SR ---------- */
2598
    {
2599
      err = gdb_write_reg(SR_CPU_REG_ADD, hex2reg (valstr));
2600
                  if(err > 0){
2601
                        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> SR\n", err);
2602
                    put_str_packet ("E01");
2603
                        return;
2604
                  }
2605
    }
2606
  else
2607
    {
2608
      /* Error response if we don't know the register */
2609
      fprintf (stderr, "Warning: Attempt to write unknown register 0x%x: "
2610
               "ignored\n", regnum);
2611
      put_str_packet ("E01");
2612
      return;
2613
    }
2614
 
2615
  put_str_packet ("OK");
2616
 
2617
}       /* rsp_write_reg () */
2618
 
2619
 
2620
/*---------------------------------------------------------------------------*/
2621
/*!Handle a RSP query request
2622
 
2623
   @param[in] p_buf  The request                                               */
2624
/*---------------------------------------------------------------------------*/
2625
static void
2626
rsp_query (struct rsp_buf *p_buf)
2627
{
2628
  if (0 == strcmp ("qC", p_buf->data))
2629
    {
2630
      /* Return the current thread ID (unsigned hex). A null response
2631
         indicates to use the previously selected thread. Since we do not
2632
         support a thread concept, this is the appropriate response. */
2633
      put_str_packet ("");
2634
    }
2635
  else if (0 == strncmp ("qCRC", p_buf->data, strlen ("qCRC")))
2636
    {
2637
      /* Return CRC of memory area */
2638
      fprintf (stderr, "Warning: RSP CRC query not supported\n");
2639
      put_str_packet ("E01");
2640
    }
2641
  else if (0 == strcmp ("qfThreadInfo", p_buf->data))
2642
    {
2643
      /* Return info about active threads. We return just '-1' */
2644
      put_str_packet ("m-1");
2645
    }
2646
  else if (0 == strcmp ("qsThreadInfo", p_buf->data))
2647
    {
2648
      /* Return info about more active threads. We have no more, so return the
2649
         end of list marker, 'l' */
2650
      put_str_packet ("l");
2651
    }
2652
  else if (0 == strncmp ("qGetTLSAddr:", p_buf->data, strlen ("qGetTLSAddr:")))
2653
    {
2654
      /* We don't support this feature */
2655
      put_str_packet ("");
2656
    }
2657
  else if (0 == strncmp ("qL", p_buf->data, strlen ("qL")))
2658
    {
2659
      /* Deprecated and replaced by 'qfThreadInfo' */
2660
      fprintf (stderr, "Warning: RSP qL deprecated: no info returned\n");
2661
      put_str_packet ("qM001");
2662
    }
2663
  else if (0 == strcmp ("qOffsets", p_buf->data))
2664
    {
2665
      /* Report any relocation */
2666
      put_str_packet ("Text=0;Data=0;Bss=0");
2667
    }
2668
  else if (0 == strncmp ("qP", p_buf->data, strlen ("qP")))
2669
    {
2670
      /* Deprecated and replaced by 'qThreadExtraInfo' */
2671
      fprintf (stderr, "Warning: RSP qP deprecated: no info returned\n");
2672
      put_str_packet ("");
2673
    }
2674
  else if (0 == strncmp ("qRcmd,", p_buf->data, strlen ("qRcmd,")))
2675
    {
2676
      /* This is used to interface to commands to do "stuff" */
2677
      rsp_command (p_buf);
2678
    }
2679
  else if (0 == strncmp ("qSupported", p_buf->data, strlen ("qSupported")))
2680
    {
2681
      /* Report a list of the features we support. For now we just ignore any
2682
                                 supplied specific feature queries, but in the future these may be
2683
                                 supported as well. Note that the packet size allows for 'G' + all the
2684
                                 registers sent to us, or a reply to 'g' with all the registers and an
2685
                                 EOS so the buffer is a well formed string. */
2686
      setup_or32();     // setup cpu
2687
      char  reply[GDB_BUF_MAX];
2688
      sprintf (reply, "PacketSize=%x", GDB_BUF_MAX);
2689
      put_str_packet (reply);
2690
    }
2691
  else if (0 == strncmp ("qSymbol:", p_buf->data, strlen ("qSymbol:")))
2692
    {
2693
      /* Offer to look up symbols. Nothing we want (for now). TODO. This just
2694
         ignores any replies to symbols we looked up, but we didn't want to
2695
         do that anyway! */
2696
      put_str_packet ("OK");
2697
    }
2698
  else if (0 == strncmp ("qThreadExtraInfo,", p_buf->data,
2699
                         strlen ("qThreadExtraInfo,")))
2700
    {
2701
      /* Report that we are runnable, but the text must be hex ASCI
2702
         digits. For now do this by steam, reusing the original packet */
2703
      sprintf (p_buf->data, "%02x%02x%02x%02x%02x%02x%02x%02x%02x",
2704
               'R', 'u', 'n', 'n', 'a', 'b', 'l', 'e', 0);
2705
      p_buf->len = strlen (p_buf->data);
2706
      put_packet (p_buf);
2707
    }
2708
  else if (0 == strncmp ("qXfer:", p_buf->data, strlen ("qXfer:")))
2709
    {
2710
      /* For now we support no 'qXfer' requests, but these should not be
2711
         expected, since they were not reported by 'qSupported' */
2712
      fprintf (stderr, "Warning: RSP 'qXfer' not supported: ignored\n");
2713
      put_str_packet ("");
2714
    }
2715
  else
2716
    {
2717
      fprintf (stderr, "Unrecognized RSP query: ignored\n");
2718
    }
2719
}       /* rsp_query () */
2720
 
2721
 
2722
/*---------------------------------------------------------------------------*/
2723
/*!Handle a RSP qRcmd request
2724
 
2725
  The actual command follows the "qRcmd," in ASCII encoded to hex
2726
 
2727
   @param[in] p_buf  The request in full                                       */
2728
/*---------------------------------------------------------------------------*/
2729
static void
2730
rsp_command (struct rsp_buf *p_buf)
2731
{
2732
  char  cmd[GDB_BUF_MAX];
2733
  unsigned int      regno;
2734
  uint32_t              temp_uint32;
2735
 
2736
  hex2ascii (cmd, &(p_buf->data[strlen ("qRcmd,")]));
2737
 
2738
  /* Work out which command it is */
2739
  if (0 == strncmp ("readspr ", cmd, strlen ("readspr")))
2740
  {
2741
    /* Parse and return error if we fail */
2742
    if( 1 != sscanf (cmd, "readspr %4x", &regno))
2743
      {
2744
        fprintf (stderr, "Warning: qRcmd %s not recognized: ignored\n", cmd);
2745
        put_str_packet ("E01");
2746
        return;
2747
      }
2748
 
2749
    /* SPR out of range */
2750
    if (regno > MAX_SPRS)
2751
      {
2752
        fprintf (stderr, "Warning: qRcmd readspr %x too large: ignored\n",
2753
                 regno);
2754
        put_str_packet ("E01");
2755
        return;
2756
      }
2757
 
2758
    /* Construct the reply */
2759
 
2760
    // Make sure the processor is stalled
2761
    gdb_ensure_or1k_stalled();
2762
 
2763
    // First set the chain 
2764
    gdb_set_chain(SC_RISC_DEBUG);       /* 1 RISC Debug Interface chain */
2765
 
2766
    // special case for NPC
2767
    if(regno == NPC_CPU_REG_ADD)
2768
      temp_uint32 = get_npc();
2769
    else
2770
      {
2771
        err = gdb_read_reg(regno, &temp_uint32);
2772
        if(err > 0){
2773
          if (DEBUG_GDB) printf("Error %d in rsp_command at reg. %x \n", err, regno);
2774
        }
2775
        else{
2776
          reg2hex (temp_uint32, cmd);
2777
          if (DEBUG_GDB) printf("Error %d Command readspr Read reg. %x = 0x%08x\n", err, regno, temp_uint32);
2778
        }
2779
      }
2780
 
2781
    // pack the result into the buffer to send back
2782
    sprintf (cmd, "%8x", (unsigned int)temp_uint32);
2783
    ascii2hex (p_buf->data, cmd);
2784
    p_buf->len = strlen (p_buf->data);
2785
    put_packet (p_buf);
2786
  }
2787
  else if (0 == strncmp ("writespr ", cmd, strlen ("writespr")))
2788
    {
2789
      unsigned int       regno;
2790
      uint32_t  val;
2791
 
2792
      /* Parse and return error if we fail */
2793
      if( 2 != sscanf (cmd, "writespr %4x %8x", &regno, &val))
2794
        {
2795
          fprintf (stderr, "Warning: qRcmd %s not recognized: ignored\n",
2796
                   cmd);
2797
          put_str_packet ("E01");
2798
          return;
2799
        }
2800
 
2801
      /* SPR out of range */
2802
      if (regno > MAX_SPRS)
2803
        {
2804
          fprintf (stderr, "Warning: qRcmd writespr %x too large: ignored\n",
2805
                   regno);
2806
          put_str_packet ("E01");
2807
          return;
2808
        }
2809
 
2810
      // Make sure the processor is stalled
2811
      gdb_ensure_or1k_stalled();
2812
 
2813
      // First set the chain 
2814
      gdb_set_chain(SC_RISC_DEBUG);     /* 1 RISC Debug Interface chain */
2815
 
2816
      /* set the relevant register */
2817
      // special case for NPC
2818
      if(regno == NPC_CPU_REG_ADD)
2819
        set_npc(val);
2820
      else
2821
        {
2822
 
2823
          err = gdb_write_reg(regno, val);
2824
          if(err > 0){
2825
            if (DEBUG_GDB) printf("Error %d in rsp_command write Reg. %x = 0x%08x\n", err, regno, val);
2826
            put_str_packet ("E01");
2827
            return;
2828
          }
2829
          else{
2830
            if (DEBUG_GDB) printf("Error %d Command writespr Write reg. %x = 0x%08x\n", err, regno, val);
2831
          }
2832
        }
2833
      put_str_packet ("OK");
2834
    }
2835
}       /* rsp_command () */
2836
 
2837
 
2838
/*---------------------------------------------------------------------------*/
2839
/*!Handle a RSP set request
2840
 
2841
   @param[in] p_buf  The request                                               */
2842
/*---------------------------------------------------------------------------*/
2843
static void
2844
rsp_set (struct rsp_buf *p_buf)
2845
{
2846
  if (0 == strncmp ("QPassSignals:", p_buf->data, strlen ("QPassSignals:")))
2847
    {
2848
      /* Passing signals not supported */
2849
      put_str_packet ("");
2850
    }
2851
  else if ((0 == strncmp ("QTDP",    p_buf->data, strlen ("QTDP")))   ||
2852
           (0 == strncmp ("QFrame",  p_buf->data, strlen ("QFrame"))) ||
2853
           (0 == strcmp  ("QTStart", p_buf->data))                    ||
2854
           (0 == strcmp  ("QTStop",  p_buf->data))                    ||
2855
           (0 == strcmp  ("QTinit",  p_buf->data))                    ||
2856
           (0 == strncmp ("QTro",    p_buf->data, strlen ("QTro"))))
2857
    {
2858
      /* All tracepoint features are not supported. This reply is really only
2859
         needed to 'QTDP', since with that the others should not be
2860
         generated. */
2861
      put_str_packet ("");
2862
    }
2863
  else
2864
    {
2865
      fprintf (stderr, "Unrecognized RSP set request: ignored\n");
2866
    }
2867
}       /* rsp_set () */
2868
 
2869
 
2870
/*---------------------------------------------------------------------------*/
2871
/*!Handle a RSP restart request
2872
 
2873
   For now we just put the program counter back to the one used with the last
2874
   vRun request. There is no point in unstalling the processor, since we'll
2875
   never get control back.                                                   */
2876
/*---------------------------------------------------------------------------*/
2877
static void
2878
rsp_restart (void)
2879
{
2880
  // Make sure the processor is stalled
2881
  gdb_ensure_or1k_stalled();
2882
 
2883
  // First set the chain 
2884
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2885
  if(err > 0){
2886
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2887
    put_str_packet ("E01");
2888
        return;
2889
        }
2890
        // OR32 Arc sim equivalent --> set_npc (rsp.start_addr);
2891
  /* Set NPC to reset vector 0x100 */
2892
  set_npc(rsp.start_addr);
2893
  /*
2894
  err = gdb_write_reg(NPC_CPU_REG_ADD, rsp.start_addr);
2895
  if(err > 0){
2896
        if (DEBUG_GDB) printf("Error %d in rsp_restart write Reg. %x = 0x%08x\n", err, NPC_CPU_REG_ADD, rsp.start_addr);
2897
    put_str_packet ("E01");
2898
        return;
2899
  }
2900
 
2901
  else{
2902
  if (DEBUG_GDB) printf("Error %d Command Reset. Set NPC to Start vector %x = 0x%08x\n", err, NPC_CPU_REG_ADD, rsp.start_addr);
2903
  }
2904
  */
2905
}       /* rsp_restart () */
2906
 
2907
 
2908
/*---------------------------------------------------------------------------*/
2909
/*!Handle a RSP step request
2910
 
2911
   Parse the command to see if there is an address. Uses the underlying
2912
   generic step function, with EXCEPT_NONE.
2913
 
2914
   @param[in] p_buf  The full step packet                          */
2915
/*---------------------------------------------------------------------------*/
2916
static void
2917
rsp_step (struct rsp_buf *p_buf)
2918
{
2919
  uint32_t  addr;               /* The address to step from, if any */
2920
 
2921
  // Make sure the processor is stalled
2922
  gdb_ensure_or1k_stalled();
2923
 
2924
  // First set the chain 
2925
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2926
  if(err > 0){
2927
        printf("Error %d to set RISC Debug Interface chain in the STEP command 's'\n", err);
2928
    rsp_client_close ();
2929
    return;
2930
        }
2931
 
2932
  if (0 == strcmp ("s", p_buf->data))
2933
  {
2934
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
2935
    /* ---------- Npc ---------- */
2936
    addr = get_npc();
2937
    /*
2938
    err = gdb_read_reg(NPC_CPU_REG_ADD, &addr);
2939
    if(err > 0){
2940
      printf("Error %d to read NPC in the STEP command 's'\n", err);
2941
      rsp_client_close ();
2942
      return;
2943
    }
2944
    */
2945
  }
2946
  else if (1 != sscanf (p_buf->data, "s%x", &addr))
2947
  {
2948
    fprintf (stderr,
2949
             "Warning: RSP step address %s not recognized: ignored\n",
2950
             p_buf->data);
2951
 
2952
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
2953
    /* ---------- NPC ---------- */
2954
    addr = get_npc();
2955
    /*
2956
    err = gdb_read_reg(NPC_CPU_REG_ADD, &addr);
2957
    if(err > 0){
2958
      printf("Error %d to read NPC in the STEP command 's'\n", err);
2959
      rsp_client_close ();
2960
      return;
2961
    }
2962
    */
2963
  }
2964
 
2965
  //if (DEBUG_GDB) printf("rsp_step() --> Read NPC = 0x%08x\n", addr);
2966
  rsp_step_generic (addr, EXCEPT_NONE);
2967
 
2968
}       /* rsp_step () */
2969
 
2970
 
2971
/*---------------------------------------------------------------------------*/
2972
/*!Handle a RSP step with signal request
2973
 
2974
   Currently null. Will use the underlying generic step function.
2975
 
2976
   @param[in] p_buf  The full step with signal packet              */
2977
/*---------------------------------------------------------------------------*/
2978
static void
2979
rsp_step_with_signal (struct rsp_buf *p_buf)
2980
{
2981
  printf ("RSP step with signal '%s' received\n", p_buf->data);
2982
 
2983
}       /* rsp_step_with_signal () */
2984
 
2985
 
2986
/*---------------------------------------------------------------------------*/
2987
/*!Generic processing of a step request
2988
 
2989
   The signal may be EXCEPT_NONE if there is no exception to be
2990
   handled. Currently the exception is ignored.
2991
 
2992
   The single step flag is set in the debug registers and then the processor
2993
   is unstalled.
2994
 
2995
   @param[in] addr    Address from which to step
2996
   @param[in] except  The exception to use (if any)                          */
2997
/*---------------------------------------------------------------------------*/
2998
static void
2999
rsp_step_generic (uint32_t  addr,
3000
                  uint32_t  except)
3001
{
3002
  uint32_t              temp_uint32;
3003
 
3004
  /* Set the address as the value of the next program counter */
3005
 
3006
  set_npc (addr);
3007
 
3008
  /* Clear Debug Reason Register (DRR) 0x3015 */
3009
  // Arc sim --> cpu_state.sprs[SPR_DRR]   = 0;
3010
  if(gdb_write_reg(DRR_CPU_REG_ADD, 0)) printf("Error write to DRR register\n");
3011
 
3012
  /* Clear watchpoint break generation in Debug Mode Register 2 (DMR2) 0x3011 */
3013
  // Arc sim --> cpu_state.sprs[SPR_DMR2] &= ~SPR_DMR2_WGB;
3014
  if(gdb_read_reg(DMR2_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR2 register\n");
3015
  temp_uint32 &= ~SPR_DMR2_WGB;
3016
  if(gdb_write_reg(DMR2_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR2 register\n");
3017
 
3018
  /* Set the single step trigger in Debug Mode Register 1 (DMR1) Register 0x3010 */
3019
  // Arc sim --> cpu_state.sprs[SPR_DMR1] |= SPR_DMR1_ST;
3020
  if(gdb_read_reg(DMR1_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR1 register\n");
3021
  temp_uint32 |= SPR_DMR1_ST;
3022
  if(gdb_write_reg(DMR1_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR1 register\n");
3023
 
3024
  /* Set traps to be handled by the debug unit in the Debug Stop Register (DSR) Register 0x3014 */
3025
  // Arc sim --> cpu_state.sprs[SPR_DSR]  |= SPR_DSR_TE;
3026
  if(gdb_read_reg(DSR_CPU_REG_ADD, &temp_uint32)) printf("Error read from DSR register\n");
3027
  temp_uint32 |= SPR_DSR_TE;
3028
  if(gdb_write_reg(DSR_CPU_REG_ADD, temp_uint32)) printf("Error write to DSR register\n");
3029
 
3030
  /* Unstall the processor */
3031
  set_stall_state (0);
3032
 
3033
  /* Note the GDB client is now waiting for a reply. */
3034
  rsp.client_waiting = 1;
3035
 
3036
}       /* rsp_step_generic () */
3037
 
3038
 
3039
/*---------------------------------------------------------------------------*/
3040
/*!Handle a RSP 'v' packet
3041
 
3042
   These are commands associated with executing the code on the target
3043
 
3044
   @param[in] p_buf  The request                                               */
3045
/*---------------------------------------------------------------------------*/
3046
static void
3047
rsp_vpkt (struct rsp_buf *p_buf)
3048
{
3049
  if (0 == strncmp ("vAttach;", p_buf->data, strlen ("vAttach;")))
3050
    {
3051
      /* Attaching is a null action, since we have no other process. We just
3052
         return a stop packet (using TRAP) to indicate we are stopped. */
3053
      put_str_packet ("S05");
3054
      return;
3055
    }
3056
  else if (0 == strcmp ("vCont?", p_buf->data))
3057
    {
3058
      /* For now we don't support this. */
3059
      put_str_packet ("");
3060
      return;
3061
    }
3062
  else if (0 == strncmp ("vCont", p_buf->data, strlen ("vCont")))
3063
    {
3064
      /* This shouldn't happen, because we've reported non-support via vCont?
3065
         above */
3066
      fprintf (stderr, "Warning: RSP vCont not supported: ignored\n" );
3067
      return;
3068
    }
3069
  else if (0 == strncmp ("vFile:", p_buf->data, strlen ("vFile:")))
3070
    {
3071
      /* For now we don't support this. */
3072
      fprintf (stderr, "Warning: RSP vFile not supported: ignored\n" );
3073
      put_str_packet ("");
3074
      return;
3075
    }
3076
  else if (0 == strncmp ("vFlashErase:", p_buf->data, strlen ("vFlashErase:")))
3077
    {
3078
      /* For now we don't support this. */
3079
      fprintf (stderr, "Warning: RSP vFlashErase not supported: ignored\n" );
3080
      put_str_packet ("E01");
3081
      return;
3082
    }
3083
  else if (0 == strncmp ("vFlashWrite:", p_buf->data, strlen ("vFlashWrite:")))
3084
    {
3085
      /* For now we don't support this. */
3086
      fprintf (stderr, "Warning: RSP vFlashWrite not supported: ignored\n" );
3087
      put_str_packet ("E01");
3088
      return;
3089
    }
3090
  else if (0 == strcmp ("vFlashDone", p_buf->data))
3091
    {
3092
      /* For now we don't support this. */
3093
      fprintf (stderr, "Warning: RSP vFlashDone not supported: ignored\n" );
3094
      put_str_packet ("E01");
3095
      return;
3096
    }
3097
  else if (0 == strncmp ("vRun;", p_buf->data, strlen ("vRun;")))
3098
    {
3099
      /* We shouldn't be given any args, but check for this */
3100
      if (p_buf->len > (int) strlen ("vRun;"))
3101
        {
3102
          fprintf (stderr, "Warning: Unexpected arguments to RSP vRun "
3103
                   "command: ignored\n");
3104
        }
3105
 
3106
      /* Restart the current program. However unlike a "R" packet, "vRun"
3107
         should behave as though it has just stopped. We use signal
3108
         5 (TRAP). */
3109
      rsp_restart ();
3110
      put_str_packet ("S05");
3111
    }
3112
  else
3113
    {
3114
      fprintf (stderr, "Warning: Unknown RSP 'v' packet type %s: ignored\n",
3115
               p_buf->data);
3116
      put_str_packet ("E01");
3117
      return;
3118
    }
3119
}       /* rsp_vpkt () */
3120
 
3121
 
3122
/*---------------------------------------------------------------------------*/
3123
/*!Handle a RSP write memory (binary) request
3124
 
3125
   Syntax is:
3126
 
3127
     X<addr>,<length>:
3128
 
3129
   Followed by the specified number of bytes as raw binary. Response should be
3130
   "OK" if all copied OK, E<nn> if error <nn> has occurred.
3131
 
3132
   The length given is the number of bytes to be written. However the number
3133
   of data bytes may be greater, since '#', '$' and '}' are escaped by
3134
   preceding them by '}' and oring with 0x20.
3135
 
3136
   @param[in] p_buf  The command received                                      */
3137
/*---------------------------------------------------------------------------*/
3138
static void
3139
rsp_write_mem_bin (struct rsp_buf *p_buf)
3140
{
3141
  unsigned int  addr;                   /* Where to write the memory */
3142
  int           len;                    /* Number of bytes to write */
3143 46 julius
  char          *bindat, *bindat_ptr;   /* Pointer to the binary data */
3144 40 julius
  int           off = 0; /* Offset to start of binary data */
3145
  int           newlen;         /* Number of bytes in bin data */
3146 46 julius
  int i;
3147
  int bytes_per_word = 4; /* Current OR implementation is 4-byte words */
3148 40 julius
 
3149
  if (2 != sscanf (p_buf->data, "X%x,%x:", &addr, &len))
3150
    {
3151
      fprintf (stderr, "Warning: Failed to recognize RSP write memory "
3152
               "command: %s\n", p_buf->data);
3153
      put_str_packet ("E01");
3154
      return;
3155
    }
3156
 
3157
  /* Find the start of the data and "unescape" it */
3158
  bindat = p_buf->data;
3159
  while(off < GDB_BUF_MAX){
3160
        if(bindat[off] == ':'){
3161
                        bindat = bindat + off + 1;
3162
                        off++;
3163
                        break;
3164
                }
3165
                off++;
3166
  }
3167
        if(off >= GDB_BUF_MAX){
3168
          put_str_packet ("E01");
3169
                return;
3170
        }
3171
 
3172
  newlen = rsp_unescape (bindat, p_buf->len - off);
3173
 
3174
  /* Sanity check */
3175
  if (newlen != len)
3176
  {
3177
    int  minlen = len < newlen ? len : newlen;
3178
 
3179
    fprintf (stderr, "Warning: Write of %d bytes requested, but %d bytes "
3180
       "supplied. %d will be written\n", len, newlen, minlen);
3181
    len = minlen;
3182
  }
3183
 
3184
  // Make sure the processor is stalled
3185
  gdb_ensure_or1k_stalled();
3186
 
3187
  // Set chain 5 --> Wishbone Memory chain
3188
  err = gdb_set_chain(SC_WISHBONE);
3189
  if(err){
3190
    put_str_packet ("E01");
3191
    return;
3192
  }
3193
 
3194
  /* Write the bytes to memory */
3195
  if (len)
3196
    {
3197
      swap_buf(bindat, len);
3198
 
3199
      if (DEBUG_GDB_BLOCK_DATA){
3200
        uint32_t  temp_uint32;
3201
        for (off = 0; off < len; off++){
3202
          temp_uint32 = (temp_uint32 << 8) | (0x000000ff & bindat[off]);
3203
          if((off %4 ) == 3){
3204
            temp_uint32 = htonl(temp_uint32);
3205
          }
3206
          switch(off % 16)
3207
            {
3208
            case 3:
3209
              printf("Add 0x%08x   Data 0x%08x  ", addr + off - 3, temp_uint32);
3210
              break;
3211
            case 7:
3212
            case 11:
3213
              printf("0x%08x  ", temp_uint32);
3214
              break;
3215
            case 15:
3216
              printf("0x%08x\n", temp_uint32);
3217
              break;
3218
            default:
3219
              break;
3220
            }
3221
          if ((len - off == 1) && (off % 16) < 15) printf("\n");
3222
        }
3223
      }
3224 46 julius
 
3225
      bindat_ptr = bindat; // Copy of this pointer so we don't trash it
3226
      if (addr & 0x3) // not perfectly aligned at beginning - fix
3227
        {
3228
          if (DEBUG_GDB) printf("rsp_write_mem_bin: address not word aligned: 0x%.8x\n", addr);fflush (stdout);
3229
          // Write enough to align us
3230
          int bytes_to_write = bytes_per_word - (addr&0x3);
3231
          if (bytes_to_write > len) bytes_to_write = len; // case of writing 1 byte to adr 0x1
3232
          if (DEBUG_GDB) printf("rsp_write_mem_bin: writing %d bytes of len (%d)\n",
3233
                                bytes_to_write, len);fflush (stdout);
3234
 
3235
          for (i=0;i<bytes_to_write;i++) err = gdb_write_byte(addr+i, (uint8_t) bindat_ptr[i]);
3236
          addr += bytes_to_write; bindat_ptr += bytes_to_write; len -= bytes_to_write;
3237
          if (DEBUG_GDB) printf("rsp_write_mem_bin: address should now be word aligned: 0x%.8x\n", addr);fflush (stdout);
3238
 
3239
        }
3240
      if ((len > 3) && !err) // now write full words, if we can
3241
        {
3242
          int words_to_write = len/bytes_per_word;
3243
          if (DEBUG_GDB) printf("rsp_write_mem_bin: writing %d words from 0x%x, len %d bytes\n",
3244
                                words_to_write, addr, len);fflush (stdout);
3245
 
3246
          err = gdb_write_block(addr, (uint32_t*)bindat_ptr, (words_to_write*bytes_per_word));
3247
          addr+=(words_to_write*bytes_per_word); bindat_ptr+=(words_to_write*bytes_per_word);
3248
          len-=(words_to_write*bytes_per_word);
3249
        }
3250
      if (len && !err)  // leftover words. Write them out
3251
        {
3252
          if (DEBUG_GDB) printf("rsp_write_mem_bin: writing remainder %d bytes to 0x%.8x\n",
3253
                                len, addr);fflush (stdout);
3254
 
3255
          for (i=0;i<len;i++) err = gdb_write_byte(addr+i, (uint8_t) bindat_ptr[i]);
3256
        }
3257 40 julius
      if(err){
3258
        put_str_packet ("E01");
3259
        return;
3260
      }
3261
      if (DEBUG_GDB) printf("Error %x\n", err);fflush (stdout);
3262
    }
3263
  put_str_packet ("OK");
3264
 
3265
}       /* rsp_write_mem_bin () */
3266
 
3267
 
3268
/*---------------------------------------------------------------------------*/
3269
/*!Handle a RSP remove breakpoint or matchpoint request
3270
 
3271
   For now only memory breakpoints are implemented, which are implemented by
3272
   substituting a breakpoint at the specified address. The implementation must
3273
   cope with the possibility of duplicate packets.
3274
 
3275
   @todo This doesn't work with icache/immu yet
3276
 
3277
   @param[in] p_buf  The command received                                      */
3278
/*---------------------------------------------------------------------------*/
3279
static void
3280
rsp_remove_matchpoint (struct rsp_buf *p_buf)
3281
{
3282
  enum mp_type       type;              /* What sort of matchpoint */
3283
  uint32_t  addr;               /* Address specified */
3284
  int                len;                       /* Matchpoint length (not used) */
3285
  struct mp_entry   *mpe;                       /* Info about the replaced instr */
3286
 
3287
  /* Break out the instruction */
3288
  if (3 != sscanf (p_buf->data, "z%1d,%x,%1d", (int *)&type, &addr, &len))
3289
    {
3290
      fprintf (stderr, "Warning: RSP matchpoint deletion request not "
3291
               "recognized: ignored\n");
3292
      put_str_packet ("E01");
3293
      return;
3294
    }
3295
 
3296
  /* Sanity check that the length is 4 */
3297
  if (4 != len)
3298
    {
3299
      fprintf (stderr, "Warning: RSP matchpoint deletion length %d not "
3300
               "valid: 4 assumed\n", len);
3301
      len = 4;
3302
    }
3303
 
3304
  /* Sort out the type of matchpoint */
3305
  switch (type)
3306
    {
3307
    case BP_MEMORY:
3308
      /* Memory breakpoint - replace the original instruction. */
3309
      mpe = mp_hash_delete (type, addr);
3310
 
3311
      /* If the BP hasn't yet been deleted, put the original instruction
3312
                         back. Don't forget to free the hash table entry afterwards. */
3313
                        if (NULL != mpe)
3314
                        {
3315
                          // Arc Sim Code -->   set_program32 (addr, mpe->instr);
3316
                          // Make sure the processor is stalled
3317
                          gdb_ensure_or1k_stalled();
3318
 
3319
                          // Set chain 5 --> Wishbone Memory chain
3320
                          err = gdb_set_chain(SC_WISHBONE);
3321
                                if(err){
3322
                                        put_str_packet ("E01");
3323
                                  return;
3324
                                }
3325
                                err = gdb_write_block(addr, &mpe->instr, 4);
3326
                                if(err){
3327
                                        put_str_packet ("E01");
3328
                                  return;
3329
                                }
3330
                          free (mpe);
3331
                        }
3332
 
3333
      put_str_packet ("OK");
3334
      return;
3335
 
3336
    case BP_HARDWARE:
3337
      put_str_packet ("");              /* Not supported */
3338
      return;
3339
 
3340
    case WP_WRITE:
3341
      put_str_packet ("");              /* Not supported */
3342
      return;
3343
 
3344
    case WP_READ:
3345
      put_str_packet ("");              /* Not supported */
3346
      return;
3347
 
3348
    case WP_ACCESS:
3349
      put_str_packet ("");              /* Not supported */
3350
      return;
3351
 
3352
    default:
3353
      fprintf (stderr, "Warning: RSP matchpoint type %d not "
3354
               "recognized: ignored\n", type);
3355
      put_str_packet ("E01");
3356
      return;
3357
 
3358
    }
3359
}       /* rsp_remove_matchpoint () */
3360
 
3361
 
3362
/*---------------------------------------------------------------------------*/
3363
/*!Handle a RSP insert breakpoint or matchpoint request
3364
 
3365
   For now only memory breakpoints are implemented, which are implemented by
3366
   substituting a breakpoint at the specified address. The implementation must
3367
   cope with the possibility of duplicate packets.
3368
 
3369
   @todo This doesn't work with icache/immu yet
3370
 
3371
   @param[in] p_buf  The command received                                      */
3372
/*---------------------------------------------------------------------------*/
3373
static void
3374
rsp_insert_matchpoint (struct rsp_buf *p_buf)
3375
{
3376
  enum mp_type       type;              /* What sort of matchpoint */
3377
  uint32_t  addr;               /* Address specified */
3378
  int                len;               /* Matchpoint length (not used) */
3379
        uint32_t      instr;
3380
 
3381
  /* Break out the instruction */
3382
  if (3 != sscanf (p_buf->data, "Z%1d,%x,%1d", (int *)&type, &addr, &len))
3383
    {
3384
      fprintf (stderr, "Warning: RSP matchpoint insertion request not "
3385
               "recognized: ignored\n");
3386
      put_str_packet ("E01");
3387
      return;
3388
    }
3389
 
3390
  /* Sanity check that the length is 4 */
3391
  if (4 != len)
3392
    {
3393
      fprintf (stderr, "Warning: RSP matchpoint insertion length %d not "
3394
               "valid: 4 assumed\n", len);
3395
      len = 4;
3396
    }
3397
 
3398
  /* Sort out the type of matchpoint */
3399
  switch (type)
3400
    {
3401
    case BP_MEMORY:             // software-breakpoint Z0  break
3402
      /* Memory breakpoint - substitute a TRAP instruction */
3403
      // Make sure the processor is stalled
3404
      gdb_ensure_or1k_stalled();
3405
 
3406
      // Set chain 5 --> Wishbone Memory chain
3407
      gdb_set_chain(SC_WISHBONE);
3408
 
3409
      // Read the data from Wishbone Memory chain
3410
      // Arc Sim Code -->   mp_hash_add (type, addr, eval_direct32 (addr, 0, 0));
3411
      gdb_read_block(addr, &instr, 4);
3412
 
3413
      mp_hash_add (type, addr, instr);
3414
 
3415
      // Arc Sim Code -->   set_program32 (addr, OR1K_TRAP_INSTR);
3416
      instr = OR1K_TRAP_INSTR;
3417
      err = gdb_write_block(addr, &instr, 4);
3418
      if(err){
3419
        put_str_packet ("E01");
3420
        return;
3421
      }
3422
      put_str_packet ("OK");
3423
      return;
3424
 
3425
    case BP_HARDWARE:   // hardware-breakpoint Z1  hbreak
3426
      put_str_packet ("");              /* Not supported */
3427
      return;
3428
 
3429
    case WP_WRITE:              // write-watchpoint    Z2  watch                                                                                                                                                        
3430
      put_str_packet ("");              /* Not supported */
3431
      return;
3432
 
3433
    case WP_READ:                       // read-watchpoint     Z3  rwatch
3434
      put_str_packet ("");              /* Not supported */
3435
      return;
3436
 
3437
    case WP_ACCESS:             // access-watchpoint   Z4  awatch
3438
      put_str_packet ("");              /* Not supported */
3439
      return;
3440
 
3441
    default:
3442
      fprintf (stderr, "Warning: RSP matchpoint type %d not "
3443
               "recognized: ignored\n", type);
3444
      put_str_packet ("E01");
3445
      return;
3446
    }
3447
}       /* rsp_insert_matchpoint () */
3448
 
3449
 
3450
/*---------------------------------------------------------------------------
3451
Setup the or32 to init state
3452
 
3453
---------------------------------------------------------------------------*/
3454
void setup_or32(void)
3455
{
3456
  uint32_t              temp_uint32;
3457
  // First set the chain 
3458
  err = gdb_set_chain(SC_REGISTER);     /* 4 Register Chain */
3459
  if(err > 0){
3460
    if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
3461
  }
3462
  if(gdb_read_reg(0x04, &temp_uint32)) printf("Error read from register\n");
3463
  if (DEBUG_GDB) printf("Read from chain 4 SC_REGISTER at add 0x00000004 = 0x%08x\n", temp_uint32 );
3464
 
3465
  if(gdb_write_reg(0x04, 0x00000001)) printf("Error write to register\n");
3466
  if (DEBUG_GDB) printf("Write to chain 4 SC_REGISTER at add 0x00000004 = 0x00000001\n");
3467
 
3468
  //    if(gdb_read_reg(0x04, &temp_uint32)) printf("Error read from register\n");
3469
  //    if (DEBUG_GDB) printf("Read from chain 4 SC_REGISTER at add 0x00000004 = 0x%08x\n", temp_uint32 );                      
3470
 
3471
  //    if(gdb_read_reg(0x04, &temp_uint32)) printf("Error read from register\n");
3472
  //    if (DEBUG_GDB) printf("Read from chain 4 SC_REGISTER at add 0x00000004 = 0x%08x\n", temp_uint32 );                      
3473
 
3474
  if(gdb_write_reg(0x00, 0x01000001)) printf("Error write to register\n");
3475
  if (DEBUG_GDB) printf("Write to chain 4 SC_REGISTER at add 0x00000000 = 0x01000001\n");
3476
}
3477
 
3478
// Function to check if the processor is stalled - if not then stall it.
3479
// this is useful in the event that GDB thinks the processor is stalled, but has, in fact
3480
// been hard reset on the board and is running.
3481
static void gdb_ensure_or1k_stalled()
3482
{
3483
  // Disable continual checking that the or1k is stalled
3484
#ifdef ENABLE_OR1K_STALL_CHECK
3485
  unsigned char stalled;
3486
  dbg_cpu0_read_ctrl(0, &stalled);
3487
  if ((stalled & 0x1) != 0x1)
3488
    {
3489
      if (DEBUG_GDB)
3490
        printf("Processor not stalled, like we thought\n");
3491
 
3492
      // Set the TAP controller to its OR1k chain
3493
      dbg_set_tap_ir(JI_DEBUG);
3494
      gdb_chain = -1;
3495
 
3496
      // Processor isn't stalled, contrary to what we though, so stall it
3497
      printf("Stalling or1k\n");
3498
      dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
3499
    }
3500
#endif
3501
  return;
3502
}
3503
 
3504
 
3505
int gdb_read_reg(uint32_t adr, uint32_t *data) {
3506 46 julius
  if (DEBUG_CMDS) printf("rreg %d\n", gdb_chain);
3507 40 julius
  switch (gdb_chain) {
3508
  case SC_RISC_DEBUG: return dbg_cpu0_read(adr, data) ? ERR_CRC : ERR_NONE;
3509
  case SC_REGISTER:   return dbg_cpu0_read_ctrl(adr, (unsigned char*)data) ?
3510
      ERR_CRC : ERR_NONE;
3511
  case SC_WISHBONE:   return dbg_wb_read32(adr, data) ? ERR_CRC : ERR_NONE;
3512
  case SC_TRACE:      *data = 0; return 0;
3513
  default:            return JTAG_PROXY_INVALID_CHAIN;
3514
  }
3515
}
3516
 
3517 46 julius
int gdb_write_byte(uint32_t adr, uint8_t data) {
3518
  if (DEBUG_CMDS) printf("wbyte %d\n", gdb_chain);
3519
  switch (gdb_chain) {
3520
  case SC_WISHBONE:   return dbg_wb_write8(adr, data) ?
3521
      ERR_CRC : ERR_NONE;
3522
  default:            return JTAG_PROXY_INVALID_CHAIN;
3523
  }
3524
}
3525
 
3526
 int gdb_write_short(uint32_t adr, uint16_t data) {
3527
   if (DEBUG_CMDS) printf("wshort %d\n", gdb_chain); fflush (stdout);
3528
   switch (gdb_chain) {
3529
  case SC_WISHBONE:   return dbg_wb_write16(adr, data) ?
3530
      ERR_CRC : ERR_NONE;
3531
  default:            return JTAG_PROXY_INVALID_CHAIN;
3532
  }
3533
}
3534
 
3535 40 julius
int gdb_write_reg(uint32_t adr, uint32_t data) {
3536 46 julius
  if (DEBUG_CMDS) printf("wreg %d\n", gdb_chain); fflush (stdout);
3537 40 julius
  switch (gdb_chain) { /* remap registers, to be compatible with jp1 */
3538
  case SC_RISC_DEBUG: if (adr == JTAG_RISCOP) adr = 0x00;
3539
    return dbg_cpu0_write(adr, data) ? ERR_CRC : ERR_NONE;
3540
  case SC_REGISTER:   return dbg_cpu0_write_ctrl(adr, data) ? ERR_CRC : ERR_NONE;
3541
  case SC_WISHBONE:   return dbg_wb_write32(adr, data) ? ERR_CRC : ERR_NONE;
3542
  case SC_TRACE:      return 0;
3543
  default:            return JTAG_PROXY_INVALID_CHAIN;
3544
  }
3545
}
3546
 
3547
int gdb_read_block(uint32_t adr, uint32_t *data, int len) {
3548 46 julius
  if (DEBUG_CMDS) printf("rb %d\n", gdb_chain); fflush (stdout);
3549 40 julius
  switch (gdb_chain) {
3550
  case SC_WISHBONE:   return dbg_wb_read_block32(adr, data, len) ?
3551
      ERR_CRC : ERR_NONE;
3552
  default:            return JTAG_PROXY_INVALID_CHAIN;
3553
  }
3554
}
3555
 
3556
int gdb_write_block(uint32_t adr, uint32_t *data, int len) {
3557 46 julius
  if (DEBUG_CMDS) printf("wb %d\n", gdb_chain); fflush (stdout);
3558 40 julius
  switch (gdb_chain) {
3559
  case SC_WISHBONE:   return dbg_wb_write_block32(adr, data, len) ?
3560
      ERR_CRC : ERR_NONE;
3561
  default:            return JTAG_PROXY_INVALID_CHAIN;
3562
  }
3563
}
3564
 
3565
int gdb_set_chain(int chain) {
3566
  int rv;
3567
  switch (chain) {
3568
  case SC_RISC_DEBUG:
3569
  case SC_REGISTER:
3570
  case SC_TRACE:
3571
  case SC_WISHBONE:   gdb_chain = chain;
3572
    rv = ERR_NONE;
3573
    break;
3574
  default:            rv = JTAG_PROXY_INVALID_CHAIN;
3575
    break;
3576
  }
3577
  return rv;
3578
}
3579
 
3580
 
3581
/*****************************************************************************
3582
* Close the connection to the client if it is open
3583
******************************************************************************/
3584
static void client_close (char err)
3585
{
3586
  if(gdb_fd) {
3587
    perror("gdb socket - " + err);
3588
    close(gdb_fd);
3589
    gdb_fd = 0;
3590
  }
3591
}       /* client_close () */
3592
 
3593
 
3594
/*---------------------------------------------------------------------------*/
3595
/* Swap a buffer of 4-byte from 1234 to 4321
3596
 
3597
   parameter[in]  p_buf and len
3598
   parameter[out] none                                                                                                                                                                                                                   */
3599
/*---------------------------------------------------------------------------*/
3600
static void swap_buf(char* p_buf, int len)
3601
{
3602
        int temp;
3603
        int n = 0;
3604
 
3605
  if (len > 2)
3606
  {
3607
    while(n < len){
3608
        // swap 0 and 3
3609
        temp = p_buf[n];
3610
                        p_buf[n] = p_buf[n + 3];
3611
                        p_buf[n + 3] = temp;
3612
        // swap 1 and 2
3613
        temp = p_buf[n + 1];
3614
                        p_buf[n + 1] = p_buf[n + 2];
3615
                        p_buf[n + 2] = temp;
3616
 
3617
                        n += 4;
3618
    }
3619
        }
3620
}
3621
 
3622
 
3623
/*---------------------------------------------------------------------------*/
3624
/*!Set the stall state of the processor
3625
 
3626
   @param[in] state  If non-zero stall the processor.                        */
3627
/*---------------------------------------------------------------------------*/
3628
static void
3629
set_stall_state (int state)
3630
{
3631
 
3632
  if(state == 0)
3633
    {
3634
      err = dbg_cpu0_write_ctrl(0, 0);     /* unstall or1k */
3635
      stallState = UNSTALLED;
3636
      npcIsCached = 0;
3637
      rsp.sigval = TARGET_SIGNAL_NONE;
3638
    }
3639
  else
3640
    {
3641
      err = dbg_cpu0_write_ctrl(0, 0x01);                                 /* stall or1k */
3642
      stallState = STALLED;
3643
    }
3644
 
3645
  if(err > 0 && DEBUG_GDB)printf("Error %d in set_stall_state Stall state = %d\n", err, state);
3646
 
3647
}       /* set_stall_state () */
3648
 
3649
 
3650
/*---------------------------------------------------------------------------*/
3651
/*!Set the reset bit of the processor's control reg in debug interface
3652
 */
3653
/*---------------------------------------------------------------------------*/
3654
static void
3655
reset_or1k (void)
3656
{
3657
 
3658
  err = dbg_cpu0_write_ctrl(0, 0x02);  /* reset or1k */
3659
 
3660
  if(err > 0 && DEBUG_GDB)printf("Error %d in reset_or1k()\n", err);
3661
 
3662
}       /* reset_or1k () */
3663
 
3664
/*---------------------------------------------------------------------------*/
3665
/*!Close down the connection with GDB in the event of a kill signal
3666
 
3667
 */
3668
/*---------------------------------------------------------------------------*/
3669
void gdb_close()
3670
{
3671
  rsp_client_close();
3672
  client_close('0');
3673
  // Maybe do other things here!
3674
}

powered by: WebSVN 2.1.0

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