1 /* Target communications support for Macraigor Systems' On-Chip Debugging
2 Copyright 1996, 1997 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, Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
32 #include "gdb-stabs.h"
34 #include <sys/types.h>
39 /* Prototypes for local functions */
41 static int ocd_read_bytes PARAMS ((CORE_ADDR memaddr,
42 char *myaddr, int len));
44 static int ocd_start_remote PARAMS ((char *dummy));
46 static int readchar PARAMS ((int timeout));
48 static void reset_packet PARAMS ((void));
50 static void output_packet PARAMS ((void));
52 static int get_quoted_char PARAMS ((int timeout));
54 static void put_quoted_char PARAMS ((int c));
56 static void ocd_interrupt PARAMS ((int signo));
58 static void ocd_interrupt_twice PARAMS ((int signo));
60 static void interrupt_query PARAMS ((void));
62 static unsigned char * ocd_do_command PARAMS ((int cmd, int *statusp, int *lenp));
64 static void ocd_put_packet PARAMS ((unsigned char *packet, int pktlen));
66 static unsigned char * ocd_get_packet PARAMS ((int cmd, int *pktlen, int timeout));
68 static struct target_ops *current_ops = NULL;
70 static int last_run_status;
72 /* This was 5 seconds, which is a long time to sit and wait.
73 Unless this is going though some terminal server or multiplexer or
74 other form of hairy serial connection, I would think 2 seconds would
78 /* FIXME: Change to allow option to set timeout value on a per target
80 static int remote_timeout = 2;
83 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
84 ocd_open knows that we don't have a file open when the program
86 static serial_t ocd_desc = NULL;
89 ocd_error (s, error_code)
95 fputs_filtered (s, gdb_stderr);
96 fputs_filtered (" ", gdb_stderr);
100 case 0x1: s = "Unknown fault"; break;
101 case 0x2: s = "Power failed"; break;
102 case 0x3: s = "Cable disconnected"; break;
103 case 0x4: s = "Couldn't enter OCD mode"; break;
104 case 0x5: s = "Target stuck in reset"; break;
105 case 0x6: s = "OCD hasn't been initialized"; break;
106 case 0x7: s = "Write verify failed"; break;
107 case 0x8: s = "Reg buff error (during MPC5xx fp reg read/write)"; break;
108 case 0x9: s = "Invalid CPU register access attempt failed"; break;
109 case 0x11: s = "Bus error"; break;
110 case 0x12: s = "Checksum error"; break;
111 case 0x13: s = "Illegal command"; break;
112 case 0x14: s = "Parameter error"; break;
113 case 0x15: s = "Internal error"; break;
114 case 0x80: s = "Flash erase error"; break;
116 sprintf (buf, "Unknown error code %d", error_code);
123 /* Return nonzero if the thread TH is still alive on the remote system. */
126 ocd_thread_alive (th)
132 /* Clean up connection to a remote debugger. */
140 SERIAL_CLOSE (ocd_desc);
144 /* Stub for catch_errors. */
147 ocd_start_remote (dummy)
150 unsigned char buf[10], *p;
155 enum ocd_target_type target_type;
157 target_type = (enum ocd_target_type)dummy;
159 immediate_quit = 1; /* Allow user to interrupt it */
161 SERIAL_SEND_BREAK (ocd_desc); /* Wake up the wiggler */
163 speed = 80; /* Divide clock by 4000 */
167 buf[2] = speed & 0xff;
168 buf[3] = target_type;
169 ocd_put_packet (buf, 4); /* Init OCD params */
170 p = ocd_get_packet (buf[0], &pktlen, remote_timeout);
173 error ("Truncated response packet from OCD device");
179 ocd_error ("OCD_INIT:", error_code);
181 ocd_do_command (OCD_AYT, &status, &pktlen);
183 p = ocd_do_command (OCD_GET_VERSION, &status, &pktlen);
185 printf_unfiltered ("[Wiggler version %x.%x, capability 0x%x]\n",
186 p[0], p[1], (p[2] << 16) | p[3]);
189 /* Reset the target */
191 ocd_do_command (OCD_RESET_RUN, &status, &pktlen);
192 /* ocd_do_command (OCD_RESET, &status, &pktlen);*/
195 /* If processor is still running, stop it. */
197 if (!(status & OCD_FLAG_BDM))
201 /* When using a target box, we want to asynchronously return status when
202 target stops. The OCD_SET_CTL_FLAGS command is ignored by Wigglers.dll
203 when using a parallel Wiggler */
204 buf[0] = OCD_SET_CTL_FLAGS;
207 ocd_put_packet (buf, 3);
209 p = ocd_get_packet (buf[0], &pktlen, remote_timeout);
212 error ("Truncated response packet from OCD device");
218 ocd_error ("OCD_SET_CTL_FLAGS:", error_code);
223 /* This is really the job of start_remote however, that makes an assumption
224 that the target is about to print out a status message of some sort. That
225 doesn't happen here (in fact, it may not be possible to get the monitor to
226 send the appropriate packet). */
228 flush_cached_frames ();
229 registers_changed ();
230 stop_pc = read_pc ();
231 set_current_frame (create_new_frame (read_fp (), stop_pc));
232 select_frame (get_current_frame (), 0);
233 print_stack_frame (selected_frame, -1, 1);
235 buf[0] = OCD_LOG_FILE;
236 buf[1] = 3; /* close existing WIGGLERS.LOG */
237 ocd_put_packet (buf, 2);
238 p = ocd_get_packet (buf[0], &pktlen, remote_timeout);
240 buf[0] = OCD_LOG_FILE;
241 buf[1] = 2; /* append to existing WIGGLERS.LOG */
242 ocd_put_packet (buf, 2);
243 p = ocd_get_packet (buf[0], &pktlen, remote_timeout);
248 /* Open a connection to a remote debugger.
249 NAME is the filename used for communication. */
251 static DCACHE *ocd_dcache;
254 ocd_open (name, from_tty, target_type, ops)
257 enum ocd_target_type target_type;
258 struct target_ops *ops;
260 unsigned char buf[10], *p;
265 error ("To open an OCD connection, you need to specify the\n\
266 device the OCD device is attached to (e.g. /dev/ttya).");
268 target_preopen (from_tty);
272 unpush_target (current_ops);
274 ocd_dcache = dcache_init (ocd_read_bytes, ocd_write_bytes);
276 if (strncmp(name,"wiggler",7) == 0)
278 ocd_desc = SERIAL_OPEN ("ocd");
280 perror_with_name (name);
282 buf[0] = OCD_LOG_FILE;
283 buf[1] = 1; /* open new or overwrite existing WIGGLERS.LOG */
284 ocd_put_packet (buf, 2);
285 p = ocd_get_packet (buf[0], &pktlen, remote_timeout);
287 buf[0] = OCD_SET_CONNECTION;
288 buf[1] = 0x01; /* atoi (name[11]); */
289 ocd_put_packet (buf, 2);
290 p = ocd_get_packet (buf[0], &pktlen, remote_timeout);
292 else /* not using Wigglers.dll */
294 ocd_desc = SERIAL_OPEN (name);
296 perror_with_name (name);
301 if (SERIAL_SETBAUDRATE (ocd_desc, baud_rate))
303 SERIAL_CLOSE (ocd_desc);
304 perror_with_name (name);
308 SERIAL_RAW (ocd_desc);
310 /* If there is something sitting in the buffer we might take it as a
311 response to a command, which would be bad. */
312 SERIAL_FLUSH_INPUT (ocd_desc);
316 puts_filtered ("Remote target wiggler connected to ");
317 puts_filtered (name);
318 puts_filtered ("\n");
320 push_target (current_ops); /* Switch to using remote target now */
322 /* Without this, some commands which require an active target (such as kill)
323 won't work. This variable serves (at least) double duty as both the pid
324 of the target process (if it has such), and as a flag indicating that a
325 target is active. These functions should be split out into seperate
326 variables, especially since GDB will someday have a notion of debugging
327 several processes. */
329 inferior_pid = 42000;
330 /* Start the remote connection; if error (0), discard this target.
331 In particular, if the user quits, be sure to discard it
332 (we'd be in an inconsistent state otherwise). */
333 if (!catch_errors (ocd_start_remote, (char *)target_type,
334 "Couldn't establish connection to remote target\n",
338 error ("Failed to connect to OCD.");
342 /* This takes a program previously attached to and detaches it. After
343 this is done, GDB can be used to debug some other program. We
344 better not have left any breakpoints in the target program or it'll
345 die when it hits one. */
348 ocd_detach (args, from_tty)
353 error ("Argument given to \"detach\" when remotely debugging.");
357 puts_filtered ("Ending remote debugging.\n");
360 /* Tell the remote machine to resume. */
363 ocd_resume (pid, step, siggnal)
365 enum target_signal siggnal;
369 dcache_flush (ocd_dcache);
372 ocd_do_command (OCD_STEP, &last_run_status, &pktlen);
374 ocd_do_command (OCD_RUN, &last_run_status, &pktlen);
383 ocd_do_command (OCD_STOP, &status, &pktlen);
385 if (!(status & OCD_FLAG_BDM))
386 error ("Can't stop target via BDM");
389 static volatile int ocd_interrupt_flag;
391 /* Send ^C to target to halt it. Target will respond, and send us a
395 ocd_interrupt (signo)
398 /* If this doesn't work, try more severe steps. */
399 signal (signo, ocd_interrupt_twice);
402 printf_unfiltered ("ocd_interrupt called\n");
409 ocd_put_packet (buf, 1);
410 ocd_interrupt_flag = 1;
414 static void (*ofunc)();
416 /* The user typed ^C twice. */
418 ocd_interrupt_twice (signo)
421 signal (signo, ofunc);
425 signal (signo, ocd_interrupt);
428 /* Ask the user what to do when an interrupt is received. */
433 target_terminal_ours ();
435 if (query ("Interrupted while waiting for the program.\n\
436 Give up (and stop debugging it)? "))
438 target_mourn_inferior ();
439 return_to_top_level (RETURN_QUIT);
442 target_terminal_inferior ();
445 /* If nonzero, ignore the next kill. */
446 static int kill_kludge;
448 /* Wait until the remote machine stops, then return,
449 storing status in STATUS just as `wait' would.
450 Returns "pid" (though it's not clear what, if anything, that
451 means in the case of this target). */
461 ocd_interrupt_flag = 0;
463 /* Target might already be stopped by the time we get here. */
464 /* If we aren't already stopped, we need to loop until we've dropped
465 back into BDM mode */
467 while (!(last_run_status & OCD_FLAG_BDM))
470 ocd_put_packet (buf, 1);
471 p = ocd_get_packet (OCD_AYT, &pktlen, -1);
473 ofunc = (void (*)()) signal (SIGINT, ocd_interrupt);
474 signal (SIGINT, ofunc);
477 error ("Truncated response packet from OCD device");
479 last_run_status = p[1];
483 ocd_error ("target_wait:", error_code);
485 if (last_run_status & OCD_FLAG_PWF)
486 error ("OCD device lost VCC at BDM interface.");
487 else if (last_run_status & OCD_FLAG_CABLE_DISC)
488 error ("OCD device cable appears to have been disconnected.");
491 if (ocd_interrupt_flag)
497 /* Read registers from the OCD device. Specify the starting and ending
498 register number. Return the number of regs actually read in *NUMREGS.
499 Returns a pointer to a static array containing the register contents. */
502 ocd_read_bdm_registers (first_bdm_regno, last_bdm_regno, reglen)
507 unsigned char buf[10];
511 int error_code, status;
514 buf[0] = OCD_READ_REGS;
515 buf[1] = first_bdm_regno >> 8;
516 buf[2] = first_bdm_regno & 0xff;
517 buf[3] = last_bdm_regno >> 8;
518 buf[4] = last_bdm_regno & 0xff;
520 ocd_put_packet (buf, 5);
521 p = ocd_get_packet (OCD_READ_REGS, &pktlen, remote_timeout);
527 ocd_error ("read_bdm_registers:", error_code);
535 error ("Register block size bad: %d", i);
544 /* Read register BDM_REGNO and returns its value ala read_register() */
547 ocd_read_bdm_register (bdm_regno)
554 p = ocd_read_bdm_registers (bdm_regno, bdm_regno, ®len);
555 regval = extract_unsigned_integer (p, reglen);
561 ocd_write_bdm_registers (first_bdm_regno, regptr, reglen)
563 unsigned char *regptr;
568 int error_code, status;
571 buf = alloca (4 + reglen);
573 buf[0] = OCD_WRITE_REGS;
574 buf[1] = first_bdm_regno >> 8;
575 buf[2] = first_bdm_regno & 0xff;
577 memcpy (buf + 4, regptr, reglen);
579 ocd_put_packet (buf, 4 + reglen);
580 p = ocd_get_packet (OCD_WRITE_REGS, &pktlen, remote_timeout);
583 error ("Truncated response packet from OCD device");
589 ocd_error ("ocd_write_bdm_registers:", error_code);
593 ocd_write_bdm_register (bdm_regno, reg)
597 unsigned char buf[4];
599 store_unsigned_integer (buf, 4, reg);
601 ocd_write_bdm_registers (bdm_regno, buf, 4);
605 ocd_prepare_to_store ()
609 /* Write memory data directly to the remote machine.
610 This does not inform the data cache; the data cache uses this.
611 MEMADDR is the address in the remote memory space.
612 MYADDR is the address of the buffer in our space.
613 LEN is the number of bytes.
615 Returns number of bytes transferred, or 0 for error. */
617 static int write_mem_command = OCD_WRITE_MEM;
620 ocd_write_bytes (memaddr, myaddr, len)
631 buf[0] = write_mem_command;
632 buf[5] = 1; /* Write as bytes */
633 buf[6] = 0; /* Don't verify */
639 int status, error_code;
641 numbytes = min (len, 256 - 8);
643 buf[1] = memaddr >> 24;
644 buf[2] = memaddr >> 16;
645 buf[3] = memaddr >> 8;
650 memcpy (&buf[8], myaddr, numbytes);
651 ocd_put_packet (buf, 8 + numbytes);
652 p = ocd_get_packet (OCD_WRITE_MEM, &pktlen, remote_timeout);
654 error ("Truncated response packet from OCD device");
659 if (error_code == 0x11) /* Got a bus error? */
661 CORE_ADDR error_address;
663 error_address = p[3] << 24;
664 error_address |= p[4] << 16;
665 error_address |= p[5] << 8;
666 error_address |= p[6];
667 numbytes = error_address - memaddr;
675 else if (error_code != 0)
676 ocd_error ("ocd_write_bytes:", error_code);
683 return origlen - len;
686 /* Read memory data directly from the remote machine.
687 This does not use the data cache; the data cache uses this.
688 MEMADDR is the address in the remote memory space.
689 MYADDR is the address of the buffer in our space.
690 LEN is the number of bytes.
692 Returns number of bytes transferred, or 0 for error. */
695 ocd_read_bytes (memaddr, myaddr, len)
706 buf[0] = OCD_READ_MEM;
707 buf[5] = 1; /* Read as bytes */
713 int status, error_code;
715 numbytes = min (len, 256 - 7);
717 buf[1] = memaddr >> 24;
718 buf[2] = memaddr >> 16;
719 buf[3] = memaddr >> 8;
724 ocd_put_packet (buf, 7);
725 p = ocd_get_packet (OCD_READ_MEM, &pktlen, remote_timeout);
727 error ("Truncated response packet from OCD device");
732 if (error_code == 0x11) /* Got a bus error? */
734 CORE_ADDR error_address;
736 error_address = p[3] << 24;
737 error_address |= p[4] << 16;
738 error_address |= p[5] << 8;
739 error_address |= p[6];
740 numbytes = error_address - memaddr;
748 else if (error_code != 0)
749 ocd_error ("ocd_read_bytes:", error_code);
751 memcpy (myaddr, &p[4], numbytes);
758 return origlen - len;
761 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
762 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
763 nonzero. Returns length of data written or read; 0 for error. */
767 ocd_xfer_memory (memaddr, myaddr, len, should_write, target)
772 struct target_ops *target; /* ignored */
774 return dcache_xfer_memory (ocd_dcache, memaddr, myaddr, len, should_write);
778 ocd_files_info (ignore)
779 struct target_ops *ignore;
781 puts_filtered ("Debugging a target over a serial line.\n");
784 /* Stuff for dealing with the packets which are part of this protocol.
785 See comment at top of file for details. */
787 /* Read a single character from the remote side, handling wierd errors. */
795 ch = SERIAL_READCHAR (ocd_desc, timeout);
800 error ("Remote connection closed");
802 perror_with_name ("Remote communication error");
810 /* Read a character from the data stream, dequoting as necessary. SYN is
811 treated special. Any SYNs appearing in the data stream are returned as the
812 distinct value RAW_SYN (which has a value > 8 bits and therefore cannot be
813 mistaken for real data). */
816 get_quoted_char (timeout)
821 ch = readchar (timeout);
826 error ("Timeout in mid-packet, aborting");
830 ch = readchar (timeout);
839 static unsigned char pkt[256 * 2 + 10], *pktp; /* Worst case */
850 if (SERIAL_WRITE (ocd_desc, pkt, pktp - pkt))
851 perror_with_name ("output_packet: write failed");
856 /* Output a quoted character. SYNs and DLEs are quoted. Everything else goes
857 through untouched. */
874 /* Send a packet to the OCD device. The packet framed by a SYN character,
875 a byte count and a checksum. The byte count only counts the number of
876 bytes between the count and the checksum. A count of zero actually
877 means 256. Any SYNs within the packet (including the checksum and
878 count) must be quoted. The quote character must be quoted as well.
879 Quoting is done by replacing the character with the two-character sequence
880 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
884 stu_put_packet (buf, len)
888 unsigned char checksum;
891 if (len == 0 || len > 256)
892 abort (); /* Can't represent 0 length packet */
898 put_quoted_char (RAW_SYN);
912 put_quoted_char (-checksum & 0xff);
919 /* Send a packet to the OCD device. The packet framed by a SYN character,
920 a byte count and a checksum. The byte count only counts the number of
921 bytes between the count and the checksum. A count of zero actually
922 means 256. Any SYNs within the packet (including the checksum and
923 count) must be quoted. The quote character must be quoted as well.
924 Quoting is done by replacing the character with the two-character sequence
925 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
929 ocd_put_packet (buf, len)
933 unsigned char checksum;
935 unsigned char *packet, *packet_ptr;
937 packet = alloca (len + 1 + 1); /* packet + SYN + checksum */
942 *packet_ptr++ = 0x55;
952 *packet_ptr++ = -checksum;
953 if (SERIAL_WRITE (ocd_desc, packet, packet_ptr - packet))
954 perror_with_name ("output_packet: write failed");
959 /* Get a packet from the OCD device. Timeout is only enforced for the
960 first byte of the packet. Subsequent bytes are expected to arrive in
961 time <= remote_timeout. Returns a pointer to a static buffer containing
962 the payload of the packet. *LENP contains the length of the packet.
965 static unsigned char *
966 stu_get_packet (cmd, lenp, timeout)
972 static unsigned char buf[256 + 10], *p;
973 unsigned char checksum;
977 ch = get_quoted_char (timeout);
980 error ("get_packet (readchar): %d", ch);
985 found_syn: /* Found the start of a packet */
990 len = get_quoted_char (remote_timeout);
1000 len++; /* Include checksum */
1004 ch = get_quoted_char (remote_timeout);
1016 error ("Response phase error. Got 0x%x, expected 0x%x", buf[0], cmd);
1018 *lenp = p - buf - 1;
1024 /* Get a packet from the OCD device. Timeout is only enforced for the
1025 first byte of the packet. Subsequent bytes are expected to arrive in
1026 time <= remote_timeout. Returns a pointer to a static buffer containing
1027 the payload of the packet. *LENP contains the length of the packet.
1030 static unsigned char *
1031 ocd_get_packet (cmd, lenp, timeout)
1038 static unsigned char packet[512];
1039 unsigned char *packet_ptr;
1040 unsigned char checksum;
1042 ch = readchar (timeout);
1045 error ("ocd_get_packet (readchar): %d", ch);
1048 error ("ocd_get_packet (readchar): %d", ch);
1050 /* Found the start of a packet */
1052 packet_ptr = packet;
1055 /* Read command char. That sort of tells us how long the packet is. */
1057 ch = readchar (timeout);
1060 error ("ocd_get_packet (readchar): %d", ch);
1067 ch = readchar (timeout);
1070 error ("ocd_get_packet (readchar): %d", ch);
1074 /* Get error code. */
1076 ch = readchar (timeout);
1079 error ("ocd_get_packet (readchar): %d", ch);
1083 switch (ch) /* Figure out length of packet */
1085 case 0x7: /* Write verify error? */
1086 len = 8; /* write address, value read back */
1088 case 0x11: /* Bus error? */
1089 /* write address, read flag */
1090 case 0x15: /* Internal error */
1091 len = 5; /* error code, vector */
1093 default: /* Error w/no params */
1096 case 0x0: /* Normal result */
1099 case OCD_AYT: /* Are You There? */
1100 case OCD_SET_BAUD_RATE: /* Set Baud Rate */
1101 case OCD_INIT: /* Initialize OCD device */
1102 case OCD_SET_SPEED: /* Set Speed */
1103 case OCD_SET_FUNC_CODE: /* Set Function Code */
1104 case OCD_SET_CTL_FLAGS: /* Set Control Flags */
1105 case OCD_SET_BUF_ADDR: /* Set Register Buffer Address */
1106 case OCD_RUN: /* Run Target from PC */
1107 case OCD_RUN_ADDR: /* Run Target from Specified Address */
1108 case OCD_STOP: /* Stop Target */
1109 case OCD_RESET_RUN: /* Reset Target and Run */
1110 case OCD_RESET: /* Reset Target and Halt */
1111 case OCD_STEP: /* Single Step */
1112 case OCD_WRITE_REGS: /* Write Register */
1113 case OCD_WRITE_MEM: /* Write Memory */
1114 case OCD_FILL_MEM: /* Fill Memory */
1115 case OCD_MOVE_MEM: /* Move Memory */
1116 case OCD_WRITE_INT_MEM: /* Write Internal Memory */
1117 case OCD_JUMP: /* Jump to Subroutine */
1118 case OCD_ERASE_FLASH: /* Erase flash memory */
1119 case OCD_PROGRAM_FLASH: /* Write flash memory */
1120 case OCD_EXIT_MON: /* Exit the flash programming monitor */
1121 case OCD_ENTER_MON: /* Enter the flash programming monitor */
1122 case OCD_LOG_FILE: /* Make Wigglers.dll save Wigglers.log */
1123 case OCD_SET_CONNECTION: /* Set type of connection in Wigglers.dll */
1126 case OCD_GET_VERSION: /* Get Version */
1129 case OCD_GET_STATUS_MASK: /* Get Status Mask */
1132 case OCD_GET_CTRS: /* Get Error Counters */
1133 case OCD_READ_REGS: /* Read Register */
1134 case OCD_READ_MEM: /* Read Memory */
1135 case OCD_READ_INT_MEM: /* Read Internal Memory */
1139 error ("ocd_get_packet: unknown packet type 0x%x\n", ch);
1143 if (len == 257) /* Byte stream? */
1144 { /* Yes, byte streams contain the length */
1145 ch = readchar (timeout);
1148 error ("ocd_get_packet (readchar): %d", ch);
1156 while (len-- >= 0) /* Do rest of packet and checksum */
1158 ch = readchar (timeout);
1161 error ("ocd_get_packet (readchar): %d", ch);
1167 error ("ocd_get_packet: bad packet checksum");
1169 if (cmd != -1 && cmd != packet[0])
1170 error ("Response phase error. Got 0x%x, expected 0x%x", packet[0], cmd);
1172 *lenp = packet_ptr - packet - 1; /* Subtract checksum byte */
1177 /* Execute a simple (one-byte) command. Returns a pointer to the data
1178 following the error code. */
1180 static unsigned char *
1181 ocd_do_command (cmd, statusp, lenp)
1186 unsigned char buf[100], *p;
1187 int status, error_code;
1190 unsigned char logbuf[100];
1194 ocd_put_packet (buf, 1); /* Send command */
1195 p = ocd_get_packet (*buf, lenp, remote_timeout);
1198 error ("Truncated response packet from OCD device");
1203 if (error_code != 0)
1205 sprintf (errbuf, "ocd_do_command (0x%x):", cmd);
1206 ocd_error (errbuf, error_code);
1209 if (status & OCD_FLAG_PWF)
1210 error ("OCD device can't detect VCC at BDM interface.");
1211 else if (status & OCD_FLAG_CABLE_DISC)
1212 error ("BDM cable appears to be disconnected.");
1216 logbuf[0] = OCD_LOG_FILE;
1217 logbuf[1] = 3; /* close existing WIGGLERS.LOG */
1218 ocd_put_packet (logbuf, 2);
1219 ocd_get_packet (logbuf[0], &logpktlen, remote_timeout);
1221 logbuf[0] = OCD_LOG_FILE;
1222 logbuf[1] = 2; /* append to existing WIGGLERS.LOG */
1223 ocd_put_packet (logbuf, 2);
1224 ocd_get_packet (logbuf[0], &logpktlen, remote_timeout);
1232 /* For some mysterious reason, wait_for_inferior calls kill instead of
1233 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1237 target_mourn_inferior ();
1241 /* Don't wait for it to die. I'm not really sure it matters whether
1243 target_mourn_inferior ();
1249 unpush_target (current_ops);
1250 generic_mourn_inferior ();
1253 /* All we actually do is set the PC to the start address of exec_bfd, and start
1254 the program at that point. */
1257 ocd_create_inferior (exec_file, args, env)
1262 if (args && (*args != '\000'))
1263 error ("Args are not supported by BDM.");
1265 clear_proceed_status ();
1266 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1270 ocd_load (args, from_tty)
1274 generic_load (args, from_tty);
1278 /* This is necessary because many things were based on the PC at the time that
1279 we attached to the monitor, which is no longer valid now that we have loaded
1280 new code (and just changed the PC). Another way to do this might be to call
1281 normal_stop, except that the stack may not be valid, and things would get
1282 horribly confused... */
1284 clear_symtab_users ();
1287 /* This should be defined for each target */
1288 /* But we want to be able to compile this file for some configurations
1289 not yet supported fully */
1291 #define BDM_BREAKPOINT {0x0,0x0,0x0,0x0} /* For ppc 8xx */
1293 #define BDM_BREAKPOINT {0x4a,0xfa} /* BGND insn used for CPU32 */
1296 /* BDM (at least on CPU32) uses a different breakpoint */
1299 ocd_insert_breakpoint (addr, contents_cache)
1301 char *contents_cache;
1303 static char break_insn[] = BDM_BREAKPOINT;
1306 val = target_read_memory (addr, contents_cache, sizeof (break_insn));
1309 val = target_write_memory (addr, break_insn, sizeof (break_insn));
1315 ocd_remove_breakpoint (addr, contents_cache)
1317 char *contents_cache;
1319 static char break_insn[] = BDM_BREAKPOINT;
1322 val = target_write_memory (addr, contents_cache, sizeof (break_insn));
1328 bdm_command (args, from_tty)
1332 error ("bdm command must be followed by `reset'");
1336 bdm_reset_command (args, from_tty)
1343 error ("Not connected to OCD device.");
1345 ocd_do_command (OCD_RESET, &status, &pktlen);
1346 dcache_flush (ocd_dcache);
1347 registers_changed ();
1351 bdm_restart_command (args, from_tty)
1358 error ("Not connected to OCD device.");
1360 ocd_do_command (OCD_RESET_RUN, &status, &pktlen);
1361 last_run_status = status;
1362 clear_proceed_status ();
1363 wait_for_inferior ();
1367 /* Temporary replacement for target_store_registers(). This prevents
1368 generic_load from trying to set the PC. */
1371 noop_store_registers (regno)
1377 bdm_update_flash_command (args, from_tty)
1382 struct cleanup *old_chain;
1383 void (*store_registers_tmp) PARAMS ((int));
1386 error ("Not connected to OCD device.");
1389 error ("Must specify file containing new OCD code.");
1391 /* old_chain = make_cleanup (flash_cleanup, 0);*/
1393 ocd_do_command (OCD_ENTER_MON, &status, &pktlen);
1395 ocd_do_command (OCD_ERASE_FLASH, &status, &pktlen);
1397 write_mem_command = OCD_PROGRAM_FLASH;
1398 store_registers_tmp = current_target.to_store_registers;
1399 current_target.to_store_registers = noop_store_registers;
1401 generic_load (args, from_tty);
1403 current_target.to_store_registers = store_registers_tmp;
1404 write_mem_command = OCD_WRITE_MEM;
1406 ocd_do_command (OCD_EXIT_MON, &status, &pktlen);
1408 /* discard_cleanups (old_chain);*/
1412 bdm_read_register_command (args, from_tty)
1416 /* XXX repeat should go on to the next register */
1419 error ("Not connected to OCD device.");
1422 error ("Must specify BDM register number.");
1427 _initialize_remote_ocd ()
1429 extern struct cmd_list_element *cmdlist;
1430 static struct cmd_list_element *ocd_cmd_list = NULL;
1432 add_show_from_set (add_set_cmd ("remotetimeout", no_class,
1433 var_integer, (char *)&remote_timeout,
1434 "Set timeout value for remote read.\n", &setlist),
1437 add_prefix_cmd ("ocd", class_obscure, bdm_command, "", &ocd_cmd_list, "ocd ",
1440 add_cmd ("reset", class_obscure, bdm_reset_command, "", &ocd_cmd_list);
1441 add_cmd ("restart", class_obscure, bdm_restart_command, "", &ocd_cmd_list);
1442 add_cmd ("update-flash", class_obscure, bdm_update_flash_command, "", &ocd_cmd_list);
1443 /* add_cmd ("read-register", class_obscure, bdm_read_register_command, "", &ocd_cmd_list);*/