1130803Smarcel/* Remote debugging interface for Densan DVE-R3900 ROM monitor for
2130803Smarcel   GDB, the GNU debugger.
3130803Smarcel   Copyright 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "gdbcore.h"
24130803Smarcel#include "target.h"
25130803Smarcel#include "monitor.h"
26130803Smarcel#include "serial.h"
27130803Smarcel#include "inferior.h"
28130803Smarcel#include "command.h"
29130803Smarcel#include "gdb_string.h"
30130803Smarcel#include <time.h>
31130803Smarcel#include "regcache.h"
32130803Smarcel#include "mips-tdep.h"
33130803Smarcel
34130803Smarcel/* Type of function passed to bfd_map_over_sections.  */
35130803Smarcel
36130803Smarceltypedef void (*section_map_func) (bfd * abfd, asection * sect, void *obj);
37130803Smarcel
38130803Smarcel/* Packet escape character used by Densan monitor.  */
39130803Smarcel
40130803Smarcel#define PESC 0xdc
41130803Smarcel
42130803Smarcel/* Maximum packet size.  This is actually smaller than necessary
43130803Smarcel   just to be safe.  */
44130803Smarcel
45130803Smarcel#define MAXPSIZE 1024
46130803Smarcel
47130803Smarcel/* External functions.  */
48130803Smarcel
49130803Smarcelextern void report_transfer_performance (unsigned long, time_t, time_t);
50130803Smarcel
51130803Smarcel/* Certain registers are "bitmapped", in that the monitor can only display
52130803Smarcel   them or let the user modify them as a series of named bitfields.
53130803Smarcel   This structure describes a field in a bitmapped register.  */
54130803Smarcel
55130803Smarcelstruct bit_field
56130803Smarcel  {
57130803Smarcel    char *prefix;		/* string appearing before the value */
58130803Smarcel    char *suffix;		/* string appearing after the value */
59130803Smarcel    char *user_name;		/* name used by human when entering field value */
60130803Smarcel    int length;			/* number of bits in the field */
61130803Smarcel    int start;			/* starting (least significant) bit number of field */
62130803Smarcel  };
63130803Smarcel
64130803Smarcel/* Local functions for register manipulation.  */
65130803Smarcel
66130803Smarcelstatic void r3900_supply_register (char *regname, int regnamelen,
67130803Smarcel				   char *val, int vallen);
68130803Smarcelstatic void fetch_bad_vaddr (void);
69130803Smarcelstatic unsigned long fetch_fields (struct bit_field *bf);
70130803Smarcelstatic void fetch_bitmapped_register (int regno, struct bit_field *bf);
71130803Smarcelstatic void r3900_fetch_registers (int regno);
72130803Smarcelstatic void store_bitmapped_register (int regno, struct bit_field *bf);
73130803Smarcelstatic void r3900_store_registers (int regno);
74130803Smarcel
75130803Smarcel/* Local functions for fast binary loading.  */
76130803Smarcel
77130803Smarcelstatic void write_long (char *buf, long n);
78130803Smarcelstatic void write_long_le (char *buf, long n);
79130803Smarcelstatic int debug_readchar (int hex);
80130803Smarcelstatic void debug_write (unsigned char *buf, int buflen);
81130803Smarcelstatic void ignore_packet (void);
82130803Smarcelstatic void send_packet (char type, unsigned char *buf, int buflen, int seq);
83130803Smarcelstatic void process_read_request (unsigned char *buf, int buflen);
84130803Smarcelstatic void count_section (bfd * abfd, asection * s,
85130803Smarcel			   unsigned int *section_count);
86130803Smarcelstatic void load_section (bfd * abfd, asection * s, unsigned int *data_count);
87130803Smarcelstatic void r3900_load (char *filename, int from_tty);
88130803Smarcel
89130803Smarcel/* Miscellaneous local functions.  */
90130803Smarcel
91130803Smarcelstatic void r3900_open (char *args, int from_tty);
92130803Smarcel
93130803Smarcel
94130803Smarcel/* Pointers to static functions in monitor.c for fetching and storing
95130803Smarcel   registers.  We can't use these function in certain cases where the Densan
96130803Smarcel   monitor acts perversely: for registers that it displays in bit-map
97130803Smarcel   format, and those that can't be modified at all.  In those cases
98130803Smarcel   we have to use our own functions to fetch and store their values.  */
99130803Smarcel
100130803Smarcelstatic void (*orig_monitor_fetch_registers) (int regno);
101130803Smarcelstatic void (*orig_monitor_store_registers) (int regno);
102130803Smarcel
103130803Smarcel/* Pointer to static function in monitor. for loading programs.
104130803Smarcel   We use this function for loading S-records via the serial link.  */
105130803Smarcel
106130803Smarcelstatic void (*orig_monitor_load) (char *file, int from_tty);
107130803Smarcel
108130803Smarcel/* This flag is set if a fast ethernet download should be used.  */
109130803Smarcel
110130803Smarcelstatic int ethernet = 0;
111130803Smarcel
112130803Smarcel/* This array of registers needs to match the indexes used by GDB. The
113130803Smarcel   whole reason this exists is because the various ROM monitors use
114130803Smarcel   different names than GDB does, and don't support all the registers
115130803Smarcel   either.  */
116130803Smarcel
117130803Smarcelstatic char *r3900_regnames[] =
118130803Smarcel{
119130803Smarcel  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
120130803Smarcel  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
121130803Smarcel  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
122130803Smarcel  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
123130803Smarcel
124130803Smarcel  "S",				/* PS_REGNUM */
125130803Smarcel  "l",				/* MIPS_EMBED_LO_REGNUM */
126130803Smarcel  "h",				/* MIPS_EMBED_HI_REGNUM */
127130803Smarcel  "B",				/* MIPS_EMBED_BADVADDR_REGNUM */
128130803Smarcel  "Pcause",			/* MIPS_EMBED_CAUSE_REGNUM */
129130803Smarcel  "p"				/* MIPS_EMBED_PC_REGNUM */
130130803Smarcel};
131130803Smarcel
132130803Smarcel
133130803Smarcel/* Table of register names produced by monitor's register dump command.  */
134130803Smarcel
135130803Smarcelstatic struct reg_entry
136130803Smarcel  {
137130803Smarcel    char *name;
138130803Smarcel    int regno;
139130803Smarcel  }
140130803Smarcelreg_table[] =
141130803Smarcel{
142130803Smarcel  {
143130803Smarcel    "r0_zero", 0
144130803Smarcel  }
145130803Smarcel  ,
146130803Smarcel  {
147130803Smarcel    "r1_at", 1
148130803Smarcel  }
149130803Smarcel  ,
150130803Smarcel  {
151130803Smarcel    "r2_v0", 2
152130803Smarcel  }
153130803Smarcel  ,
154130803Smarcel  {
155130803Smarcel    "r3_v1", 3
156130803Smarcel  }
157130803Smarcel  ,
158130803Smarcel  {
159130803Smarcel    "r4_a0", 4
160130803Smarcel  }
161130803Smarcel  ,
162130803Smarcel  {
163130803Smarcel    "r5_a1", 5
164130803Smarcel  }
165130803Smarcel  ,
166130803Smarcel  {
167130803Smarcel    "r6_a2", 6
168130803Smarcel  }
169130803Smarcel  ,
170130803Smarcel  {
171130803Smarcel    "r7_a3", 7
172130803Smarcel  }
173130803Smarcel  ,
174130803Smarcel  {
175130803Smarcel    "r8_t0", 8
176130803Smarcel  }
177130803Smarcel  ,
178130803Smarcel  {
179130803Smarcel    "r9_t1", 9
180130803Smarcel  }
181130803Smarcel  ,
182130803Smarcel  {
183130803Smarcel    "r10_t2", 10
184130803Smarcel  }
185130803Smarcel  ,
186130803Smarcel  {
187130803Smarcel    "r11_t3", 11
188130803Smarcel  }
189130803Smarcel  ,
190130803Smarcel  {
191130803Smarcel    "r12_t4", 12
192130803Smarcel  }
193130803Smarcel  ,
194130803Smarcel  {
195130803Smarcel    "r13_t5", 13
196130803Smarcel  }
197130803Smarcel  ,
198130803Smarcel  {
199130803Smarcel    "r14_t6", 14
200130803Smarcel  }
201130803Smarcel  ,
202130803Smarcel  {
203130803Smarcel    "r15_t7", 15
204130803Smarcel  }
205130803Smarcel  ,
206130803Smarcel  {
207130803Smarcel    "r16_s0", 16
208130803Smarcel  }
209130803Smarcel  ,
210130803Smarcel  {
211130803Smarcel    "r17_s1", 17
212130803Smarcel  }
213130803Smarcel  ,
214130803Smarcel  {
215130803Smarcel    "r18_s2", 18
216130803Smarcel  }
217130803Smarcel  ,
218130803Smarcel  {
219130803Smarcel    "r19_s3", 19
220130803Smarcel  }
221130803Smarcel  ,
222130803Smarcel  {
223130803Smarcel    "r20_s4", 20
224130803Smarcel  }
225130803Smarcel  ,
226130803Smarcel  {
227130803Smarcel    "r21_s5", 21
228130803Smarcel  }
229130803Smarcel  ,
230130803Smarcel  {
231130803Smarcel    "r22_s6", 22
232130803Smarcel  }
233130803Smarcel  ,
234130803Smarcel  {
235130803Smarcel    "r23_s7", 23
236130803Smarcel  }
237130803Smarcel  ,
238130803Smarcel  {
239130803Smarcel    "r24_t8", 24
240130803Smarcel  }
241130803Smarcel  ,
242130803Smarcel  {
243130803Smarcel    "r25_t9", 25
244130803Smarcel  }
245130803Smarcel  ,
246130803Smarcel  {
247130803Smarcel    "r26_k0", 26
248130803Smarcel  }
249130803Smarcel  ,
250130803Smarcel  {
251130803Smarcel    "r27_k1", 27
252130803Smarcel  }
253130803Smarcel  ,
254130803Smarcel  {
255130803Smarcel    "r28_gp", 28
256130803Smarcel  }
257130803Smarcel  ,
258130803Smarcel  {
259130803Smarcel    "r29_sp", 29
260130803Smarcel  }
261130803Smarcel  ,
262130803Smarcel  {
263130803Smarcel    "r30_fp", 30
264130803Smarcel  }
265130803Smarcel  ,
266130803Smarcel  {
267130803Smarcel    "r31_ra", 31
268130803Smarcel  }
269130803Smarcel  ,
270130803Smarcel  {
271130803Smarcel    "HI", MIPS_EMBED_HI_REGNUM
272130803Smarcel  }
273130803Smarcel  ,
274130803Smarcel  {
275130803Smarcel    "LO", MIPS_EMBED_LO_REGNUM
276130803Smarcel  }
277130803Smarcel  ,
278130803Smarcel  {
279130803Smarcel    "PC", MIPS_EMBED_PC_REGNUM
280130803Smarcel  }
281130803Smarcel  ,
282130803Smarcel  {
283130803Smarcel    "BadV", MIPS_EMBED_BADVADDR_REGNUM
284130803Smarcel  }
285130803Smarcel  ,
286130803Smarcel  {
287130803Smarcel    NULL, 0
288130803Smarcel  }
289130803Smarcel};
290130803Smarcel
291130803Smarcel
292130803Smarcel/* The monitor displays the cache register along with the status register,
293130803Smarcel   as if they were a single register.  So when we want to fetch the
294130803Smarcel   status register, parse but otherwise ignore the fields of the
295130803Smarcel   cache register that the monitor displays.  Register fields that should
296130803Smarcel   be ignored have a length of zero in the tables below.  */
297130803Smarcel
298130803Smarcelstatic struct bit_field status_fields[] =
299130803Smarcel{
300130803Smarcel  /* Status register portion */
301130803Smarcel  {"SR[<CU=", " ", "cu", 4, 28},
302130803Smarcel  {"RE=", " ", "re", 1, 25},
303130803Smarcel  {"BEV=", " ", "bev", 1, 22},
304130803Smarcel  {"TS=", " ", "ts", 1, 21},
305130803Smarcel  {"Nmi=", " ", "nmi", 1, 20},
306130803Smarcel  {"INT=", " ", "int", 6, 10},
307130803Smarcel  {"SW=", ">]", "sw", 2, 8},
308130803Smarcel  {"[<KUO=", " ", "kuo", 1, 5},
309130803Smarcel  {"IEO=", " ", "ieo", 1, 4},
310130803Smarcel  {"KUP=", " ", "kup", 1, 3},
311130803Smarcel  {"IEP=", " ", "iep", 1, 2},
312130803Smarcel  {"KUC=", " ", "kuc", 1, 1},
313130803Smarcel  {"IEC=", ">]", "iec", 1, 0},
314130803Smarcel
315130803Smarcel  /* Cache register portion (dummy for parsing only) */
316130803Smarcel  {"CR[<IalO=", " ", "ialo", 0, 13},
317130803Smarcel  {"DalO=", " ", "dalo", 0, 12},
318130803Smarcel  {"IalP=", " ", "ialp", 0, 11},
319130803Smarcel  {"DalP=", " ", "dalp", 0, 10},
320130803Smarcel  {"IalC=", " ", "ialc", 0, 9},
321130803Smarcel  {"DalC=", ">] ", "dalc", 0, 8},
322130803Smarcel
323130803Smarcel  {NULL, NULL, 0, 0}		/* end of table marker */
324130803Smarcel};
325130803Smarcel
326130803Smarcel
327130803Smarcel#if 0				/* FIXME: Enable when we add support for modifying cache register.  */
328130803Smarcelstatic struct bit_field cache_fields[] =
329130803Smarcel{
330130803Smarcel  /* Status register portion (dummy for parsing only) */
331130803Smarcel  {"SR[<CU=", " ", "cu", 0, 28},
332130803Smarcel  {"RE=", " ", "re", 0, 25},
333130803Smarcel  {"BEV=", " ", "bev", 0, 22},
334130803Smarcel  {"TS=", " ", "ts", 0, 21},
335130803Smarcel  {"Nmi=", " ", "nmi", 0, 20},
336130803Smarcel  {"INT=", " ", "int", 0, 10},
337130803Smarcel  {"SW=", ">]", "sw", 0, 8},
338130803Smarcel  {"[<KUO=", " ", "kuo", 0, 5},
339130803Smarcel  {"IEO=", " ", "ieo", 0, 4},
340130803Smarcel  {"KUP=", " ", "kup", 0, 3},
341130803Smarcel  {"IEP=", " ", "iep", 0, 2},
342130803Smarcel  {"KUC=", " ", "kuc", 0, 1},
343130803Smarcel  {"IEC=", ">]", "iec", 0, 0},
344130803Smarcel
345130803Smarcel  /* Cache register portion  */
346130803Smarcel  {"CR[<IalO=", " ", "ialo", 1, 13},
347130803Smarcel  {"DalO=", " ", "dalo", 1, 12},
348130803Smarcel  {"IalP=", " ", "ialp", 1, 11},
349130803Smarcel  {"DalP=", " ", "dalp", 1, 10},
350130803Smarcel  {"IalC=", " ", "ialc", 1, 9},
351130803Smarcel  {"DalC=", ">] ", "dalc", 1, 8},
352130803Smarcel
353130803Smarcel  {NULL, NULL, NULL, 0, 0}	/* end of table marker */
354130803Smarcel};
355130803Smarcel#endif
356130803Smarcel
357130803Smarcel
358130803Smarcelstatic struct bit_field cause_fields[] =
359130803Smarcel{
360130803Smarcel  {"<BD=", " ", "bd", 1, 31},
361130803Smarcel  {"CE=", " ", "ce", 2, 28},
362130803Smarcel  {"IP=", " ", "ip", 6, 10},
363130803Smarcel  {"SW=", " ", "sw", 2, 8},
364130803Smarcel  {"EC=", ">]", "ec", 5, 2},
365130803Smarcel
366130803Smarcel  {NULL, NULL, NULL, 0, 0}	/* end of table marker */
367130803Smarcel};
368130803Smarcel
369130803Smarcel
370130803Smarcel/* The monitor prints register values in the form
371130803Smarcel
372130803Smarcel   regname = xxxx xxxx
373130803Smarcel
374130803Smarcel   We look up the register name in a table, and remove the embedded space in
375130803Smarcel   the hex value before passing it to monitor_supply_register.  */
376130803Smarcel
377130803Smarcelstatic void
378130803Smarcelr3900_supply_register (char *regname, int regnamelen, char *val, int vallen)
379130803Smarcel{
380130803Smarcel  int regno = -1;
381130803Smarcel  int i;
382130803Smarcel  char valbuf[10];
383130803Smarcel  char *p;
384130803Smarcel
385130803Smarcel  /* Perform some sanity checks on the register name and value.  */
386130803Smarcel  if (regnamelen < 2 || regnamelen > 7 || vallen != 9)
387130803Smarcel    return;
388130803Smarcel
389130803Smarcel  /* Look up the register name.  */
390130803Smarcel  for (i = 0; reg_table[i].name != NULL; i++)
391130803Smarcel    {
392130803Smarcel      int rlen = strlen (reg_table[i].name);
393130803Smarcel      if (rlen == regnamelen && strncmp (regname, reg_table[i].name, rlen) == 0)
394130803Smarcel	{
395130803Smarcel	  regno = reg_table[i].regno;
396130803Smarcel	  break;
397130803Smarcel	}
398130803Smarcel    }
399130803Smarcel  if (regno == -1)
400130803Smarcel    return;
401130803Smarcel
402130803Smarcel  /* Copy the hex value to a buffer and eliminate the embedded space. */
403130803Smarcel  for (i = 0, p = valbuf; i < vallen; i++)
404130803Smarcel    if (val[i] != ' ')
405130803Smarcel      *p++ = val[i];
406130803Smarcel  *p = '\0';
407130803Smarcel
408130803Smarcel  monitor_supply_register (regno, valbuf);
409130803Smarcel}
410130803Smarcel
411130803Smarcel
412130803Smarcel/* Fetch the BadVaddr register.  Unlike the other registers, this
413130803Smarcel   one can't be modified, and the monitor won't even prompt to let
414130803Smarcel   you modify it.  */
415130803Smarcel
416130803Smarcelstatic void
417130803Smarcelfetch_bad_vaddr (void)
418130803Smarcel{
419130803Smarcel  char buf[20];
420130803Smarcel
421130803Smarcel  monitor_printf ("xB\r");
422130803Smarcel  monitor_expect ("BadV=", NULL, 0);
423130803Smarcel  monitor_expect_prompt (buf, sizeof (buf));
424130803Smarcel  monitor_supply_register (mips_regnum (current_gdbarch)->badvaddr, buf);
425130803Smarcel}
426130803Smarcel
427130803Smarcel
428130803Smarcel/* Read a series of bit fields from the monitor, and return their
429130803Smarcel   combined binary value.  */
430130803Smarcel
431130803Smarcelstatic unsigned long
432130803Smarcelfetch_fields (struct bit_field *bf)
433130803Smarcel{
434130803Smarcel  char buf[20];
435130803Smarcel  unsigned long val = 0;
436130803Smarcel  unsigned long bits;
437130803Smarcel
438130803Smarcel  for (; bf->prefix != NULL; bf++)
439130803Smarcel    {
440130803Smarcel      monitor_expect (bf->prefix, NULL, 0);	/* get prefix */
441130803Smarcel      monitor_expect (bf->suffix, buf, sizeof (buf));	/* hex value, suffix */
442130803Smarcel      if (bf->length != 0)
443130803Smarcel	{
444130803Smarcel	  bits = strtoul (buf, NULL, 16);	/* get field value */
445130803Smarcel	  bits &= ((1 << bf->length) - 1);	/* mask out useless bits */
446130803Smarcel	  val |= bits << bf->start;	/* insert into register */
447130803Smarcel	}
448130803Smarcel
449130803Smarcel    }
450130803Smarcel
451130803Smarcel  return val;
452130803Smarcel}
453130803Smarcel
454130803Smarcel
455130803Smarcelstatic void
456130803Smarcelfetch_bitmapped_register (int regno, struct bit_field *bf)
457130803Smarcel{
458130803Smarcel  unsigned long val;
459130803Smarcel  unsigned char regbuf[MAX_REGISTER_SIZE];
460130803Smarcel  char *regname = NULL;
461130803Smarcel
462130803Smarcel  if (regno >= sizeof (r3900_regnames) / sizeof (r3900_regnames[0]))
463130803Smarcel    internal_error (__FILE__, __LINE__,
464130803Smarcel                    "fetch_bitmapped_register: regno out of bounds");
465130803Smarcel  else
466130803Smarcel    regname = r3900_regnames[regno];
467130803Smarcel
468130803Smarcel  monitor_printf ("x%s\r", regname);
469130803Smarcel  val = fetch_fields (bf);
470130803Smarcel  monitor_printf (".\r");
471130803Smarcel  monitor_expect_prompt (NULL, 0);
472130803Smarcel
473130803Smarcel  /* supply register stores in target byte order, so swap here */
474130803Smarcel
475130803Smarcel  store_unsigned_integer (regbuf, DEPRECATED_REGISTER_RAW_SIZE (regno), val);
476130803Smarcel  supply_register (regno, regbuf);
477130803Smarcel
478130803Smarcel}
479130803Smarcel
480130803Smarcel
481130803Smarcel/* Fetch all registers (if regno is -1), or one register from the
482130803Smarcel   monitor.  For most registers, we can use the generic monitor_
483130803Smarcel   monitor_fetch_registers function.  But others are displayed in
484130803Smarcel   a very unusual fashion by the monitor, and must be handled specially.  */
485130803Smarcel
486130803Smarcelstatic void
487130803Smarcelr3900_fetch_registers (int regno)
488130803Smarcel{
489130803Smarcel  if (regno == mips_regnum (current_gdbarch)->badvaddr)
490130803Smarcel    fetch_bad_vaddr ();
491130803Smarcel  else if (regno == PS_REGNUM)
492130803Smarcel    fetch_bitmapped_register (PS_REGNUM, status_fields);
493130803Smarcel  else if (regno == mips_regnum (current_gdbarch)->cause)
494130803Smarcel    fetch_bitmapped_register (mips_regnum (current_gdbarch)->cause,
495130803Smarcel			      cause_fields);
496130803Smarcel  else
497130803Smarcel    orig_monitor_fetch_registers (regno);
498130803Smarcel}
499130803Smarcel
500130803Smarcel
501130803Smarcel/* Write the new value of the bitmapped register to the monitor.  */
502130803Smarcel
503130803Smarcelstatic void
504130803Smarcelstore_bitmapped_register (int regno, struct bit_field *bf)
505130803Smarcel{
506130803Smarcel  unsigned long oldval, newval;
507130803Smarcel  char *regname = NULL;
508130803Smarcel
509130803Smarcel  if (regno >= sizeof (r3900_regnames) / sizeof (r3900_regnames[0]))
510130803Smarcel    internal_error (__FILE__, __LINE__,
511130803Smarcel                    "fetch_bitmapped_register: regno out of bounds");
512130803Smarcel  else
513130803Smarcel    regname = r3900_regnames[regno];
514130803Smarcel
515130803Smarcel  /* Fetch the current value of the register.  */
516130803Smarcel  monitor_printf ("x%s\r", regname);
517130803Smarcel  oldval = fetch_fields (bf);
518130803Smarcel  newval = read_register (regno);
519130803Smarcel
520130803Smarcel  /* To save time, write just the fields that have changed.  */
521130803Smarcel  for (; bf->prefix != NULL; bf++)
522130803Smarcel    {
523130803Smarcel      if (bf->length != 0)
524130803Smarcel	{
525130803Smarcel	  unsigned long oldbits, newbits, mask;
526130803Smarcel
527130803Smarcel	  mask = (1 << bf->length) - 1;
528130803Smarcel	  oldbits = (oldval >> bf->start) & mask;
529130803Smarcel	  newbits = (newval >> bf->start) & mask;
530130803Smarcel	  if (oldbits != newbits)
531130803Smarcel	    monitor_printf ("%s %lx ", bf->user_name, newbits);
532130803Smarcel	}
533130803Smarcel    }
534130803Smarcel
535130803Smarcel  monitor_printf (".\r");
536130803Smarcel  monitor_expect_prompt (NULL, 0);
537130803Smarcel}
538130803Smarcel
539130803Smarcel
540130803Smarcelstatic void
541130803Smarcelr3900_store_registers (int regno)
542130803Smarcel{
543130803Smarcel  if (regno == PS_REGNUM)
544130803Smarcel    store_bitmapped_register (PS_REGNUM, status_fields);
545130803Smarcel  else if (regno == mips_regnum (current_gdbarch)->cause)
546130803Smarcel    store_bitmapped_register (mips_regnum (current_gdbarch)->cause,
547130803Smarcel			      cause_fields);
548130803Smarcel  else
549130803Smarcel    orig_monitor_store_registers (regno);
550130803Smarcel}
551130803Smarcel
552130803Smarcel
553130803Smarcel/* Write a 4-byte integer to the buffer in big-endian order.  */
554130803Smarcel
555130803Smarcelstatic void
556130803Smarcelwrite_long (char *buf, long n)
557130803Smarcel{
558130803Smarcel  buf[0] = (n >> 24) & 0xff;
559130803Smarcel  buf[1] = (n >> 16) & 0xff;
560130803Smarcel  buf[2] = (n >> 8) & 0xff;
561130803Smarcel  buf[3] = n & 0xff;
562130803Smarcel}
563130803Smarcel
564130803Smarcel
565130803Smarcel/* Write a 4-byte integer to the buffer in little-endian order.  */
566130803Smarcel
567130803Smarcelstatic void
568130803Smarcelwrite_long_le (char *buf, long n)
569130803Smarcel{
570130803Smarcel  buf[0] = n & 0xff;
571130803Smarcel  buf[1] = (n >> 8) & 0xff;
572130803Smarcel  buf[2] = (n >> 16) & 0xff;
573130803Smarcel  buf[3] = (n >> 24) & 0xff;
574130803Smarcel}
575130803Smarcel
576130803Smarcel
577130803Smarcel/* Read a character from the monitor.  If remote debugging is on,
578130803Smarcel   print the received character.  If HEX is non-zero, print the
579130803Smarcel   character in hexadecimal; otherwise, print it in ASCII.  */
580130803Smarcel
581130803Smarcelstatic int
582130803Smarceldebug_readchar (int hex)
583130803Smarcel{
584130803Smarcel  char buf[10];
585130803Smarcel  int c = monitor_readchar ();
586130803Smarcel
587130803Smarcel  if (remote_debug > 0)
588130803Smarcel    {
589130803Smarcel      if (hex)
590130803Smarcel	sprintf (buf, "[%02x]", c & 0xff);
591130803Smarcel      else if (c == '\0')
592130803Smarcel	strcpy (buf, "\\0");
593130803Smarcel      else
594130803Smarcel	{
595130803Smarcel	  buf[0] = c;
596130803Smarcel	  buf[1] = '\0';
597130803Smarcel	}
598130803Smarcel      puts_debug ("Read -->", buf, "<--");
599130803Smarcel    }
600130803Smarcel  return c;
601130803Smarcel}
602130803Smarcel
603130803Smarcel
604130803Smarcel/* Send a buffer of characters to the monitor.  If remote debugging is on,
605130803Smarcel   print the sent buffer in hex.  */
606130803Smarcel
607130803Smarcelstatic void
608130803Smarceldebug_write (unsigned char *buf, int buflen)
609130803Smarcel{
610130803Smarcel  char s[10];
611130803Smarcel
612130803Smarcel  monitor_write (buf, buflen);
613130803Smarcel
614130803Smarcel  if (remote_debug > 0)
615130803Smarcel    {
616130803Smarcel      while (buflen-- > 0)
617130803Smarcel	{
618130803Smarcel	  sprintf (s, "[%02x]", *buf & 0xff);
619130803Smarcel	  puts_debug ("Sent -->", s, "<--");
620130803Smarcel	  buf++;
621130803Smarcel	}
622130803Smarcel    }
623130803Smarcel}
624130803Smarcel
625130803Smarcel
626130803Smarcel/* Ignore a packet sent to us by the monitor.  It send packets
627130803Smarcel   when its console is in "communications interface" mode.   A packet
628130803Smarcel   is of this form:
629130803Smarcel
630130803Smarcel   start of packet flag (one byte: 0xdc)
631130803Smarcel   packet type (one byte)
632130803Smarcel   length (low byte)
633130803Smarcel   length (high byte)
634130803Smarcel   data (length bytes)
635130803Smarcel
636130803Smarcel   The last two bytes of the data field are a checksum, but we don't
637130803Smarcel   bother to verify it.
638130803Smarcel */
639130803Smarcel
640130803Smarcelstatic void
641130803Smarcelignore_packet (void)
642130803Smarcel{
643130803Smarcel  int c = -1;
644130803Smarcel  int len;
645130803Smarcel
646130803Smarcel  /* Ignore lots of trash (messages about section addresses, for example)
647130803Smarcel     until we see the start of a packet.  */
648130803Smarcel  for (len = 0; len < 256; len++)
649130803Smarcel    {
650130803Smarcel      c = debug_readchar (0);
651130803Smarcel      if (c == PESC)
652130803Smarcel	break;
653130803Smarcel    }
654130803Smarcel  if (len == 8)
655130803Smarcel    error ("Packet header byte not found; %02x seen instead.", c);
656130803Smarcel
657130803Smarcel  /* Read the packet type and length.  */
658130803Smarcel  c = debug_readchar (1);	/* type */
659130803Smarcel
660130803Smarcel  c = debug_readchar (1);	/* low byte of length */
661130803Smarcel  len = c & 0xff;
662130803Smarcel
663130803Smarcel  c = debug_readchar (1);	/* high byte of length */
664130803Smarcel  len += (c & 0xff) << 8;
665130803Smarcel
666130803Smarcel  /* Ignore the rest of the packet.  */
667130803Smarcel  while (len-- > 0)
668130803Smarcel    c = debug_readchar (1);
669130803Smarcel}
670130803Smarcel
671130803Smarcel
672130803Smarcel/* Encapsulate some data into a packet and send it to the monitor.
673130803Smarcel
674130803Smarcel   The 'p' packet is a special case.  This is a packet we send
675130803Smarcel   in response to a read ('r') packet from the monitor.  This function
676130803Smarcel   appends a one-byte sequence number to the data field of such a packet.
677130803Smarcel */
678130803Smarcel
679130803Smarcelstatic void
680130803Smarcelsend_packet (char type, unsigned char *buf, int buflen, int seq)
681130803Smarcel{
682130803Smarcel  unsigned char hdr[4];
683130803Smarcel  int len = buflen;
684130803Smarcel  int sum, i;
685130803Smarcel
686130803Smarcel  /* If this is a 'p' packet, add one byte for a sequence number.  */
687130803Smarcel  if (type == 'p')
688130803Smarcel    len++;
689130803Smarcel
690130803Smarcel  /* If the buffer has a non-zero length, add two bytes for a checksum.  */
691130803Smarcel  if (len > 0)
692130803Smarcel    len += 2;
693130803Smarcel
694130803Smarcel  /* Write the packet header.  */
695130803Smarcel  hdr[0] = PESC;
696130803Smarcel  hdr[1] = type;
697130803Smarcel  hdr[2] = len & 0xff;
698130803Smarcel  hdr[3] = (len >> 8) & 0xff;
699130803Smarcel  debug_write (hdr, sizeof (hdr));
700130803Smarcel
701130803Smarcel  if (len)
702130803Smarcel    {
703130803Smarcel      /* Write the packet data.  */
704130803Smarcel      debug_write (buf, buflen);
705130803Smarcel
706130803Smarcel      /* Write the sequence number if this is a 'p' packet.  */
707130803Smarcel      if (type == 'p')
708130803Smarcel	{
709130803Smarcel	  hdr[0] = seq;
710130803Smarcel	  debug_write (hdr, 1);
711130803Smarcel	}
712130803Smarcel
713130803Smarcel      /* Write the checksum.  */
714130803Smarcel      sum = 0;
715130803Smarcel      for (i = 0; i < buflen; i++)
716130803Smarcel	{
717130803Smarcel	  int tmp = (buf[i] & 0xff);
718130803Smarcel	  if (i & 1)
719130803Smarcel	    sum += tmp;
720130803Smarcel	  else
721130803Smarcel	    sum += tmp << 8;
722130803Smarcel	}
723130803Smarcel      if (type == 'p')
724130803Smarcel	{
725130803Smarcel	  if (buflen & 1)
726130803Smarcel	    sum += (seq & 0xff);
727130803Smarcel	  else
728130803Smarcel	    sum += (seq & 0xff) << 8;
729130803Smarcel	}
730130803Smarcel      sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
731130803Smarcel      sum += (sum >> 16) & 1;
732130803Smarcel      sum = ~sum;
733130803Smarcel
734130803Smarcel      hdr[0] = (sum >> 8) & 0xff;
735130803Smarcel      hdr[1] = sum & 0xff;
736130803Smarcel      debug_write (hdr, 2);
737130803Smarcel    }
738130803Smarcel}
739130803Smarcel
740130803Smarcel
741130803Smarcel/* Respond to an expected read request from the monitor by sending
742130803Smarcel   data in chunks.  Handle all acknowledgements and handshaking packets.
743130803Smarcel
744130803Smarcel   The monitor expects a response consisting of a one or more 'p' packets,
745130803Smarcel   each followed by a portion of the data requested.  The 'p' packet
746130803Smarcel   contains only a four-byte integer, the value of which is the number
747130803Smarcel   of bytes of data we are about to send.  Following the 'p' packet,
748130803Smarcel   the monitor expects the data bytes themselves in raw, unpacketized,
749130803Smarcel   form, without even a checksum.
750130803Smarcel */
751130803Smarcel
752130803Smarcelstatic void
753130803Smarcelprocess_read_request (unsigned char *buf, int buflen)
754130803Smarcel{
755130803Smarcel  unsigned char len[4];
756130803Smarcel  int i, chunk;
757130803Smarcel  unsigned char seq;
758130803Smarcel
759130803Smarcel  /* Discard the read request.  FIXME: we have to hope it's for
760130803Smarcel     the exact number of bytes we want to send; should check for this.  */
761130803Smarcel  ignore_packet ();
762130803Smarcel
763130803Smarcel  for (i = chunk = 0, seq = 0; i < buflen; i += chunk, seq++)
764130803Smarcel    {
765130803Smarcel      /* Don't send more than MAXPSIZE bytes at a time.  */
766130803Smarcel      chunk = buflen - i;
767130803Smarcel      if (chunk > MAXPSIZE)
768130803Smarcel	chunk = MAXPSIZE;
769130803Smarcel
770130803Smarcel      /* Write a packet containing the number of bytes we are sending.  */
771130803Smarcel      write_long_le (len, chunk);
772130803Smarcel      send_packet ('p', len, sizeof (len), seq);
773130803Smarcel
774130803Smarcel      /* Write the data in raw form following the packet.  */
775130803Smarcel      debug_write (&buf[i], chunk);
776130803Smarcel
777130803Smarcel      /* Discard the ACK packet.  */
778130803Smarcel      ignore_packet ();
779130803Smarcel    }
780130803Smarcel
781130803Smarcel  /* Send an "end of data" packet.  */
782130803Smarcel  send_packet ('e', "", 0, 0);
783130803Smarcel}
784130803Smarcel
785130803Smarcel
786130803Smarcel/* Count loadable sections (helper function for r3900_load).  */
787130803Smarcel
788130803Smarcelstatic void
789130803Smarcelcount_section (bfd *abfd, asection *s, unsigned int *section_count)
790130803Smarcel{
791130803Smarcel  if (s->flags & SEC_LOAD && bfd_section_size (abfd, s) != 0)
792130803Smarcel    (*section_count)++;
793130803Smarcel}
794130803Smarcel
795130803Smarcel
796130803Smarcel/* Load a single BFD section (helper function for r3900_load).
797130803Smarcel
798130803Smarcel   WARNING: this code is filled with assumptions about how
799130803Smarcel   the Densan monitor loads programs.  The monitor issues
800130803Smarcel   packets containing read requests, but rather than respond
801130803Smarcel   to them in an general way, we expect them to following
802130803Smarcel   a certain pattern.
803130803Smarcel
804130803Smarcel   For example, we know that the monitor will start loading by
805130803Smarcel   issuing an 8-byte read request for the binary file header.
806130803Smarcel   We know this is coming and ignore the actual contents
807130803Smarcel   of the read request packet.
808130803Smarcel */
809130803Smarcel
810130803Smarcelstatic void
811130803Smarcelload_section (bfd *abfd, asection *s, unsigned int *data_count)
812130803Smarcel{
813130803Smarcel  if (s->flags & SEC_LOAD)
814130803Smarcel    {
815130803Smarcel      bfd_size_type section_size = bfd_section_size (abfd, s);
816130803Smarcel      bfd_vma section_base = bfd_section_lma (abfd, s);
817130803Smarcel      unsigned char *buffer;
818130803Smarcel      unsigned char header[8];
819130803Smarcel
820130803Smarcel      /* Don't output zero-length sections.  */
821130803Smarcel      if (section_size == 0)
822130803Smarcel	return;
823130803Smarcel      if (data_count)
824130803Smarcel	*data_count += section_size;
825130803Smarcel
826130803Smarcel      /* Print some fluff about the section being loaded.  */
827130803Smarcel      printf_filtered ("Loading section %s, size 0x%lx lma ",
828130803Smarcel		       bfd_section_name (abfd, s), (long) section_size);
829130803Smarcel      print_address_numeric (section_base, 1, gdb_stdout);
830130803Smarcel      printf_filtered ("\n");
831130803Smarcel      gdb_flush (gdb_stdout);
832130803Smarcel
833130803Smarcel      /* Write the section header (location and size).  */
834130803Smarcel      write_long (&header[0], (long) section_base);
835130803Smarcel      write_long (&header[4], (long) section_size);
836130803Smarcel      process_read_request (header, sizeof (header));
837130803Smarcel
838130803Smarcel      /* Read the section contents into a buffer, write it out,
839130803Smarcel         then free the buffer.  */
840130803Smarcel      buffer = (unsigned char *) xmalloc (section_size);
841130803Smarcel      bfd_get_section_contents (abfd, s, buffer, 0, section_size);
842130803Smarcel      process_read_request (buffer, section_size);
843130803Smarcel      xfree (buffer);
844130803Smarcel    }
845130803Smarcel}
846130803Smarcel
847130803Smarcel
848130803Smarcel/* When the ethernet is used as the console port on the Densan board,
849130803Smarcel   we can use the "Rm" command to do a fast binary load.  The format
850130803Smarcel   of the download data is:
851130803Smarcel
852130803Smarcel   number of sections (4 bytes)
853130803Smarcel   starting address (4 bytes)
854130803Smarcel   repeat for each section:
855130803Smarcel   location address (4 bytes)
856130803Smarcel   section size (4 bytes)
857130803Smarcel   binary data
858130803Smarcel
859130803Smarcel   The 4-byte fields are all in big-endian order.
860130803Smarcel
861130803Smarcel   Using this command is tricky because we have to put the monitor
862130803Smarcel   into a special funky "communications interface" mode, in which
863130803Smarcel   it sends and receives packets of data along with the normal prompt.
864130803Smarcel */
865130803Smarcel
866130803Smarcelstatic void
867130803Smarcelr3900_load (char *filename, int from_tty)
868130803Smarcel{
869130803Smarcel  bfd *abfd;
870130803Smarcel  unsigned int data_count = 0;
871130803Smarcel  time_t start_time, end_time;	/* for timing of download */
872130803Smarcel  int section_count = 0;
873130803Smarcel  unsigned char buffer[8];
874130803Smarcel
875130803Smarcel  /* If we are not using the ethernet, use the normal monitor load,
876130803Smarcel     which sends S-records over the serial link.  */
877130803Smarcel  if (!ethernet)
878130803Smarcel    {
879130803Smarcel      orig_monitor_load (filename, from_tty);
880130803Smarcel      return;
881130803Smarcel    }
882130803Smarcel
883130803Smarcel  /* Open the file.  */
884130803Smarcel  if (filename == NULL || filename[0] == 0)
885130803Smarcel    filename = get_exec_file (1);
886130803Smarcel  abfd = bfd_openr (filename, 0);
887130803Smarcel  if (!abfd)
888130803Smarcel    error ("Unable to open file %s\n", filename);
889130803Smarcel  if (bfd_check_format (abfd, bfd_object) == 0)
890130803Smarcel    error ("File is not an object file\n");
891130803Smarcel
892130803Smarcel  /* Output the "vconsi" command to get the monitor in the communication
893130803Smarcel     state where it will accept a load command.  This will cause
894130803Smarcel     the monitor to emit a packet before each prompt, so ignore the packet.  */
895130803Smarcel  monitor_printf ("vconsi\r");
896130803Smarcel  ignore_packet ();
897130803Smarcel  monitor_expect_prompt (NULL, 0);
898130803Smarcel
899130803Smarcel  /* Output the "Rm" (load) command and respond to the subsequent "open"
900130803Smarcel     packet by sending an ACK packet.  */
901130803Smarcel  monitor_printf ("Rm\r");
902130803Smarcel  ignore_packet ();
903130803Smarcel  send_packet ('a', "", 0, 0);
904130803Smarcel
905130803Smarcel  /* Output the fast load header (number of sections and starting address).  */
906130803Smarcel  bfd_map_over_sections ((bfd *) abfd, (section_map_func) count_section,
907130803Smarcel			 &section_count);
908130803Smarcel  write_long (&buffer[0], (long) section_count);
909130803Smarcel  if (exec_bfd)
910130803Smarcel    write_long (&buffer[4], (long) bfd_get_start_address (exec_bfd));
911130803Smarcel  else
912130803Smarcel    write_long (&buffer[4], 0);
913130803Smarcel  process_read_request (buffer, sizeof (buffer));
914130803Smarcel
915130803Smarcel  /* Output the section data.  */
916130803Smarcel  start_time = time (NULL);
917130803Smarcel  bfd_map_over_sections (abfd, (section_map_func) load_section, &data_count);
918130803Smarcel  end_time = time (NULL);
919130803Smarcel
920130803Smarcel  /* Acknowledge the close packet and put the monitor back into
921130803Smarcel     "normal" mode so it won't send packets any more.  */
922130803Smarcel  ignore_packet ();
923130803Smarcel  send_packet ('a', "", 0, 0);
924130803Smarcel  monitor_expect_prompt (NULL, 0);
925130803Smarcel  monitor_printf ("vconsx\r");
926130803Smarcel  monitor_expect_prompt (NULL, 0);
927130803Smarcel
928130803Smarcel  /* Print start address and download performance information.  */
929130803Smarcel  printf_filtered ("Start address 0x%lx\n", (long) bfd_get_start_address (abfd));
930130803Smarcel  report_transfer_performance (data_count, start_time, end_time);
931130803Smarcel
932130803Smarcel  /* Finally, make the PC point at the start address */
933130803Smarcel  if (exec_bfd)
934130803Smarcel    write_pc (bfd_get_start_address (exec_bfd));
935130803Smarcel
936130803Smarcel  inferior_ptid = null_ptid;		/* No process now */
937130803Smarcel
938130803Smarcel  /* This is necessary because many things were based on the PC at the
939130803Smarcel     time that we attached to the monitor, which is no longer valid
940130803Smarcel     now that we have loaded new code (and just changed the PC).
941130803Smarcel     Another way to do this might be to call normal_stop, except that
942130803Smarcel     the stack may not be valid, and things would get horribly
943130803Smarcel     confused... */
944130803Smarcel  clear_symtab_users ();
945130803Smarcel}
946130803Smarcel
947130803Smarcel
948130803Smarcel/* Commands to send to the monitor when first connecting:
949130803Smarcel   * The bare carriage return forces a prompt from the monitor
950130803Smarcel   (monitor doesn't prompt immediately after a reset).
951130803Smarcel   * The "vconsx" switches the monitor back to interactive mode
952130803Smarcel   in case an aborted download had left it in packet mode.
953130803Smarcel   * The "Xtr" command causes subsequent "t" (trace) commands to display
954130803Smarcel   the general registers only.
955130803Smarcel   * The "Xxr" command does the same thing for the "x" (examine
956130803Smarcel   registers) command.
957130803Smarcel   * The "bx" command clears all breakpoints.
958130803Smarcel */
959130803Smarcel
960130803Smarcelstatic char *r3900_inits[] =
961130803Smarcel{"\r", "vconsx\r", "Xtr\r", "Xxr\r", "bx\r", NULL};
962130803Smarcelstatic char *dummy_inits[] =
963130803Smarcel{NULL};
964130803Smarcel
965130803Smarcelstatic struct target_ops r3900_ops;
966130803Smarcelstatic struct monitor_ops r3900_cmds;
967130803Smarcel
968130803Smarcelstatic void
969130803Smarcelr3900_open (char *args, int from_tty)
970130803Smarcel{
971130803Smarcel  char buf[64];
972130803Smarcel  int i;
973130803Smarcel
974130803Smarcel  monitor_open (args, &r3900_cmds, from_tty);
975130803Smarcel
976130803Smarcel  /* We have to handle sending the init strings ourselves, because
977130803Smarcel     the first two strings we send (carriage returns) may not be echoed
978130803Smarcel     by the monitor, but the rest will be.  */
979130803Smarcel  monitor_printf_noecho ("\r\r");
980130803Smarcel  for (i = 0; r3900_inits[i] != NULL; i++)
981130803Smarcel    {
982130803Smarcel      monitor_printf (r3900_inits[i]);
983130803Smarcel      monitor_expect_prompt (NULL, 0);
984130803Smarcel    }
985130803Smarcel
986130803Smarcel  /* Attempt to determine whether the console device is ethernet or serial.
987130803Smarcel     This will tell us which kind of load to use (S-records over a serial
988130803Smarcel     link, or the Densan fast binary multi-section format over the net).  */
989130803Smarcel
990130803Smarcel  ethernet = 0;
991130803Smarcel  monitor_printf ("v\r");
992130803Smarcel  if (monitor_expect ("console device :", NULL, 0) != -1)
993130803Smarcel    if (monitor_expect ("\n", buf, sizeof (buf)) != -1)
994130803Smarcel      if (strstr (buf, "ethernet") != NULL)
995130803Smarcel	ethernet = 1;
996130803Smarcel  monitor_expect_prompt (NULL, 0);
997130803Smarcel}
998130803Smarcel
999130803Smarcelvoid
1000130803Smarcel_initialize_r3900_rom (void)
1001130803Smarcel{
1002130803Smarcel  r3900_cmds.flags = MO_NO_ECHO_ON_OPEN |
1003130803Smarcel    MO_ADDR_BITS_REMOVE |
1004130803Smarcel    MO_CLR_BREAK_USES_ADDR |
1005130803Smarcel    MO_GETMEM_READ_SINGLE |
1006130803Smarcel    MO_PRINT_PROGRAM_OUTPUT;
1007130803Smarcel
1008130803Smarcel  r3900_cmds.init = dummy_inits;
1009130803Smarcel  r3900_cmds.cont = "g\r";
1010130803Smarcel  r3900_cmds.step = "t\r";
1011130803Smarcel  r3900_cmds.set_break = "b %A\r";	/* COREADDR */
1012130803Smarcel  r3900_cmds.clr_break = "b %A,0\r";	/* COREADDR */
1013130803Smarcel  r3900_cmds.fill = "fx %A s %x %x\r";	/* COREADDR, len, val */
1014130803Smarcel
1015130803Smarcel  r3900_cmds.setmem.cmdb = "sx %A %x\r";	/* COREADDR, val */
1016130803Smarcel  r3900_cmds.setmem.cmdw = "sh %A %x\r";	/* COREADDR, val */
1017130803Smarcel  r3900_cmds.setmem.cmdl = "sw %A %x\r";	/* COREADDR, val */
1018130803Smarcel
1019130803Smarcel  r3900_cmds.getmem.cmdb = "sx %A\r";	/* COREADDR */
1020130803Smarcel  r3900_cmds.getmem.cmdw = "sh %A\r";	/* COREADDR */
1021130803Smarcel  r3900_cmds.getmem.cmdl = "sw %A\r";	/* COREADDR */
1022130803Smarcel  r3900_cmds.getmem.resp_delim = " : ";
1023130803Smarcel  r3900_cmds.getmem.term = " ";
1024130803Smarcel  r3900_cmds.getmem.term_cmd = ".\r";
1025130803Smarcel
1026130803Smarcel  r3900_cmds.setreg.cmd = "x%s %x\r";	/* regname, val */
1027130803Smarcel
1028130803Smarcel  r3900_cmds.getreg.cmd = "x%s\r";	/* regname */
1029130803Smarcel  r3900_cmds.getreg.resp_delim = "=";
1030130803Smarcel  r3900_cmds.getreg.term = " ";
1031130803Smarcel  r3900_cmds.getreg.term_cmd = ".\r";
1032130803Smarcel
1033130803Smarcel  r3900_cmds.dump_registers = "x\r";
1034130803Smarcel  r3900_cmds.register_pattern =
1035130803Smarcel    "\\([a-zA-Z0-9_]+\\) *=\\([0-9a-f]+ [0-9a-f]+\\b\\)";
1036130803Smarcel  r3900_cmds.supply_register = r3900_supply_register;
1037130803Smarcel  /* S-record download, via "keyboard port".  */
1038130803Smarcel  r3900_cmds.load = "r0\r";
1039130803Smarcel  r3900_cmds.prompt = "#";
1040130803Smarcel  r3900_cmds.line_term = "\r";
1041130803Smarcel  r3900_cmds.target = &r3900_ops;
1042130803Smarcel  r3900_cmds.stopbits = SERIAL_1_STOPBITS;
1043130803Smarcel  r3900_cmds.regnames = r3900_regnames;
1044130803Smarcel  r3900_cmds.magic = MONITOR_OPS_MAGIC;
1045130803Smarcel
1046130803Smarcel  init_monitor_ops (&r3900_ops);
1047130803Smarcel
1048130803Smarcel  r3900_ops.to_shortname = "r3900";
1049130803Smarcel  r3900_ops.to_longname = "R3900 monitor";
1050130803Smarcel  r3900_ops.to_doc = "Debug using the DVE R3900 monitor.\n\
1051130803SmarcelSpecify the serial device it is connected to (e.g. /dev/ttya).";
1052130803Smarcel  r3900_ops.to_open = r3900_open;
1053130803Smarcel
1054130803Smarcel  /* Override the functions to fetch and store registers.  But save the
1055130803Smarcel     addresses of the default functions, because we will use those functions
1056130803Smarcel     for "normal" registers.  */
1057130803Smarcel
1058130803Smarcel  orig_monitor_fetch_registers = r3900_ops.to_fetch_registers;
1059130803Smarcel  orig_monitor_store_registers = r3900_ops.to_store_registers;
1060130803Smarcel  r3900_ops.to_fetch_registers = r3900_fetch_registers;
1061130803Smarcel  r3900_ops.to_store_registers = r3900_store_registers;
1062130803Smarcel
1063130803Smarcel  /* Override the load function, but save the address of the default
1064130803Smarcel     function to use when loading S-records over a serial link.  */
1065130803Smarcel  orig_monitor_load = r3900_ops.to_load;
1066130803Smarcel  r3900_ops.to_load = r3900_load;
1067130803Smarcel
1068130803Smarcel  add_target (&r3900_ops);
1069130803Smarcel}
1070