Commit | Line | Data |
---|---|---|
7e9576e0 MA |
1 | /* Remote debugging interface for Densan DVE-R3900 ROM monitor for |
2 | GDB, the GNU debugger. | |
3 | Copyright 1997 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "gdbcore.h" | |
23 | #include "target.h" | |
24 | #include "monitor.h" | |
25 | #include "serial.h" | |
26 | ||
27 | static void r3900_open PARAMS ((char *args, int from_tty)); | |
28 | ||
29 | /* Pointers to static functions in monitor.c for fetching and storing | |
30 | registers. We can't use these function in certain cases where the Densan | |
31 | monitor acts perversely: for registers that it displays in bit-map | |
32 | format, and those that can't be modified at all. In those cases | |
33 | we have to use our own functions to fetch and store their values. */ | |
34 | ||
35 | static void (*orig_monitor_fetch_registers) PARAMS ((int regno)); | |
36 | static void (*orig_monitor_store_registers) PARAMS ((int regno)); | |
37 | ||
38 | ||
39 | /* This array of registers needs to match the indexes used by GDB. The | |
40 | whole reason this exists is because the various ROM monitors use | |
41 | different names than GDB does, and don't support all the registers | |
42 | either. */ | |
43 | ||
44 | static char *r3900_regnames[NUM_REGS] = | |
45 | { | |
46 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
47 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", | |
48 | "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", | |
49 | "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", | |
50 | ||
51 | "S", /* PS_REGNUM */ | |
52 | "l", /* LO_REGNUM */ | |
53 | "h", /* HI_REGNUM */ | |
54 | "B", /* BADVADDR_REGNUM */ | |
55 | "Pcause", /* CAUSE_REGNUM */ | |
56 | "p" /* PC_REGNUM */ | |
57 | }; | |
58 | ||
59 | ||
60 | /* Table of register names produced by monitor's register dump command. */ | |
61 | ||
62 | static struct reg_entry | |
63 | { | |
64 | char *name; | |
65 | int regno; | |
66 | } reg_table[] = | |
67 | { | |
68 | { "r0_zero", 0 }, { "r1_at", 1 }, { "r2_v0", 2 }, { "r3_v1", 3 }, | |
69 | { "r4_a0", 4 }, { "r5_a1", 5 }, { "r6_a2", 6 }, { "r7_a3", 7 }, | |
70 | { "r8_t0", 8 }, { "r9_t1", 9 }, { "r10_t2", 10 }, { "r11_t3", 11 }, | |
71 | { "r12_t4", 12 }, { "r13_t5", 13 }, { "r14_t6", 14 }, { "r15_t7", 15 }, | |
72 | { "r16_s0", 16 }, { "r17_s1", 17 }, { "r18_s2", 18 }, { "r19_s3", 19 }, | |
73 | { "r20_s4", 20 }, { "r21_s5", 21 }, { "r22_s6", 22 }, { "r23_s7", 23 }, | |
74 | { "r24_t8", 24 }, { "r25_t9", 25 }, { "r26_k0", 26 }, { "r27_k1", 27 }, | |
75 | { "r28_gp", 28 }, { "r29_sp", 29 }, { "r30_fp", 30 }, { "r31_ra", 31 }, | |
76 | { "HI", HI_REGNUM }, | |
77 | { "LO", LO_REGNUM }, | |
78 | { "PC", PC_REGNUM }, | |
79 | { "BadV", BADVADDR_REGNUM }, | |
80 | { NULL, 0 } | |
81 | }; | |
82 | ||
83 | ||
84 | /* The monitor prints register values in the form | |
85 | ||
86 | regname = xxxx xxxx | |
87 | ||
88 | We look up the register name in a table, and remove the embedded space in | |
89 | the hex value before passing it to monitor_supply_register. */ | |
90 | ||
91 | static void | |
92 | r3900_supply_register (regname, regnamelen, val, vallen) | |
93 | char *regname; | |
94 | int regnamelen; | |
95 | char *val; | |
96 | int vallen; | |
97 | { | |
98 | int regno = -1; | |
99 | int i; | |
100 | char valbuf[10]; | |
101 | char *p; | |
102 | ||
103 | /* Perform some sanity checks on the register name and value. */ | |
104 | if (regnamelen < 2 || regnamelen > 7 || vallen != 9) | |
105 | return; | |
106 | ||
107 | /* Look up the register name. */ | |
108 | for (i = 0; reg_table[i].name != NULL; i++) | |
109 | { | |
110 | int rlen = strlen (reg_table[i].name); | |
111 | if (rlen == regnamelen && strncmp (regname, reg_table[i].name, rlen) == 0) | |
112 | { | |
113 | regno = reg_table[i].regno; | |
114 | break; | |
115 | } | |
116 | } | |
117 | if (regno == -1) | |
118 | return; | |
119 | ||
120 | /* Copy the hex value to a buffer and eliminate the embedded space. */ | |
121 | for (i = 0, p = valbuf; i < vallen; i++) | |
122 | if (val[i] != ' ') | |
123 | *p++ = val[i]; | |
124 | *p = '\0'; | |
125 | ||
126 | monitor_supply_register (regno, valbuf); | |
127 | } | |
128 | ||
129 | /* Fetch the BadVaddr register. Unlike the other registers, this | |
130 | one can't be modified, and the monitor won't even prompt to let | |
131 | you modify it. */ | |
132 | ||
133 | static void | |
134 | r3900_fetch_badvaddr() | |
135 | { | |
136 | char buf[20]; | |
137 | int c; | |
138 | ||
139 | monitor_printf ("xB\r"); | |
140 | monitor_expect ("BadV=", NULL, 0); | |
141 | monitor_expect_prompt (buf, sizeof(buf)); | |
142 | monitor_supply_register (BADVADDR_REGNUM, buf); | |
143 | } | |
144 | ||
145 | ||
146 | /* Certain registers are "bitmapped", in that the monitor can only display | |
147 | them or let the user modify them as a series of named bitfields. | |
148 | This structure describes a field in a bitmapped register. */ | |
149 | ||
150 | struct bit_field | |
151 | { | |
152 | char *prefix; /* string appearing before the value */ | |
153 | char *suffix; /* string appearing after the value */ | |
154 | char *user_name; /* name used by human when entering field value */ | |
155 | int length; /* number of bits in the field */ | |
156 | int start; /* starting (least significant) bit number of field */ | |
157 | }; | |
158 | ||
159 | /* The monitor displays the cache register along with the status register, | |
160 | as if they were a single register. So when we want to fetch the | |
161 | status register, parse but otherwise ignore the fields of the | |
162 | cache register that the monitor displays. Register fields that should | |
163 | be ignored have a length of zero in the tables below. */ | |
164 | ||
165 | static struct bit_field status_fields [] = | |
166 | { | |
167 | /* Status register portion */ | |
168 | { "SR[<CU=", " ", "cu", 4, 28 }, | |
169 | { "RE=", " ", "re", 1, 25 }, | |
170 | { "BEV=", " ", "bev", 1, 22 }, | |
171 | { "TS=", " ", "ts", 1, 21 }, | |
172 | { "Nmi=", " ", "nmi", 1, 20 }, | |
173 | { "INT=", " ", "int", 6, 10 }, | |
174 | { "SW=", ">]", "sw", 2, 8 }, | |
175 | { "[<KUO=", " ", "kuo", 1, 5 }, | |
176 | { "IEO=", " ", "ieo", 1, 4 }, | |
177 | { "KUP=", " ", "kup", 1, 3 }, | |
178 | { "IEP=", " ", "iep", 1, 2 }, | |
179 | { "KUC=", " ", "kuc", 1, 1 }, | |
180 | { "IEC=", ">]", "iec", 1, 0 }, | |
181 | ||
182 | /* Cache register portion (dummy for parsing only) */ | |
183 | { "CR[<IalO="," ", "ialo", 0, 13 }, | |
184 | { "DalO=", " ", "dalo", 0, 12 }, | |
185 | { "IalP=", " ", "ialp", 0, 11 }, | |
186 | { "DalP=", " ", "dalp", 0, 10 }, | |
187 | { "IalC=", " ", "ialc", 0, 9 }, | |
188 | { "DalC=", ">] ", "dalc", 0, 8 }, | |
189 | ||
190 | { NULL, NULL, 0, 0 } /* end of table marker */ | |
191 | }; | |
192 | ||
193 | ||
194 | static struct bit_field cache_fields [] = | |
195 | { | |
196 | /* Status register portion (dummy for parsing only) */ | |
197 | { "SR[<CU=", " ", "cu", 0, 28 }, | |
198 | { "RE=", " ", "re", 0, 25 }, | |
199 | { "BEV=", " ", "bev", 0, 22 }, | |
200 | { "TS=", " ", "ts", 0, 21 }, | |
201 | { "Nmi=", " ", "nmi", 0, 20 }, | |
202 | { "INT=", " ", "int", 0, 10 }, | |
203 | { "SW=", ">]", "sw", 0, 8 }, | |
204 | { "[<KUO=", " ", "kuo", 0, 5 }, | |
205 | { "IEO=", " ", "ieo", 0, 4 }, | |
206 | { "KUP=", " ", "kup", 0, 3 }, | |
207 | { "IEP=", " ", "iep", 0, 2 }, | |
208 | { "KUC=", " ", "kuc", 0, 1 }, | |
209 | { "IEC=", ">]", "iec", 0, 0 }, | |
210 | ||
211 | /* Cache register portion */ | |
212 | { "CR[<IalO="," ", "ialo", 1, 13 }, | |
213 | { "DalO=", " ", "dalo", 1, 12 }, | |
214 | { "IalP=", " ", "ialp", 1, 11 }, | |
215 | { "DalP=", " ", "dalp", 1, 10 }, | |
216 | { "IalC=", " ", "ialc", 1, 9 }, | |
217 | { "DalC=", ">] ", "dalc", 1, 8 }, | |
218 | ||
219 | { NULL, NULL, NULL, 0, 0 } /* end of table marker */ | |
220 | }; | |
221 | ||
222 | static struct bit_field cause_fields[] = | |
223 | { | |
224 | { "<BD=", " ", "bd", 1, 31 }, | |
225 | { "CE=", " ", "ce", 2, 28 }, | |
226 | { "IP=", " ", "ip", 6, 10 }, | |
227 | { "SW=", " ", "sw", 2, 8 }, | |
228 | { "EC=", ">]" , "ec", 5, 2 }, | |
229 | ||
230 | { NULL, NULL, NULL, 0, 0 } /* end of table marker */ | |
231 | }; | |
232 | ||
233 | ||
234 | /* Read a series of bit fields from the monitor, and return their | |
235 | combined binary value. */ | |
236 | ||
237 | static unsigned long | |
238 | r3900_fetch_fields (bf) | |
239 | struct bit_field *bf; | |
240 | { | |
241 | char buf[20]; | |
242 | int c; | |
243 | unsigned long val = 0; | |
244 | unsigned long bits; | |
245 | ||
246 | for ( ; bf->prefix != NULL; bf++) | |
247 | { | |
248 | monitor_expect (bf->prefix, NULL, 0); /* get prefix */ | |
249 | monitor_expect (bf->suffix, buf, sizeof (buf)); /* hex value, suffix */ | |
250 | if (bf->length != 0) | |
251 | { | |
252 | bits = strtoul (buf, NULL, 16); /* get field value */ | |
253 | bits &= ((1 << bf->length) - 1); /* mask out useless bits */ | |
254 | val |= bits << bf->start; /* insert into register */ | |
255 | } | |
256 | ||
257 | } | |
258 | ||
259 | return val; | |
260 | } | |
261 | ||
262 | static void | |
263 | r3900_fetch_bitmapped_register (regno, bf) | |
264 | int regno; | |
265 | struct bit_field *bf; | |
266 | { | |
267 | char buf[20]; | |
268 | int c; | |
269 | unsigned long val; | |
270 | unsigned long bits; | |
271 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; | |
272 | ||
273 | monitor_printf ("x%s\r", r3900_regnames[regno]); | |
274 | val = r3900_fetch_fields (bf); | |
275 | monitor_printf (".\r"); | |
276 | monitor_expect_prompt (NULL, 0); | |
277 | ||
278 | /* supply register stores in target byte order, so swap here */ | |
279 | ||
280 | store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val); | |
281 | supply_register (regno, regbuf); | |
282 | ||
283 | } | |
284 | ||
285 | /* Fetch all registers (if regno is -1), or one register from the | |
286 | monitor. For most registers, we can use the generic monitor_ | |
287 | monitor_fetch_registers function. But others are displayed in | |
288 | very unusual fashion and must be handled specially. */ | |
289 | ||
290 | static void | |
291 | r3900_fetch_registers (regno) | |
292 | int regno; | |
293 | { | |
294 | switch (regno) | |
295 | { | |
296 | case BADVADDR_REGNUM: | |
297 | r3900_fetch_badvaddr (); | |
298 | return; | |
299 | case PS_REGNUM: | |
300 | r3900_fetch_bitmapped_register (PS_REGNUM, status_fields); | |
301 | return; | |
302 | case CAUSE_REGNUM: | |
303 | r3900_fetch_bitmapped_register (CAUSE_REGNUM, cause_fields); | |
304 | return; | |
305 | default: | |
306 | orig_monitor_fetch_registers (regno); | |
307 | } | |
308 | } | |
309 | ||
310 | ||
311 | /* Write the new value of the bitmapped register to the monitor. */ | |
312 | ||
313 | static void | |
314 | r3900_store_bitmapped_register (regno, bf) | |
315 | int regno; | |
316 | struct bit_field *bf; | |
317 | { | |
318 | unsigned long oldval, newval; | |
319 | ||
320 | /* Fetch the current value of the register. */ | |
321 | monitor_printf ("x%s\r", r3900_regnames[regno]); | |
322 | oldval = r3900_fetch_fields (bf); | |
323 | newval = read_register (regno); | |
324 | ||
325 | /* To save time, write just the fields that have changed. */ | |
326 | for ( ; bf->prefix != NULL; bf++) | |
327 | { | |
328 | if (bf->length != 0) | |
329 | { | |
330 | unsigned long oldbits, newbits, mask; | |
331 | ||
332 | mask = (1 << bf->length) - 1; | |
333 | oldbits = (oldval >> bf->start) & mask; | |
334 | newbits = (newval >> bf->start) & mask; | |
335 | if (oldbits != newbits) | |
336 | monitor_printf ("%s %x ", bf->user_name, newbits); | |
337 | } | |
338 | } | |
339 | ||
340 | monitor_printf (".\r"); | |
341 | monitor_expect_prompt (NULL, 0); | |
342 | } | |
343 | ||
344 | static void | |
345 | r3900_store_registers (regno) | |
346 | int regno; | |
347 | { | |
348 | switch (regno) | |
349 | { | |
350 | case PS_REGNUM: | |
351 | r3900_store_bitmapped_register (PS_REGNUM, status_fields); | |
352 | return; | |
353 | case CAUSE_REGNUM: | |
354 | r3900_store_bitmapped_register (CAUSE_REGNUM, cause_fields); | |
355 | return; | |
356 | default: | |
357 | orig_monitor_store_registers (regno); | |
358 | } | |
359 | } | |
360 | ||
361 | static void | |
362 | r3900_load (monops, filename, from_tty) | |
363 | struct monitor_ops *monops; | |
364 | char *filename; | |
365 | int from_tty; | |
366 | { | |
367 | extern int inferior_pid; | |
368 | ||
369 | generic_load (filename, from_tty); | |
370 | ||
371 | /* Finally, make the PC point at the start address */ | |
372 | if (exec_bfd) | |
373 | write_pc (bfd_get_start_address (exec_bfd)); | |
374 | ||
375 | inferior_pid = 0; /* No process now */ | |
376 | } | |
377 | ||
378 | ||
379 | static struct target_ops r3900_ops; | |
380 | ||
381 | /* Commands to send to the monitor when first connecting: | |
382 | * The bare carriage return forces a prompt from the monitor | |
383 | (monitor doesn't prompt after a reset). | |
384 | * The "Xtr" command causes subsequent "t" (trace) commands to display | |
385 | the general registers only. | |
386 | * The "Xxr" command does the same thing for the "x" (examine | |
387 | registers) command. | |
388 | * The "bx" command clears all breakpoints. | |
389 | */ | |
390 | ||
391 | static char *r3900_inits[] = {"\r", "Xtr\r", "Xxr\r", "bx\r", NULL}; | |
392 | ||
393 | static struct monitor_ops r3900_cmds; | |
394 | ||
395 | static void | |
396 | r3900_open (args, from_tty) | |
397 | char *args; | |
398 | int from_tty; | |
399 | { | |
400 | monitor_open (args, &r3900_cmds, from_tty); | |
401 | } | |
402 | ||
403 | void | |
404 | _initialize_r3900_rom () | |
405 | { | |
352f9e9d | 406 | r3900_cmds.flags = MO_NO_ECHO_ON_OPEN | |
7e9576e0 | 407 | MO_ADDR_BITS_REMOVE | |
352f9e9d MA |
408 | MO_CLR_BREAK_USES_ADDR | |
409 | MO_PRINT_PROGRAM_OUTPUT; | |
7e9576e0 MA |
410 | |
411 | r3900_cmds.init = r3900_inits; | |
412 | r3900_cmds.cont = "g\r"; | |
413 | r3900_cmds.step = "t\r"; | |
414 | r3900_cmds.set_break = "b %Lx\r"; /* COREADDR */ | |
415 | r3900_cmds.clr_break = "b %Lx,0\r"; /* COREADDR */ | |
416 | r3900_cmds.fill = "fx %Lx s %x %x\r"; /* COREADDR, len, val */ | |
417 | ||
418 | r3900_cmds.setmem.cmdb = "sx %Lx %x\r"; /* COREADDR, val */ | |
419 | r3900_cmds.setmem.cmdw = "sh %Lx %x\r"; /* COREADDR, val */ | |
420 | r3900_cmds.setmem.cmdl = "sw %Lx %x\r"; /* COREADDR, val */ | |
421 | ||
422 | r3900_cmds.getmem.cmdb = "dx %Lx s %x\r"; /* COREADDR, len */ | |
423 | r3900_cmds.getmem.resp_delim = " : "; | |
424 | ||
425 | r3900_cmds.setreg.cmd = "x%s %x\r"; /* regname, val */ | |
426 | ||
427 | r3900_cmds.getreg.cmd = "x%s\r"; /* regname */ | |
428 | r3900_cmds.getreg.resp_delim = "="; | |
429 | r3900_cmds.getreg.term = " "; | |
430 | r3900_cmds.getreg.term_cmd = ".\r"; | |
431 | ||
432 | r3900_cmds.dump_registers = "x\r"; | |
433 | r3900_cmds.register_pattern = | |
434 | "\\([a-zA-Z0-9_]+\\) *=\\([0-9a-f]+ [0-9a-f]+\\b\\)"; | |
435 | r3900_cmds.supply_register = r3900_supply_register; | |
436 | /* S-record download, via "keyboard port". */ | |
437 | r3900_cmds.load = "r0\r"; | |
438 | #if 0 /* FIXME - figure out how to get fast load to work */ | |
439 | r3900_cmds.load_routine = r3900_load; | |
440 | #endif | |
441 | r3900_cmds.prompt = "#"; | |
442 | r3900_cmds.line_term = "\r"; | |
443 | r3900_cmds.target = &r3900_ops; | |
444 | r3900_cmds.stopbits = SERIAL_1_STOPBITS; | |
445 | r3900_cmds.regnames = r3900_regnames; | |
446 | r3900_cmds.magic = MONITOR_OPS_MAGIC; | |
447 | ||
448 | init_monitor_ops (&r3900_ops); | |
449 | ||
450 | r3900_ops.to_shortname = "r3900"; | |
451 | r3900_ops.to_longname = "R3900 monitor"; | |
452 | r3900_ops.to_doc = "Debug using the DVE R3900 monitor.\n\ | |
453 | Specify the serial device it is connected to (e.g. /dev/ttya)."; | |
454 | r3900_ops.to_open = r3900_open; | |
455 | ||
456 | /* Override the functions to fetch and store registers. But save the | |
457 | addresses of the default functions, because we will use those functions | |
458 | for "normal" registers. */ | |
459 | ||
460 | orig_monitor_fetch_registers = r3900_ops.to_fetch_registers; | |
461 | orig_monitor_store_registers = r3900_ops.to_store_registers; | |
462 | r3900_ops.to_fetch_registers = r3900_fetch_registers; | |
463 | r3900_ops.to_store_registers = r3900_store_registers; | |
464 | ||
465 | add_target (&r3900_ops); | |
466 | } |