1 /* Target dependent code for the Fujitsu SPARClite for GDB, the GNU debugger.
2 Copyright 1994, 1995, 1996, 1999 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "breakpoint.h"
26 #include <sys/types.h>
28 #if (!defined(__GO32__) && !defined(_WIN32)) || defined(__CYGWIN32__)
32 #include <sys/socket.h>
33 #include <netinet/in.h>
37 static struct target_ops sparclite_ops
;
39 static char *remote_target_name
= NULL
;
40 static serial_t remote_desc
= NULL
;
41 static int serial_flag
;
43 static int udp_fd
= -1;
46 static serial_t open_tty
PARAMS ((char *name
));
47 static int send_resp
PARAMS ((serial_t desc
, char c
));
48 static void close_tty
PARAMS ((int ignore
));
50 static int recv_udp_buf
PARAMS ((int fd
, unsigned char *buf
, int len
, int timeout
));
51 static int send_udp_buf
PARAMS ((int fd
, unsigned char *buf
, int len
));
53 static void sparclite_open
PARAMS ((char *name
, int from_tty
));
54 static void sparclite_close
PARAMS ((int quitting
));
55 static void download
PARAMS ((char *target_name
, char *args
, int from_tty
,
56 void (*write_routine
) (bfd
* from_bfd
,
59 bfd_vma to_addr
, int len
),
60 void (*start_routine
) (bfd_vma entry
)));
61 static void sparclite_serial_start
PARAMS ((bfd_vma entry
));
62 static void sparclite_serial_write
PARAMS ((bfd
* from_bfd
, asection
* from_sec
,
64 bfd_vma to_addr
, int len
));
66 static unsigned short calc_checksum
PARAMS ((unsigned char *buffer
,
68 static void sparclite_udp_start
PARAMS ((bfd_vma entry
));
69 static void sparclite_udp_write
PARAMS ((bfd
* from_bfd
, asection
* from_sec
,
70 file_ptr from_addr
, bfd_vma to_addr
,
73 static void sparclite_download
PARAMS ((char *filename
, int from_tty
));
75 #define DDA2_SUP_ASI 0xb000000
76 #define DDA1_SUP_ASI 0xb0000
78 #define DDA2_ASI_MASK 0xff000000
79 #define DDA1_ASI_MASK 0xff0000
80 #define DIA2_SUP_MODE 0x8000
81 #define DIA1_SUP_MODE 0x4000
82 #define DDA2_ENABLE 0x100
83 #define DDA1_ENABLE 0x80
84 #define DIA2_ENABLE 0x40
85 #define DIA1_ENABLE 0x20
86 #define DSINGLE_STEP 0x10 /* not used */
87 #define DDV_TYPE_MASK 0xc
88 #define DDV_TYPE_LOAD 0x0
89 #define DDV_TYPE_STORE 0x4
90 #define DDV_TYPE_ACCESS 0x8
91 #define DDV_TYPE_ALWAYS 0xc
96 sparclite_insert_watchpoint (addr
, len
, type
)
103 dcr
= read_register (DCR_REGNUM
);
105 if (!(dcr
& DDA1_ENABLE
))
107 write_register (DDA1_REGNUM
, addr
);
108 dcr
&= ~(DDA1_ASI_MASK
| DDV_TYPE_MASK
);
109 dcr
|= (DDA1_SUP_ASI
| DDA1_ENABLE
);
112 write_register (DDV1_REGNUM
, 0);
113 write_register (DDV2_REGNUM
, 0xffffffff);
114 dcr
|= (DDV_TYPE_LOAD
& (~DDV_COND
& ~DDV_MASK
));
118 write_register (DDV1_REGNUM
, 0);
119 write_register (DDV2_REGNUM
, 0xffffffff);
120 dcr
|= (DDV_TYPE_STORE
& (~DDV_COND
& ~DDV_MASK
));
124 write_register (DDV1_REGNUM
, 0);
125 write_register (DDV2_REGNUM
, 0xffffffff);
126 dcr
|= (DDV_TYPE_ACCESS
);
128 write_register (DCR_REGNUM
, dcr
);
130 else if (!(dcr
& DDA2_ENABLE
))
132 write_register (DDA2_REGNUM
, addr
);
133 dcr
&= ~(DDA2_ASI_MASK
& DDV_TYPE_MASK
);
134 dcr
|= (DDA2_SUP_ASI
| DDA2_ENABLE
);
137 write_register (DDV1_REGNUM
, 0);
138 write_register (DDV2_REGNUM
, 0xffffffff);
139 dcr
|= (DDV_TYPE_LOAD
& ~DDV_COND
& ~DDV_MASK
);
143 write_register (DDV1_REGNUM
, 0);
144 write_register (DDV2_REGNUM
, 0xffffffff);
145 dcr
|= (DDV_TYPE_STORE
& ~DDV_COND
& ~DDV_MASK
);
149 write_register (DDV1_REGNUM
, 0);
150 write_register (DDV2_REGNUM
, 0xffffffff);
151 dcr
|= (DDV_TYPE_ACCESS
);
153 write_register (DCR_REGNUM
, dcr
);
162 sparclite_remove_watchpoint (addr
, len
, type
)
167 CORE_ADDR dcr
, dda1
, dda2
;
169 dcr
= read_register (DCR_REGNUM
);
170 dda1
= read_register (DDA1_REGNUM
);
171 dda2
= read_register (DDA2_REGNUM
);
173 if ((dcr
& DDA1_ENABLE
) && addr
== dda1
)
174 write_register (DCR_REGNUM
, (dcr
& ~DDA1_ENABLE
));
175 else if ((dcr
& DDA2_ENABLE
) && addr
== dda2
)
176 write_register (DCR_REGNUM
, (dcr
& ~DDA2_ENABLE
));
184 sparclite_insert_hw_breakpoint (addr
, len
)
190 dcr
= read_register (DCR_REGNUM
);
192 if (!(dcr
& DIA1_ENABLE
))
194 write_register (DIA1_REGNUM
, addr
);
195 write_register (DCR_REGNUM
, (dcr
| DIA1_ENABLE
| DIA1_SUP_MODE
));
197 else if (!(dcr
& DIA2_ENABLE
))
199 write_register (DIA2_REGNUM
, addr
);
200 write_register (DCR_REGNUM
, (dcr
| DIA2_ENABLE
| DIA2_SUP_MODE
));
209 sparclite_remove_hw_breakpoint (addr
, shadow
)
213 CORE_ADDR dcr
, dia1
, dia2
;
215 dcr
= read_register (DCR_REGNUM
);
216 dia1
= read_register (DIA1_REGNUM
);
217 dia2
= read_register (DIA2_REGNUM
);
219 if ((dcr
& DIA1_ENABLE
) && addr
== dia1
)
220 write_register (DCR_REGNUM
, (dcr
& ~DIA1_ENABLE
));
221 else if ((dcr
& DIA2_ENABLE
) && addr
== dia2
)
222 write_register (DCR_REGNUM
, (dcr
& ~DIA2_ENABLE
));
230 sparclite_check_watch_resources (type
, cnt
, ot
)
235 /* Watchpoints not supported on simulator. */
236 if (strcmp (target_shortname
, "sim") == 0)
239 if (type
== bp_hardware_breakpoint
)
241 if (TARGET_HW_BREAK_LIMIT
== 0)
243 else if (cnt
<= TARGET_HW_BREAK_LIMIT
)
248 if (TARGET_HW_WATCH_LIMIT
== 0)
252 else if (cnt
<= TARGET_HW_WATCH_LIMIT
)
259 sparclite_stopped_data_address ()
261 CORE_ADDR dsr
, dda1
, dda2
;
263 dsr
= read_register (DSR_REGNUM
);
264 dda1
= read_register (DDA1_REGNUM
);
265 dda2
= read_register (DDA2_REGNUM
);
281 desc
= SERIAL_OPEN (name
);
283 perror_with_name (name
);
287 if (SERIAL_SETBAUDRATE (desc
, baud_rate
))
290 perror_with_name (name
);
296 SERIAL_FLUSH_INPUT (desc
);
301 /* Read a single character from the remote end, masking it down to 7 bits. */
304 readchar (desc
, timeout
)
311 ch
= SERIAL_READCHAR (desc
, timeout
);
316 error ("SPARClite remote connection closed");
318 perror_with_name ("SPARClite communication error");
320 error ("SPARClite remote timeout");
322 if (remote_debug
> 0)
324 sprintf (s
, "[%02x]", ch
& 0xff);
325 puts_debug ("read -->", s
, "<--");
332 debug_serial_write (desc
, buf
, len
)
339 SERIAL_WRITE (desc
, buf
, len
);
340 if (remote_debug
> 0)
344 sprintf (s
, "[%02x]", *buf
& 0xff);
345 puts_debug ("Sent -->", s
, "<--");
357 debug_serial_write (desc
, &c
, 1);
358 return readchar (desc
, remote_timeout
);
368 SERIAL_CLOSE (remote_desc
);
375 recv_udp_buf (fd
, buf
, len
, timeout
)
384 FD_SET (fd
, &readfds
);
388 struct timeval timebuf
;
390 timebuf
.tv_sec
= timeout
;
392 cc
= select (fd
+ 1, &readfds
, 0, 0, &timebuf
);
395 cc
= select (fd
+ 1, &readfds
, 0, 0, 0);
401 perror_with_name ("recv_udp_buf: Bad return value from select:");
403 cc
= recv (fd
, buf
, len
, 0);
406 perror_with_name ("Got an error from recv: ");
410 send_udp_buf (fd
, buf
, len
)
416 cc
= send (fd
, buf
, len
, 0);
422 perror_with_name ("Got an error from send: ");
424 error ("Short count in send: tried %d, sent %d\n", len
, cc
);
426 #endif /* HAVE_SOCKETS */
429 sparclite_open (name
, from_tty
)
433 struct cleanup
*old_chain
;
438 error ("You need to specify what device or hostname is associated with the SparcLite board.");
440 target_preopen (from_tty
);
442 unpush_target (&sparclite_ops
);
444 if (remote_target_name
)
445 free (remote_target_name
);
447 remote_target_name
= strsave (name
);
449 /* We need a 'serial' or 'udp' keyword to disambiguate host:port, which can
450 mean either a serial port on a terminal server, or the IP address of a
451 SPARClite demo board. If there's no colon, then it pretty much has to be
452 a local device (except for DOS... grrmble) */
454 p
= strchr (name
, ' ');
459 while ((*p
!= '\000') && isspace (*p
))
462 if (strncmp (name
, "serial", strlen (name
)) == 0)
464 else if (strncmp (name
, "udp", strlen (name
)) == 0)
467 error ("Must specify either `serial' or `udp'.");
473 if (!strchr (name
, ':'))
474 serial_flag
= 1; /* No colon is unambiguous (local device) */
476 error ("Usage: target sparclite serial /dev/ttyb\n\
477 or: target sparclite udp host");
482 remote_desc
= open_tty (p
);
484 old_chain
= make_cleanup ((make_cleanup_func
) close_tty
, 0);
486 c
= send_resp (remote_desc
, 0x00);
489 error ("Unknown response (0x%x) from SparcLite. Try resetting the board.",
492 c
= send_resp (remote_desc
, 0x55);
495 error ("Sparclite appears to be ill.");
501 struct sockaddr_in sockaddr
;
502 unsigned char buffer
[100];
505 /* Setup the socket. Must be raw UDP. */
507 he
= gethostbyname (p
);
510 error ("No such host %s.", p
);
512 udp_fd
= socket (PF_INET
, SOCK_DGRAM
, 0);
514 old_chain
= make_cleanup (close
, udp_fd
);
516 sockaddr
.sin_family
= PF_INET
;
517 sockaddr
.sin_port
= htons (7000);
518 memcpy (&sockaddr
.sin_addr
.s_addr
, he
->h_addr
, sizeof (struct in_addr
));
520 if (connect (udp_fd
, &sockaddr
, sizeof (sockaddr
)))
521 perror_with_name ("Connect failed");
526 send_udp_buf (udp_fd
, buffer
, 2); /* Request version */
527 cc
= recv_udp_buf (udp_fd
, buffer
, sizeof (buffer
), 5); /* Get response */
529 error ("SPARClite isn't responding.");
532 error ("SPARClite appears to be ill.");
534 error ("UDP downloading is not supported for DOS hosts.");
535 #endif /* HAVE_SOCKETS */
538 printf_unfiltered ("[SPARClite appears to be alive]\n");
540 push_target (&sparclite_ops
);
542 discard_cleanups (old_chain
);
548 sparclite_close (quitting
)
554 else if (udp_fd
!= -1)
559 #define LOAD_ADDRESS 0x40000000
562 download (target_name
, args
, from_tty
, write_routine
, start_routine
)
566 void (*write_routine
) PARAMS ((bfd
* from_bfd
, asection
* from_sec
,
567 file_ptr from_addr
, bfd_vma to_addr
, int len
));
568 void (*start_routine
) PARAMS ((bfd_vma entry
));
570 struct cleanup
*old_chain
;
575 #define WRITESIZE 1024
584 while (*args
!= '\000')
588 while (isspace (*args
))
593 while ((*args
!= '\000') && !isspace (*args
))
601 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
603 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
606 error ("unknown option `%s'", arg
);
610 filename
= get_exec_file (1);
612 pbfd
= bfd_openr (filename
, gnutarget
);
615 perror_with_name (filename
);
618 old_chain
= make_cleanup ((make_cleanup_func
) bfd_close
, pbfd
);
620 if (!bfd_check_format (pbfd
, bfd_object
))
621 error ("\"%s\" is not an object file: %s", filename
,
622 bfd_errmsg (bfd_get_error ()));
624 for (section
= pbfd
->sections
; section
; section
= section
->next
)
626 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
628 bfd_vma section_address
;
629 bfd_size_type section_size
;
631 const char *section_name
;
633 section_name
= bfd_get_section_name (pbfd
, section
);
635 section_address
= bfd_get_section_vma (pbfd
, section
);
637 /* Adjust sections from a.out files, since they don't
638 carry their addresses with. */
639 if (bfd_get_flavour (pbfd
) == bfd_target_aout_flavour
)
641 if (strcmp (section_name
, ".text") == 0)
642 section_address
= bfd_get_start_address (pbfd
);
643 else if (strcmp (section_name
, ".data") == 0)
645 /* Read the first 8 bytes of the data section.
646 There should be the string 'DaTa' followed by
647 a word containing the actual section address. */
650 char signature
[4]; /* 'DaTa' */
651 unsigned char sdata
[4]; /* &sdata */
654 bfd_get_section_contents (pbfd
, section
, &marker
, 0,
656 if (strncmp (marker
.signature
, "DaTa", 4) == 0)
658 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
659 section_address
= bfd_getb32 (marker
.sdata
);
661 section_address
= bfd_getl32 (marker
.sdata
);
666 section_size
= bfd_get_section_size_before_reloc (section
);
669 printf_filtered ("[Loading section %s at 0x%x (%d bytes)]\n",
670 bfd_get_section_name (pbfd
, section
),
675 while (section_size
> 0)
678 static char inds
[] = "|/-\\";
683 count
= min (section_size
, WRITESIZE
);
685 write_routine (pbfd
, section
, fptr
, section_address
, count
);
689 printf_unfiltered ("\r%c", inds
[k
++ % 4]);
690 gdb_flush (gdb_stdout
);
693 section_address
+= count
;
695 section_size
-= count
;
702 entry
= bfd_get_start_address (pbfd
);
705 printf_unfiltered ("[Starting %s at 0x%x]\n", filename
, entry
);
707 start_routine (entry
);
710 do_cleanups (old_chain
);
714 sparclite_serial_start (entry
)
721 store_unsigned_integer (buffer
+ 1, 4, entry
);
723 debug_serial_write (remote_desc
, buffer
, 1 + 4);
724 i
= readchar (remote_desc
, remote_timeout
);
726 error ("Can't start SparcLite. Error code %d\n", i
);
730 sparclite_serial_write (from_bfd
, from_sec
, from_addr
, to_addr
, len
)
737 char buffer
[4 + 4 + WRITESIZE
]; /* addr + len + data */
738 unsigned char checksum
;
741 store_unsigned_integer (buffer
, 4, to_addr
); /* Address */
742 store_unsigned_integer (buffer
+ 4, 4, len
); /* Length */
744 bfd_get_section_contents (from_bfd
, from_sec
, buffer
+ 8, from_addr
, len
);
747 for (i
= 0; i
< len
; i
++)
748 checksum
+= buffer
[8 + i
];
750 i
= send_resp (remote_desc
, 0x01);
753 error ("Bad response from load command (0x%x)", i
);
755 debug_serial_write (remote_desc
, buffer
, 4 + 4 + len
);
756 i
= readchar (remote_desc
, remote_timeout
);
759 error ("Bad checksum from load command (0x%x)", i
);
764 static unsigned short
765 calc_checksum (buffer
, count
)
766 unsigned char *buffer
;
769 unsigned short checksum
;
772 for (; count
> 0; count
-= 2, buffer
+= 2)
773 checksum
+= (*buffer
<< 8) | *(buffer
+ 1);
776 checksum
+= *buffer
<< 8;
782 sparclite_udp_start (entry
)
785 unsigned char buffer
[6];
790 buffer
[2] = entry
>> 24;
791 buffer
[3] = entry
>> 16;
792 buffer
[4] = entry
>> 8;
795 send_udp_buf (udp_fd
, buffer
, 6); /* Send start addr */
796 i
= recv_udp_buf (udp_fd
, buffer
, sizeof (buffer
), -1); /* Get response */
798 if (i
< 1 || buffer
[0] != 0x55)
799 error ("Failed to take start address.");
803 sparclite_udp_write (from_bfd
, from_sec
, from_addr
, to_addr
, len
)
810 unsigned char buffer
[2000];
811 unsigned short checksum
;
812 static int pkt_num
= 0;
813 static unsigned long old_addr
= -1;
818 if (to_addr
!= old_addr
)
820 buffer
[0] = 0x1; /* Load command */
821 buffer
[1] = 0x1; /* Loading address */
822 buffer
[2] = to_addr
>> 24;
823 buffer
[3] = to_addr
>> 16;
824 buffer
[4] = to_addr
>> 8;
828 for (i
= 0; i
< 6; i
++)
829 checksum
+= buffer
[i
];
832 send_udp_buf (udp_fd
, buffer
, 6);
833 i
= recv_udp_buf (udp_fd
, buffer
, sizeof buffer
, -1);
836 error ("Got back short checksum for load addr.");
838 if (checksum
!= buffer
[0])
839 error ("Got back bad checksum for load addr.");
841 pkt_num
= 0; /* Load addr resets packet seq # */
845 bfd_get_section_contents (from_bfd
, from_sec
, buffer
+ 6, from_addr
,
848 checksum
= calc_checksum (buffer
+ 6, len
);
850 buffer
[0] = 0x1; /* Load command */
851 buffer
[1] = 0x2; /* Loading data */
852 buffer
[2] = pkt_num
>> 8;
854 buffer
[4] = checksum
>> 8;
855 buffer
[5] = checksum
;
857 send_udp_buf (udp_fd
, buffer
, len
+ 6);
858 i
= recv_udp_buf (udp_fd
, buffer
, sizeof buffer
, 3);
862 fprintf_unfiltered (gdb_stderr
, "send_data: timeout sending %d bytes to address 0x%x retrying\n", len
, to_addr
);
866 if (buffer
[0] != 0xff)
867 error ("Got back bad response for load data.");
876 #endif /* HAVE_SOCKETS */
879 sparclite_download (filename
, from_tty
)
885 download (remote_target_name
, filename
, from_tty
, sparclite_udp_write
,
886 sparclite_udp_start
);
888 abort (); /* sparclite_open should prevent this! */
891 download (remote_target_name
, filename
, from_tty
, sparclite_serial_write
,
892 sparclite_serial_start
);
895 /* Set up the sparclite target vector. */
898 init_sparclite_ops (void)
900 sparclite_ops
.to_shortname
= "sparclite";
901 sparclite_ops
.to_longname
= "SPARClite download target";
902 sparclite_ops
.to_doc
= "Download to a remote SPARClite target board via serial of UDP.\n\
903 Specify the device it is connected to (e.g. /dev/ttya).";
904 sparclite_ops
.to_open
= sparclite_open
;
905 sparclite_ops
.to_close
= sparclite_close
;
906 sparclite_ops
.to_load
= sparclite_download
;
907 sparclite_ops
.to_stratum
= download_stratum
;
908 sparclite_ops
.to_magic
= OPS_MAGIC
;
912 _initialize_sparcl_tdep ()
914 init_sparclite_ops ();
915 add_target (&sparclite_ops
);