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