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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [remote-or1k.c] - Blame information for rev 373

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

Line No. Rev Author Line
1 106 markom
/* Remote debugging interface for various or1k debugging protocols.
2 118 markom
   Currently supported or1k targets are: simulator, jtag, dummy.
3
 
4 106 markom
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
5
   Contributed by Cygnus Support.  Written by Marko Mlinar
6
   <markom@opencores.org>
7
 
8
   This file is part of GDB.
9
 
10
   This program is free software; you can redistribute it and/or modify
11
   it under the terms of the GNU General Public License as published by
12
   the Free Software Foundation; either version 2 of the License, or
13
   (at your option) any later version.
14
 
15
   This program is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
   GNU General Public License for more details.
19
 
20
   You should have received a copy of the GNU General Public License
21
   along with this program; if not, write to the Free Software
22
   Foundation, Inc., 59 Temple Place - Suite 330,
23
   Boston, MA 02111-1307, USA.  */
24
 
25
#include "defs.h"
26
#include "inferior.h"
27
#include "bfd.h"
28
#include "symfile.h"
29
#include "gdb_wait.h"
30
#include "gdbcmd.h"
31
#include "gdbcore.h"
32
#include "target.h"
33
#include "remote-utils.h"
34
#include "gdb_string.h"
35
#include "tm.h"
36
 
37
#include <signal.h>
38
#include <sys/types.h>
39
#include <sys/stat.h>
40
#include <sys/ioctl.h>
41
#include <fcntl.h>
42
 
43 362 markom
#define debug if (remote_debug) printf_unfiltered
44
 
45 125 chris
/* The following prototype is necessary or the compiler will not
46
   correctly promote the data argument to ULONGEST */
47
static void or1k_write_reg (unsigned int, ULONGEST);
48 372 markom
static int insn_has_delay_slot (unsigned int);
49 125 chris
 
50 118 markom
/* JTAG or1k target ops.  */
51 106 markom
extern void jtag_init PARAMS ((char * args));
52 122 markom
extern ULONGEST jtag_read_reg PARAMS ((unsigned int regno));
53
extern void jtag_write_reg PARAMS ((unsigned int regno, ULONGEST data));
54 106 markom
extern void jtag_done PARAMS ((void));
55 143 chris
extern int jtag_read_block PARAMS ((unsigned int regno, void* block, int nRegisters));
56
extern int jtag_write_block PARAMS ((unsigned int regno, void* block, int nRegisters));
57 113 markom
extern void jtag_set_chain PARAMS ((int chain));
58 106 markom
struct target_ops or1k_jtag_ops;
59
static struct or1k_target_ops or1k_target_jtag =
60
  {
61
    "jtag",
62
    jtag_init,
63
    jtag_done,
64
    jtag_read_reg,
65
    jtag_write_reg,
66 143 chris
    jtag_read_block,
67
    jtag_write_block,
68 113 markom
    jtag_set_chain,
69 106 markom
    NULL,
70
    &or1k_jtag_ops,
71
    OPS_MAGIC
72
  };
73
 
74 118 markom
/* simulator or1k target ops.  */
75 106 markom
struct target_ops or1k_sim_ops;
76
static struct or1k_target_ops or1k_target_sim =
77
  {
78
    "simulator",
79
    NULL,
80
    NULL,
81
    NULL,
82
    NULL,
83
    NULL,
84
    NULL,
85 143 chris
    NULL,
86
    NULL,
87 106 markom
    &or1k_sim_ops,
88
    OPS_MAGIC
89
  };
90
 
91 118 markom
/* dummy or1k target ops.  */
92 106 markom
struct target_ops or1k_dummy_ops;
93
static struct or1k_target_ops or1k_target_dummy =
94
  {
95
    "dummy",
96
    NULL,
97
    NULL,
98
    NULL,
99
    NULL,
100
    NULL,
101
    NULL,
102 143 chris
    NULL,
103
    NULL,
104 106 markom
    &or1k_dummy_ops,
105
    OPS_MAGIC
106
  };
107
 
108
const char *str_err[] =
109
  {
110
    "None", "CRC error"
111
  };
112
 
113
const char *status_name[] =
114
  {
115
    "UNDEFINED", "CONNECTING", "DISCONNECTING", "RUNNING", "STOPPED"
116
  };
117
 
118 113 markom
/* Names for matchpoint related stuff.  */
119
static char *ct_names[] =
120
  {
121
    "DIS", "IFEA", "LEA", "SEA", "AEA", "LDATA", "SDATA", "ADATA"
122
  };
123
 
124
static char *cc_names[] =
125
  {
126
    "&", "==", "<", "<=", ">", ">=", "!=", "ERR"
127
  };
128
 
129
static char *ch_names[] =
130
  {
131
    "ERR", "&", "|", "ERR"
132
  };
133
 
134 106 markom
/* Implementation specific information.  Set by or1k_initialize.  */
135
struct struct_or1k_implementation or1k_implementation;
136
 
137
/* Current target status.  */
138
static enum target_status or1k_status = TARGET_UNDEFINED;
139
 
140
/* The target vector.  */
141
struct target_ops or1k_dummy_ops, or1k_jtag_ops, or1k_sim_ops;
142
 
143
/* Currently active target description (if or1k_is_open == 1) */
144
static struct target_ops *current_ops;
145
 
146
/* Currently active or1k target operations.  */
147
static struct or1k_target_ops *current_or1k_target = NULL;
148
 
149
/* Set to 1 if the target is open.  */
150
static int or1k_is_open = 0;
151
 
152
/* Error last occured, zero = ok.  */
153 118 markom
int err = 0;
154 106 markom
 
155 362 markom
/* Nonzero, if we changed something (except DMR1 which is updated on every run anyway).  */
156 118 markom
int debug_regs_changed;
157
 
158 106 markom
/* Number of interrupts while waiting for process.  */
159
static int interrupt_count = 0;
160
 
161 113 markom
/* Reason of last stop.  */
162
static int hit_watchpoint = 0;
163
 
164 106 markom
/* Current register values.  */
165 118 markom
unsigned int dmr1 = 0;
166
unsigned int dmr2 = 0;
167
unsigned int dsr = 0;
168
unsigned int drr = 0;
169 373 markom
//unsigned int npc = 0;
170
//unsigned int ppc = 0;
171 106 markom
 
172 118 markom
/* Current matchpoints.  */
173
unsigned int dvr[MAX_MATCHPOINTS];
174
struct dcr_struct dcr[MAX_MATCHPOINTS];
175 106 markom
 
176 362 markom
/* Number of matchpoint users */
177 118 markom
int matchpoint_user_count[MAX_MATCHPOINTS] = {0};
178
 
179 362 markom
/* Old SIGINT handler.  */
180
static void (*ofunc) PARAMS ((int));
181
 
182
 
183 106 markom
/* Handle low-level error that we can't recover from.  Note that just
184
   error()ing out from target_wait or some such low-level place will cause
185
   all hell to break loose--the rest of GDB will tend to get left in an
186
   inconsistent state.  */
187
 
188
static NORETURN void
189
or1k_error (char *string,...)
190
{
191
  va_list args;
192
 
193
  va_start (args, string);
194
 
195
  target_terminal_ours ();
196
  wrap_here ("");               /* Force out any buffered output */
197
  gdb_flush (gdb_stdout);
198
  if (error_pre_print)
199
    fprintf_filtered (gdb_stderr, error_pre_print);
200
  vfprintf_filtered (gdb_stderr, string, args);
201
  fprintf_filtered (gdb_stderr, "\n");
202
  va_end (args);
203
  gdb_flush (gdb_stderr);
204
 
205
  /* Clean up in such a way that or1k_close won't try to talk to the
206
     board (it almost surely won't work since we weren't able to talk to
207
     it).  */
208
  or1k_is_open = 0;
209
 
210
  printf_unfiltered ("Ending remote or1k debugging.\n");
211
  target_mourn_inferior ();
212
 
213
  return_to_top_level (RETURN_ERROR);
214
}
215
 
216
const char *
217
or1k_err_name (e)
218
     int e;
219
{
220
  return str_err[e];
221
}
222
 
223
/* putc_readable - print a character, displaying non-printable chars in
224
   ^x notation or in hex.  */
225
 
226
static void
227
fputc_readable (ch, file)
228
     int ch;
229
     struct ui_file *file;
230
{
231
  if (ch == '\n')
232
    fputc_unfiltered ('\n', file);
233
  else if (ch == '\r')
234
    fprintf_unfiltered (file, "\\r");
235
  else if (ch < 0x20)           /* ASCII control character */
236
    fprintf_unfiltered (file, "^%c", ch + '@');
237
  else if (ch >= 0x7f)          /* non-ASCII characters (rubout or greater) */
238
    fprintf_unfiltered (file, "[%02x]", ch & 0xff);
239
  else
240
    fputc_unfiltered (ch, file);
241
}
242
 
243
 
244
/* puts_readable - print a string, displaying non-printable chars in
245
   ^x notation or in hex.  */
246
 
247
static void
248
fputs_readable (string, file)
249
     char *string;
250
     struct ui_file *file;
251
{
252
  int c;
253
 
254
  while ((c = *string++) != '\0')
255
    fputc_readable (c, file);
256
}
257
 
258 113 markom
/* Sets scan chain.  */
259
 
260
static void
261
or1k_set_chain (chain)
262
     int chain;
263
{
264
  if (current_or1k_target != NULL && current_or1k_target->to_set_chain != NULL)
265
    current_or1k_target->to_set_chain (chain);
266
}
267
 
268 106 markom
/* Sets register/memory regno to data.  */
269
 
270 113 markom
static void
271 106 markom
or1k_write_reg (regno, data)
272
     unsigned int regno;
273 122 markom
     ULONGEST data;
274 106 markom
{
275 113 markom
  if (current_or1k_target != NULL && current_or1k_target->to_write_reg != NULL)
276
    current_or1k_target->to_write_reg (regno, data);
277 106 markom
}
278
 
279
/* Reads register/memory from regno.  */
280
 
281 122 markom
static ULONGEST
282 106 markom
or1k_read_reg (regno)
283
     unsigned int regno;
284
{
285 113 markom
  if (current_or1k_target != NULL && current_or1k_target->to_read_reg != NULL)
286
    return current_or1k_target->to_read_reg (regno);
287 106 markom
  else
288
    return 0x1234;
289
}
290
 
291 113 markom
/* Sets SPR register regno to data.  */
292
 
293
void
294
or1k_write_spr_reg (regno, data)
295
     unsigned int regno;
296
     unsigned int data;
297
{
298
  or1k_set_chain (SC_RISC_DEBUG);
299 362 markom
  or1k_write_reg (regno, (ULONGEST)data);
300 113 markom
}
301
 
302
/* Reads register SPR from regno.  */
303
 
304
unsigned int
305
or1k_read_spr_reg (regno)
306
     unsigned int regno;
307
{
308
  or1k_set_chain (SC_RISC_DEBUG);
309 362 markom
  return or1k_read_reg (regno);
310 113 markom
}
311
 
312 362 markom
/* Sets mem to data.  */
313
 
314
void
315
or1k_write_mem (addr, data)
316
     unsigned int addr;
317
     unsigned int data;
318
{
319
  or1k_set_chain (SC_WISHBONE);
320
  or1k_write_reg (addr, (ULONGEST)data);
321
}
322
 
323
/* Reads register SPR from regno.  */
324
 
325
unsigned int
326
or1k_read_mem (addr)
327
     unsigned int addr;
328
{
329
  or1k_set_chain (SC_WISHBONE);
330
  return or1k_read_reg (addr);
331
}
332
 
333 106 markom
/* Stalls the CPU.  */
334
 
335
static void
336
or1k_stall ()
337
{
338 113 markom
  int val;
339
  or1k_set_chain (SC_REGISTER);
340
  val = or1k_read_reg (JTAG_RISCOP);
341
  or1k_write_reg (JTAG_RISCOP, val | 1);
342 362 markom
  or1k_read_reg (JTAG_RISCOP);
343 118 markom
 
344
  /* Be cautious - disable trace.  */
345
  val = or1k_read_reg (JTAG_MODER);
346
  or1k_write_reg (JTAG_MODER, val & ~2);
347 106 markom
}
348
 
349
/* Unstalls the CPU.  */
350
 
351
static void
352
or1k_unstall ()
353
{
354 113 markom
  unsigned int val;
355 143 chris
 
356 207 chris
 
357 113 markom
  or1k_set_chain (SC_REGISTER);
358
  val = or1k_read_reg (JTAG_RISCOP);
359
  or1k_write_reg (JTAG_RISCOP, val & ~1);
360 362 markom
  or1k_read_reg (JTAG_RISCOP);
361 106 markom
}
362
 
363 113 markom
/* Resets the CPU and stalls it.  */
364 118 markom
 
365 106 markom
static void
366 113 markom
or1k_reset ()
367
{
368
  unsigned int val;
369 362 markom
  int i;
370
  debug ("%08x\n", or1k_read_reg (JTAG_RISCOP));
371 113 markom
  or1k_set_chain (SC_REGISTER);
372 118 markom
 
373
  /* Be cautious - disable trace.  */
374
  val = or1k_read_reg (JTAG_MODER);
375
  or1k_write_reg (JTAG_MODER, val & ~2);
376
 
377 113 markom
  val = or1k_read_reg (JTAG_RISCOP);
378
  val &= ~3;
379 118 markom
  /* Assert reset signal.  */
380
  or1k_write_reg (JTAG_RISCOP, val | 3);
381
 
382 362 markom
  /* Just do something */
383
  for (i = 0; i < 100; i++)
384
    or1k_read_reg (JTAG_RISCOP);
385
 
386 118 markom
  /* give it some time */
387
  usleep (1000);
388 125 chris
 
389 362 markom
  or1k_set_chain (SC_REGISTER);
390 118 markom
  /* Release reset signal, but keep in stall state.  */
391
  or1k_write_reg (JTAG_RISCOP, val | 1);
392 362 markom
  or1k_read_reg (JTAG_RISCOP);
393 113 markom
}
394
 
395 118 markom
/* Synchronizes debug registers in memory with those on target,
396
   if there is any change.  */
397
 
398 113 markom
static void
399 118 markom
or1k_commit_debug_registers ()
400
{
401
  int i;
402
  unsigned int u;
403
  if (!debug_regs_changed)
404
    return;
405
 
406
  /* Matchpoints (breakpoints, watchpoints).  */
407
  for (i = 0; i < NUM_MATCHPOINTS; i++)
408
    {
409
      unsigned int u;
410
      or1k_write_spr_reg (DVR0_SPRNUM + i, dvr[i]);
411
      memcpy (&u, &dcr[i], sizeof(dcr[i]));
412
      or1k_write_spr_reg (DCR0_SPRNUM + i, u);
413
    }
414
  or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
415
  or1k_write_spr_reg (DMR2_SPRNUM, dmr2);
416
 
417
  /* Trace dependent.  */
418
  or1k_set_chain (SC_REGISTER);
419
  memcpy (&u, &or1k_htrace.trig, sizeof(or1k_htrace.trig));
420
  or1k_write_reg (JTAG_TSEL, u);
421
  memcpy (&u, &or1k_htrace.qual, sizeof(or1k_htrace.qual));
422
  or1k_write_reg (JTAG_QSEL, u);
423
  memcpy (&u, &or1k_htrace.stop, sizeof(or1k_htrace.stop));
424
  or1k_write_reg (JTAG_SSEL, u);
425
  debug_regs_changed = 0;
426
  for (i = 0; i < NUM_RECORDS; i++)
427
    {
428
      memcpy (&u, &or1k_htrace.recwp[i], sizeof(or1k_htrace.recwp[i]));
429
      or1k_write_reg (JTAG_RECWP0 + i, u);
430
    }
431
  memcpy (&u, &or1k_htrace.recbp, sizeof(or1k_htrace.recbp));
432
  or1k_write_reg (JTAG_RECBP0, u);
433
  memcpy (&u, &or1k_htrace.moder, sizeof(or1k_htrace.moder));
434
  or1k_write_reg (JTAG_MODER, u);
435
}
436
 
437
static void
438 106 markom
or1k_set_undefined_cleanups (arg)
439
     PTR arg;
440
{
441
  or1k_status = TARGET_UNDEFINED;
442
}
443
 
444
/* Initialize a new connection to the or1k board, and make sure we are
445
   really connected.  */
446
 
447
static void
448
or1k_init (args)
449
     char *args;
450
{
451
  struct cleanup *old_cleanups = make_cleanup (or1k_set_undefined_cleanups, NULL);
452
  int i;
453 115 markom
  unsigned int tmp;
454 118 markom
  FILE *f;
455 106 markom
 
456
  /* What is this code doing here?  I don't see any way it can happen, and
457
     it might mean or1k_initializing didn't get cleared properly.
458
     So I'll make it a warning.  */
459
 
460
  if (or1k_status == TARGET_CONNECTING)
461
    {
462
      warning ("internal error: or1k_initialize called twice");
463
      return;
464
    }
465
 
466
  or1k_status = TARGET_CONNECTING;
467
  if (current_or1k_target != NULL && current_or1k_target->to_init != NULL)
468
    current_or1k_target->to_init (args);
469 122 markom
 
470 362 markom
  debug("%08x\n", read_pc ());
471
  or1k_stall ();
472
  debug("%08x\n", read_pc ());
473
  usleep (1000);
474
 
475 106 markom
  /* Determine implementation configuration.  */
476 113 markom
  or1k_implementation.VR = or1k_read_spr_reg (VR_SPRNUM);
477
  or1k_implementation.UPR = or1k_read_spr_reg (UPR_SPRNUM);
478 118 markom
 
479 115 markom
  /* Determine number of gpr_regs.  */
480
  tmp = or1k_read_spr_reg (CPUCFGR_SPRNUM);
481
  or1k_implementation.num_gpr_regs = ((tmp >> 4) & 1)?(16):(32);
482 118 markom
 
483 115 markom
  /* Is any vector or floating point support present? */
484
  or1k_implementation.vf_present = ((tmp >> 7) & 7) != 0;
485
  or1k_implementation.num_vfpr_regs = (or1k_implementation.vf_present)?(32):(0);
486 106 markom
 
487 115 markom
  /* Determine max number of supported matchpoints.  */
488
  tmp = or1k_read_spr_reg (DCFGR_SPRNUM);
489
  or1k_implementation.num_matchpoints = tmp & 7;
490 113 markom
  or1k_implementation.num_used_matchpoints = 0;
491 115 markom
  or1k_implementation.has_counters = tmp & 4 == 1;
492 106 markom
 
493
  /* Is implementation supported? */
494
 
495
  /* First we should have system and debug groups implemented. */
496
  if (or1k_implementation.VR & (1 << SPR_SYSTEM_GROUP) == 0)
497
    error ("System group should be available in the or1k implementation.");
498
  if (or1k_implementation.VR & (1 << SPR_DEBUG_GROUP) == 0)
499
    error ("Debug group should be available in the or1k implementation.");
500 115 markom
  if (or1k_implementation.has_counters)
501
    warning ("Counters not supported.");
502 106 markom
 
503
  /* Delete break, watch, catch points.  */
504
  for(i = 0; i < NUM_MATCHPOINTS; i++)
505 118 markom
    {
506
      memset (&dcr[i], 0, sizeof (dcr[i]));
507
      matchpoint_user_count[i] = 0;
508
    }
509 106 markom
 
510
  dmr1 = 0;
511
  dmr2 = 0;
512 118 markom
  memset (&or1k_htrace, 0, sizeof (or1k_htrace));
513
 
514
  /* RECSELDEPEND = 0 does not match our trace scheme. */
515
  or1k_htrace.moder.rec_sel_dep = 1;
516
 
517
  debug_regs_changed = 1;
518
  or1k_commit_debug_registers ();
519
 
520 106 markom
  if (err != 0)
521
    error ("Cannot connect.");
522
 
523 362 markom
  /* Enable exceptions */
524
  or1k_write_spr_reg (SR_SPRNUM, or1k_read_spr_reg(SR_SPRNUM) | 0x2);
525
 
526 106 markom
  /* Stop when breakpoint occurs.  */
527 372 markom
  or1k_write_spr_reg (DSR_SPRNUM, dsr = 0x2000);
528 106 markom
 
529
  do_cleanups (old_cleanups);
530
 
531
  /* This should cause an error if not connected.  */
532
  or1k_fetch_registers (-1);
533
 
534
  set_current_frame (create_new_frame (read_fp (), read_pc ()));
535
  select_frame (get_current_frame (), 0);
536 118 markom
 
537
  /* Just empty it.  */
538
  if ((f = fopen (TRACE_FILENAME, "wb+")) == NULL)
539
    error ("Cannot open trace file.");
540
  fclose (f);
541
  trace_size = 0;
542 363 markom
  or1k_status = TARGET_STOPPED;
543 106 markom
}
544
 
545
/* Kill the process running on the board.  */
546
 
547
void
548
or1k_kill ()
549
{
550
  if (or1k_status != TARGET_RUNNING)
551
    return;
552
  or1k_status = TARGET_UNDEFINED;
553 113 markom
  or1k_reset();
554
  or1k_status = TARGET_STOPPED;
555 106 markom
 
556
  inferior_pid = 0;
557
}
558
 
559
/* Open a connection to the remote board.  */
560
 
561
static void
562
or1k_open (name, from_tty)
563
     char *name;
564
     int from_tty;
565
{
566
  or1k_init (name);
567
 
568
  /* Switch to using remote target now.  */
569
  current_ops = current_or1k_target->gdb_ops;
570
  or1k_is_open = 1;
571
  push_target (current_ops);
572
 
573
  /* FIXME: Should we call start_remote here?  */
574
 
575
/* This is really the job of start_remote however, that makes an assumption
576
   that the target is about to print out a status message of some sort.  That
577
   doesn't happen here (in fact, it may not be possible to get the monitor to
578
   send the appropriate packet).  */
579
 
580
  flush_cached_frames ();
581
  registers_changed ();
582
  stop_pc = read_pc ();
583
  set_current_frame (create_new_frame (read_fp (), stop_pc));
584
  select_frame (get_current_frame (), 0);
585
  print_stack_frame (selected_frame, -1, 1);
586
}
587
 
588 362 markom
/* This is the generic stop called via the target vector. When a target
589
   interrupt is requested, either by the command line or the GUI, we
590
   will eventually end up here. */
591
static void
592
or1k_stop ()
593
{
594
  /* Send a break or a ^C, depending on user preference.  */
595
  debug ("remote_stop called\n");
596
 
597
  /* We should not stop the target immediately, since it can be in an
598
     unfinished state.  So we do a single step.  This should not affect
599
     on anything.  */
600
  or1k_stall ();
601
  /* HW STEP.  Set DMR1_ST.  */
602
  dmr1 |= DMR1_ST;
603
  or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
604
  dmr1 &= ~DMR1_ST;
605
  or1k_unstall ();
606
}
607
 
608 106 markom
/* Close a connection to the remote board.  */
609
 
610
static void
611
or1k_close (quitting)
612
     int quitting;
613
{
614
  if (or1k_is_open)
615
    {
616
      or1k_kill ();
617
      if (current_or1k_target != NULL && current_or1k_target->to_done != NULL)
618
        current_or1k_target->to_done ();
619
      current_or1k_target = NULL;
620
    }
621
  generic_mourn_inferior ();
622
}
623
 
624
/* Detach from the remote board.  */
625
 
626
static void
627
or1k_detach (args, from_tty)
628
     char *args;
629
     int from_tty;
630
{
631
  if (args)
632
    error ("Argument given to \"detach\" when remotely debugging.");
633
 
634
  pop_target ();
635
 
636
  or1k_close (1);
637
 
638
  if (from_tty)
639
    printf_unfiltered ("Ending remote or1k debugging.\n");
640
}
641
 
642 118 markom
/* Appends trace data to the trace file.  */
643
 
644
static void
645
or1k_read_trace ()
646
{
647
  struct htrace_data_struct data;
648 122 markom
  ULONGEST tmp;
649 118 markom
  int first = 1;
650
  FILE *fd;
651
  if ((fd = fopen (TRACE_FILENAME, "ab")) == NULL)
652
    {
653
      warning ("Cannot append to trace file.");
654
      return;
655
    }
656
 
657
  or1k_set_chain (SC_TRACE);
658
  while (1)
659
    {
660
      tmp = or1k_read_reg (0);
661
      memcpy (&data, &tmp, sizeof (data));
662
 
663
      /* Last record reached. */
664
      if (!data.valid)
665
        break;
666
      data.valid = first;
667
      first = 0;
668
      if (!fwrite (&data, sizeof (data), 1, fd))
669
        {
670
          warning ("Cannot write trace data");
671
          break;
672
        }
673
    }
674
  fclose (fd);
675
}
676
 
677 362 markom
/* ^C Interrupt handling */
678
 
679
/* Ask the user what to do when an interrupt is received.  */
680
static void
681
interrupt_query ()
682
{
683
  target_terminal_ours ();
684
 
685
  if (query ("Interrupted while waiting for the program.\n\
686
Give up (and stop debugging it)? "))
687
    {
688
      target_mourn_inferior ();
689
      return_to_top_level (RETURN_QUIT);
690
    }
691
 
692
  target_terminal_inferior ();
693
}
694
 
695
static void or1k_interrupt_twice (int signo);
696
 
697
 
698
/* The command line interface's stop routine. This function is installed
699
   as a signal handler for SIGINT. The first time a user requests a
700
   stop, we call remote_stop to send a break or ^C. If there is no
701
   response from the target (it didn't stop when the user requested it),
702
   we ask the user if he'd like to detach from the target. */
703
static void
704
or1k_interrupt (signo)
705
     int signo;
706
{
707
        debug ("interrupt");
708
  /* If this doesn't work, try more severe steps. */
709
  signal (signo, or1k_interrupt_twice);
710
 
711
  debug ("remote_interrupt called\n");
712
 
713
  interrupt_count++;
714
}
715
 
716
/* The user typed ^C twice.  */
717
 
718
static void
719
or1k_interrupt_twice (signo)
720
     int signo;
721
{
722
        debug ("interrupt2");
723
  signal (signo, ofunc);
724
  interrupt_query ();
725
  signal (signo, or1k_interrupt);
726
}
727
 
728 106 markom
/* Resume execution of the target process.  STEP says whether to single-step
729
   or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
730
   to the target, or zero for no signal.  */
731
 
732
static void
733
or1k_resume (pid, step, siggnal)
734
     int pid, step;
735
     enum target_signal siggnal;
736 362 markom
{
737 372 markom
  unsigned int pc;
738
  unsigned int ppc;
739
  unsigned int npc;
740
  unsigned int val;
741 373 markom
  int two_steps = 0;
742 372 markom
 
743
  pc = read_pc();
744
  npc = or1k_read_spr_reg (PC_SPRNUM);
745
  ppc = or1k_read_spr_reg (PPC_SPRNUM);
746
  debug ("pc = %08x BP = %x npc = %08x ppc = %08x\n", pc, breakpoint_here_p (pc), npc, ppc);
747 362 markom
  debug ("resume %i, %i, %i\n",step, siggnal, or1k_status);
748 106 markom
  if (or1k_status != TARGET_STOPPED)
749
    if (or1k_status == TARGET_RUNNING)
750
      error ("Program is already running.");
751
    else
752
      error ("The program is not being run.");
753
 
754 150 chris
 
755 106 markom
  /* Clear reason register for later.  */
756 113 markom
  or1k_write_spr_reg (DRR_SPRNUM, 0);
757 106 markom
 
758 362 markom
  or1k_commit_debug_registers ();
759
 
760 373 markom
  if (step) {
761
    /* Always set program counter to branch instruction and reexecute it */
762
    if (insn_has_delay_slot (or1k_fetch_instruction (ppc)))
763
      {
764
        or1k_write_spr_reg (PC_SPRNUM, ppc);
765
        two_steps = 1;
766
      }
767
    else if (breakpoint_here_p (pc) && ((ppc + 4) != npc))
768
      {
769
        /* Trapped on delay slot instruction. */
770
        /* Set PC to branch insn preceding delay slot. */
771
        or1k_write_spr_reg (PC_SPRNUM, ppc - 4);
772
        two_steps = 1;
773
      } else if (breakpoint_here_p (pc))
774
        or1k_write_spr_reg (PC_SPRNUM, pc);
775
 
776 106 markom
      /* HW STEP.  Set DMR1_ST.  */
777
      dmr1 |= DMR1_ST;
778 113 markom
      or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
779 106 markom
      dmr1 &= ~DMR1_ST;
780 372 markom
 
781 373 markom
      if (two_steps)
782
        {
783 372 markom
          or1k_unstall ();
784
 
785
          or1k_set_chain (SC_REGISTER);
786
          val = or1k_read_reg (JTAG_RISCOP);
787 373 markom
          do
788 372 markom
            val = or1k_read_reg (JTAG_RISCOP);
789 373 markom
          while ((val & 1) == 0);
790 372 markom
        }
791 106 markom
    }
792 150 chris
  else
793
    {
794
      dmr1 &= ~DMR1_ST;
795
      or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
796 373 markom
      if (breakpoint_here_p (pc))
797
        or1k_write_spr_reg (PC_SPRNUM, pc);
798 150 chris
    }
799 106 markom
 
800 207 chris
  /* We can now continue normally, independent of step */
801 106 markom
  or1k_unstall ();
802
  or1k_status = TARGET_RUNNING;
803 362 markom
  debug ("-resume %i, %i, %i\n",step, siggnal, or1k_status);
804 106 markom
}
805
 
806
/* Wait until the remote stops, and return a wait status.  */
807
 
808
static int
809
or1k_wait (pid, status)
810
     int pid;
811
     struct target_waitstatus *status;
812
{
813 362 markom
  unsigned long val;
814
  unsigned long pc;
815 372 markom
  unsigned long npc;
816
  unsigned long ppc;
817 362 markom
  char buf[MAX_REGISTER_RAW_SIZE];
818 106 markom
  interrupt_count = 0;
819
 
820 362 markom
  debug ("wait %i %i\n", pid, or1k_status);
821 106 markom
  /* If we have not sent a single step or continue command, then the
822
     board is waiting for us to do something.  Return a status
823
     indicating that it is stopped.  */
824
  if (or1k_status != TARGET_RUNNING)
825
    {
826
      if (or1k_status != TARGET_STOPPED)
827
        error("Target in invalid state.");
828
      status->kind = TARGET_WAITKIND_STOPPED;
829
      status->value.sig = TARGET_SIGNAL_TRAP;
830
      return 0;
831
    }
832
 
833
  if (err)
834
    or1k_error ("Remote failure: %s", or1k_err_name (err));
835
 
836 362 markom
  /* Set new signal handler */
837
  ofunc = signal (SIGINT, or1k_interrupt);
838 106 markom
 
839 362 markom
  /* Wait for risc to stop.  */
840
  do {
841
    or1k_set_chain (SC_REGISTER);
842
    val = or1k_read_reg (JTAG_RISCOP);
843 372 markom
 
844 362 markom
    /* When we press Ctrl-C, interrupt count is set, but we must wait
845
         for or1k_read_reg to finish, otherwise we would interrupt transaction.  */
846
    if (interrupt_count)
847 372 markom
      or1k_stop ();
848 106 markom
 
849 362 markom
    usleep (10);
850
    debug ("%i", val);
851 372 markom
  } while ((val & 1) == 0);
852
 
853 362 markom
  drr = or1k_read_spr_reg (DRR_SPRNUM);
854 372 markom
 
855 362 markom
  /* Restore old INT signal handler */
856
  signal (SIGINT, ofunc);
857
 
858 372 markom
  /* Single step does not set trap exception, so we set it manually to simplify our code */
859
  dmr1 = or1k_read_spr_reg (DMR1_SPRNUM);
860
  if (dmr1 & DMR1_ST)
861
    drr |= DRR_TE;
862 362 markom
 
863
  status->kind = TARGET_WAITKIND_STOPPED;
864
 
865
  debug ("epcr0 = %08x\n", or1k_read_spr_reg (EPCR0_SPRNUM));
866
  debug ("drr = %08x\n", drr);
867
 
868 363 markom
  registers_changed ();
869 362 markom
  pc = read_pc ();
870 372 markom
  npc = or1k_read_spr_reg (PC_SPRNUM);
871
  ppc = or1k_read_spr_reg (PPC_SPRNUM);
872
  debug ("npc = %08x ppc = %08x\n", npc, ppc);
873 362 markom
 
874 372 markom
  if (drr & DRR_TE)
875
    {
876
      /* If single step is not set, we should correct the pc.  */
877
      if (!(dmr1 & DMR1_ST))
878
        /* PC has already stepped over the l.trap instruction.  */
879
        pc = ppc;
880
      status->value.sig = TARGET_SIGNAL_TRAP;
881
      drr &= ~DRR_TE;
882
    }
883
  else if (drr & DRR_RSTE)
884
    {
885
      status->value.sig = TARGET_SIGNAL_REALTIME_33;
886
      drr &= ~DRR_RSTE;
887
    }
888 106 markom
  else if (drr & DRR_BUSEE)
889 372 markom
    {
890
      status->value.sig = TARGET_SIGNAL_BUS;
891
      drr &= ~DRR_BUSEE;
892
    }
893 106 markom
  else if (drr & DRR_AE)
894 372 markom
    {
895
      status->value.sig = TARGET_SIGNAL_REALTIME_36;
896
      drr &= ~DRR_AE;
897
    }
898 106 markom
  else if (drr & DRR_IIE)
899 372 markom
    {
900
      status->value.sig = TARGET_SIGNAL_ILL;
901
      drr &= ~DRR_IIE;
902
    }
903
  else if (drr & DRR_RE)
904
    {
905
      status->value.sig = TARGET_SIGNAL_REALTIME_39;
906
      drr &= ~DRR_RE;
907
    }
908
  else if (drr & DRR_IME)
909
    {
910
      status->value.sig = TARGET_SIGNAL_REALTIME_38;
911
      drr &= ~DRR_IME;
912
    }
913 106 markom
  else if (drr & DRR_DME)
914 372 markom
    {
915
      status->value.sig = TARGET_SIGNAL_REALTIME_37;
916
      drr &= ~DRR_DME;
917
    }
918
  else if (drr & DRR_DPFE)
919
    {
920
      status->value.sig = TARGET_SIGNAL_REALTIME_34;
921
      drr &= ~DRR_DPFE;
922
    }
923
  else if (drr & DRR_IPFE)
924
    {
925
      status->value.sig = TARGET_SIGNAL_REALTIME_35;
926
      drr &= ~DRR_DPFE;
927
    }
928 106 markom
  else if (drr & DRR_SCE)
929
    {
930 372 markom
      status->value.sig = TARGET_SIGNAL_REALTIME_40;
931
      drr &= ~DRR_SCE;
932
    }
933
  else if (drr & DRR_HPINTE)
934
    {
935
      status->value.sig = TARGET_SIGNAL_INT;
936
      drr &= ~DRR_HPINTE;
937
    }
938
  else if (drr & DRR_LPINTE)
939
    {
940
      status->value.sig = TARGET_SIGNAL_INT;
941
      drr &= ~DRR_LPINTE;
942
    }
943
  else
944
    {
945 106 markom
      status->value.sig = TARGET_SIGNAL_UNKNOWN;
946
      warning ("Invalid exception occured.");
947 372 markom
    }
948 106 markom
 
949 372 markom
  /* Update drr register */
950
  or1k_write_spr_reg (DRR_SPRNUM, drr);
951
 
952 362 markom
  /* Write into PC flushes the pipeline! */
953
  /* We got the number the register holds, but gdb expects to see a
954
     value in the target byte ordering.  */
955 372 markom
/*  write_pc (pc);
956
*/
957
  store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), pc);
958
  supply_register (PC_REGNUM, buf);
959
 
960 363 markom
  /*or1k_write_spr_reg (PC_SPRNUM, pc);
961 362 markom
  store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), pc);
962 363 markom
  supply_register (PC_REGNUM, buf);*/
963 362 markom
 
964 113 markom
  /* Log remote stop.  */
965 106 markom
  or1k_status = TARGET_STOPPED;
966 113 markom
 
967
  /* Determine what caused trap - breakpoint or watchpoint.  */
968
  if (status->value.sig == TARGET_SIGNAL_TRAP)
969
    {
970
      /* Search all active breakpoints for a match.  */
971
      CORE_ADDR pc = read_pc ();
972
      int breakpoint = 0;
973
      int i;
974 150 chris
      unsigned char break_bytes[4] = BRK_INSTR_STRUCT;
975
      unsigned long b_insn = ntohl(*((unsigned long*)break_bytes));
976 151 chris
      unsigned long value = pc;
977 143 chris
 
978 113 markom
      for (i = 0; i < or1k_implementation.num_used_matchpoints; i++)
979 118 markom
        if (dvr[i] == pc && dcr[i].dp && dcr[i].cc == CC_EQUAL
980
            && !dcr[i].sc && dcr[i].ct == CT_FETCH)
981 113 markom
          {
982
            breakpoint = 1;
983
            break;
984
          }
985
      hit_watchpoint = !breakpoint;
986 143 chris
 
987
      /* Cause the trap/breakpoint exception to be ignored. This is
988
         the behavior of the simulator when the PC value is changed
989
         by a write command. All pending exceptions are cleared and
990 150 chris
         the simulator continues at the PC value specified. We need
991
         to do this if the instruction at the current PC has the
992
         value BRK_INSTR_STRUCT */
993
 
994 362 markom
      if(b_insn == or1k_read_mem((pc & 3)))
995 150 chris
        {
996
          or1k_write_spr_reg(PC_SPRNUM,value);
997
        }
998 113 markom
    }
999
  else
1000
    hit_watchpoint = 0;
1001 106 markom
 
1002
  /* If the stop PC is in the _exit function, assume
1003
     we hit the 'break 0x3ff' instruction in _exit, so this
1004
     is not a normal breakpoint.  */
1005
  {
1006
    char *func_name;
1007
    CORE_ADDR func_start;
1008
    CORE_ADDR pc = read_pc ();
1009
 
1010
    find_pc_partial_function (pc, &func_name, &func_start, NULL);
1011
    if (func_name != NULL && strcmp (func_name, "_exit") == 0
1012
        && func_start == pc)
1013
      status->kind = TARGET_WAITKIND_EXITED;
1014
  }
1015 118 markom
 
1016
  or1k_read_trace ();
1017 362 markom
  debug ("-wait %i %i\n", pid, or1k_status);
1018 106 markom
  return 0;
1019
}
1020
 
1021 113 markom
/* Fetch a word from the target board.  All memory accesses to the
1022
   remote board are word aligned.  */
1023 106 markom
 
1024 118 markom
unsigned int
1025 106 markom
or1k_fetch_word (addr)
1026
     CORE_ADDR addr;
1027
{
1028 113 markom
  if (addr & 3)
1029
    {
1030
      int subaddr = addr & 3;
1031
      unsigned char buf[8];
1032
      unsigned int low, high;
1033
      addr >>= 2;
1034 362 markom
      low = or1k_read_mem (addr << 2);
1035
      high = or1k_read_reg ((addr + 1) << 2);
1036 113 markom
      memcpy (&buf[0], &low, 4);
1037
      memcpy (&buf[4], &high, 4);
1038
      memcpy (&low, &buf[subaddr], 4);
1039
      return low;
1040
    }
1041
  else
1042
    {
1043 362 markom
      return or1k_read_mem (addr);
1044 113 markom
    }
1045 106 markom
}
1046
 
1047
/* Store a word to the target board.  Returns errno code or zero for
1048 113 markom
   success.  All memory accesses to the remote board are word aligned.  */
1049 118 markom
 
1050 106 markom
static int
1051
or1k_store_word (addr, val)
1052
     CORE_ADDR addr;
1053
     unsigned int val;
1054
{
1055 113 markom
  if (addr & 3)
1056
    {
1057
      int subaddr = addr & 3;
1058
      unsigned char buf[8];
1059
      unsigned int low, high;
1060
      addr >>= 2;
1061 362 markom
      low = or1k_read_mem (addr << 2);
1062
      high = or1k_read_mem ((addr + 1) << 2);
1063 113 markom
      memcpy (&buf[0], &low, 4);
1064
      memcpy (&buf[4], &high, 4);
1065
      memcpy (&buf[subaddr], &val, 4);
1066
      memcpy (&low, &buf[0], 4);
1067
      memcpy (&high, &buf[4], 4);
1068 362 markom
      or1k_write_mem (addr << 2, low);
1069
      or1k_write_mem ((addr + 1) << 2, high);
1070 113 markom
    }
1071
  else
1072 362 markom
    {
1073
      or1k_write_mem (addr, val);
1074 113 markom
    }
1075 106 markom
  return err;
1076
}
1077
 
1078
/* Fetch the remote registers.  */
1079
 
1080
void
1081
or1k_fetch_registers (regno)
1082
     int regno;
1083
{
1084
  unsigned int val;
1085
 
1086
  if (regno == -1)
1087
    {
1088
      for (regno = 0; regno < NUM_REGS; regno++)
1089
        or1k_fetch_registers (regno);
1090
      return;
1091
    }
1092
 
1093
  if (regno >= NUM_REGS)
1094
    error("Invalid register number!");
1095
 
1096
  /* Convert to SPRNUM and read.  */
1097 113 markom
  val = or1k_read_spr_reg (REGNUM_TO_SPRNUM(regno));
1098 106 markom
 
1099
  {
1100
    char buf[MAX_REGISTER_RAW_SIZE];
1101
 
1102
    /* We got the number the register holds, but gdb expects to see a
1103
       value in the target byte ordering.  */
1104
    store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1105
    supply_register (regno, buf);
1106
  }
1107
  if (err)
1108 118 markom
    or1k_error ("Can't read register %d(%i): %s", regno,
1109 362 markom
                REGNUM_TO_SPRNUM(regno), or1k_err_name (err));
1110 106 markom
}
1111
 
1112
/* Fetch and return instruction from the specified location.  */
1113
 
1114
unsigned int
1115
or1k_fetch_instruction (addr)
1116
     CORE_ADDR addr;
1117
{
1118
  char buf[OR1K_INSTLEN];
1119
  int status;
1120
 
1121
  status = read_memory_nobpt (addr, buf, OR1K_INSTLEN);
1122
  if (status)
1123
    memory_error (status, addr);
1124
  return extract_unsigned_integer (buf, OR1K_INSTLEN);
1125
}
1126
 
1127 113 markom
/* Currently not needed.  */
1128
 
1129 106 markom
static void
1130
or1k_prepare_to_store ()
1131
{
1132
}
1133
 
1134
/* Store remote register(s).  */
1135
 
1136
static void
1137
or1k_store_registers (regno)
1138
     int regno;
1139
{
1140
  if (regno == -1)
1141
    {
1142
      for (regno = 0; regno < NUM_REGS; regno++)
1143
        or1k_store_registers (regno);
1144
      return;
1145
    }
1146
 
1147
  if (regno >= NUM_REGS)
1148
    error("Invalid register number!");
1149
 
1150 362 markom
  or1k_write_spr_reg (REGNUM_TO_SPRNUM(regno), read_register (regno));
1151 106 markom
  if (err)
1152 113 markom
    or1k_error ("Can't write register %d(%i): %s", regno, REGNUM_TO_SPRNUM(regno), or1k_err_name (err));
1153 106 markom
}
1154
 
1155
/* Read or write LEN bytes from inferior memory at MEMADDR,
1156
   transferring to or from debugger address MYADDR.  Write to inferior
1157
   if SHOULD_WRITE is nonzero.  Returns length of data written or
1158
   read; 0 for error.  Note that protocol gives us the correct value
1159
   for a longword, since it transfers values in ASCII.  We want the
1160
   byte values, so we have to swap the longword values.  */
1161
 
1162
static int
1163
or1k_xfer_memory (memaddr, myaddr, len, write, ignore)
1164
     CORE_ADDR memaddr;
1165
     char *myaddr;
1166
     int len;
1167
     int write;
1168
     struct target_ops *ignore;
1169
{
1170
  register int i;
1171
  /* Round starting address down to longword boundary.  */
1172
  register CORE_ADDR addr = memaddr & ~3;
1173
  /* Round ending address up; get number of longwords that makes.  */
1174
  register int count = (((memaddr + len) - addr) + 3) / 4;
1175
  /* Allocate buffer of that many longwords.  */
1176
  register char *buffer = alloca (count * 4);
1177
  int status;
1178
 
1179 143 chris
  int block_xfer_size = 256; /* CZ 21/06/01 ... number of 32 bit words */
1180
  int nBlocks = (count + block_xfer_size -1)/block_xfer_size;
1181
  int terminate = 0;  /* Terminate the printing of '*'s... */
1182
 
1183 362 markom
#ifdef DEBUG_JTAG
1184
  debug ("xfer_memory %s addr=%x, len=%i, \n", write?"write":"read", memaddr, len);
1185
  fflush(stdout);
1186
#endif
1187
 
1188
#if 0
1189 106 markom
  if (memaddr >= MEM_SPACE)
1190
    error("Invalid address");
1191 362 markom
#endif
1192 106 markom
 
1193 135 chris
  /* (CZ 21/06/01 -- because upper layers which know nothing about
1194
     Or1k or JTAG call this function directly, it is always necessary
1195
     to set the chain to point to the Debug Unit. Otherwise, it may
1196 143 chris
     be pointing to the Development Interface chain, in which case
1197 135 chris
     we're going to get bupkiss... */
1198
 
1199 106 markom
  if (write)
1200
    {
1201
      /* Fill start and end extra bytes of buffer with existing data.  */
1202
      if (addr != memaddr || len < 4)
1203
        {
1204
          /* Need part of initial word -- fetch it.  */
1205
          store_unsigned_integer (&buffer[0], 4, or1k_fetch_word (addr));
1206
        }
1207
 
1208
      if (count > 1)
1209
        {
1210
          /* Need part of last word -- fetch it.  FIXME: we do this even
1211
             if we don't need it.  */
1212
          store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1213
                                  or1k_fetch_word (addr + (count - 1) * 4));
1214
        }
1215
 
1216
      /* Copy data to be written over corresponding part of buffer */
1217
      memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1218
 
1219 143 chris
      /* CZ: rewrote the block transfer routines to make the code
1220
         a little more efficient for implementations that can handle
1221
         variable sized scan chains. Might be useful in the future.
1222
         Certainly makes downloads to the simulator more efficient. */
1223
      for(i=0;i<nBlocks;i++,count-=block_xfer_size,addr += block_xfer_size*4)
1224 106 markom
        {
1225 143 chris
          int j;
1226
          int n = count < block_xfer_size ? count : block_xfer_size;
1227
          unsigned long *__buf;
1228
 
1229
          if(!(__buf = (unsigned long*)malloc(n*sizeof(unsigned long))))
1230 106 markom
            {
1231 143 chris
              errno = ERR_MEM;
1232
              return 0;
1233
            }
1234
 
1235
          for(j=0;j<n;j++)
1236
            __buf[j] = (unsigned long)extract_unsigned_integer(&buffer[(i * block_xfer_size +j)*4], 4);
1237 362 markom
          or1k_set_chain (SC_WISHBONE);
1238 143 chris
          status = or1k_store_block(addr,__buf,n);
1239
          free(__buf);
1240
          if(n == block_xfer_size)
1241
            {
1242 362 markom
              debug ("*");
1243 106 markom
              gdb_flush (gdb_stdout);
1244
            }
1245
          if (status)
1246
            {
1247
              errno = status;
1248
              return 0;
1249
            }
1250
          /* FIXME: Do we want a QUIT here?  */
1251
        }
1252 143 chris
      if (terminate)
1253 362 markom
        debug ("\n");
1254 106 markom
    }
1255
  else
1256
    {
1257 143 chris
      for(i=0;i<nBlocks;i++,count-=block_xfer_size,addr += block_xfer_size*4)
1258 106 markom
        {
1259 143 chris
          int j;
1260
          int n = count < block_xfer_size ? count : block_xfer_size;
1261
          unsigned long *__buf;
1262
 
1263 362 markom
    or1k_set_chain (SC_WISHBONE);
1264 143 chris
          __buf = (unsigned long*)malloc(n*sizeof(unsigned long));
1265
          status = or1k_load_block(addr,__buf,n);
1266
          if (!status)
1267
            for(j=0;j<n;j++)
1268
              store_unsigned_integer (&buffer[(i * block_xfer_size +j)*4], 4, __buf[j]);
1269
          else
1270
            errno = status;
1271
          free(__buf);
1272
 
1273
          if(status)
1274
            return 0;
1275 106 markom
        }
1276
      /* Copy appropriate bytes out of the buffer.  */
1277
      memcpy (myaddr, buffer + (memaddr & 3), len);
1278
    }
1279
  return len;
1280
}
1281
 
1282 143 chris
int or1k_load_block(CORE_ADDR addr,void* buffer,int nRegisters)
1283
{
1284
  int i=0;
1285 362 markom
  unsigned int regno = addr;
1286 143 chris
 
1287
  if (current_or1k_target != NULL && current_or1k_target->to_read_block != NULL)
1288
    return current_or1k_target->to_read_block (regno,buffer,nRegisters);
1289
  else
1290
    for(i=0;i<nRegisters;i++)
1291
      ((unsigned long*)buffer)[i] = 0x1234;
1292
  return 0;
1293
}
1294
 
1295
int or1k_store_block(CORE_ADDR addr,void* buffer,int nRegisters)
1296
{
1297 362 markom
  unsigned int regno = addr;
1298
 
1299 143 chris
  if (current_or1k_target != NULL && current_or1k_target->to_write_block != NULL)
1300
    return current_or1k_target->to_write_block (regno,buffer,nRegisters);
1301
  return 0;
1302
}
1303
 
1304 106 markom
/* Print info on this target.  */
1305
 
1306
static void
1307
or1k_files_info (ignore)
1308
     struct target_ops *ignore;
1309
{
1310
  char *file = "nothing";
1311
 
1312
  if (exec_bfd)
1313
    file = bfd_get_filename (exec_bfd);
1314
 
1315
  printf_filtered ("or1k_files_info: file \"%s\"\n", file);
1316
 
1317
  if (exec_bfd)
1318
    {
1319
      printf_filtered ("\tAttached to %s running program %s\n",
1320
                       target_shortname, file);
1321
    }
1322
  /* Print target info. */
1323
  printf_filtered ("Status: %s\n", status_name[or1k_status]);
1324
}
1325
 
1326
/* Tell whether we can support a hardware breakpoint.  */
1327 118 markom
 
1328 106 markom
static int
1329
or1k_can_use_hardware_breakpoint ()
1330
{
1331
  int i;
1332 118 markom
 
1333 106 markom
  /* Search for unused breakpoint.  */
1334 113 markom
  return or1k_implementation.num_used_matchpoints < or1k_implementation.num_matchpoints;
1335 106 markom
}
1336
 
1337
/* Insert a breakpoint.  On targets that don't have built-in breakpoint
1338
   support, we read the contents of the target location and stash it,
1339
   then overwrite it with a breakpoint instruction.  ADDR is the target
1340
   location in the target machine.  CONTENTS_CACHE is a pointer to
1341
   memory allocated for saving the target contents.  It is guaranteed
1342
   by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1343
   is accomplished via BREAKPOINT_MAX).  */
1344
 
1345 113 markom
int
1346 106 markom
or1k_insert_breakpoint (addr, contents_cache)
1347
     CORE_ADDR addr;
1348
     char *contents_cache;
1349
{
1350
  if (or1k_can_use_hardware_breakpoint())
1351
    return set_breakpoint (addr);
1352
  else
1353
    return memory_insert_breakpoint (addr, contents_cache);
1354
}
1355
 
1356 113 markom
int
1357 106 markom
or1k_remove_breakpoint (addr, contents_cache)
1358
     CORE_ADDR addr;
1359
     char *contents_cache;
1360
{
1361
  /* First try to remove HW breakpoint at address */
1362
  if (clear_breakpoint (addr))
1363
    return memory_remove_breakpoint (addr, contents_cache);
1364
  else
1365
    return 0;
1366
}
1367
 
1368 113 markom
/* Tell whether this target can support a hardware breakpoint.  CNT
1369
   is the number of hardware breakpoints already installed.  This
1370
   implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro.
1371
   Lower bound is estimated. !!! Can we estimate better? */
1372
 
1373
int
1374
or1k_can_use_hardware_watchpoint (bp_type, cnt)
1375
     enum bptype bp_type;
1376
     int cnt;
1377
{
1378
  /* Are there at least two matchpoints left for watch? - estimate lower bound  */
1379
  return cnt + ((bp_type == bp_hardware_watchpoint)?(1):(0))
1380
    <= or1k_implementation.num_matchpoints;
1381
}
1382
 
1383 118 markom
/* Moves matchpoint.  This is very tricky - we have to update
1384
   all references to matchpoint indexes.  We assume here that
1385
   matchpoint with index to is unused! */
1386
 
1387
static void
1388
move_matchpoint (int from, int to)
1389
{
1390
  int i, j, tmp, chaining;
1391
  if (matchpoint_user_count[to] != 0)
1392
    error ("Internal: Destination matchpoint still has users");
1393
  matchpoint_user_count[to] = matchpoint_user_count[from];
1394
  matchpoint_user_count[from] = 0;
1395
  debug_regs_changed = 1;
1396
 
1397
  dvr[to] = dvr[from];
1398
  dcr[to] = dcr[from];
1399
  dcr[from].dp = 0;
1400
 
1401
  /* Copy chaining bits.  */
1402
  chaining = dmr1 & (3 << (2 * from));
1403
  dmr1 &= ~(3 << (2 * to));
1404
  dmr1 |= chaining << (2 * to);
1405
  dmr1 &= ~(3 << (2 * from));
1406
 
1407
  /* Copy watchpoint bits */
1408
  tmp = dmr2 & (1 << from);
1409
  dmr2 &= 1 << to;
1410
  dmr2 |= tmp << to;
1411
  dmr2 &= 1 << from;
1412
 
1413
  /* Update hwatch table.  Here we assume that matchpoint
1414
     group is connected (it cannot be implemented in HW
1415
     otherwise), so if we move first, we will have to move
1416
     others later.  */
1417
  for (i = 0; i < num_hw_watches; i++)
1418
    if (or1k_hwatch[i].matchpoint_start == from)
1419
      or1k_hwatch[i].matchpoint_start = to;
1420
 
1421
  /* Update htrace struct.  */
1422
  tmp = or1k_htrace.trig.wp_trig & (1 << from);
1423
  or1k_htrace.trig.wp_trig &= 1 << to;
1424
  or1k_htrace.trig.wp_trig |= tmp << to;
1425
  or1k_htrace.trig.wp_trig &= 1 << from;
1426
 
1427
  tmp = or1k_htrace.qual.wp_trig & (1 << from);
1428
  or1k_htrace.qual.wp_trig &= 1 << to;
1429
  or1k_htrace.qual.wp_trig |= tmp << to;
1430
  or1k_htrace.qual.wp_trig &= 1 << from;
1431
 
1432
  tmp = or1k_htrace.stop.wp_trig & (1 << from);
1433
  or1k_htrace.stop.wp_trig &= 1 << to;
1434
  or1k_htrace.stop.wp_trig |= tmp << to;
1435
  or1k_htrace.stop.wp_trig &= 1 << from;
1436
 
1437
  for (i = 0; i < MAX_MATCHPOINTS; i++)
1438
    {
1439
      tmp = or1k_htrace.wp_record_uses[i] & (1 << from);
1440
      or1k_htrace.wp_record_uses[i] &= 1 << to;
1441
      or1k_htrace.wp_record_uses[i] |= tmp << to;
1442
      or1k_htrace.wp_record_uses[i] &= 1 << from;
1443
    }
1444
 
1445
  /* Do we need to move other references also? */
1446
}
1447
 
1448 113 markom
/* Sifts unused matchpoints to higher indexses.  */
1449
 
1450 118 markom
void
1451 113 markom
sift_matchpoints ()
1452
{
1453
  int i, first_free = 0;
1454
  for (i = 0; i < or1k_implementation.num_matchpoints; i++)
1455
    if (dcr[i].dp)
1456
      {
1457 118 markom
        /* Move references.  */
1458
        move_matchpoint (i, first_free);
1459
 
1460 113 markom
        first_free++;
1461
      }
1462 118 markom
 
1463
  /* Unused matchpoints should be disabled by move_matchpoint,
1464
     so we are done here.  */
1465 113 markom
}
1466
 
1467
/* Translates gdb watchpoint type into one in DCR register.  */
1468
 
1469
static int
1470
translate_type (gdb_type)
1471
     int gdb_type;
1472
{
1473
  switch (gdb_type)
1474
    {
1475
    case 0:
1476
      return CT_SDATA;
1477
    case 1:
1478
      return CT_LDATA;
1479
    case 2:
1480
      return CT_ADATA;
1481
    default:
1482
      error ("Invalid type.");
1483
    }
1484
}
1485
 
1486
/* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1487
   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1488
   watchpoint. */
1489
 
1490
int
1491
or1k_insert_watchpoint (addr, len, type)
1492
     CORE_ADDR addr;
1493
     int len;
1494
     int type;
1495
{
1496
  int i;
1497 118 markom
 
1498 113 markom
  if (len < 1)
1499
    return -1;
1500
 
1501
  type = translate_type (type);
1502
 
1503
  /* Moves unused watchpoints to the top.  */
1504
  sift_matchpoints ();
1505 118 markom
 
1506 113 markom
  /* Place at first free matchpoint.  */
1507
  i = or1k_implementation.num_used_matchpoints;
1508
  dvr[i] = addr;
1509
  dcr[i].dp = 1;
1510
  dcr[i].cc = CC_GREATE;
1511
  dcr[i].sc = 0;
1512
  dcr[i].ct = type;
1513 118 markom
 
1514 113 markom
  /* Set && chaining here.  */
1515
  dmr1 &= ~(3 << (2 * i));
1516 118 markom
  dmr1 |= CHAINING_AND << (2 * i);
1517
 
1518 113 markom
  /* Set upper watchpoint bound.  */
1519
  i++;
1520
  dvr[i] = addr + len - 1;
1521
  dcr[i].dp = 1;
1522
  dcr[i].cc = CC_LESSE;
1523
  dcr[i].sc = 0;
1524
  dcr[i].ct = type;
1525 118 markom
 
1526 113 markom
  /* Matchpoints will cause breakpoints */
1527 118 markom
  dmr2 |= (1 << i);
1528 113 markom
  or1k_implementation.num_used_matchpoints += 2;
1529 118 markom
  debug_regs_changed = 1;
1530 113 markom
  return 0;
1531
}
1532
 
1533
/* Removes a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1534
   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1535
   watchpoint. */
1536 118 markom
 
1537 113 markom
int
1538
or1k_remove_watchpoint (addr, len, type)
1539
     CORE_ADDR addr;
1540
     int len;
1541
     int type;
1542
{
1543
  int i, found = -1;
1544 118 markom
 
1545 113 markom
  if (len < 1)
1546
    return -1;
1547
 
1548
  type = translate_type (type);
1549
 
1550
  /* Find the right one.  */
1551
  for (i = 0; i < or1k_implementation.num_used_matchpoints; i++)
1552
    if (dvr[i] == addr && dcr[i].dp && dcr[i].cc == CC_GREATE && !dcr[i].sc && dcr[i].ct == type
1553
        && dvr[i + 1] == addr + len - 1 && dcr[i + 1].dp && dcr[i + 1].cc == CC_LESSE
1554
        && !dcr[i + 1].sc && dcr[i + 1].ct == type)
1555
      {
1556
        found = i;
1557
        break;
1558
      }
1559
 
1560
  if (found < 0)
1561
    return -1;
1562
 
1563
  dcr[found].dp = 0;
1564
  dcr[found + 1].dp = 0;
1565
 
1566
  /* Matchpoints will not cause breakpoints anymore. */
1567 118 markom
  dmr2 &= ~(1 << i);
1568 113 markom
  or1k_implementation.num_used_matchpoints -= 2;
1569 118 markom
  debug_regs_changed = 1;
1570 113 markom
  return 0;
1571
}
1572
 
1573
int
1574
or1k_stopped_by_watchpoint (void)
1575
{
1576 143 chris
  /* For now, no watchpoints */
1577
  return 0;
1578
 
1579
  /* return hit_watchpoint; */
1580 113 markom
}
1581
 
1582 106 markom
/* Insert a breakpoint.  */
1583
 
1584
int
1585
set_breakpoint (addr)
1586
     CORE_ADDR addr;
1587
{
1588
  int i;
1589 118 markom
 
1590 106 markom
  /* Search for unused breakpoint.  */
1591
  for (i = 0; i < NUM_MATCHPOINTS; i++)
1592
    if (dcr[i].dp == 0) break;
1593
  if (i >= NUM_MATCHPOINTS) return 1;
1594
  dvr[i] = addr;
1595
  dcr[i].dp = 1;
1596
  dcr[i].cc = CC_EQUAL;
1597
  dcr[i].sc = 0;
1598
  dcr[i].ct = CT_FETCH;
1599 113 markom
  or1k_implementation.num_used_matchpoints++;
1600
 
1601
  /* No chaining here.  */
1602
  dmr1 &= ~(3 << (2*i));
1603 118 markom
 
1604 113 markom
  /* Matchpoints will cause breakpoints */
1605 118 markom
  dmr2 |= (1 << i);
1606
  debug_regs_changed = 1;
1607 106 markom
  return 0;
1608
}
1609
 
1610
/* Clear a breakpoint.  */
1611
 
1612
int
1613
clear_breakpoint (addr)
1614
     CORE_ADDR addr;
1615
{
1616
  int i;
1617 118 markom
 
1618 106 markom
  /* Search for matching breakpoint.  */
1619
  for (i = 0; i < NUM_MATCHPOINTS; i++)
1620
    if ((dcr[i].dp == 1) && (dvr[i] == addr) && (dcr[i].cc == CC_EQUAL)
1621
        && (dcr[i].sc == 0) && (dcr[i].ct == CT_FETCH)) break;
1622
 
1623
  if (i >= NUM_MATCHPOINTS) return 1;
1624
  dcr[i].dp = 0;
1625 118 markom
 
1626 113 markom
  /* Matchpoints will cause breakpoints */
1627 118 markom
  dmr2 &= ~(1 << i);
1628 113 markom
  or1k_implementation.num_used_matchpoints--;
1629 118 markom
  debug_regs_changed = 1;
1630 106 markom
  return 0;
1631
}
1632
 
1633
/* Start running on the target board.  */
1634
 
1635
static void
1636
or1k_create_inferior (execfile, args, env)
1637
     char *execfile;
1638
     char *args;
1639
     char **env;
1640
{
1641
  CORE_ADDR entry_pt;
1642
 
1643
  if (args && *args)
1644
    {
1645 118 markom
      warning ("Can't pass arguments to remote OR1K board; arguments ignored.");
1646
 
1647 106 markom
      /* And don't try to use them on the next "run" command.  */
1648
      execute_command ("set args", 0);
1649
    }
1650
 
1651
  if (execfile == 0 || exec_bfd == 0)
1652
    error ("No executable file specified");
1653
 
1654
  or1k_kill ();
1655
  remove_breakpoints ();
1656
 
1657
  entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1658
  init_wait_for_inferior ();
1659
 
1660
  /* FIXME: Should we set inferior_pid here?  */
1661 113 markom
 
1662 118 markom
  /* Needed to get correct instruction in cache */
1663
  insert_breakpoints ();
1664 106 markom
  clear_proceed_status ();
1665
  or1k_status = TARGET_STOPPED;
1666
  proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1667
}
1668
 
1669
/* Clean up after a process.  Actually nothing to do.  */
1670
 
1671
static void
1672
or1k_mourn_inferior ()
1673
{
1674
  generic_mourn_inferior ();
1675
}
1676
 
1677
static void
1678
or1k_dummy_open (name, from_tty)
1679
     char *name;
1680
     int from_tty;
1681
{
1682
  target_preopen (from_tty);
1683
  if (or1k_is_open)
1684
    unpush_target (current_ops);
1685
  current_or1k_target = &or1k_target_dummy;
1686
  or1k_open (name, from_tty);
1687
}
1688
 
1689
static void
1690
or1k_jtag_open (name, from_tty)
1691
     char *name;
1692
     int from_tty;
1693
{
1694
  target_preopen (from_tty);
1695
  if (or1k_is_open)
1696
    unpush_target (current_ops);
1697
  current_or1k_target = &or1k_target_jtag;
1698
  or1k_open (name, from_tty);
1699
}
1700
 
1701
static void
1702
or1k_sim_open (name, from_tty)
1703
     char *name;
1704
     int from_tty;
1705
{
1706
  /* target_preopen (from_tty); - do we need this ? */
1707
  if (or1k_is_open)
1708
    unpush_target (current_ops);
1709
  current_or1k_target = &or1k_target_sim;
1710
  or1k_open (name, from_tty);
1711
}
1712
 
1713
/* Executes command on the target.  */
1714
 
1715
void
1716
or1k_sim_cmd (char *args, int from_tty)
1717
{
1718
  if (current_or1k_target != NULL && current_or1k_target->to_exec_command != NULL)
1719
    current_or1k_target->to_exec_command (args, from_tty);
1720
  else
1721
    error ("Command not supported on this target. ");
1722
}
1723
 
1724 113 markom
/* Displays matchpoints usage.  */
1725
 
1726 106 markom
void
1727 113 markom
info_matchpoints_command (char *args, int from_tty)
1728
{
1729
  int i;
1730
  for (i = 0; i < or1k_implementation.num_matchpoints; i++)
1731
    {
1732
      printf_filtered ("WP%i ", i);
1733
      if (dcr[i].dp)
1734
        {
1735
          int chaining = (dmr1 << 2*i) & 3;
1736
          printf_filtered ("= %s ", ct_names[dcr[i].ct]);
1737
          if (dcr[i]. sc)
1738
            printf_filtered ("s%s %i", cc_names[dcr[i].cc], (int)dvr[i]);
1739
          else
1740
            printf_filtered ("%s %u", cc_names[dcr[i].cc], (unsigned int)dvr[i]);
1741
          if (chaining)
1742
            printf_filtered ("%s WP%i", ch_names[chaining], i - 1);
1743
        }
1744
      else
1745
        printf_filtered ("NOT USED");
1746
      if ((dmr2 >> i) & 1)
1747
        printf_filtered (", causes breakpoint");
1748
      if ((dmr2 >> (i + 11)) & 1)
1749
        printf_filtered (", increments counter");
1750
      printf_filtered ("\n");
1751
    }
1752
}
1753 372 markom
static int
1754
insn_has_delay_slot (insn)
1755
     unsigned int insn;
1756
{
1757
  if (((insn >> 26) <= 4) || ((insn >> 26) == 17) || ((insn >> 26) == 18))
1758
    return 1;
1759
  else
1760
    return 0;
1761
}
1762 113 markom
 
1763
void
1764 106 markom
_initialize_remote_or1k ()
1765
{
1766
  /* Initialize the fields in or1k_ops that are common to all targets.  */
1767
  or1k_dummy_ops.to_close = or1k_close;
1768
  or1k_dummy_ops.to_detach = or1k_detach;
1769
  or1k_dummy_ops.to_resume = or1k_resume;
1770 362 markom
  or1k_dummy_ops.to_wait = or1k_wait;
1771 106 markom
  or1k_dummy_ops.to_fetch_registers = or1k_fetch_registers;
1772
  or1k_dummy_ops.to_store_registers = or1k_store_registers;
1773
  or1k_dummy_ops.to_prepare_to_store = or1k_prepare_to_store;
1774
  or1k_dummy_ops.to_xfer_memory = or1k_xfer_memory;
1775
  or1k_dummy_ops.to_files_info = or1k_files_info;
1776
  or1k_dummy_ops.to_insert_breakpoint = or1k_insert_breakpoint;
1777
  or1k_dummy_ops.to_remove_breakpoint = or1k_remove_breakpoint;
1778
  or1k_dummy_ops.to_kill = or1k_kill;
1779
  or1k_dummy_ops.to_load = generic_load;
1780
  or1k_dummy_ops.to_create_inferior = or1k_create_inferior;
1781
  or1k_dummy_ops.to_mourn_inferior = or1k_mourn_inferior;
1782
  or1k_dummy_ops.to_stratum = process_stratum;
1783 362 markom
  or1k_dummy_ops.to_stop = or1k_stop;
1784 118 markom
 
1785
  /* We can access memory while program is running.  */
1786
  or1k_dummy_ops.to_has_all_memory = 0;
1787
 
1788 106 markom
  or1k_dummy_ops.to_has_memory = 1;
1789
  or1k_dummy_ops.to_has_stack = 1;
1790
  or1k_dummy_ops.to_has_registers = 1;
1791
  or1k_dummy_ops.to_has_execution = 1;
1792
  or1k_dummy_ops.to_magic = OPS_MAGIC;
1793
 
1794
  /* Copy the common fields to all target vectors.  */
1795
  or1k_jtag_ops = or1k_sim_ops = or1k_dummy_ops;
1796 362 markom
 
1797
  /* Initialize target-specific fields in the target vectors adn add targets.  */
1798 106 markom
  or1k_jtag_ops.to_shortname = "jtag";
1799
  or1k_jtag_ops.to_longname = "Remote or1k debugging over JTAG port";
1800 125 chris
  or1k_jtag_ops.to_doc = "Debug a board using the OR1K remote debugging protocol"
1801
    " over a parallel line.\nThe argument is the parallel port it is connected "
1802
    "to, or, if it is formatted\nas a URL of the form jtag://<hostname>:<port>,"
1803
    " then the argument refers to\na remote JTAG proxy server.\n";
1804 362 markom
  or1k_jtag_ops.to_open = or1k_jtag_open;
1805 106 markom
  add_target (&or1k_jtag_ops);
1806
 
1807
  or1k_dummy_ops.to_shortname = "dummy";
1808
  or1k_dummy_ops.to_longname = "Dummy target";
1809
  or1k_dummy_ops.to_doc = "Actually no real target attached - more like /dev/null.\n";
1810
  or1k_dummy_ops.to_open = or1k_dummy_open;
1811
  add_target (&or1k_dummy_ops);
1812
 
1813
  or1k_sim_ops.to_shortname = "sim";
1814
  or1k_sim_ops.to_longname = "Remote or1k debugging using architecture simulator";
1815
  or1k_sim_ops.to_doc = "Debug using an architecture simulator.\n";
1816
  or1k_sim_ops.to_open = or1k_sim_open;
1817
  add_target (&or1k_sim_ops);
1818 113 markom
  add_info ("matchpoints", info_matchpoints_command, "Show current matchpoints allocation status.");
1819 106 markom
}

powered by: WebSVN 2.1.0

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