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 143

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

powered by: WebSVN 2.1.0

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