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 150

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 150 chris
  or1k_write_reg (regno + REG_SPACE, (ULONGEST)data);
290 113 markom
}
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 150 chris
 
629 106 markom
  /* Clear reason register for later.  */
630 113 markom
  or1k_write_spr_reg (DRR_SPRNUM, 0);
631 106 markom
 
632 150 chris
  /* Else clause added by CZ 26/06/01 */
633 106 markom
  if (step)
634
    {
635
      /* HW STEP.  Set DMR1_ST.  */
636
      dmr1 |= DMR1_ST;
637 113 markom
      or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
638 106 markom
      dmr1 &= ~DMR1_ST;
639
    }
640 150 chris
  else
641
    {
642
      dmr1 &= ~DMR1_ST;
643
      or1k_write_spr_reg (DMR1_SPRNUM, dmr1);
644
    }
645 106 markom
 
646 118 markom
  or1k_commit_debug_registers ();
647 106 markom
  /* Run the target. */
648
  or1k_unstall ();
649
  or1k_status = TARGET_RUNNING;
650
}
651
 
652
/* Wait until the remote stops, and return a wait status.  */
653
 
654
static int
655
or1k_wait (pid, status)
656
     int pid;
657
     struct target_waitstatus *status;
658
{
659
  interrupt_count = 0;
660
 
661
  /* If we have not sent a single step or continue command, then the
662
     board is waiting for us to do something.  Return a status
663
     indicating that it is stopped.  */
664
  if (or1k_status != TARGET_RUNNING)
665
    {
666
      if (or1k_status != TARGET_STOPPED)
667
        error("Target in invalid state.");
668
      status->kind = TARGET_WAITKIND_STOPPED;
669
      status->value.sig = TARGET_SIGNAL_TRAP;
670
      return 0;
671
    }
672
 
673
  if (err)
674
    or1k_error ("Remote failure: %s", or1k_err_name (err));
675
 
676
  /* Wait for or1k DRR register to be nonzero.  */
677
  do
678
    {
679 113 markom
      drr = or1k_read_spr_reg (DRR_SPRNUM);
680 106 markom
      usleep (10);
681
    }
682
  while (drr == 0);
683
 
684
  status->kind = TARGET_WAITKIND_STOPPED;
685 113 markom
  or1k_flush_pipeline ();
686 106 markom
 
687
  if (drr & DRR_RSTE)
688
    status->value.sig = TARGET_SIGNAL_REALTIME_33;
689
  else if (drr & DRR_BUSEE)
690
    status->value.sig = TARGET_SIGNAL_BUS;
691
  else if (drr & DRR_DPFE)
692
    status->value.sig = TARGET_SIGNAL_REALTIME_34;
693
  else if (drr & DRR_IPFE)
694
    status->value.sig = TARGET_SIGNAL_REALTIME_35;
695
  else if (drr & DRR_LPINTE)
696
    status->value.sig = TARGET_SIGNAL_INT;
697
  else if (drr & DRR_AE)
698
    status->value.sig = TARGET_SIGNAL_REALTIME_36;
699
  else if (drr & DRR_IIE)
700
    status->value.sig = TARGET_SIGNAL_ILL;
701
  else if (drr & DRR_HPINTE)
702
    status->value.sig = TARGET_SIGNAL_INT;
703
  else if (drr & DRR_DME)
704
    status->value.sig = TARGET_SIGNAL_REALTIME_37;
705
  else if (drr & DRR_IME)
706
    status->value.sig = TARGET_SIGNAL_REALTIME_38;
707
  else if (drr & DRR_RE)
708
    status->value.sig = TARGET_SIGNAL_REALTIME_39;
709
  else if (drr & DRR_SCE)
710
    status->value.sig = TARGET_SIGNAL_REALTIME_40;
711
  else if (drr & DRR_BE)
712
    status->value.sig = TARGET_SIGNAL_TRAP;
713
  else
714
    {
715
      status->value.sig = TARGET_SIGNAL_UNKNOWN;
716
      warning ("Invalid exception occured.");
717
    }
718
 
719 113 markom
  /* Log remote stop.  */
720 106 markom
  or1k_status = TARGET_STOPPED;
721 113 markom
 
722
  /* Determine what caused trap - breakpoint or watchpoint.  */
723
  if (status->value.sig == TARGET_SIGNAL_TRAP)
724
    {
725
      /* Search all active breakpoints for a match.  */
726
      CORE_ADDR pc = read_pc ();
727
      int breakpoint = 0;
728
      int i;
729 150 chris
      unsigned char break_bytes[4] = BRK_INSTR_STRUCT;
730
      unsigned long b_insn = ntohl(*((unsigned long*)break_bytes));
731
      unsigned long value;
732 143 chris
 
733 113 markom
      for (i = 0; i < or1k_implementation.num_used_matchpoints; i++)
734 118 markom
        if (dvr[i] == pc && dcr[i].dp && dcr[i].cc == CC_EQUAL
735
            && !dcr[i].sc && dcr[i].ct == CT_FETCH)
736 113 markom
          {
737
            breakpoint = 1;
738
            break;
739
          }
740
      hit_watchpoint = !breakpoint;
741 143 chris
 
742
      /* Cause the trap/breakpoint exception to be ignored. This is
743
         the behavior of the simulator when the PC value is changed
744
         by a write command. All pending exceptions are cleared and
745 150 chris
         the simulator continues at the PC value specified. We need
746
         to do this if the instruction at the current PC has the
747
         value BRK_INSTR_STRUCT */
748
 
749
      if(b_insn == or1k_read_reg((pc >> 2) + MEM_SPACE))
750
        {
751
          or1k_write_spr_reg(PC_SPRNUM,value);
752
        }
753 113 markom
    }
754
  else
755
    hit_watchpoint = 0;
756 106 markom
 
757
  /* If the stop PC is in the _exit function, assume
758
     we hit the 'break 0x3ff' instruction in _exit, so this
759
     is not a normal breakpoint.  */
760
  {
761
    char *func_name;
762
    CORE_ADDR func_start;
763
    CORE_ADDR pc = read_pc ();
764
 
765
    find_pc_partial_function (pc, &func_name, &func_start, NULL);
766
    if (func_name != NULL && strcmp (func_name, "_exit") == 0
767
        && func_start == pc)
768
      status->kind = TARGET_WAITKIND_EXITED;
769
  }
770 118 markom
 
771
  or1k_read_trace ();
772 106 markom
  return 0;
773
}
774
 
775 113 markom
/* Fetch a word from the target board.  All memory accesses to the
776
   remote board are word aligned.  */
777 106 markom
 
778 118 markom
unsigned int
779 106 markom
or1k_fetch_word (addr)
780
     CORE_ADDR addr;
781
{
782 113 markom
  if (addr & 3)
783
    {
784
      int subaddr = addr & 3;
785
      unsigned char buf[8];
786
      unsigned int low, high;
787
      addr >>= 2;
788
      low = or1k_read_reg (addr + MEM_SPACE);
789
      high = or1k_read_reg (addr + 1 + MEM_SPACE);
790
      memcpy (&buf[0], &low, 4);
791
      memcpy (&buf[4], &high, 4);
792
      memcpy (&low, &buf[subaddr], 4);
793
      return low;
794
    }
795
  else
796
    {
797
      addr >>= 2;
798
      return or1k_read_reg (addr + MEM_SPACE);
799
    }
800 106 markom
}
801
 
802
/* Store a word to the target board.  Returns errno code or zero for
803 113 markom
   success.  All memory accesses to the remote board are word aligned.  */
804 118 markom
 
805 106 markom
static int
806
or1k_store_word (addr, val)
807
     CORE_ADDR addr;
808
     unsigned int val;
809
{
810 113 markom
  if (addr & 3)
811
    {
812
      int subaddr = addr & 3;
813
      unsigned char buf[8];
814
      unsigned int low, high;
815
      addr >>= 2;
816
      low = or1k_read_reg (addr + MEM_SPACE);
817
      high = or1k_read_reg (addr + 1 + MEM_SPACE);
818
      memcpy (&buf[0], &low, 4);
819
      memcpy (&buf[4], &high, 4);
820
      memcpy (&buf[subaddr], &val, 4);
821
      memcpy (&low, &buf[0], 4);
822
      memcpy (&high, &buf[4], 4);
823
      or1k_write_reg (addr + MEM_SPACE, low);
824
      or1k_write_reg (addr + 1 + MEM_SPACE, high);
825
    }
826
  else
827
    {
828
      addr >>= 2;
829
      or1k_write_reg (addr + MEM_SPACE, val);
830
    }
831 106 markom
  return err;
832
}
833
 
834
/* Fetch the remote registers.  */
835
 
836
void
837
or1k_fetch_registers (regno)
838
     int regno;
839
{
840
  unsigned int val;
841
 
842
  if (regno == -1)
843
    {
844
      for (regno = 0; regno < NUM_REGS; regno++)
845
        or1k_fetch_registers (regno);
846
      return;
847
    }
848
 
849
  if (regno >= NUM_REGS)
850
    error("Invalid register number!");
851
 
852
  /* Convert to SPRNUM and read.  */
853 113 markom
  val = or1k_read_spr_reg (REGNUM_TO_SPRNUM(regno));
854 106 markom
 
855
  {
856
    char buf[MAX_REGISTER_RAW_SIZE];
857
 
858
    /* We got the number the register holds, but gdb expects to see a
859
       value in the target byte ordering.  */
860
    store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
861
    supply_register (regno, buf);
862
  }
863
  if (err)
864 118 markom
    or1k_error ("Can't read register %d(%i): %s", regno,
865
                REGNUM_TO_SPRNUM(regno) + REG_SPACE, or1k_err_name (err));
866 106 markom
}
867
 
868
/* Fetch and return instruction from the specified location.  */
869
 
870
unsigned int
871
or1k_fetch_instruction (addr)
872
     CORE_ADDR addr;
873
{
874
  char buf[OR1K_INSTLEN];
875
  int status;
876
 
877
  status = read_memory_nobpt (addr, buf, OR1K_INSTLEN);
878
  if (status)
879
    memory_error (status, addr);
880
  return extract_unsigned_integer (buf, OR1K_INSTLEN);
881
}
882
 
883 113 markom
/* Currently not needed.  */
884
 
885 106 markom
static void
886
or1k_prepare_to_store ()
887
{
888
}
889
 
890
/* Store remote register(s).  */
891
 
892
static void
893
or1k_store_registers (regno)
894
     int regno;
895
{
896
  if (regno == -1)
897
    {
898
      for (regno = 0; regno < NUM_REGS; regno++)
899
        or1k_store_registers (regno);
900
      return;
901
    }
902
 
903
  if (regno >= NUM_REGS)
904
    error("Invalid register number!");
905
 
906 113 markom
  or1k_write_spr_reg (REGNUM_TO_SPRNUM(regno), or1k_read_spr_reg (REGNUM_TO_SPRNUM(regno)));
907 106 markom
  if (err)
908 113 markom
    or1k_error ("Can't write register %d(%i): %s", regno, REGNUM_TO_SPRNUM(regno), or1k_err_name (err));
909 106 markom
}
910
 
911
/* Read or write LEN bytes from inferior memory at MEMADDR,
912
   transferring to or from debugger address MYADDR.  Write to inferior
913
   if SHOULD_WRITE is nonzero.  Returns length of data written or
914
   read; 0 for error.  Note that protocol gives us the correct value
915
   for a longword, since it transfers values in ASCII.  We want the
916
   byte values, so we have to swap the longword values.  */
917
 
918
static int
919
or1k_xfer_memory (memaddr, myaddr, len, write, ignore)
920
     CORE_ADDR memaddr;
921
     char *myaddr;
922
     int len;
923
     int write;
924
     struct target_ops *ignore;
925
{
926
  register int i;
927
  /* Round starting address down to longword boundary.  */
928
  register CORE_ADDR addr = memaddr & ~3;
929
  /* Round ending address up; get number of longwords that makes.  */
930
  register int count = (((memaddr + len) - addr) + 3) / 4;
931
  /* Allocate buffer of that many longwords.  */
932
  register char *buffer = alloca (count * 4);
933
  int status;
934
 
935 143 chris
  int block_xfer_size = 256; /* CZ 21/06/01 ... number of 32 bit words */
936
  int nBlocks = (count + block_xfer_size -1)/block_xfer_size;
937
  int terminate = 0;  /* Terminate the printing of '*'s... */
938
 
939 106 markom
  if (memaddr >= MEM_SPACE)
940
    error("Invalid address");
941
 
942 135 chris
  /* (CZ 21/06/01 -- because upper layers which know nothing about
943
     Or1k or JTAG call this function directly, it is always necessary
944
     to set the chain to point to the Debug Unit. Otherwise, it may
945 143 chris
     be pointing to the Development Interface chain, in which case
946 135 chris
     we're going to get bupkiss... */
947
 
948
  or1k_set_chain (SC_RISC_DEBUG);
949
 
950 106 markom
  if (write)
951
    {
952
      /* Fill start and end extra bytes of buffer with existing data.  */
953
      if (addr != memaddr || len < 4)
954
        {
955
          /* Need part of initial word -- fetch it.  */
956
          store_unsigned_integer (&buffer[0], 4, or1k_fetch_word (addr));
957
        }
958
 
959
      if (count > 1)
960
        {
961
          /* Need part of last word -- fetch it.  FIXME: we do this even
962
             if we don't need it.  */
963
          store_unsigned_integer (&buffer[(count - 1) * 4], 4,
964
                                  or1k_fetch_word (addr + (count - 1) * 4));
965
        }
966
 
967
      /* Copy data to be written over corresponding part of buffer */
968
      memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
969
 
970 143 chris
      /* CZ: rewrote the block transfer routines to make the code
971
         a little more efficient for implementations that can handle
972
         variable sized scan chains. Might be useful in the future.
973
         Certainly makes downloads to the simulator more efficient. */
974
      for(i=0;i<nBlocks;i++,count-=block_xfer_size,addr += block_xfer_size*4)
975 106 markom
        {
976 143 chris
          int j;
977
          int n = count < block_xfer_size ? count : block_xfer_size;
978
          unsigned long *__buf;
979
 
980
          if(!(__buf = (unsigned long*)malloc(n*sizeof(unsigned long))))
981 106 markom
            {
982 143 chris
              errno = ERR_MEM;
983
              return 0;
984
            }
985
 
986
          for(j=0;j<n;j++)
987
            __buf[j] = (unsigned long)extract_unsigned_integer(&buffer[(i * block_xfer_size +j)*4], 4);
988
          status = or1k_store_block(addr,__buf,n);
989
          free(__buf);
990
          if(n == block_xfer_size)
991
            {
992 106 markom
              printf_unfiltered ("*");
993
              gdb_flush (gdb_stdout);
994
            }
995
          if (status)
996
            {
997
              errno = status;
998
              return 0;
999
            }
1000
          /* FIXME: Do we want a QUIT here?  */
1001
        }
1002 143 chris
      if (terminate)
1003 106 markom
        printf_unfiltered ("\n");
1004
    }
1005
  else
1006
    {
1007 143 chris
      for(i=0;i<nBlocks;i++,count-=block_xfer_size,addr += block_xfer_size*4)
1008 106 markom
        {
1009 143 chris
          int j;
1010
          int n = count < block_xfer_size ? count : block_xfer_size;
1011
          unsigned long *__buf;
1012
 
1013
          __buf = (unsigned long*)malloc(n*sizeof(unsigned long));
1014
          status = or1k_load_block(addr,__buf,n);
1015
          if (!status)
1016
            for(j=0;j<n;j++)
1017
              store_unsigned_integer (&buffer[(i * block_xfer_size +j)*4], 4, __buf[j]);
1018
          else
1019
            errno = status;
1020
          free(__buf);
1021
 
1022
          if(status)
1023
            return 0;
1024 106 markom
        }
1025
      /* Copy appropriate bytes out of the buffer.  */
1026
      memcpy (myaddr, buffer + (memaddr & 3), len);
1027
    }
1028
  return len;
1029
}
1030
 
1031 143 chris
int or1k_load_block(CORE_ADDR addr,void* buffer,int nRegisters)
1032
{
1033
  int i=0;
1034
  unsigned int regno = (addr >> 2) + MEM_SPACE;
1035
 
1036
  if (current_or1k_target != NULL && current_or1k_target->to_read_block != NULL)
1037
    return current_or1k_target->to_read_block (regno,buffer,nRegisters);
1038
  else
1039
    for(i=0;i<nRegisters;i++)
1040
      ((unsigned long*)buffer)[i] = 0x1234;
1041
  return 0;
1042
}
1043
 
1044
int or1k_store_block(CORE_ADDR addr,void* buffer,int nRegisters)
1045
{
1046
  unsigned int regno = (addr >> 2) + MEM_SPACE;
1047
 
1048
  if (current_or1k_target != NULL && current_or1k_target->to_write_block != NULL)
1049
    return current_or1k_target->to_write_block (regno,buffer,nRegisters);
1050
  return 0;
1051
}
1052
 
1053 113 markom
/* Flushes pipeline. May not be needed by all implementations, but
1054
   it doen't hurt to do it. This function should be called every time
1055
   or1k stops.
1056
   When or1k stops it still has instructions in pipeline.
1057
   We do this by inserting nop instructions.
1058
   IF cycle remains unaffacted by writing to DIR, and it still holds
1059
   instruction, that caused break or watchpoint. */
1060
 
1061
void
1062
or1k_flush_pipeline ()
1063
{
1064
  or1k_write_spr_reg (DIR_SPRNUM, NOP_INSTR);
1065
  or1k_write_spr_reg (DIR_SPRNUM, NOP_INSTR);
1066
  or1k_write_spr_reg (DIR_SPRNUM, NOP_INSTR);
1067
}
1068
 
1069 106 markom
/* Print info on this target.  */
1070
 
1071
static void
1072
or1k_files_info (ignore)
1073
     struct target_ops *ignore;
1074
{
1075
  char *file = "nothing";
1076
 
1077
  if (exec_bfd)
1078
    file = bfd_get_filename (exec_bfd);
1079
 
1080
  printf_filtered ("or1k_files_info: file \"%s\"\n", file);
1081
 
1082
  if (exec_bfd)
1083
    {
1084
      printf_filtered ("\tAttached to %s running program %s\n",
1085
                       target_shortname, file);
1086
    }
1087
  /* Print target info. */
1088
  printf_filtered ("Status: %s\n", status_name[or1k_status]);
1089
}
1090
 
1091
/* Tell whether we can support a hardware breakpoint.  */
1092 118 markom
 
1093 106 markom
static int
1094
or1k_can_use_hardware_breakpoint ()
1095
{
1096
  int i;
1097 118 markom
 
1098 106 markom
  /* Search for unused breakpoint.  */
1099 113 markom
  return or1k_implementation.num_used_matchpoints < or1k_implementation.num_matchpoints;
1100 106 markom
}
1101
 
1102
/* Insert a breakpoint.  On targets that don't have built-in breakpoint
1103
   support, we read the contents of the target location and stash it,
1104
   then overwrite it with a breakpoint instruction.  ADDR is the target
1105
   location in the target machine.  CONTENTS_CACHE is a pointer to
1106
   memory allocated for saving the target contents.  It is guaranteed
1107
   by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1108
   is accomplished via BREAKPOINT_MAX).  */
1109
 
1110 113 markom
int
1111 106 markom
or1k_insert_breakpoint (addr, contents_cache)
1112
     CORE_ADDR addr;
1113
     char *contents_cache;
1114
{
1115
  if (or1k_can_use_hardware_breakpoint())
1116
    return set_breakpoint (addr);
1117
  else
1118
    return memory_insert_breakpoint (addr, contents_cache);
1119
}
1120
 
1121 113 markom
int
1122 106 markom
or1k_remove_breakpoint (addr, contents_cache)
1123
     CORE_ADDR addr;
1124
     char *contents_cache;
1125
{
1126
  /* First try to remove HW breakpoint at address */
1127
  if (clear_breakpoint (addr))
1128
    return memory_remove_breakpoint (addr, contents_cache);
1129
  else
1130
    return 0;
1131
}
1132
 
1133 113 markom
/* Tell whether this target can support a hardware breakpoint.  CNT
1134
   is the number of hardware breakpoints already installed.  This
1135
   implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro.
1136
   Lower bound is estimated. !!! Can we estimate better? */
1137
 
1138
int
1139
or1k_can_use_hardware_watchpoint (bp_type, cnt)
1140
     enum bptype bp_type;
1141
     int cnt;
1142
{
1143
  /* Are there at least two matchpoints left for watch? - estimate lower bound  */
1144
  return cnt + ((bp_type == bp_hardware_watchpoint)?(1):(0))
1145
    <= or1k_implementation.num_matchpoints;
1146
}
1147
 
1148 118 markom
/* Moves matchpoint.  This is very tricky - we have to update
1149
   all references to matchpoint indexes.  We assume here that
1150
   matchpoint with index to is unused! */
1151
 
1152
static void
1153
move_matchpoint (int from, int to)
1154
{
1155
  int i, j, tmp, chaining;
1156
  if (matchpoint_user_count[to] != 0)
1157
    error ("Internal: Destination matchpoint still has users");
1158
  matchpoint_user_count[to] = matchpoint_user_count[from];
1159
  matchpoint_user_count[from] = 0;
1160
  debug_regs_changed = 1;
1161
 
1162
  dvr[to] = dvr[from];
1163
  dcr[to] = dcr[from];
1164
  dcr[from].dp = 0;
1165
 
1166
  /* Copy chaining bits.  */
1167
  chaining = dmr1 & (3 << (2 * from));
1168
  dmr1 &= ~(3 << (2 * to));
1169
  dmr1 |= chaining << (2 * to);
1170
  dmr1 &= ~(3 << (2 * from));
1171
 
1172
  /* Copy watchpoint bits */
1173
  tmp = dmr2 & (1 << from);
1174
  dmr2 &= 1 << to;
1175
  dmr2 |= tmp << to;
1176
  dmr2 &= 1 << from;
1177
 
1178
  /* Update hwatch table.  Here we assume that matchpoint
1179
     group is connected (it cannot be implemented in HW
1180
     otherwise), so if we move first, we will have to move
1181
     others later.  */
1182
  for (i = 0; i < num_hw_watches; i++)
1183
    if (or1k_hwatch[i].matchpoint_start == from)
1184
      or1k_hwatch[i].matchpoint_start = to;
1185
 
1186
  /* Update htrace struct.  */
1187
  tmp = or1k_htrace.trig.wp_trig & (1 << from);
1188
  or1k_htrace.trig.wp_trig &= 1 << to;
1189
  or1k_htrace.trig.wp_trig |= tmp << to;
1190
  or1k_htrace.trig.wp_trig &= 1 << from;
1191
 
1192
  tmp = or1k_htrace.qual.wp_trig & (1 << from);
1193
  or1k_htrace.qual.wp_trig &= 1 << to;
1194
  or1k_htrace.qual.wp_trig |= tmp << to;
1195
  or1k_htrace.qual.wp_trig &= 1 << from;
1196
 
1197
  tmp = or1k_htrace.stop.wp_trig & (1 << from);
1198
  or1k_htrace.stop.wp_trig &= 1 << to;
1199
  or1k_htrace.stop.wp_trig |= tmp << to;
1200
  or1k_htrace.stop.wp_trig &= 1 << from;
1201
 
1202
  for (i = 0; i < MAX_MATCHPOINTS; i++)
1203
    {
1204
      tmp = or1k_htrace.wp_record_uses[i] & (1 << from);
1205
      or1k_htrace.wp_record_uses[i] &= 1 << to;
1206
      or1k_htrace.wp_record_uses[i] |= tmp << to;
1207
      or1k_htrace.wp_record_uses[i] &= 1 << from;
1208
    }
1209
 
1210
  /* Do we need to move other references also? */
1211
}
1212
 
1213 113 markom
/* Sifts unused matchpoints to higher indexses.  */
1214
 
1215 118 markom
void
1216 113 markom
sift_matchpoints ()
1217
{
1218
  int i, first_free = 0;
1219
  for (i = 0; i < or1k_implementation.num_matchpoints; i++)
1220
    if (dcr[i].dp)
1221
      {
1222 118 markom
        /* Move references.  */
1223
        move_matchpoint (i, first_free);
1224
 
1225 113 markom
        first_free++;
1226
      }
1227 118 markom
 
1228
  /* Unused matchpoints should be disabled by move_matchpoint,
1229
     so we are done here.  */
1230 113 markom
}
1231
 
1232
/* Translates gdb watchpoint type into one in DCR register.  */
1233
 
1234
static int
1235
translate_type (gdb_type)
1236
     int gdb_type;
1237
{
1238
  switch (gdb_type)
1239
    {
1240
    case 0:
1241
      return CT_SDATA;
1242
    case 1:
1243
      return CT_LDATA;
1244
    case 2:
1245
      return CT_ADATA;
1246
    default:
1247
      error ("Invalid type.");
1248
    }
1249
}
1250
 
1251
/* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1252
   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1253
   watchpoint. */
1254
 
1255
int
1256
or1k_insert_watchpoint (addr, len, type)
1257
     CORE_ADDR addr;
1258
     int len;
1259
     int type;
1260
{
1261
  int i;
1262 118 markom
 
1263 113 markom
  if (len < 1)
1264
    return -1;
1265
 
1266
  type = translate_type (type);
1267
 
1268
  /* Moves unused watchpoints to the top.  */
1269
  sift_matchpoints ();
1270 118 markom
 
1271 113 markom
  /* Place at first free matchpoint.  */
1272
  i = or1k_implementation.num_used_matchpoints;
1273
  dvr[i] = addr;
1274
  dcr[i].dp = 1;
1275
  dcr[i].cc = CC_GREATE;
1276
  dcr[i].sc = 0;
1277
  dcr[i].ct = type;
1278 118 markom
 
1279 113 markom
  /* Set && chaining here.  */
1280
  dmr1 &= ~(3 << (2 * i));
1281 118 markom
  dmr1 |= CHAINING_AND << (2 * i);
1282
 
1283 113 markom
  /* Set upper watchpoint bound.  */
1284
  i++;
1285
  dvr[i] = addr + len - 1;
1286
  dcr[i].dp = 1;
1287
  dcr[i].cc = CC_LESSE;
1288
  dcr[i].sc = 0;
1289
  dcr[i].ct = type;
1290 118 markom
 
1291 113 markom
  /* Matchpoints will cause breakpoints */
1292 118 markom
  dmr2 |= (1 << i);
1293 113 markom
  or1k_implementation.num_used_matchpoints += 2;
1294 118 markom
  debug_regs_changed = 1;
1295 113 markom
  return 0;
1296
}
1297
 
1298
/* Removes a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
1299
   for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1300
   watchpoint. */
1301 118 markom
 
1302 113 markom
int
1303
or1k_remove_watchpoint (addr, len, type)
1304
     CORE_ADDR addr;
1305
     int len;
1306
     int type;
1307
{
1308
  int i, found = -1;
1309 118 markom
 
1310 113 markom
  if (len < 1)
1311
    return -1;
1312
 
1313
  type = translate_type (type);
1314
 
1315
  /* Find the right one.  */
1316
  for (i = 0; i < or1k_implementation.num_used_matchpoints; i++)
1317
    if (dvr[i] == addr && dcr[i].dp && dcr[i].cc == CC_GREATE && !dcr[i].sc && dcr[i].ct == type
1318
        && dvr[i + 1] == addr + len - 1 && dcr[i + 1].dp && dcr[i + 1].cc == CC_LESSE
1319
        && !dcr[i + 1].sc && dcr[i + 1].ct == type)
1320
      {
1321
        found = i;
1322
        break;
1323
      }
1324
 
1325
  if (found < 0)
1326
    return -1;
1327
 
1328
  dcr[found].dp = 0;
1329
  dcr[found + 1].dp = 0;
1330
 
1331
  /* Matchpoints will not cause breakpoints anymore. */
1332 118 markom
  dmr2 &= ~(1 << i);
1333 113 markom
  or1k_implementation.num_used_matchpoints -= 2;
1334 118 markom
  debug_regs_changed = 1;
1335 113 markom
  return 0;
1336
}
1337
 
1338
int
1339
or1k_stopped_by_watchpoint (void)
1340
{
1341 143 chris
  /* For now, no watchpoints */
1342
  return 0;
1343
 
1344
  /* return hit_watchpoint; */
1345 113 markom
}
1346
 
1347 106 markom
/* Insert a breakpoint.  */
1348
 
1349
int
1350
set_breakpoint (addr)
1351
     CORE_ADDR addr;
1352
{
1353
  int i;
1354 118 markom
 
1355 106 markom
  /* Search for unused breakpoint.  */
1356
  for (i = 0; i < NUM_MATCHPOINTS; i++)
1357
    if (dcr[i].dp == 0) break;
1358
  if (i >= NUM_MATCHPOINTS) return 1;
1359
  dvr[i] = addr;
1360
  dcr[i].dp = 1;
1361
  dcr[i].cc = CC_EQUAL;
1362
  dcr[i].sc = 0;
1363
  dcr[i].ct = CT_FETCH;
1364 113 markom
  or1k_implementation.num_used_matchpoints++;
1365
 
1366
  /* No chaining here.  */
1367
  dmr1 &= ~(3 << (2*i));
1368 118 markom
 
1369 113 markom
  /* Matchpoints will cause breakpoints */
1370 118 markom
  dmr2 |= (1 << i);
1371
  debug_regs_changed = 1;
1372 106 markom
  return 0;
1373
}
1374
 
1375
/* Clear a breakpoint.  */
1376
 
1377
int
1378
clear_breakpoint (addr)
1379
     CORE_ADDR addr;
1380
{
1381
  int i;
1382 118 markom
 
1383 106 markom
  /* Search for matching breakpoint.  */
1384
  for (i = 0; i < NUM_MATCHPOINTS; i++)
1385
    if ((dcr[i].dp == 1) && (dvr[i] == addr) && (dcr[i].cc == CC_EQUAL)
1386
        && (dcr[i].sc == 0) && (dcr[i].ct == CT_FETCH)) break;
1387
 
1388
  if (i >= NUM_MATCHPOINTS) return 1;
1389
  dcr[i].dp = 0;
1390 118 markom
 
1391 113 markom
  /* Matchpoints will cause breakpoints */
1392 118 markom
  dmr2 &= ~(1 << i);
1393 113 markom
  or1k_implementation.num_used_matchpoints--;
1394 118 markom
  debug_regs_changed = 1;
1395 106 markom
  return 0;
1396
}
1397
 
1398
/* Start running on the target board.  */
1399
 
1400
static void
1401
or1k_create_inferior (execfile, args, env)
1402
     char *execfile;
1403
     char *args;
1404
     char **env;
1405
{
1406
  CORE_ADDR entry_pt;
1407
 
1408
  if (args && *args)
1409
    {
1410 118 markom
      warning ("Can't pass arguments to remote OR1K board; arguments ignored.");
1411
 
1412 106 markom
      /* And don't try to use them on the next "run" command.  */
1413
      execute_command ("set args", 0);
1414
    }
1415
 
1416
  if (execfile == 0 || exec_bfd == 0)
1417
    error ("No executable file specified");
1418
 
1419
  or1k_kill ();
1420
  remove_breakpoints ();
1421
 
1422
  entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1423
  init_wait_for_inferior ();
1424
 
1425
  /* FIXME: Should we set inferior_pid here?  */
1426 113 markom
 
1427 118 markom
  /* Needed to get correct instruction in cache */
1428
  insert_breakpoints ();
1429 106 markom
  clear_proceed_status ();
1430
  or1k_status = TARGET_STOPPED;
1431
  proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1432
}
1433
 
1434
/* Clean up after a process.  Actually nothing to do.  */
1435
 
1436
static void
1437
or1k_mourn_inferior ()
1438
{
1439
  generic_mourn_inferior ();
1440
}
1441
 
1442
static void
1443
or1k_dummy_open (name, from_tty)
1444
     char *name;
1445
     int from_tty;
1446
{
1447
  target_preopen (from_tty);
1448
  if (or1k_is_open)
1449
    unpush_target (current_ops);
1450
  current_or1k_target = &or1k_target_dummy;
1451
  or1k_open (name, from_tty);
1452
}
1453
 
1454
static void
1455
or1k_jtag_open (name, from_tty)
1456
     char *name;
1457
     int from_tty;
1458
{
1459
  target_preopen (from_tty);
1460
  if (or1k_is_open)
1461
    unpush_target (current_ops);
1462
  current_or1k_target = &or1k_target_jtag;
1463
  or1k_open (name, from_tty);
1464
}
1465
 
1466
static void
1467
or1k_sim_open (name, from_tty)
1468
     char *name;
1469
     int from_tty;
1470
{
1471
  /* target_preopen (from_tty); - do we need this ? */
1472
  if (or1k_is_open)
1473
    unpush_target (current_ops);
1474
  current_or1k_target = &or1k_target_sim;
1475
  or1k_open (name, from_tty);
1476
}
1477
 
1478
/* Executes command on the target.  */
1479
 
1480
void
1481
or1k_sim_cmd (char *args, int from_tty)
1482
{
1483
  if (current_or1k_target != NULL && current_or1k_target->to_exec_command != NULL)
1484
    current_or1k_target->to_exec_command (args, from_tty);
1485
  else
1486
    error ("Command not supported on this target. ");
1487
}
1488
 
1489 113 markom
/* Displays matchpoints usage.  */
1490
 
1491 106 markom
void
1492 113 markom
info_matchpoints_command (char *args, int from_tty)
1493
{
1494
  int i;
1495
  for (i = 0; i < or1k_implementation.num_matchpoints; i++)
1496
    {
1497
      printf_filtered ("WP%i ", i);
1498
      if (dcr[i].dp)
1499
        {
1500
          int chaining = (dmr1 << 2*i) & 3;
1501
          printf_filtered ("= %s ", ct_names[dcr[i].ct]);
1502
          if (dcr[i]. sc)
1503
            printf_filtered ("s%s %i", cc_names[dcr[i].cc], (int)dvr[i]);
1504
          else
1505
            printf_filtered ("%s %u", cc_names[dcr[i].cc], (unsigned int)dvr[i]);
1506
          if (chaining)
1507
            printf_filtered ("%s WP%i", ch_names[chaining], i - 1);
1508
        }
1509
      else
1510
        printf_filtered ("NOT USED");
1511
      if ((dmr2 >> i) & 1)
1512
        printf_filtered (", causes breakpoint");
1513
      if ((dmr2 >> (i + 11)) & 1)
1514
        printf_filtered (", increments counter");
1515
      printf_filtered ("\n");
1516
    }
1517
}
1518
 
1519
void
1520 106 markom
_initialize_remote_or1k ()
1521
{
1522
  /* Initialize the fields in or1k_ops that are common to all targets.  */
1523
  or1k_dummy_ops.to_close = or1k_close;
1524
  or1k_dummy_ops.to_detach = or1k_detach;
1525
  or1k_dummy_ops.to_resume = or1k_resume;
1526
  or1k_dummy_ops.to_wait = or1k_wait;
1527
  or1k_dummy_ops.to_fetch_registers = or1k_fetch_registers;
1528
  or1k_dummy_ops.to_store_registers = or1k_store_registers;
1529
  or1k_dummy_ops.to_prepare_to_store = or1k_prepare_to_store;
1530
  or1k_dummy_ops.to_xfer_memory = or1k_xfer_memory;
1531
  or1k_dummy_ops.to_files_info = or1k_files_info;
1532
  or1k_dummy_ops.to_insert_breakpoint = or1k_insert_breakpoint;
1533
  or1k_dummy_ops.to_remove_breakpoint = or1k_remove_breakpoint;
1534
  or1k_dummy_ops.to_kill = or1k_kill;
1535
  or1k_dummy_ops.to_load = generic_load;
1536
  or1k_dummy_ops.to_create_inferior = or1k_create_inferior;
1537
  or1k_dummy_ops.to_mourn_inferior = or1k_mourn_inferior;
1538
  or1k_dummy_ops.to_stratum = process_stratum;
1539 118 markom
 
1540
  /* We can access memory while program is running.  */
1541
  or1k_dummy_ops.to_has_all_memory = 0;
1542
 
1543 106 markom
  or1k_dummy_ops.to_has_memory = 1;
1544
  or1k_dummy_ops.to_has_stack = 1;
1545
  or1k_dummy_ops.to_has_registers = 1;
1546
  or1k_dummy_ops.to_has_execution = 1;
1547
  or1k_dummy_ops.to_magic = OPS_MAGIC;
1548
 
1549
  /* Copy the common fields to all target vectors.  */
1550
  or1k_jtag_ops = or1k_sim_ops = or1k_dummy_ops;
1551
 
1552
  /* Initialize target-specific fields in the target vectors adn add targets.  */
1553
  or1k_jtag_ops.to_shortname = "jtag";
1554
  or1k_jtag_ops.to_longname = "Remote or1k debugging over JTAG port";
1555 125 chris
  or1k_jtag_ops.to_doc = "Debug a board using the OR1K remote debugging protocol"
1556
    " over a parallel line.\nThe argument is the parallel port it is connected "
1557
    "to, or, if it is formatted\nas a URL of the form jtag://<hostname>:<port>,"
1558
    " then the argument refers to\na remote JTAG proxy server.\n";
1559 106 markom
  or1k_jtag_ops.to_open = or1k_jtag_open;
1560
  add_target (&or1k_jtag_ops);
1561
 
1562
  or1k_dummy_ops.to_shortname = "dummy";
1563
  or1k_dummy_ops.to_longname = "Dummy target";
1564
  or1k_dummy_ops.to_doc = "Actually no real target attached - more like /dev/null.\n";
1565
  or1k_dummy_ops.to_open = or1k_dummy_open;
1566
  add_target (&or1k_dummy_ops);
1567
 
1568
  or1k_sim_ops.to_shortname = "sim";
1569
  or1k_sim_ops.to_longname = "Remote or1k debugging using architecture simulator";
1570
  or1k_sim_ops.to_doc = "Debug using an architecture simulator.\n";
1571
  or1k_sim_ops.to_open = or1k_sim_open;
1572
  add_target (&or1k_sim_ops);
1573 113 markom
  add_info ("matchpoints", info_matchpoints_command, "Show current matchpoints allocation status.");
1574 106 markom
}

powered by: WebSVN 2.1.0

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