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 374

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

powered by: WebSVN 2.1.0

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