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,
19 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
33 #include "gdb-stabs.h"
35 #include <sys/types.h>
40 /* Prototypes for local functions */
42 static int ocd_read_bytes
PARAMS ((CORE_ADDR memaddr
,
43 char *myaddr
, int len
));
45 static int ocd_start_remote
PARAMS ((PTR dummy
));
47 static int readchar
PARAMS ((int timeout
));
49 static void reset_packet
PARAMS ((void));
51 static void output_packet
PARAMS ((void));
53 static int get_quoted_char
PARAMS ((int timeout
));
55 static void put_quoted_char
PARAMS ((int c
));
57 static void ocd_interrupt
PARAMS ((int signo
));
59 static void ocd_interrupt_twice
PARAMS ((int signo
));
61 static void interrupt_query
PARAMS ((void));
63 static unsigned char *ocd_do_command
PARAMS ((int cmd
, int *statusp
, int *lenp
));
65 static void ocd_put_packet
PARAMS ((unsigned char *packet
, int pktlen
));
67 static unsigned char *ocd_get_packet
PARAMS ((int cmd
, int *pktlen
, int timeout
));
69 static struct target_ops
*current_ops
= NULL
;
71 static int last_run_status
;
73 /* This was 5 seconds, which is a long time to sit and wait.
74 Unless this is going though some terminal server or multiplexer or
75 other form of hairy serial connection, I would think 2 seconds would
79 /* FIXME: Change to allow option to set timeout value on a per target
81 static int remote_timeout
= 2;
84 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
85 ocd_open knows that we don't have a file open when the program
87 static serial_t ocd_desc
= NULL
;
90 ocd_error (s
, error_code
)
96 fputs_filtered (s
, gdb_stderr
);
97 fputs_filtered (" ", gdb_stderr
);
108 s
= "Cable disconnected";
111 s
= "Couldn't enter OCD mode";
114 s
= "Target stuck in reset";
117 s
= "OCD hasn't been initialized";
120 s
= "Write verify failed";
123 s
= "Reg buff error (during MPC5xx fp reg read/write)";
126 s
= "Invalid CPU register access attempt failed";
132 s
= "Checksum error";
135 s
= "Illegal command";
138 s
= "Parameter error";
141 s
= "Internal error";
144 s
= "Flash erase error";
147 sprintf (buf
, "Unknown error code %d", error_code
);
154 /* Return nonzero if the thread TH is still alive on the remote system. */
157 ocd_thread_alive (th
)
163 /* Clean up connection to a remote debugger. */
171 SERIAL_CLOSE (ocd_desc
);
175 /* Stub for catch_errors. */
178 ocd_start_remote (dummy
)
181 unsigned char buf
[10], *p
;
186 enum ocd_target_type target_type
;
188 target_type
= *(enum ocd_target_type
*) dummy
;
190 immediate_quit
= 1; /* Allow user to interrupt it */
192 SERIAL_SEND_BREAK (ocd_desc
); /* Wake up the wiggler */
194 speed
= 80; /* Divide clock by 4000 */
198 buf
[2] = speed
& 0xff;
199 buf
[3] = target_type
;
200 ocd_put_packet (buf
, 4); /* Init OCD params */
201 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
204 error ("Truncated response packet from OCD device");
210 ocd_error ("OCD_INIT:", error_code
);
212 ocd_do_command (OCD_AYT
, &status
, &pktlen
);
214 p
= ocd_do_command (OCD_GET_VERSION
, &status
, &pktlen
);
216 printf_unfiltered ("[Wiggler version %x.%x, capability 0x%x]\n",
217 p
[0], p
[1], (p
[2] << 16) | p
[3]);
220 /* Reset the target */
222 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
223 /* ocd_do_command (OCD_RESET, &status, &pktlen); */
226 /* If processor is still running, stop it. */
228 if (!(status
& OCD_FLAG_BDM
))
232 /* When using a target box, we want to asynchronously return status when
233 target stops. The OCD_SET_CTL_FLAGS command is ignored by Wigglers.dll
234 when using a parallel Wiggler */
235 buf
[0] = OCD_SET_CTL_FLAGS
;
238 ocd_put_packet (buf
, 3);
240 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
243 error ("Truncated response packet from OCD device");
249 ocd_error ("OCD_SET_CTL_FLAGS:", error_code
);
254 /* This is really the job of start_remote however, that makes an assumption
255 that the target is about to print out a status message of some sort. That
256 doesn't happen here (in fact, it may not be possible to get the monitor to
257 send the appropriate packet). */
259 flush_cached_frames ();
260 registers_changed ();
261 stop_pc
= read_pc ();
262 set_current_frame (create_new_frame (read_fp (), stop_pc
));
263 select_frame (get_current_frame (), 0);
264 print_stack_frame (selected_frame
, -1, 1);
266 buf
[0] = OCD_LOG_FILE
;
267 buf
[1] = 3; /* close existing WIGGLERS.LOG */
268 ocd_put_packet (buf
, 2);
269 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
271 buf
[0] = OCD_LOG_FILE
;
272 buf
[1] = 2; /* append to existing WIGGLERS.LOG */
273 ocd_put_packet (buf
, 2);
274 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
279 /* Open a connection to a remote debugger.
280 NAME is the filename used for communication. */
282 static DCACHE
*ocd_dcache
;
285 ocd_open (name
, from_tty
, target_type
, ops
)
288 enum ocd_target_type target_type
;
289 struct target_ops
*ops
;
291 unsigned char buf
[10], *p
;
296 error ("To open an OCD connection, you need to specify the\n\
297 device the OCD device is attached to (e.g. /dev/ttya).");
299 target_preopen (from_tty
);
303 unpush_target (current_ops
);
305 ocd_dcache
= dcache_init (ocd_read_bytes
, ocd_write_bytes
);
307 if (strncmp (name
, "wiggler", 7) == 0)
309 ocd_desc
= SERIAL_OPEN ("ocd");
311 perror_with_name (name
);
313 buf
[0] = OCD_LOG_FILE
;
314 buf
[1] = 1; /* open new or overwrite existing WIGGLERS.LOG */
315 ocd_put_packet (buf
, 2);
316 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
318 buf
[0] = OCD_SET_CONNECTION
;
319 buf
[1] = 0x01; /* atoi (name[11]); */
320 ocd_put_packet (buf
, 2);
321 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
324 /* not using Wigglers.dll */
326 ocd_desc
= SERIAL_OPEN (name
);
328 perror_with_name (name
);
333 if (SERIAL_SETBAUDRATE (ocd_desc
, baud_rate
))
335 SERIAL_CLOSE (ocd_desc
);
336 perror_with_name (name
);
340 SERIAL_RAW (ocd_desc
);
342 /* If there is something sitting in the buffer we might take it as a
343 response to a command, which would be bad. */
344 SERIAL_FLUSH_INPUT (ocd_desc
);
348 puts_filtered ("Remote target wiggler connected to ");
349 puts_filtered (name
);
350 puts_filtered ("\n");
352 push_target (current_ops
); /* Switch to using remote target now */
354 /* Without this, some commands which require an active target (such as kill)
355 won't work. This variable serves (at least) double duty as both the pid
356 of the target process (if it has such), and as a flag indicating that a
357 target is active. These functions should be split out into seperate
358 variables, especially since GDB will someday have a notion of debugging
359 several processes. */
361 inferior_pid
= 42000;
362 /* Start the remote connection; if error (0), discard this target.
363 In particular, if the user quits, be sure to discard it
364 (we'd be in an inconsistent state otherwise). */
365 if (!catch_errors (ocd_start_remote
, &target_type
,
366 "Couldn't establish connection to remote target\n",
370 error ("Failed to connect to OCD.");
374 /* This takes a program previously attached to and detaches it. After
375 this is done, GDB can be used to debug some other program. We
376 better not have left any breakpoints in the target program or it'll
377 die when it hits one. */
380 ocd_detach (args
, from_tty
)
385 error ("Argument given to \"detach\" when remotely debugging.");
389 puts_filtered ("Ending remote debugging.\n");
392 /* Tell the remote machine to resume. */
395 ocd_resume (pid
, step
, siggnal
)
397 enum target_signal siggnal
;
401 dcache_flush (ocd_dcache
);
404 ocd_do_command (OCD_STEP
, &last_run_status
, &pktlen
);
406 ocd_do_command (OCD_RUN
, &last_run_status
, &pktlen
);
415 ocd_do_command (OCD_STOP
, &status
, &pktlen
);
417 if (!(status
& OCD_FLAG_BDM
))
418 error ("Can't stop target via BDM");
421 static volatile int ocd_interrupt_flag
;
423 /* Send ^C to target to halt it. Target will respond, and send us a
427 ocd_interrupt (signo
)
430 /* If this doesn't work, try more severe steps. */
431 signal (signo
, ocd_interrupt_twice
);
434 printf_unfiltered ("ocd_interrupt called\n");
441 ocd_put_packet (buf
, 1);
442 ocd_interrupt_flag
= 1;
446 static void (*ofunc
) ();
448 /* The user typed ^C twice. */
450 ocd_interrupt_twice (signo
)
453 signal (signo
, ofunc
);
457 signal (signo
, ocd_interrupt
);
460 /* Ask the user what to do when an interrupt is received. */
465 target_terminal_ours ();
467 if (query ("Interrupted while waiting for the program.\n\
468 Give up (and stop debugging it)? "))
470 target_mourn_inferior ();
471 return_to_top_level (RETURN_QUIT
);
474 target_terminal_inferior ();
477 /* If nonzero, ignore the next kill. */
478 static int kill_kludge
;
480 /* Wait until the remote machine stops, then return,
481 storing status in STATUS just as `wait' would.
482 Returns "pid" (though it's not clear what, if anything, that
483 means in the case of this target). */
493 ocd_interrupt_flag
= 0;
495 /* Target might already be stopped by the time we get here. */
496 /* If we aren't already stopped, we need to loop until we've dropped
497 back into BDM mode */
499 while (!(last_run_status
& OCD_FLAG_BDM
))
502 ocd_put_packet (buf
, 1);
503 p
= ocd_get_packet (OCD_AYT
, &pktlen
, -1);
505 ofunc
= (void (*)()) signal (SIGINT
, ocd_interrupt
);
506 signal (SIGINT
, ofunc
);
509 error ("Truncated response packet from OCD device");
511 last_run_status
= p
[1];
515 ocd_error ("target_wait:", error_code
);
517 if (last_run_status
& OCD_FLAG_PWF
)
518 error ("OCD device lost VCC at BDM interface.");
519 else if (last_run_status
& OCD_FLAG_CABLE_DISC
)
520 error ("OCD device cable appears to have been disconnected.");
523 if (ocd_interrupt_flag
)
529 /* Read registers from the OCD device. Specify the starting and ending
530 register number. Return the number of regs actually read in *NUMREGS.
531 Returns a pointer to a static array containing the register contents. */
534 ocd_read_bdm_registers (first_bdm_regno
, last_bdm_regno
, reglen
)
539 unsigned char buf
[10];
543 int error_code
, status
;
546 buf
[0] = OCD_READ_REGS
;
547 buf
[1] = first_bdm_regno
>> 8;
548 buf
[2] = first_bdm_regno
& 0xff;
549 buf
[3] = last_bdm_regno
>> 8;
550 buf
[4] = last_bdm_regno
& 0xff;
552 ocd_put_packet (buf
, 5);
553 p
= ocd_get_packet (OCD_READ_REGS
, &pktlen
, remote_timeout
);
559 ocd_error ("read_bdm_registers:", error_code
);
567 error ("Register block size bad: %d", i
);
576 /* Read register BDM_REGNO and returns its value ala read_register() */
579 ocd_read_bdm_register (bdm_regno
)
586 p
= ocd_read_bdm_registers (bdm_regno
, bdm_regno
, ®len
);
587 regval
= extract_unsigned_integer (p
, reglen
);
593 ocd_write_bdm_registers (first_bdm_regno
, regptr
, reglen
)
595 unsigned char *regptr
;
600 int error_code
, status
;
603 buf
= alloca (4 + reglen
);
605 buf
[0] = OCD_WRITE_REGS
;
606 buf
[1] = first_bdm_regno
>> 8;
607 buf
[2] = first_bdm_regno
& 0xff;
609 memcpy (buf
+ 4, regptr
, reglen
);
611 ocd_put_packet (buf
, 4 + reglen
);
612 p
= ocd_get_packet (OCD_WRITE_REGS
, &pktlen
, remote_timeout
);
615 error ("Truncated response packet from OCD device");
621 ocd_error ("ocd_write_bdm_registers:", error_code
);
625 ocd_write_bdm_register (bdm_regno
, reg
)
629 unsigned char buf
[4];
631 store_unsigned_integer (buf
, 4, reg
);
633 ocd_write_bdm_registers (bdm_regno
, buf
, 4);
637 ocd_prepare_to_store ()
641 /* Write memory data directly to the remote machine.
642 This does not inform the data cache; the data cache uses this.
643 MEMADDR is the address in the remote memory space.
644 MYADDR is the address of the buffer in our space.
645 LEN is the number of bytes.
647 Returns number of bytes transferred, or 0 for error. */
649 static int write_mem_command
= OCD_WRITE_MEM
;
652 ocd_write_bytes (memaddr
, myaddr
, len
)
663 buf
[0] = write_mem_command
;
664 buf
[5] = 1; /* Write as bytes */
665 buf
[6] = 0; /* Don't verify */
671 int status
, error_code
;
673 numbytes
= min (len
, 256 - 8);
675 buf
[1] = memaddr
>> 24;
676 buf
[2] = memaddr
>> 16;
677 buf
[3] = memaddr
>> 8;
682 memcpy (&buf
[8], myaddr
, numbytes
);
683 ocd_put_packet (buf
, 8 + numbytes
);
684 p
= ocd_get_packet (OCD_WRITE_MEM
, &pktlen
, remote_timeout
);
686 error ("Truncated response packet from OCD device");
691 if (error_code
== 0x11) /* Got a bus error? */
693 CORE_ADDR error_address
;
695 error_address
= p
[3] << 24;
696 error_address
|= p
[4] << 16;
697 error_address
|= p
[5] << 8;
698 error_address
|= p
[6];
699 numbytes
= error_address
- memaddr
;
707 else if (error_code
!= 0)
708 ocd_error ("ocd_write_bytes:", error_code
);
715 return origlen
- len
;
718 /* Read memory data directly from the remote machine.
719 This does not use the data cache; the data cache uses this.
720 MEMADDR is the address in the remote memory space.
721 MYADDR is the address of the buffer in our space.
722 LEN is the number of bytes.
724 Returns number of bytes transferred, or 0 for error. */
727 ocd_read_bytes (memaddr
, myaddr
, len
)
738 buf
[0] = OCD_READ_MEM
;
739 buf
[5] = 1; /* Read as bytes */
745 int status
, error_code
;
747 numbytes
= min (len
, 256 - 7);
749 buf
[1] = memaddr
>> 24;
750 buf
[2] = memaddr
>> 16;
751 buf
[3] = memaddr
>> 8;
756 ocd_put_packet (buf
, 7);
757 p
= ocd_get_packet (OCD_READ_MEM
, &pktlen
, remote_timeout
);
759 error ("Truncated response packet from OCD device");
764 if (error_code
== 0x11) /* Got a bus error? */
766 CORE_ADDR error_address
;
768 error_address
= p
[3] << 24;
769 error_address
|= p
[4] << 16;
770 error_address
|= p
[5] << 8;
771 error_address
|= p
[6];
772 numbytes
= error_address
- memaddr
;
780 else if (error_code
!= 0)
781 ocd_error ("ocd_read_bytes:", error_code
);
783 memcpy (myaddr
, &p
[4], numbytes
);
790 return origlen
- len
;
793 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
794 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
795 nonzero. Returns length of data written or read; 0 for error. */
799 ocd_xfer_memory (memaddr
, myaddr
, len
, should_write
, target
)
804 struct target_ops
*target
; /* ignored */
806 return dcache_xfer_memory (ocd_dcache
, memaddr
, myaddr
, len
, should_write
);
810 ocd_files_info (ignore
)
811 struct target_ops
*ignore
;
813 puts_filtered ("Debugging a target over a serial line.\n");
816 /* Stuff for dealing with the packets which are part of this protocol.
817 See comment at top of file for details. */
819 /* Read a single character from the remote side, handling wierd errors. */
827 ch
= SERIAL_READCHAR (ocd_desc
, timeout
);
832 error ("Remote connection closed");
834 perror_with_name ("Remote communication error");
842 /* Read a character from the data stream, dequoting as necessary. SYN is
843 treated special. Any SYNs appearing in the data stream are returned as the
844 distinct value RAW_SYN (which has a value > 8 bits and therefore cannot be
845 mistaken for real data). */
848 get_quoted_char (timeout
)
853 ch
= readchar (timeout
);
858 error ("Timeout in mid-packet, aborting");
862 ch
= readchar (timeout
);
871 static unsigned char pkt
[256 * 2 + 10], *pktp
; /* Worst case */
882 if (SERIAL_WRITE (ocd_desc
, pkt
, pktp
- pkt
))
883 perror_with_name ("output_packet: write failed");
888 /* Output a quoted character. SYNs and DLEs are quoted. Everything else goes
889 through untouched. */
906 /* Send a packet to the OCD device. The packet framed by a SYN character,
907 a byte count and a checksum. The byte count only counts the number of
908 bytes between the count and the checksum. A count of zero actually
909 means 256. Any SYNs within the packet (including the checksum and
910 count) must be quoted. The quote character must be quoted as well.
911 Quoting is done by replacing the character with the two-character sequence
912 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
916 stu_put_packet (buf
, len
)
920 unsigned char checksum
;
923 if (len
== 0 || len
> 256)
924 abort (); /* Can't represent 0 length packet */
930 put_quoted_char (RAW_SYN
);
944 put_quoted_char (-checksum
& 0xff);
951 /* Send a packet to the OCD device. The packet framed by a SYN character,
952 a byte count and a checksum. The byte count only counts the number of
953 bytes between the count and the checksum. A count of zero actually
954 means 256. Any SYNs within the packet (including the checksum and
955 count) must be quoted. The quote character must be quoted as well.
956 Quoting is done by replacing the character with the two-character sequence
957 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
961 ocd_put_packet (buf
, len
)
965 unsigned char checksum
;
967 unsigned char *packet
, *packet_ptr
;
969 packet
= alloca (len
+ 1 + 1); /* packet + SYN + checksum */
974 *packet_ptr
++ = 0x55;
984 *packet_ptr
++ = -checksum
;
985 if (SERIAL_WRITE (ocd_desc
, packet
, packet_ptr
- packet
))
986 perror_with_name ("output_packet: write failed");
991 /* Get a packet from the OCD device. Timeout is only enforced for the
992 first byte of the packet. Subsequent bytes are expected to arrive in
993 time <= remote_timeout. Returns a pointer to a static buffer containing
994 the payload of the packet. *LENP contains the length of the packet.
997 static unsigned char *
998 stu_get_packet (cmd
, lenp
, timeout
)
1004 static unsigned char buf
[256 + 10], *p
;
1005 unsigned char checksum
;
1009 ch
= get_quoted_char (timeout
);
1012 error ("get_packet (readchar): %d", ch
);
1017 found_syn
: /* Found the start of a packet */
1022 len
= get_quoted_char (remote_timeout
);
1032 len
++; /* Include checksum */
1036 ch
= get_quoted_char (remote_timeout
);
1048 error ("Response phase error. Got 0x%x, expected 0x%x", buf
[0], cmd
);
1050 *lenp
= p
- buf
- 1;
1056 /* Get a packet from the OCD device. Timeout is only enforced for the
1057 first byte of the packet. Subsequent bytes are expected to arrive in
1058 time <= remote_timeout. Returns a pointer to a static buffer containing
1059 the payload of the packet. *LENP contains the length of the packet.
1062 static unsigned char *
1063 ocd_get_packet (cmd
, lenp
, timeout
)
1070 static unsigned char packet
[512];
1071 unsigned char *packet_ptr
;
1072 unsigned char checksum
;
1074 ch
= readchar (timeout
);
1077 error ("ocd_get_packet (readchar): %d", ch
);
1080 error ("ocd_get_packet (readchar): %d", ch
);
1082 /* Found the start of a packet */
1084 packet_ptr
= packet
;
1087 /* Read command char. That sort of tells us how long the packet is. */
1089 ch
= readchar (timeout
);
1092 error ("ocd_get_packet (readchar): %d", ch
);
1099 ch
= readchar (timeout
);
1102 error ("ocd_get_packet (readchar): %d", ch
);
1106 /* Get error code. */
1108 ch
= readchar (timeout
);
1111 error ("ocd_get_packet (readchar): %d", ch
);
1115 switch (ch
) /* Figure out length of packet */
1117 case 0x7: /* Write verify error? */
1118 len
= 8; /* write address, value read back */
1120 case 0x11: /* Bus error? */
1121 /* write address, read flag */
1122 case 0x15: /* Internal error */
1123 len
= 5; /* error code, vector */
1125 default: /* Error w/no params */
1128 case 0x0: /* Normal result */
1131 case OCD_AYT
: /* Are You There? */
1132 case OCD_SET_BAUD_RATE
: /* Set Baud Rate */
1133 case OCD_INIT
: /* Initialize OCD device */
1134 case OCD_SET_SPEED
: /* Set Speed */
1135 case OCD_SET_FUNC_CODE
: /* Set Function Code */
1136 case OCD_SET_CTL_FLAGS
: /* Set Control Flags */
1137 case OCD_SET_BUF_ADDR
: /* Set Register Buffer Address */
1138 case OCD_RUN
: /* Run Target from PC */
1139 case OCD_RUN_ADDR
: /* Run Target from Specified Address */
1140 case OCD_STOP
: /* Stop Target */
1141 case OCD_RESET_RUN
: /* Reset Target and Run */
1142 case OCD_RESET
: /* Reset Target and Halt */
1143 case OCD_STEP
: /* Single Step */
1144 case OCD_WRITE_REGS
: /* Write Register */
1145 case OCD_WRITE_MEM
: /* Write Memory */
1146 case OCD_FILL_MEM
: /* Fill Memory */
1147 case OCD_MOVE_MEM
: /* Move Memory */
1148 case OCD_WRITE_INT_MEM
: /* Write Internal Memory */
1149 case OCD_JUMP
: /* Jump to Subroutine */
1150 case OCD_ERASE_FLASH
: /* Erase flash memory */
1151 case OCD_PROGRAM_FLASH
: /* Write flash memory */
1152 case OCD_EXIT_MON
: /* Exit the flash programming monitor */
1153 case OCD_ENTER_MON
: /* Enter the flash programming monitor */
1154 case OCD_LOG_FILE
: /* Make Wigglers.dll save Wigglers.log */
1155 case OCD_SET_CONNECTION
: /* Set type of connection in Wigglers.dll */
1158 case OCD_GET_VERSION
: /* Get Version */
1161 case OCD_GET_STATUS_MASK
: /* Get Status Mask */
1164 case OCD_GET_CTRS
: /* Get Error Counters */
1165 case OCD_READ_REGS
: /* Read Register */
1166 case OCD_READ_MEM
: /* Read Memory */
1167 case OCD_READ_INT_MEM
: /* Read Internal Memory */
1171 error ("ocd_get_packet: unknown packet type 0x%x\n", ch
);
1175 if (len
== 257) /* Byte stream? */
1176 { /* Yes, byte streams contain the length */
1177 ch
= readchar (timeout
);
1180 error ("ocd_get_packet (readchar): %d", ch
);
1188 while (len
-- >= 0) /* Do rest of packet and checksum */
1190 ch
= readchar (timeout
);
1193 error ("ocd_get_packet (readchar): %d", ch
);
1199 error ("ocd_get_packet: bad packet checksum");
1201 if (cmd
!= -1 && cmd
!= packet
[0])
1202 error ("Response phase error. Got 0x%x, expected 0x%x", packet
[0], cmd
);
1204 *lenp
= packet_ptr
- packet
- 1; /* Subtract checksum byte */
1209 /* Execute a simple (one-byte) command. Returns a pointer to the data
1210 following the error code. */
1212 static unsigned char *
1213 ocd_do_command (cmd
, statusp
, lenp
)
1218 unsigned char buf
[100], *p
;
1219 int status
, error_code
;
1222 unsigned char logbuf
[100];
1226 ocd_put_packet (buf
, 1); /* Send command */
1227 p
= ocd_get_packet (*buf
, lenp
, remote_timeout
);
1230 error ("Truncated response packet from OCD device");
1235 if (error_code
!= 0)
1237 sprintf (errbuf
, "ocd_do_command (0x%x):", cmd
);
1238 ocd_error (errbuf
, error_code
);
1241 if (status
& OCD_FLAG_PWF
)
1242 error ("OCD device can't detect VCC at BDM interface.");
1243 else if (status
& OCD_FLAG_CABLE_DISC
)
1244 error ("BDM cable appears to be disconnected.");
1248 logbuf
[0] = OCD_LOG_FILE
;
1249 logbuf
[1] = 3; /* close existing WIGGLERS.LOG */
1250 ocd_put_packet (logbuf
, 2);
1251 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1253 logbuf
[0] = OCD_LOG_FILE
;
1254 logbuf
[1] = 2; /* append to existing WIGGLERS.LOG */
1255 ocd_put_packet (logbuf
, 2);
1256 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1264 /* For some mysterious reason, wait_for_inferior calls kill instead of
1265 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1269 target_mourn_inferior ();
1273 /* Don't wait for it to die. I'm not really sure it matters whether
1275 target_mourn_inferior ();
1281 unpush_target (current_ops
);
1282 generic_mourn_inferior ();
1285 /* All we actually do is set the PC to the start address of exec_bfd, and start
1286 the program at that point. */
1289 ocd_create_inferior (exec_file
, args
, env
)
1294 if (args
&& (*args
!= '\000'))
1295 error ("Args are not supported by BDM.");
1297 clear_proceed_status ();
1298 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1302 ocd_load (args
, from_tty
)
1306 generic_load (args
, from_tty
);
1310 /* This is necessary because many things were based on the PC at the time that
1311 we attached to the monitor, which is no longer valid now that we have loaded
1312 new code (and just changed the PC). Another way to do this might be to call
1313 normal_stop, except that the stack may not be valid, and things would get
1314 horribly confused... */
1316 clear_symtab_users ();
1319 /* This should be defined for each target */
1320 /* But we want to be able to compile this file for some configurations
1321 not yet supported fully */
1323 #define BDM_BREAKPOINT {0x0,0x0,0x0,0x0} /* For ppc 8xx */
1325 #define BDM_BREAKPOINT {0x4a,0xfa} /* BGND insn used for CPU32 */
1328 /* BDM (at least on CPU32) uses a different breakpoint */
1331 ocd_insert_breakpoint (addr
, contents_cache
)
1333 char *contents_cache
;
1335 static char break_insn
[] = BDM_BREAKPOINT
;
1338 val
= target_read_memory (addr
, contents_cache
, sizeof (break_insn
));
1341 val
= target_write_memory (addr
, break_insn
, sizeof (break_insn
));
1347 ocd_remove_breakpoint (addr
, contents_cache
)
1349 char *contents_cache
;
1351 static char break_insn
[] = BDM_BREAKPOINT
;
1354 val
= target_write_memory (addr
, contents_cache
, sizeof (break_insn
));
1360 bdm_command (args
, from_tty
)
1364 error ("bdm command must be followed by `reset'");
1368 bdm_reset_command (args
, from_tty
)
1375 error ("Not connected to OCD device.");
1377 ocd_do_command (OCD_RESET
, &status
, &pktlen
);
1378 dcache_flush (ocd_dcache
);
1379 registers_changed ();
1383 bdm_restart_command (args
, from_tty
)
1390 error ("Not connected to OCD device.");
1392 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
1393 last_run_status
= status
;
1394 clear_proceed_status ();
1395 wait_for_inferior ();
1399 /* Temporary replacement for target_store_registers(). This prevents
1400 generic_load from trying to set the PC. */
1403 noop_store_registers (regno
)
1409 bdm_update_flash_command (args
, from_tty
)
1414 struct cleanup
*old_chain
;
1415 void (*store_registers_tmp
) PARAMS ((int));
1418 error ("Not connected to OCD device.");
1421 error ("Must specify file containing new OCD code.");
1423 /* old_chain = make_cleanup (flash_cleanup, 0); */
1425 ocd_do_command (OCD_ENTER_MON
, &status
, &pktlen
);
1427 ocd_do_command (OCD_ERASE_FLASH
, &status
, &pktlen
);
1429 write_mem_command
= OCD_PROGRAM_FLASH
;
1430 store_registers_tmp
= current_target
.to_store_registers
;
1431 current_target
.to_store_registers
= noop_store_registers
;
1433 generic_load (args
, from_tty
);
1435 current_target
.to_store_registers
= store_registers_tmp
;
1436 write_mem_command
= OCD_WRITE_MEM
;
1438 ocd_do_command (OCD_EXIT_MON
, &status
, &pktlen
);
1440 /* discard_cleanups (old_chain); */
1444 bdm_read_register_command (args
, from_tty
)
1448 /* XXX repeat should go on to the next register */
1451 error ("Not connected to OCD device.");
1454 error ("Must specify BDM register number.");
1459 _initialize_remote_ocd ()
1461 extern struct cmd_list_element
*cmdlist
;
1462 static struct cmd_list_element
*ocd_cmd_list
= NULL
;
1464 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1465 var_integer
, (char *) &remote_timeout
,
1466 "Set timeout value for remote read.\n", &setlist
),
1469 add_prefix_cmd ("ocd", class_obscure
, bdm_command
, "", &ocd_cmd_list
, "ocd ",
1472 add_cmd ("reset", class_obscure
, bdm_reset_command
, "", &ocd_cmd_list
);
1473 add_cmd ("restart", class_obscure
, bdm_restart_command
, "", &ocd_cmd_list
);
1474 add_cmd ("update-flash", class_obscure
, bdm_update_flash_command
, "", &ocd_cmd_list
);
1475 /* add_cmd ("read-register", class_obscure, bdm_read_register_command, "", &ocd_cmd_list); */