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