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__)
31 #include <sys/socket.h>
32 #include <netinet/in.h>
36 static struct target_ops sparclite_ops;
38 static char *remote_target_name = NULL;
39 static serial_t remote_desc = NULL;
40 static int serial_flag;
42 static int udp_fd = -1;
45 static serial_t open_tty (char *name);
46 static int send_resp (serial_t desc, char c);
47 static void close_tty (void * ignore);
49 static int recv_udp_buf (int fd, unsigned char *buf, int len, int timeout);
50 static int send_udp_buf (int fd, unsigned char *buf, int len);
52 static void sparclite_open (char *name, int from_tty);
53 static void sparclite_close (int quitting);
54 static void download (char *target_name, char *args, int from_tty,
55 void (*write_routine) (bfd * from_bfd,
58 bfd_vma to_addr, int len),
59 void (*start_routine) (bfd_vma entry));
60 static void sparclite_serial_start (bfd_vma entry);
61 static void sparclite_serial_write (bfd * from_bfd, asection * from_sec,
63 bfd_vma to_addr, int len);
65 static unsigned short calc_checksum (unsigned char *buffer, int count);
66 static void sparclite_udp_start (bfd_vma entry);
67 static void sparclite_udp_write (bfd * from_bfd, asection * from_sec,
68 file_ptr from_addr, bfd_vma to_addr,
71 static void sparclite_download (char *filename, int from_tty);
73 #define DDA2_SUP_ASI 0xb000000
74 #define DDA1_SUP_ASI 0xb0000
76 #define DDA2_ASI_MASK 0xff000000
77 #define DDA1_ASI_MASK 0xff0000
78 #define DIA2_SUP_MODE 0x8000
79 #define DIA1_SUP_MODE 0x4000
80 #define DDA2_ENABLE 0x100
81 #define DDA1_ENABLE 0x80
82 #define DIA2_ENABLE 0x40
83 #define DIA1_ENABLE 0x20
84 #define DSINGLE_STEP 0x10 /* not used */
85 #define DDV_TYPE_MASK 0xc
86 #define DDV_TYPE_LOAD 0x0
87 #define DDV_TYPE_STORE 0x4
88 #define DDV_TYPE_ACCESS 0x8
89 #define DDV_TYPE_ALWAYS 0xc
94 sparclite_insert_watchpoint (addr, len, type)
101 dcr = read_register (DCR_REGNUM);
103 if (!(dcr & DDA1_ENABLE))
105 write_register (DDA1_REGNUM, addr);
106 dcr &= ~(DDA1_ASI_MASK | DDV_TYPE_MASK);
107 dcr |= (DDA1_SUP_ASI | DDA1_ENABLE);
110 write_register (DDV1_REGNUM, 0);
111 write_register (DDV2_REGNUM, 0xffffffff);
112 dcr |= (DDV_TYPE_LOAD & (~DDV_COND & ~DDV_MASK));
116 write_register (DDV1_REGNUM, 0);
117 write_register (DDV2_REGNUM, 0xffffffff);
118 dcr |= (DDV_TYPE_STORE & (~DDV_COND & ~DDV_MASK));
122 write_register (DDV1_REGNUM, 0);
123 write_register (DDV2_REGNUM, 0xffffffff);
124 dcr |= (DDV_TYPE_ACCESS);
126 write_register (DCR_REGNUM, dcr);
128 else if (!(dcr & DDA2_ENABLE))
130 write_register (DDA2_REGNUM, addr);
131 dcr &= ~(DDA2_ASI_MASK & DDV_TYPE_MASK);
132 dcr |= (DDA2_SUP_ASI | DDA2_ENABLE);
135 write_register (DDV1_REGNUM, 0);
136 write_register (DDV2_REGNUM, 0xffffffff);
137 dcr |= (DDV_TYPE_LOAD & ~DDV_COND & ~DDV_MASK);
141 write_register (DDV1_REGNUM, 0);
142 write_register (DDV2_REGNUM, 0xffffffff);
143 dcr |= (DDV_TYPE_STORE & ~DDV_COND & ~DDV_MASK);
147 write_register (DDV1_REGNUM, 0);
148 write_register (DDV2_REGNUM, 0xffffffff);
149 dcr |= (DDV_TYPE_ACCESS);
151 write_register (DCR_REGNUM, dcr);
160 sparclite_remove_watchpoint (addr, len, type)
165 CORE_ADDR dcr, dda1, dda2;
167 dcr = read_register (DCR_REGNUM);
168 dda1 = read_register (DDA1_REGNUM);
169 dda2 = read_register (DDA2_REGNUM);
171 if ((dcr & DDA1_ENABLE) && addr == dda1)
172 write_register (DCR_REGNUM, (dcr & ~DDA1_ENABLE));
173 else if ((dcr & DDA2_ENABLE) && addr == dda2)
174 write_register (DCR_REGNUM, (dcr & ~DDA2_ENABLE));
182 sparclite_insert_hw_breakpoint (addr, len)
188 dcr = read_register (DCR_REGNUM);
190 if (!(dcr & DIA1_ENABLE))
192 write_register (DIA1_REGNUM, addr);
193 write_register (DCR_REGNUM, (dcr | DIA1_ENABLE | DIA1_SUP_MODE));
195 else if (!(dcr & DIA2_ENABLE))
197 write_register (DIA2_REGNUM, addr);
198 write_register (DCR_REGNUM, (dcr | DIA2_ENABLE | DIA2_SUP_MODE));
207 sparclite_remove_hw_breakpoint (addr, shadow)
211 CORE_ADDR dcr, dia1, dia2;
213 dcr = read_register (DCR_REGNUM);
214 dia1 = read_register (DIA1_REGNUM);
215 dia2 = read_register (DIA2_REGNUM);
217 if ((dcr & DIA1_ENABLE) && addr == dia1)
218 write_register (DCR_REGNUM, (dcr & ~DIA1_ENABLE));
219 else if ((dcr & DIA2_ENABLE) && addr == dia2)
220 write_register (DCR_REGNUM, (dcr & ~DIA2_ENABLE));
228 sparclite_check_watch_resources (type, cnt, ot)
233 /* Watchpoints not supported on simulator. */
234 if (strcmp (target_shortname, "sim") == 0)
237 if (type == bp_hardware_breakpoint)
239 if (TARGET_HW_BREAK_LIMIT == 0)
241 else if (cnt <= TARGET_HW_BREAK_LIMIT)
246 if (TARGET_HW_WATCH_LIMIT == 0)
250 else if (cnt <= TARGET_HW_WATCH_LIMIT)
257 sparclite_stopped_data_address ()
259 CORE_ADDR dsr, dda1, dda2;
261 dsr = read_register (DSR_REGNUM);
262 dda1 = read_register (DDA1_REGNUM);
263 dda2 = read_register (DDA2_REGNUM);
279 desc = SERIAL_OPEN (name);
281 perror_with_name (name);
285 if (SERIAL_SETBAUDRATE (desc, baud_rate))
288 perror_with_name (name);
294 SERIAL_FLUSH_INPUT (desc);
299 /* Read a single character from the remote end, masking it down to 7 bits. */
302 readchar (desc, timeout)
309 ch = SERIAL_READCHAR (desc, timeout);
314 error ("SPARClite remote connection closed");
316 perror_with_name ("SPARClite communication error");
318 error ("SPARClite remote timeout");
320 if (remote_debug > 0)
322 sprintf (s, "[%02x]", ch & 0xff);
323 puts_debug ("read -->", s, "<--");
330 debug_serial_write (desc, buf, len)
337 SERIAL_WRITE (desc, buf, len);
338 if (remote_debug > 0)
342 sprintf (s, "[%02x]", *buf & 0xff);
343 puts_debug ("Sent -->", s, "<--");
355 debug_serial_write (desc, &c, 1);
356 return readchar (desc, remote_timeout);
360 close_tty (void *ignore)
365 SERIAL_CLOSE (remote_desc);
372 recv_udp_buf (fd, buf, len, timeout)
381 FD_SET (fd, &readfds);
385 struct timeval timebuf;
387 timebuf.tv_sec = timeout;
389 cc = select (fd + 1, &readfds, 0, 0, &timebuf);
392 cc = select (fd + 1, &readfds, 0, 0, 0);
398 perror_with_name ("recv_udp_buf: Bad return value from select:");
400 cc = recv (fd, buf, len, 0);
403 perror_with_name ("Got an error from recv: ");
407 send_udp_buf (fd, buf, len)
413 cc = send (fd, buf, len, 0);
419 perror_with_name ("Got an error from send: ");
421 error ("Short count in send: tried %d, sent %d\n", len, cc);
423 #endif /* HAVE_SOCKETS */
426 sparclite_open (name, from_tty)
430 struct cleanup *old_chain;
435 error ("You need to specify what device or hostname is associated with the SparcLite board.");
437 target_preopen (from_tty);
439 unpush_target (&sparclite_ops);
441 if (remote_target_name)
442 free (remote_target_name);
444 remote_target_name = strsave (name);
446 /* We need a 'serial' or 'udp' keyword to disambiguate host:port, which can
447 mean either a serial port on a terminal server, or the IP address of a
448 SPARClite demo board. If there's no colon, then it pretty much has to be
449 a local device (except for DOS... grrmble) */
451 p = strchr (name, ' ');
456 while ((*p != '\000') && isspace (*p))
459 if (strncmp (name, "serial", strlen (name)) == 0)
461 else if (strncmp (name, "udp", strlen (name)) == 0)
464 error ("Must specify either `serial' or `udp'.");
470 if (!strchr (name, ':'))
471 serial_flag = 1; /* No colon is unambiguous (local device) */
473 error ("Usage: target sparclite serial /dev/ttyb\n\
474 or: target sparclite udp host");
479 remote_desc = open_tty (p);
481 old_chain = make_cleanup (close_tty, 0 /*ignore*/);
483 c = send_resp (remote_desc, 0x00);
486 error ("Unknown response (0x%x) from SparcLite. Try resetting the board.",
489 c = send_resp (remote_desc, 0x55);
492 error ("Sparclite appears to be ill.");
498 struct sockaddr_in sockaddr;
499 unsigned char buffer[100];
502 /* Setup the socket. Must be raw UDP. */
504 he = gethostbyname (p);
507 error ("No such host %s.", p);
509 udp_fd = socket (PF_INET, SOCK_DGRAM, 0);
511 old_chain = make_cleanup (close, udp_fd);
513 sockaddr.sin_family = PF_INET;
514 sockaddr.sin_port = htons (7000);
515 memcpy (&sockaddr.sin_addr.s_addr, he->h_addr, sizeof (struct in_addr));
517 if (connect (udp_fd, &sockaddr, sizeof (sockaddr)))
518 perror_with_name ("Connect failed");
523 send_udp_buf (udp_fd, buffer, 2); /* Request version */
524 cc = recv_udp_buf (udp_fd, buffer, sizeof (buffer), 5); /* Get response */
526 error ("SPARClite isn't responding.");
529 error ("SPARClite appears to be ill.");
531 error ("UDP downloading is not supported for DOS hosts.");
532 #endif /* HAVE_SOCKETS */
535 printf_unfiltered ("[SPARClite appears to be alive]\n");
537 push_target (&sparclite_ops);
539 discard_cleanups (old_chain);
545 sparclite_close (quitting)
551 else if (udp_fd != -1)
556 #define LOAD_ADDRESS 0x40000000
559 download (target_name, args, from_tty, write_routine, start_routine)
563 void (*write_routine) (bfd * from_bfd, asection * from_sec,
564 file_ptr from_addr, bfd_vma to_addr, int len);
565 void (*start_routine) (bfd_vma entry);
567 struct cleanup *old_chain;
572 #define WRITESIZE 1024
581 while (*args != '\000')
585 while (isspace (*args))
590 while ((*args != '\000') && !isspace (*args))
598 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
600 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
603 error ("unknown option `%s'", arg);
607 filename = get_exec_file (1);
609 pbfd = bfd_openr (filename, gnutarget);
612 perror_with_name (filename);
615 old_chain = make_cleanup_bfd_close (pbfd);
617 if (!bfd_check_format (pbfd, bfd_object))
618 error ("\"%s\" is not an object file: %s", filename,
619 bfd_errmsg (bfd_get_error ()));
621 for (section = pbfd->sections; section; section = section->next)
623 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
625 bfd_vma section_address;
626 bfd_size_type section_size;
628 const char *section_name;
630 section_name = bfd_get_section_name (pbfd, section);
632 section_address = bfd_get_section_vma (pbfd, section);
634 /* Adjust sections from a.out files, since they don't
635 carry their addresses with. */
636 if (bfd_get_flavour (pbfd) == bfd_target_aout_flavour)
638 if (strcmp (section_name, ".text") == 0)
639 section_address = bfd_get_start_address (pbfd);
640 else if (strcmp (section_name, ".data") == 0)
642 /* Read the first 8 bytes of the data section.
643 There should be the string 'DaTa' followed by
644 a word containing the actual section address. */
647 char signature[4]; /* 'DaTa' */
648 unsigned char sdata[4]; /* &sdata */
651 bfd_get_section_contents (pbfd, section, &marker, 0,
653 if (strncmp (marker.signature, "DaTa", 4) == 0)
655 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
656 section_address = bfd_getb32 (marker.sdata);
658 section_address = bfd_getl32 (marker.sdata);
663 section_size = bfd_get_section_size_before_reloc (section);
666 printf_filtered ("[Loading section %s at 0x%x (%d bytes)]\n",
667 bfd_get_section_name (pbfd, section),
672 while (section_size > 0)
675 static char inds[] = "|/-\\";
680 count = min (section_size, WRITESIZE);
682 write_routine (pbfd, section, fptr, section_address, count);
686 printf_unfiltered ("\r%c", inds[k++ % 4]);
687 gdb_flush (gdb_stdout);
690 section_address += count;
692 section_size -= count;
699 entry = bfd_get_start_address (pbfd);
702 printf_unfiltered ("[Starting %s at 0x%x]\n", filename, entry);
704 start_routine (entry);
707 do_cleanups (old_chain);
711 sparclite_serial_start (entry)
718 store_unsigned_integer (buffer + 1, 4, entry);
720 debug_serial_write (remote_desc, buffer, 1 + 4);
721 i = readchar (remote_desc, remote_timeout);
723 error ("Can't start SparcLite. Error code %d\n", i);
727 sparclite_serial_write (from_bfd, from_sec, from_addr, to_addr, len)
734 char buffer[4 + 4 + WRITESIZE]; /* addr + len + data */
735 unsigned char checksum;
738 store_unsigned_integer (buffer, 4, to_addr); /* Address */
739 store_unsigned_integer (buffer + 4, 4, len); /* Length */
741 bfd_get_section_contents (from_bfd, from_sec, buffer + 8, from_addr, len);
744 for (i = 0; i < len; i++)
745 checksum += buffer[8 + i];
747 i = send_resp (remote_desc, 0x01);
750 error ("Bad response from load command (0x%x)", i);
752 debug_serial_write (remote_desc, buffer, 4 + 4 + len);
753 i = readchar (remote_desc, remote_timeout);
756 error ("Bad checksum from load command (0x%x)", i);
761 static unsigned short
762 calc_checksum (buffer, count)
763 unsigned char *buffer;
766 unsigned short checksum;
769 for (; count > 0; count -= 2, buffer += 2)
770 checksum += (*buffer << 8) | *(buffer + 1);
773 checksum += *buffer << 8;
779 sparclite_udp_start (entry)
782 unsigned char buffer[6];
787 buffer[2] = entry >> 24;
788 buffer[3] = entry >> 16;
789 buffer[4] = entry >> 8;
792 send_udp_buf (udp_fd, buffer, 6); /* Send start addr */
793 i = recv_udp_buf (udp_fd, buffer, sizeof (buffer), -1); /* Get response */
795 if (i < 1 || buffer[0] != 0x55)
796 error ("Failed to take start address.");
800 sparclite_udp_write (from_bfd, from_sec, from_addr, to_addr, len)
807 unsigned char buffer[2000];
808 unsigned short checksum;
809 static int pkt_num = 0;
810 static unsigned long old_addr = -1;
815 if (to_addr != old_addr)
817 buffer[0] = 0x1; /* Load command */
818 buffer[1] = 0x1; /* Loading address */
819 buffer[2] = to_addr >> 24;
820 buffer[3] = to_addr >> 16;
821 buffer[4] = to_addr >> 8;
825 for (i = 0; i < 6; i++)
826 checksum += buffer[i];
829 send_udp_buf (udp_fd, buffer, 6);
830 i = recv_udp_buf (udp_fd, buffer, sizeof buffer, -1);
833 error ("Got back short checksum for load addr.");
835 if (checksum != buffer[0])
836 error ("Got back bad checksum for load addr.");
838 pkt_num = 0; /* Load addr resets packet seq # */
842 bfd_get_section_contents (from_bfd, from_sec, buffer + 6, from_addr,
845 checksum = calc_checksum (buffer + 6, len);
847 buffer[0] = 0x1; /* Load command */
848 buffer[1] = 0x2; /* Loading data */
849 buffer[2] = pkt_num >> 8;
851 buffer[4] = checksum >> 8;
852 buffer[5] = checksum;
854 send_udp_buf (udp_fd, buffer, len + 6);
855 i = recv_udp_buf (udp_fd, buffer, sizeof buffer, 3);
859 fprintf_unfiltered (gdb_stderr, "send_data: timeout sending %d bytes to address 0x%x retrying\n", len, to_addr);
863 if (buffer[0] != 0xff)
864 error ("Got back bad response for load data.");
873 #endif /* HAVE_SOCKETS */
876 sparclite_download (filename, from_tty)
882 download (remote_target_name, filename, from_tty, sparclite_udp_write,
883 sparclite_udp_start);
885 abort (); /* sparclite_open should prevent this! */
888 download (remote_target_name, filename, from_tty, sparclite_serial_write,
889 sparclite_serial_start);
892 /* Set up the sparclite target vector. */
895 init_sparclite_ops (void)
897 sparclite_ops.to_shortname = "sparclite";
898 sparclite_ops.to_longname = "SPARClite download target";
899 sparclite_ops.to_doc = "Download to a remote SPARClite target board via serial of UDP.\n\
900 Specify the device it is connected to (e.g. /dev/ttya).";
901 sparclite_ops.to_open = sparclite_open;
902 sparclite_ops.to_close = sparclite_close;
903 sparclite_ops.to_load = sparclite_download;
904 sparclite_ops.to_stratum = download_stratum;
905 sparclite_ops.to_magic = OPS_MAGIC;
909 _initialize_sparcl_tdep ()
911 init_sparclite_ops ();
912 add_target (&sparclite_ops);