]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-m32r-sdi.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / remote-m32r-sdi.c
CommitLineData
b4b4b794
KI
1/* Remote debugging interface for M32R/SDI.
2
6aba47ca 3 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
b4b4b794
KI
4
5 Contributed by Renesas Technology Co.
6 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
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
197e01b6
EZ
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
b4b4b794
KI
24
25#include "defs.h"
26#include "gdbcmd.h"
27#include "gdbcore.h"
28#include "inferior.h"
29#include "target.h"
30#include "regcache.h"
31#include "gdb_string.h"
32#include <ctype.h>
33#include <signal.h>
cbba9205
KI
34#ifdef __MINGW32__
35#include <winsock.h>
36#else
b4b4b794 37#include <netinet/in.h>
cbba9205 38#endif
b4b4b794
KI
39#include <sys/types.h>
40#include <sys/time.h>
41#include <signal.h>
42#include <time.h>
43
44
45#include "serial.h"
46
47/* Descriptor for I/O to remote machine. */
48
49static struct serial *sdi_desc = NULL;
50
51#define SDI_TIMEOUT 30
52
53
54#define SDIPORT 3232
55
56static char chip_name[64];
57
58static int step_mode;
59static unsigned long last_pc_addr = 0xffffffff;
60static unsigned char last_pc_addr_data[2];
61
62static int mmu_on = 0;
63
64static int use_ib_breakpoints = 1;
65
66#define MAX_BREAKPOINTS 1024
67static int max_ib_breakpoints;
68static unsigned long bp_address[MAX_BREAKPOINTS];
69static unsigned char bp_data[MAX_BREAKPOINTS][4];
b4b4b794
KI
70
71/* dbt -> nop */
72static const unsigned char dbt_bp_entry[] = {
73 0x10, 0xe0, 0x70, 0x00
74};
75
76#define MAX_ACCESS_BREAKS 4
77static int max_access_breaks;
78static unsigned long ab_address[MAX_ACCESS_BREAKS];
79static unsigned int ab_type[MAX_ACCESS_BREAKS];
80static unsigned int ab_size[MAX_ACCESS_BREAKS];
81static CORE_ADDR hit_watchpoint_addr = 0;
82
83static int interrupted = 0;
84
85/* Forward data declarations */
86extern struct target_ops m32r_ops;
87
88
89/* Commands */
90#define SDI_OPEN 1
91#define SDI_CLOSE 2
92#define SDI_RELEASE 3
93#define SDI_READ_CPU_REG 4
94#define SDI_WRITE_CPU_REG 5
95#define SDI_READ_MEMORY 6
96#define SDI_WRITE_MEMORY 7
97#define SDI_EXEC_CPU 8
98#define SDI_STOP_CPU 9
99#define SDI_WAIT_FOR_READY 10
100#define SDI_GET_ATTR 11
101#define SDI_SET_ATTR 12
102#define SDI_STATUS 13
103
104/* Attributes */
105#define SDI_ATTR_NAME 1
106#define SDI_ATTR_BRK 2
107#define SDI_ATTR_ABRK 3
108#define SDI_ATTR_CACHE 4
109#define SDI_CACHE_TYPE_M32102 0
110#define SDI_CACHE_TYPE_CHAOS 1
111#define SDI_ATTR_MEM_ACCESS 5
112#define SDI_MEM_ACCESS_DEBUG_DMA 0
113#define SDI_MEM_ACCESS_MON_CODE 1
114
115/* Registers */
116#define SDI_REG_R0 0
117#define SDI_REG_R1 1
118#define SDI_REG_R2 2
119#define SDI_REG_R3 3
120#define SDI_REG_R4 4
121#define SDI_REG_R5 5
122#define SDI_REG_R6 6
123#define SDI_REG_R7 7
124#define SDI_REG_R8 8
125#define SDI_REG_R9 9
126#define SDI_REG_R10 10
127#define SDI_REG_R11 11
128#define SDI_REG_R12 12
129#define SDI_REG_FP 13
130#define SDI_REG_LR 14
131#define SDI_REG_SP 15
132#define SDI_REG_PSW 16
133#define SDI_REG_CBR 17
134#define SDI_REG_SPI 18
135#define SDI_REG_SPU 19
136#define SDI_REG_CR4 20
137#define SDI_REG_EVB 21
138#define SDI_REG_BPC 22
139#define SDI_REG_CR7 23
140#define SDI_REG_BBPSW 24
141#define SDI_REG_CR9 25
142#define SDI_REG_CR10 26
143#define SDI_REG_CR11 27
144#define SDI_REG_CR12 28
145#define SDI_REG_WR 29
146#define SDI_REG_BBPC 30
147#define SDI_REG_PBP 31
148#define SDI_REG_ACCH 32
149#define SDI_REG_ACCL 33
150#define SDI_REG_ACC1H 34
151#define SDI_REG_ACC1L 35
152
153
154/* Low level communication functions */
155
156/* Check an ack packet from the target */
157static int
158get_ack (void)
159{
160 int c;
161
717eb1cf 162 if (!sdi_desc)
b4b4b794
KI
163 return -1;
164
165 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
166
167 if (c < 0)
168 return -1;
169
717eb1cf 170 if (c != '+') /* error */
b4b4b794
KI
171 return -1;
172
173 return 0;
174}
175
176/* Send data to the target and check an ack packet */
177static int
178send_data (void *buf, int len)
179{
180 int ret;
181
717eb1cf 182 if (!sdi_desc)
b4b4b794
KI
183 return -1;
184
185 if (serial_write (sdi_desc, buf, len) != 0)
186 return -1;
187
188 if (get_ack () == -1)
189 return -1;
190
191 return len;
192}
193
194/* Receive data from the target */
195static int
196recv_data (void *buf, int len)
197{
198 int total = 0;
199 int c;
200
717eb1cf 201 if (!sdi_desc)
b4b4b794
KI
202 return -1;
203
204 while (total < len)
205 {
206 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
207
208 if (c < 0)
209 return -1;
210
211 ((unsigned char *) buf)[total++] = c;
212 }
213
214 return len;
215}
216
217/* Store unsigned long parameter on packet */
218static void
219store_long_parameter (void *buf, long val)
220{
221 val = htonl (val);
222 memcpy (buf, &val, 4);
223}
224
e22f895c
KI
225static int
226send_cmd (unsigned char cmd)
227{
228 unsigned char buf[1];
229 buf[0] = cmd;
230 return send_data (buf, 1);
231}
232
233static int
234send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
235{
236 unsigned char buf[2];
237 buf[0] = cmd;
238 buf[1] = arg1;
239 return send_data (buf, 2);
240}
241
242static int
243send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
244{
245 unsigned char buf[6];
246 buf[0] = cmd;
247 buf[1] = arg1;
248 store_long_parameter (buf + 2, arg2);
249 return send_data (buf, 6);
250}
251
252static int
253send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
254 unsigned long arg3)
255{
256 unsigned char buf[13];
257 buf[0] = cmd;
258 store_long_parameter (buf + 1, arg1);
259 store_long_parameter (buf + 5, arg2);
260 store_long_parameter (buf + 9, arg3);
261 return send_data (buf, 13);
262}
263
264static unsigned char
265recv_char_data (void)
266{
267 unsigned char val;
268 recv_data (&val, 1);
269 return val;
270}
271
272static unsigned long
273recv_long_data (void)
274{
275 unsigned long val;
276 recv_data (&val, 4);
277 return ntohl (val);
278}
279
280
b4b4b794
KI
281/* Check if MMU is on */
282static void
283check_mmu_status (void)
284{
285 unsigned long val;
b4b4b794
KI
286
287 /* Read PC address */
e22f895c 288 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
b4b4b794 289 return;
e22f895c 290 val = recv_long_data ();
b4b4b794
KI
291 if ((val & 0xc0000000) == 0x80000000)
292 {
293 mmu_on = 1;
294 return;
295 }
296
297 /* Read EVB address */
e22f895c 298 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
b4b4b794 299 return;
e22f895c 300 val = recv_long_data ();
b4b4b794
KI
301 if ((val & 0xc0000000) == 0x80000000)
302 {
303 mmu_on = 1;
304 return;
305 }
306
307 mmu_on = 0;
308}
309
310
311/* This is called not only when we first attach, but also when the
312 user types "run" after having attached. */
313static void
717eb1cf 314m32r_create_inferior (char *execfile, char *args, char **env, int from_tty)
b4b4b794
KI
315{
316 CORE_ADDR entry_pt;
317
318 if (args && *args)
8a3fe4f8 319 error (_("Cannot pass arguments to remote STDEBUG process"));
b4b4b794
KI
320
321 if (execfile == 0 || exec_bfd == 0)
8a3fe4f8 322 error (_("No executable file specified"));
b4b4b794
KI
323
324 if (remote_debug)
325 fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
326 args);
327
328 entry_pt = bfd_get_start_address (exec_bfd);
329
330 /* The "process" (board) is already stopped awaiting our commands, and
331 the program is already downloaded. We just set its PC and go. */
332
333 clear_proceed_status ();
334
335 /* Tell wait_for_inferior that we've started a new process. */
336 init_wait_for_inferior ();
337
338 /* Set up the "saved terminal modes" of the inferior
339 based on what modes we are starting it with. */
340 target_terminal_init ();
341
342 /* Install inferior's terminal modes. */
343 target_terminal_inferior ();
344
281b533b 345 write_pc (entry_pt);
b4b4b794
KI
346}
347
348/* Open a connection to a remote debugger.
349 NAME is the filename used for communication. */
350
351static void
352m32r_open (char *args, int from_tty)
353{
354 struct hostent *host_ent;
355 struct sockaddr_in server_addr;
356 char *port_str, hostname[256];
357 int port;
b4b4b794
KI
358 int i, n;
359 int yes = 1;
360
361 if (remote_debug)
362 fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
363
364 target_preopen (from_tty);
365
366 push_target (&m32r_ops);
367
368 if (args == NULL)
369 sprintf (hostname, "localhost:%d", SDIPORT);
370 else
371 {
372 port_str = strchr (args, ':');
373 if (port_str == NULL)
717eb1cf 374 sprintf (hostname, "%s:%d", args, SDIPORT);
b4b4b794
KI
375 else
376 strcpy (hostname, args);
377 }
378
379 sdi_desc = serial_open (hostname);
380 if (!sdi_desc)
8a3fe4f8 381 error (_("Connection refused."));
b4b4b794
KI
382
383 if (get_ack () == -1)
8a3fe4f8 384 error (_("Cannot connect to SDI target."));
b4b4b794 385
e22f895c 386 if (send_cmd (SDI_OPEN) == -1)
8a3fe4f8 387 error (_("Cannot connect to SDI target."));
b4b4b794
KI
388
389 /* Get maximum number of ib breakpoints */
e22f895c
KI
390 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
391 max_ib_breakpoints = recv_char_data ();
b4b4b794
KI
392 if (remote_debug)
393 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
394
395 /* Initialize breakpoints. */
396 for (i = 0; i < MAX_BREAKPOINTS; i++)
397 bp_address[i] = 0xffffffff;
398
399 /* Get maximum number of access breaks. */
e22f895c
KI
400 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
401 max_access_breaks = recv_char_data ();
b4b4b794
KI
402 if (remote_debug)
403 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
404
405 /* Initialize access breask. */
406 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
407 ab_address[i] = 0x00000000;
408
409 check_mmu_status ();
410
411 /* Get the name of chip on target board. */
e22f895c 412 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
b4b4b794
KI
413 recv_data (chip_name, 64);
414
415 if (from_tty)
416 printf_filtered ("Remote %s connected to %s\n", target_shortname,
417 chip_name);
418}
419
420/* Close out all files and local state before this target loses control. */
421
422static void
423m32r_close (int quitting)
424{
b4b4b794
KI
425 if (remote_debug)
426 fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
427
428 if (sdi_desc)
429 {
e22f895c 430 send_cmd (SDI_CLOSE);
b4b4b794
KI
431 serial_close (sdi_desc);
432 sdi_desc = NULL;
433 }
434
435 inferior_ptid = null_ptid;
436 return;
437}
438
439/* Tell the remote machine to resume. */
440
441static void
442m32r_resume (ptid_t ptid, int step, enum target_signal sig)
443{
444 unsigned long pc_addr, bp_addr, ab_addr;
e22f895c 445 int ib_breakpoints;
b4b4b794
KI
446 unsigned char buf[13];
447 int i;
448
449 if (remote_debug)
450 {
451 if (step)
452 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
453 else
454 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
455 }
456
457 check_mmu_status ();
458
459 pc_addr = read_pc ();
460 if (remote_debug)
461 fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
462
463 /* At pc address there is a parallel instruction with +2 offset,
464 so we have to make it a serial instruction or avoid it. */
465 if (pc_addr == last_pc_addr)
466 {
467 /* Avoid a parallel nop. */
468 if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
469 {
470 pc_addr += 2;
471 /* Now we can forget this instruction. */
472 last_pc_addr = 0xffffffff;
473 }
474 /* Clear a parallel bit. */
475 else
476 {
477 buf[0] = SDI_WRITE_MEMORY;
478 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
479 store_long_parameter (buf + 1, pc_addr);
480 else
481 store_long_parameter (buf + 1, pc_addr - 1);
482 store_long_parameter (buf + 5, 1);
483 buf[9] = last_pc_addr_data[0] & 0x7f;
484 send_data (buf, 10);
485 }
486 }
487
488 /* Set PC. */
e22f895c 489 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
b4b4b794
KI
490
491 /* step mode. */
492 step_mode = step;
493 if (step)
494 {
495 /* Set PBP. */
e22f895c 496 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
b4b4b794
KI
497 }
498 else
499 {
e22f895c
KI
500 /* Unset PBP. */
501 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
502 }
503
504 if (use_ib_breakpoints)
505 ib_breakpoints = max_ib_breakpoints;
506 else
507 ib_breakpoints = 0;
508
509 /* Set ib breakpoints. */
510 for (i = 0; i < ib_breakpoints; i++)
511 {
512 bp_addr = bp_address[i];
b4b4b794 513
e22f895c
KI
514 if (bp_addr == 0xffffffff)
515 continue;
516
517 /* Set PBP. */
518 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
519 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
520 0x00000006);
b4b4b794 521 else
e22f895c
KI
522 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
523 0x06000000);
524
525 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
526 }
527
528 /* Set dbt breakpoints. */
529 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
530 {
531 bp_addr = bp_address[i];
532
533 if (bp_addr == 0xffffffff)
534 continue;
535
536 if (!mmu_on)
537 bp_addr &= 0x7fffffff;
b4b4b794 538
e22f895c
KI
539 /* Write DBT instruction. */
540 buf[0] = SDI_WRITE_MEMORY;
492e5c6b 541 store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
e22f895c
KI
542 store_long_parameter (buf + 5, 4);
543 if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
b4b4b794 544 {
e22f895c 545 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
b4b4b794 546 {
e22f895c
KI
547 buf[9] = dbt_bp_entry[0];
548 buf[10] = dbt_bp_entry[1];
549 buf[11] = dbt_bp_entry[2];
550 buf[12] = dbt_bp_entry[3];
551 }
552 else
553 {
554 buf[9] = dbt_bp_entry[3];
555 buf[10] = dbt_bp_entry[2];
556 buf[11] = dbt_bp_entry[1];
557 buf[12] = dbt_bp_entry[0];
b4b4b794
KI
558 }
559 }
e22f895c 560 else
b4b4b794 561 {
e22f895c 562 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
b4b4b794 563 {
e22f895c 564 if ((bp_addr & 2) == 0)
b4b4b794 565 {
e22f895c
KI
566 buf[9] = dbt_bp_entry[0];
567 buf[10] = dbt_bp_entry[1];
568 buf[11] = bp_data[i][2] & 0x7f;
569 buf[12] = bp_data[i][3];
b4b4b794
KI
570 }
571 else
572 {
e22f895c
KI
573 buf[9] = bp_data[i][0];
574 buf[10] = bp_data[i][1];
575 buf[11] = dbt_bp_entry[0];
576 buf[12] = dbt_bp_entry[1];
b4b4b794
KI
577 }
578 }
e22f895c 579 else
b4b4b794 580 {
e22f895c 581 if ((bp_addr & 2) == 0)
b4b4b794 582 {
e22f895c
KI
583 buf[9] = bp_data[i][0];
584 buf[10] = bp_data[i][1] & 0x7f;
585 buf[11] = dbt_bp_entry[1];
586 buf[12] = dbt_bp_entry[0];
b4b4b794
KI
587 }
588 else
589 {
e22f895c
KI
590 buf[9] = dbt_bp_entry[1];
591 buf[10] = dbt_bp_entry[0];
592 buf[11] = bp_data[i][2];
593 buf[12] = bp_data[i][3];
b4b4b794 594 }
e22f895c
KI
595 }
596 }
597 send_data (buf, 13);
598 }
b4b4b794 599
e22f895c
KI
600 /* Set access breaks. */
601 for (i = 0; i < max_access_breaks; i++)
602 {
603 ab_addr = ab_address[i];
b4b4b794 604
e22f895c
KI
605 if (ab_addr == 0x00000000)
606 continue;
b4b4b794 607
e22f895c
KI
608 /* DBC register */
609 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
610 {
611 switch (ab_type[i])
612 {
613 case 0: /* write watch */
614 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
615 0x00000086);
616 break;
617 case 1: /* read watch */
618 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
619 0x00000046);
620 break;
621 case 2: /* access watch */
622 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
623 0x00000006);
624 break;
625 }
626 }
627 else
628 {
629 switch (ab_type[i])
630 {
631 case 0: /* write watch */
632 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
633 0x86000000);
634 break;
635 case 1: /* read watch */
636 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
637 0x46000000);
638 break;
639 case 2: /* access watch */
640 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
641 0x06000000);
642 break;
b4b4b794
KI
643 }
644 }
645
e22f895c
KI
646 /* DBAH register */
647 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
648
649 /* DBAL register */
650 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
651 0xffffffff);
652
653 /* DBD register */
654 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
655 0x00000000);
656
657 /* DBDM register */
658 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
659 0x00000000);
b4b4b794
KI
660 }
661
e22f895c
KI
662 /* Resume program. */
663 send_cmd (SDI_EXEC_CPU);
b4b4b794
KI
664
665 /* Without this, some commands which require an active target (such as kill)
666 won't work. This variable serves (at least) double duty as both the pid
667 of the target process (if it has such), and as a flag indicating that a
668 target is active. These functions should be split out into seperate
669 variables, especially since GDB will someday have a notion of debugging
670 several processes. */
671 inferior_ptid = pid_to_ptid (32);
672
673 return;
674}
675
676/* Wait until the remote machine stops, then return,
677 storing status in STATUS just as `wait' would. */
678
679static void
680gdb_cntrl_c (int signo)
681{
682 if (remote_debug)
683 fprintf_unfiltered (gdb_stdlog, "interrupt\n");
684 interrupted = 1;
685}
686
687static ptid_t
688m32r_wait (ptid_t ptid, struct target_waitstatus *status)
689{
690 static RETSIGTYPE (*prev_sigint) ();
691 unsigned long bp_addr, pc_addr;
e22f895c 692 int ib_breakpoints;
b4b4b794
KI
693 long i;
694 unsigned char buf[13];
695 unsigned long val;
696 int ret, c;
697
698 if (remote_debug)
699 fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
700
701 status->kind = TARGET_WAITKIND_EXITED;
702 status->value.sig = 0;
703
704 interrupted = 0;
705 prev_sigint = signal (SIGINT, gdb_cntrl_c);
706
707 /* Wait for ready */
708 buf[0] = SDI_WAIT_FOR_READY;
709 if (serial_write (sdi_desc, buf, 1) != 0)
8a3fe4f8 710 error (_("Remote connection closed"));
b4b4b794
KI
711
712 while (1)
713 {
714 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
715 if (c < 0)
8a3fe4f8 716 error (_("Remote connection closed"));
b4b4b794 717
717eb1cf 718 if (c == '-') /* error */
b4b4b794
KI
719 {
720 status->kind = TARGET_WAITKIND_STOPPED;
721 status->value.sig = TARGET_SIGNAL_HUP;
722 return inferior_ptid;
723 }
724 else if (c == '+') /* stopped */
725 break;
726
727 if (interrupted)
728 ret = serial_write (sdi_desc, "!", 1); /* packet to interrupt */
729 else
730 ret = serial_write (sdi_desc, ".", 1); /* packet to wait */
731 if (ret != 0)
8a3fe4f8 732 error (_("Remote connection closed"));
b4b4b794
KI
733 }
734
735 status->kind = TARGET_WAITKIND_STOPPED;
736 if (interrupted)
737 status->value.sig = TARGET_SIGNAL_INT;
738 else
739 status->value.sig = TARGET_SIGNAL_TRAP;
740
741 interrupted = 0;
742 signal (SIGINT, prev_sigint);
743
744 check_mmu_status ();
745
746 /* Recover parallel bit. */
747 if (last_pc_addr != 0xffffffff)
748 {
749 buf[0] = SDI_WRITE_MEMORY;
750 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
751 store_long_parameter (buf + 1, last_pc_addr);
752 else
753 store_long_parameter (buf + 1, last_pc_addr - 1);
754 store_long_parameter (buf + 5, 1);
755 buf[9] = last_pc_addr_data[0];
756 send_data (buf, 10);
757 last_pc_addr = 0xffffffff;
758 }
759
e22f895c
KI
760 if (use_ib_breakpoints)
761 ib_breakpoints = max_ib_breakpoints;
762 else
763 ib_breakpoints = 0;
b4b4b794 764
e22f895c
KI
765 /* Set back pc by 2 if m32r is stopped with dbt. */
766 last_pc_addr = 0xffffffff;
767 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
768 pc_addr = recv_long_data () - 2;
769 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
770 {
771 if (pc_addr == bp_address[i])
b4b4b794 772 {
e22f895c
KI
773 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
774
775 /* If there is a parallel instruction with +2 offset at pc
776 address, we have to take care of it later. */
777 if ((pc_addr & 0x2) != 0)
b4b4b794 778 {
e22f895c 779 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
b4b4b794 780 {
e22f895c 781 if ((bp_data[i][2] & 0x80) != 0)
b4b4b794 782 {
e22f895c
KI
783 last_pc_addr = pc_addr;
784 last_pc_addr_data[0] = bp_data[i][2];
785 last_pc_addr_data[1] = bp_data[i][3];
b4b4b794 786 }
e22f895c
KI
787 }
788 else
789 {
790 if ((bp_data[i][1] & 0x80) != 0)
b4b4b794 791 {
e22f895c
KI
792 last_pc_addr = pc_addr;
793 last_pc_addr_data[0] = bp_data[i][1];
794 last_pc_addr_data[1] = bp_data[i][0];
b4b4b794
KI
795 }
796 }
b4b4b794 797 }
e22f895c 798 break;
b4b4b794 799 }
e22f895c 800 }
b4b4b794 801
e22f895c
KI
802 /* Remove ib breakpoints. */
803 for (i = 0; i < ib_breakpoints; i++)
804 {
805 if (bp_address[i] != 0xffffffff)
806 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
807 0x00000000);
808 }
809 /* Remove dbt breakpoints. */
810 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
811 {
812 bp_addr = bp_address[i];
813 if (bp_addr != 0xffffffff)
b4b4b794 814 {
e22f895c
KI
815 if (!mmu_on)
816 bp_addr &= 0x7fffffff;
492e5c6b 817 buf[0] = SDI_WRITE_MEMORY;
e22f895c
KI
818 store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
819 store_long_parameter (buf + 5, 4);
820 buf[9] = bp_data[i][0];
821 buf[10] = bp_data[i][1];
822 buf[11] = bp_data[i][2];
823 buf[12] = bp_data[i][3];
824 send_data (buf, 13);
b4b4b794 825 }
e22f895c 826 }
b4b4b794 827
e22f895c
KI
828 /* Remove access breaks. */
829 hit_watchpoint_addr = 0;
830 for (i = 0; i < max_access_breaks; i++)
831 {
832 if (ab_address[i] != 0x00000000)
b4b4b794 833 {
e22f895c
KI
834 buf[0] = SDI_READ_MEMORY;
835 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
836 store_long_parameter (buf + 5, 4);
837 serial_write (sdi_desc, buf, 9);
838 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
839 if (c != '-' && recv_data (buf, 4) != -1)
b4b4b794 840 {
e22f895c 841 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
b4b4b794 842 {
e22f895c
KI
843 if ((buf[3] & 0x1) == 0x1)
844 hit_watchpoint_addr = ab_address[i];
845 }
846 else
847 {
848 if ((buf[0] & 0x1) == 0x1)
849 hit_watchpoint_addr = ab_address[i];
b4b4b794 850 }
b4b4b794 851 }
b4b4b794 852
e22f895c
KI
853 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
854 0x00000000);
855 }
b4b4b794 856 }
e22f895c
KI
857
858 if (remote_debug)
859 fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
b4b4b794
KI
860
861 return inferior_ptid;
862}
863
864/* Terminate the open connection to the remote debugger.
865 Use this when you want to detach and do something else
866 with your gdb. */
867static void
868m32r_detach (char *args, int from_tty)
869{
870 if (remote_debug)
871 fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
872
873 m32r_resume (inferior_ptid, 0, 0);
874
875 /* calls m32r_close to do the real work */
876 pop_target ();
877 if (from_tty)
878 fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
879 target_shortname);
880}
881
882/* Return the id of register number REGNO. */
883
884static int
885get_reg_id (int regno)
886{
887 switch (regno)
888 {
889 case 20:
890 return SDI_REG_BBPC;
891 case 21:
892 return SDI_REG_BPC;
893 case 22:
894 return SDI_REG_ACCL;
895 case 23:
896 return SDI_REG_ACCH;
897 case 24:
898 return SDI_REG_EVB;
899 }
900
901 return regno;
902}
903
904/* Read the remote registers into the block REGS. */
905
906static void m32r_fetch_register (int);
907
908static void
909m32r_fetch_registers (void)
910{
911 int regno;
912
913 for (regno = 0; regno < NUM_REGS; regno++)
914 m32r_fetch_register (regno);
915}
916
917/* Fetch register REGNO, or all registers if REGNO is -1.
918 Returns errno value. */
919static void
920m32r_fetch_register (int regno)
921{
922 unsigned long val, val2, regid;
b4b4b794
KI
923
924 if (regno == -1)
925 m32r_fetch_registers ();
926 else
927 {
928 char buffer[MAX_REGISTER_SIZE];
929
930 regid = get_reg_id (regno);
e22f895c
KI
931 send_one_arg_cmd (SDI_READ_CPU_REG, regid);
932 val = recv_long_data ();
b4b4b794
KI
933
934 if (regid == SDI_REG_PSW)
935 {
e22f895c
KI
936 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
937 val2 = recv_long_data ();
b4b4b794
KI
938 val = ((0x00c1 & val2) << 8) | ((0xc100 & val) >> 8);
939 }
940
941 if (remote_debug)
942 fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
943 regno, val);
944
945 /* We got the number the register holds, but gdb expects to see a
946 value in the target byte ordering. */
947 store_unsigned_integer (buffer, 4, val);
23a6d369 948 regcache_raw_supply (current_regcache, regno, buffer);
b4b4b794
KI
949 }
950 return;
951}
952
953/* Store the remote registers from the contents of the block REGS. */
954
955static void m32r_store_register (int);
956
957static void
958m32r_store_registers (void)
959{
960 int regno;
961
962 for (regno = 0; regno < NUM_REGS; regno++)
963 m32r_store_register (regno);
964
965 registers_changed ();
966}
967
968/* Store register REGNO, or all if REGNO == 0.
969 Return errno value. */
970static void
971m32r_store_register (int regno)
972{
973 int regid;
974 ULONGEST regval, tmp;
b4b4b794
KI
975
976 if (regno == -1)
977 m32r_store_registers ();
978 else
979 {
980 regcache_cooked_read_unsigned (current_regcache, regno, &regval);
981 regid = get_reg_id (regno);
982
983 if (regid == SDI_REG_PSW)
984 {
985 unsigned long psw, bbpsw;
986
e22f895c
KI
987 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
988 psw = recv_long_data ();
b4b4b794 989
e22f895c
KI
990 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
991 bbpsw = recv_long_data ();
b4b4b794
KI
992
993 tmp = (0x00c1 & psw) | ((0x00c1 & regval) << 8);
e22f895c 994 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
b4b4b794
KI
995
996 tmp = (0x0030 & bbpsw) | ((0xc100 & regval) >> 8);
e22f895c 997 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
b4b4b794
KI
998 }
999 else
1000 {
e22f895c 1001 send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
b4b4b794
KI
1002 }
1003
1004 if (remote_debug)
1005 fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1006 regno, (unsigned long) regval);
1007 }
1008}
1009
1010/* Get ready to modify the registers array. On machines which store
1011 individual registers, this doesn't need to do anything. On machines
1012 which store all the registers in one fell swoop, this makes sure
1013 that registers contains all the registers from the program being
1014 debugged. */
1015
1016static void
1017m32r_prepare_to_store (void)
1018{
1019 /* Do nothing, since we can store individual regs */
1020 if (remote_debug)
1021 fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1022}
1023
1024static void
1025m32r_files_info (struct target_ops *target)
1026{
1027 char *file = "nothing";
1028
1029 if (exec_bfd)
1030 {
1031 file = bfd_get_filename (exec_bfd);
1032 printf_filtered ("\tAttached to %s running program %s\n",
1033 chip_name, file);
1034 }
1035}
1036
1037/* Read/Write memory. */
1038static int
16ac4ab5 1039m32r_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len,
b4b4b794
KI
1040 int write,
1041 struct mem_attrib *attrib, struct target_ops *target)
1042{
1043 unsigned long taddr;
1044 unsigned char buf[0x2000];
1045 int ret, c;
1046
1047 taddr = memaddr;
1048
1049 if (!mmu_on)
1050 {
1051 if ((taddr & 0xa0000000) == 0x80000000)
1052 taddr &= 0x7fffffff;
1053 }
1054
1055 if (remote_debug)
1056 {
1057 if (write)
1058 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%08lx,%d,write)\n",
1059 memaddr, len);
1060 else
1061 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%08lx,%d,read)\n",
1062 memaddr, len);
1063 }
1064
1065 if (write)
1066 {
1067 buf[0] = SDI_WRITE_MEMORY;
1068 store_long_parameter (buf + 1, taddr);
1069 store_long_parameter (buf + 5, len);
1070 if (len < 0x1000)
1071 {
1072 memcpy (buf + 9, myaddr, len);
1073 ret = send_data (buf, len + 9) - 9;
1074 }
1075 else
1076 {
1077 if (serial_write (sdi_desc, buf, 9) != 0)
1078 {
1079 if (remote_debug)
1080 fprintf_unfiltered (gdb_stdlog,
1081 "m32r_xfer_memory() failed\n");
1082 return 0;
1083 }
1084 ret = send_data (myaddr, len);
1085 }
1086 }
1087 else
1088 {
1089 buf[0] = SDI_READ_MEMORY;
1090 store_long_parameter (buf + 1, taddr);
1091 store_long_parameter (buf + 5, len);
1092 if (serial_write (sdi_desc, buf, 9) != 0)
1093 {
1094 if (remote_debug)
1095 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1096 return 0;
1097 }
1098
1099 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1100 if (c < 0 || c == '-')
1101 {
1102 if (remote_debug)
1103 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1104 return 0;
1105 }
1106
1107 ret = recv_data (myaddr, len);
1108 }
1109
1110 if (ret <= 0)
1111 {
1112 if (remote_debug)
1113 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1114 return 0;
1115 }
1116
1117 return ret;
1118}
1119
1120static void
1121m32r_kill (void)
1122{
1123 if (remote_debug)
1124 fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1125
1126 inferior_ptid = null_ptid;
1127
1128 return;
1129}
1130
1131/* Clean up when a program exits.
1132
1133 The program actually lives on in the remote processor's RAM, and may be
1134 run again without a download. Don't leave it full of breakpoint
1135 instructions. */
1136
1137static void
1138m32r_mourn_inferior (void)
1139{
1140 if (remote_debug)
1141 fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1142
1143 remove_breakpoints ();
1144 generic_mourn_inferior ();
1145}
1146
1147static int
8181d85f 1148m32r_insert_breakpoint (struct bp_target_info *bp_tgt)
b4b4b794 1149{
8181d85f 1150 CORE_ADDR addr = bp_tgt->placed_address;
b4b4b794
KI
1151 int ib_breakpoints;
1152 unsigned char buf[13];
1153 int i, c;
1154
1155 if (remote_debug)
8181d85f
DJ
1156 fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%08lx,...)\n",
1157 addr);
b4b4b794
KI
1158
1159 if (use_ib_breakpoints)
1160 ib_breakpoints = max_ib_breakpoints;
1161 else
1162 ib_breakpoints = 0;
1163
1164 for (i = 0; i < MAX_BREAKPOINTS; i++)
1165 {
1166 if (bp_address[i] == 0xffffffff)
1167 {
1168 bp_address[i] = addr;
1169 if (i >= ib_breakpoints)
1170 {
1171 buf[0] = SDI_READ_MEMORY;
1172 if (mmu_on)
1173 store_long_parameter (buf + 1, addr & 0xfffffffc);
1174 else
1175 store_long_parameter (buf + 1, addr & 0x7ffffffc);
1176 store_long_parameter (buf + 5, 4);
1177 serial_write (sdi_desc, buf, 9);
1178 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1179 if (c != '-')
1180 recv_data (bp_data[i], 4);
1181 }
1182 return 0;
1183 }
1184 }
1185
8a3fe4f8 1186 error (_("Too many breakpoints"));
b4b4b794
KI
1187 return 1;
1188}
1189
1190static int
8181d85f 1191m32r_remove_breakpoint (struct bp_target_info *bp_tgt)
b4b4b794 1192{
8181d85f 1193 CORE_ADDR addr = bp_tgt->placed_address;
b4b4b794
KI
1194 int i;
1195
1196 if (remote_debug)
8181d85f
DJ
1197 fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%08lx)\n",
1198 addr);
b4b4b794
KI
1199
1200 for (i = 0; i < MAX_BREAKPOINTS; i++)
1201 {
1202 if (bp_address[i] == addr)
1203 {
1204 bp_address[i] = 0xffffffff;
1205 break;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
1212static void
1213m32r_load (char *args, int from_tty)
1214{
1215 struct cleanup *old_chain;
1216 asection *section;
1217 bfd *pbfd;
1218 bfd_vma entry;
1219 char *filename;
1220 int quiet;
1221 int nostart;
2b71414d 1222 struct timeval start_time, end_time;
b4b4b794
KI
1223 unsigned long data_count; /* Number of bytes transferred to memory */
1224 int ret;
1225 static RETSIGTYPE (*prev_sigint) ();
1226
1227 /* for direct tcp connections, we can do a fast binary download */
1228 quiet = 0;
1229 nostart = 0;
1230 filename = NULL;
1231
1232 while (*args != '\000')
1233 {
1234 char *arg;
1235
1236 while (isspace (*args))
1237 args++;
1238
1239 arg = args;
1240
1241 while ((*args != '\000') && !isspace (*args))
1242 args++;
1243
1244 if (*args != '\000')
1245 *args++ = '\000';
1246
1247 if (*arg != '-')
1248 filename = arg;
1249 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1250 quiet = 1;
1251 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1252 nostart = 1;
1253 else
8a3fe4f8 1254 error (_("Unknown option `%s'"), arg);
b4b4b794
KI
1255 }
1256
1257 if (!filename)
1258 filename = get_exec_file (1);
1259
1260 pbfd = bfd_openr (filename, gnutarget);
1261 if (pbfd == NULL)
1262 {
1263 perror_with_name (filename);
1264 return;
1265 }
1266 old_chain = make_cleanup_bfd_close (pbfd);
1267
1268 if (!bfd_check_format (pbfd, bfd_object))
8a3fe4f8 1269 error (_("\"%s\" is not an object file: %s"), filename,
b4b4b794
KI
1270 bfd_errmsg (bfd_get_error ()));
1271
2b71414d 1272 gettimeofday (&start_time, NULL);
b4b4b794
KI
1273 data_count = 0;
1274
1275 interrupted = 0;
1276 prev_sigint = signal (SIGINT, gdb_cntrl_c);
1277
1278 for (section = pbfd->sections; section; section = section->next)
1279 {
1280 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1281 {
1282 bfd_vma section_address;
1283 bfd_size_type section_size;
1284 file_ptr fptr;
1285 int n;
1286
1287 section_address = bfd_section_lma (pbfd, section);
2c500098 1288 section_size = bfd_get_section_size (section);
b4b4b794
KI
1289
1290 if (!mmu_on)
1291 {
1292 if ((section_address & 0xa0000000) == 0x80000000)
1293 section_address &= 0x7fffffff;
1294 }
1295
1296 if (!quiet)
1297 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1298 bfd_get_section_name (pbfd, section),
1299 section_address, (int) section_size);
1300
1301 fptr = 0;
1302
1303 data_count += section_size;
1304
1305 n = 0;
1306 while (section_size > 0)
1307 {
1308 char unsigned buf[0x1000 + 9];
1309 int count;
1310
1311 count = min (section_size, 0x1000);
1312
1313 buf[0] = SDI_WRITE_MEMORY;
1314 store_long_parameter (buf + 1, section_address);
1315 store_long_parameter (buf + 5, count);
1316
1317 bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1318 if (send_data (buf, count + 9) <= 0)
8a3fe4f8 1319 error (_("Error while downloading %s section."),
b4b4b794
KI
1320 bfd_get_section_name (pbfd, section));
1321
1322 if (!quiet)
1323 {
1324 printf_unfiltered (".");
1325 if (n++ > 60)
1326 {
1327 printf_unfiltered ("\n");
1328 n = 0;
1329 }
1330 gdb_flush (gdb_stdout);
1331 }
1332
1333 section_address += count;
1334 fptr += count;
1335 section_size -= count;
1336
1337 if (interrupted)
1338 break;
1339 }
1340
1341 if (!quiet && !interrupted)
1342 {
1343 printf_unfiltered ("done.\n");
1344 gdb_flush (gdb_stdout);
1345 }
1346 }
1347
1348 if (interrupted)
1349 {
1350 printf_unfiltered ("Interrupted.\n");
1351 break;
1352 }
1353 }
1354
1355 interrupted = 0;
1356 signal (SIGINT, prev_sigint);
1357
2b71414d 1358 gettimeofday (&end_time, NULL);
b4b4b794
KI
1359
1360 /* Make the PC point at the start address */
1361 if (exec_bfd)
1362 write_pc (bfd_get_start_address (exec_bfd));
1363
1364 inferior_ptid = null_ptid; /* No process now */
1365
1366 /* This is necessary because many things were based on the PC at the time
1367 that we attached to the monitor, which is no longer valid now that we
1368 have loaded new code (and just changed the PC). Another way to do this
1369 might be to call normal_stop, except that the stack may not be valid,
1370 and things would get horribly confused... */
1371
1372 clear_symtab_users ();
1373
1374 if (!nostart)
1375 {
1376 entry = bfd_get_start_address (pbfd);
1377
1378 if (!quiet)
1379 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename, entry);
1380 }
1381
2b71414d
DJ
1382 print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
1383 &end_time);
b4b4b794
KI
1384
1385 do_cleanups (old_chain);
1386}
1387
1388static void
1389m32r_stop (void)
1390{
b4b4b794
KI
1391 if (remote_debug)
1392 fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1393
e22f895c 1394 send_cmd (SDI_STOP_CPU);
b4b4b794
KI
1395
1396 return;
1397}
1398
1399
37814c18
KI
1400/* Tell whether this target can support a hardware breakpoint. CNT
1401 is the number of hardware breakpoints already installed. This
1402 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
b4b4b794
KI
1403
1404int
37814c18 1405m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
b4b4b794 1406{
37814c18 1407 return sdi_desc != NULL && cnt < max_access_breaks;
b4b4b794
KI
1408}
1409
1410/* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1411 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1412 watchpoint. */
1413
1414int
37814c18 1415m32r_insert_watchpoint (CORE_ADDR addr, int len, int type)
b4b4b794
KI
1416{
1417 int i;
1418
1419 if (remote_debug)
37814c18 1420 fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%08lx,%d,%d)\n",
b4b4b794
KI
1421 addr, len, type);
1422
1423 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1424 {
1425 if (ab_address[i] == 0x00000000)
1426 {
1427 ab_address[i] = addr;
1428 ab_size[i] = len;
1429 ab_type[i] = type;
1430 return 0;
1431 }
1432 }
1433
8a3fe4f8 1434 error (_("Too many watchpoints"));
b4b4b794
KI
1435 return 1;
1436}
1437
1438int
1439m32r_remove_watchpoint (CORE_ADDR addr, int len, int type)
1440{
1441 int i;
1442
1443 if (remote_debug)
1444 fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%08lx,%d,%d)\n",
1445 addr, len, type);
1446
1447 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1448 {
1449 if (ab_address[i] == addr)
1450 {
1451 ab_address[i] = 0x00000000;
1452 break;
1453 }
1454 }
1455
1456 return 0;
1457}
1458
4aa7a7f5
JJ
1459int
1460m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
b4b4b794 1461{
4aa7a7f5
JJ
1462 int rc = 0;
1463 if (hit_watchpoint_addr != 0x00000000)
1464 {
1465 *addr_p = hit_watchpoint_addr;
1466 rc = 1;
1467 }
1468 return rc;
b4b4b794
KI
1469}
1470
1471int
1472m32r_stopped_by_watchpoint (void)
1473{
4aa7a7f5
JJ
1474 CORE_ADDR addr;
1475 return m32r_stopped_data_address (&current_target, &addr);
b4b4b794
KI
1476}
1477
1478
1479static void
1480sdireset_command (char *args, int from_tty)
1481{
b4b4b794
KI
1482 if (remote_debug)
1483 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1484
e22f895c 1485 send_cmd (SDI_OPEN);
b4b4b794
KI
1486
1487 inferior_ptid = null_ptid;
1488}
1489
1490
1491static void
1492sdistatus_command (char *args, int from_tty)
1493{
1494 unsigned char buf[4096];
1495 int i, c;
1496
1497 if (remote_debug)
1498 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1499
1500 if (!sdi_desc)
1501 return;
1502
e22f895c 1503 send_cmd (SDI_STATUS);
b4b4b794
KI
1504 for (i = 0; i < 4096; i++)
1505 {
1506 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1507 if (c < 0)
717eb1cf 1508 return;
b4b4b794
KI
1509 buf[i] = c;
1510 if (c == 0)
717eb1cf
AC
1511 break;
1512 }
b4b4b794
KI
1513
1514 printf_filtered ("%s", buf);
1515}
1516
1517
1518static void
1519debug_chaos_command (char *args, int from_tty)
1520{
1521 unsigned char buf[3];
1522
1523 buf[0] = SDI_SET_ATTR;
1524 buf[1] = SDI_ATTR_CACHE;
1525 buf[2] = SDI_CACHE_TYPE_CHAOS;
1526 send_data (buf, 3);
1527}
1528
1529
1530static void
1531use_debug_dma_command (char *args, int from_tty)
1532{
1533 unsigned char buf[3];
1534
1535 buf[0] = SDI_SET_ATTR;
1536 buf[1] = SDI_ATTR_MEM_ACCESS;
1537 buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1538 send_data (buf, 3);
1539}
1540
1541static void
1542use_mon_code_command (char *args, int from_tty)
1543{
1544 unsigned char buf[3];
1545
1546 buf[0] = SDI_SET_ATTR;
1547 buf[1] = SDI_ATTR_MEM_ACCESS;
1548 buf[2] = SDI_MEM_ACCESS_MON_CODE;
1549 send_data (buf, 3);
1550}
1551
1552
1553static void
1554use_ib_breakpoints_command (char *args, int from_tty)
1555{
1556 use_ib_breakpoints = 1;
1557}
1558
1559static void
1560use_dbt_breakpoints_command (char *args, int from_tty)
1561{
1562 use_ib_breakpoints = 0;
1563}
1564
1565
1566/* Define the target subroutine names */
1567
1568struct target_ops m32r_ops;
1569
1570static void
1571init_m32r_ops (void)
1572{
1573 m32r_ops.to_shortname = "m32rsdi";
1574 m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1575 m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1576 m32r_ops.to_open = m32r_open;
1577 m32r_ops.to_close = m32r_close;
1578 m32r_ops.to_detach = m32r_detach;
1579 m32r_ops.to_resume = m32r_resume;
1580 m32r_ops.to_wait = m32r_wait;
1581 m32r_ops.to_fetch_registers = m32r_fetch_register;
1582 m32r_ops.to_store_registers = m32r_store_register;
1583 m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
c8e73a31 1584 m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
b4b4b794
KI
1585 m32r_ops.to_files_info = m32r_files_info;
1586 m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1587 m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
37814c18
KI
1588 m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
1589 m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
1590 m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
1591 m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
1592 m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
b4b4b794
KI
1593 m32r_ops.to_kill = m32r_kill;
1594 m32r_ops.to_load = m32r_load;
1595 m32r_ops.to_create_inferior = m32r_create_inferior;
1596 m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1597 m32r_ops.to_stop = m32r_stop;
1598 m32r_ops.to_stratum = process_stratum;
1599 m32r_ops.to_has_all_memory = 1;
1600 m32r_ops.to_has_memory = 1;
1601 m32r_ops.to_has_stack = 1;
1602 m32r_ops.to_has_registers = 1;
1603 m32r_ops.to_has_execution = 1;
1604 m32r_ops.to_magic = OPS_MAGIC;
1605};
1606
1607
1608extern initialize_file_ftype _initialize_remote_m32r;
1609
1610void
1611_initialize_remote_m32r (void)
1612{
1613 int i;
1614
1615 init_m32r_ops ();
1616
1617 /* Initialize breakpoints. */
1618 for (i = 0; i < MAX_BREAKPOINTS; i++)
1619 bp_address[i] = 0xffffffff;
1620
1621 /* Initialize access breaks. */
1622 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1623 ab_address[i] = 0x00000000;
1624
1625 add_target (&m32r_ops);
1626
1627 add_com ("sdireset", class_obscure, sdireset_command,
1bedd215 1628 _("Reset SDI connection."));
b4b4b794
KI
1629
1630 add_com ("sdistatus", class_obscure, sdistatus_command,
1bedd215 1631 _("Show status of SDI connection."));
b4b4b794
KI
1632
1633 add_com ("debug_chaos", class_obscure, debug_chaos_command,
1bedd215 1634 _("Debug M32R/Chaos."));
b4b4b794
KI
1635
1636 add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1bedd215 1637 _("Use debug DMA mem access."));
b4b4b794 1638 add_com ("use_mon_code", class_obscure, use_mon_code_command,
1bedd215 1639 _("Use mon code mem access."));
b4b4b794
KI
1640
1641 add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1bedd215 1642 _("Set breakpoints by IB break."));
b4b4b794 1643 add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1bedd215 1644 _("Set breakpoints by dbt."));
b4b4b794 1645}