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
77 /* Changed to allow option to set timeout value.
78 was static int remote_timeout = 2; */
79 extern int remote_timeout
;
81 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
82 ocd_open knows that we don't have a file open when the program
84 static serial_t ocd_desc
= NULL
;
87 ocd_error (s
, error_code
)
93 fputs_filtered (s
, gdb_stderr
);
94 fputs_filtered (" ", gdb_stderr
);
98 case 0x1: s
= "Unknown fault"; break;
99 case 0x2: s
= "Power failed"; break;
100 case 0x3: s
= "Cable disconnected"; break;
101 case 0x4: s
= "Couldn't enter OCD mode"; break;
102 case 0x5: s
= "Target stuck in reset"; break;
103 case 0x6: s
= "OCD hasn't been initialized"; break;
104 case 0x7: s
= "Write verify failed"; break;
105 case 0x8: s
= "Reg buff error (during MPC5xx fp reg read/write)"; break;
106 case 0x9: s
= "Invalid CPU register access attempt failed"; break;
107 case 0x11: s
= "Bus error"; break;
108 case 0x12: s
= "Checksum error"; break;
109 case 0x13: s
= "Illegal command"; break;
110 case 0x14: s
= "Parameter error"; break;
111 case 0x15: s
= "Internal error"; break;
112 case 0x80: s
= "Flash erase error"; break;
114 sprintf (buf
, "Unknown error code %d", error_code
);
121 /* Return nonzero if the thread TH is still alive on the remote system. */
124 ocd_thread_alive (th
)
130 /* Clean up connection to a remote debugger. */
138 SERIAL_CLOSE (ocd_desc
);
142 /* Stub for catch_errors. */
145 ocd_start_remote (dummy
)
148 unsigned char buf
[10], *p
;
153 enum ocd_target_type target_type
;
155 target_type
= (enum ocd_target_type
)dummy
;
157 immediate_quit
= 1; /* Allow user to interrupt it */
159 SERIAL_SEND_BREAK (ocd_desc
); /* Wake up the wiggler */
161 speed
= 80; /* Divide clock by 4000 */
165 buf
[2] = speed
& 0xff;
166 buf
[3] = target_type
;
167 ocd_put_packet (buf
, 4); /* Init OCD params */
168 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
171 error ("Truncated response packet from OCD device");
177 ocd_error ("OCD_INIT:", error_code
);
179 ocd_do_command (OCD_AYT
, &status
, &pktlen
);
181 p
= ocd_do_command (OCD_GET_VERSION
, &status
, &pktlen
);
183 printf_unfiltered ("[Wiggler version %x.%x, capability 0x%x]\n",
184 p
[0], p
[1], (p
[2] << 16) | p
[3]);
187 /* Reset the target */
189 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
190 /* ocd_do_command (OCD_RESET, &status, &pktlen);*/
193 /* If processor is still running, stop it. */
195 if (!(status
& OCD_FLAG_BDM
))
199 buf
[0] = OCD_SET_CTL_FLAGS
;
201 buf
[2] = 1; /* Asynchronously return status when target stops */
202 ocd_put_packet (buf
, 3);
204 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
207 error ("Truncated response packet from OCD device");
213 ocd_error ("OCD_SET_CTL_FLAGS:", error_code
);
218 /* This is really the job of start_remote however, that makes an assumption
219 that the target is about to print out a status message of some sort. That
220 doesn't happen here (in fact, it may not be possible to get the monitor to
221 send the appropriate packet). */
223 flush_cached_frames ();
224 registers_changed ();
225 stop_pc
= read_pc ();
226 set_current_frame (create_new_frame (read_fp (), stop_pc
));
227 select_frame (get_current_frame (), 0);
228 print_stack_frame (selected_frame
, -1, 1);
230 buf
[0] = OCD_LOG_FILE
;
231 buf
[1] = 3; /* close existing WIGGLERS.LOG */
232 ocd_put_packet (buf
, 2);
233 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
235 buf
[0] = OCD_LOG_FILE
;
236 buf
[1] = 2; /* append to existing WIGGLERS.LOG */
237 ocd_put_packet (buf
, 2);
238 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
243 /* Open a connection to a remote debugger.
244 NAME is the filename used for communication. */
246 static DCACHE
*ocd_dcache
;
249 ocd_open (name
, from_tty
, target_type
, ops
)
252 enum ocd_target_type target_type
;
253 struct target_ops
*ops
;
255 unsigned char buf
[10], *p
;
260 error ("To open an OCD connection, you need to specify the\n\
261 device the OCD device is attached to (e.g. /dev/ttya).");
263 target_preopen (from_tty
);
267 unpush_target (current_ops
);
269 ocd_dcache
= dcache_init (ocd_read_bytes
, ocd_write_bytes
);
271 if (strncmp(name
,"wiggler",7) == 0)
273 ocd_desc
= SERIAL_OPEN ("ocd");
275 perror_with_name (name
);
277 buf
[0] = OCD_LOG_FILE
;
278 buf
[1] = 1; /* open new or overwrite existing WIGGLERS.LOG */
279 ocd_put_packet (buf
, 2);
280 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
282 buf
[0] = OCD_SET_CONNECTION
;
283 buf
[1] = 0x01; /* atoi (name[11]); */
284 ocd_put_packet (buf
, 2);
285 p
= ocd_get_packet (buf
[0], &pktlen
, remote_timeout
);
287 else /* not using Wigglers.dll */
289 ocd_desc
= SERIAL_OPEN (name
);
291 perror_with_name (name
);
296 if (SERIAL_SETBAUDRATE (ocd_desc
, baud_rate
))
298 SERIAL_CLOSE (ocd_desc
);
299 perror_with_name (name
);
303 SERIAL_RAW (ocd_desc
);
305 /* If there is something sitting in the buffer we might take it as a
306 response to a command, which would be bad. */
307 SERIAL_FLUSH_INPUT (ocd_desc
);
311 puts_filtered ("Remote target wiggler connected to ");
312 puts_filtered (name
);
313 puts_filtered ("\n");
315 push_target (current_ops
); /* Switch to using remote target now */
317 /* Without this, some commands which require an active target (such as kill)
318 won't work. This variable serves (at least) double duty as both the pid
319 of the target process (if it has such), and as a flag indicating that a
320 target is active. These functions should be split out into seperate
321 variables, especially since GDB will someday have a notion of debugging
322 several processes. */
324 inferior_pid
= 42000;
325 /* Start the remote connection; if error (0), discard this target.
326 In particular, if the user quits, be sure to discard it
327 (we'd be in an inconsistent state otherwise). */
328 if (!catch_errors (ocd_start_remote
, (char *)target_type
,
329 "Couldn't establish connection to remote target\n",
334 /* This takes a program previously attached to and detaches it. After
335 this is done, GDB can be used to debug some other program. We
336 better not have left any breakpoints in the target program or it'll
337 die when it hits one. */
340 ocd_detach (args
, from_tty
)
345 error ("Argument given to \"detach\" when remotely debugging.");
349 puts_filtered ("Ending remote debugging.\n");
352 /* Tell the remote machine to resume. */
355 ocd_resume (pid
, step
, siggnal
)
357 enum target_signal siggnal
;
361 dcache_flush (ocd_dcache
);
364 ocd_do_command (OCD_STEP
, &last_run_status
, &pktlen
);
366 ocd_do_command (OCD_RUN
, &last_run_status
, &pktlen
);
375 ocd_do_command (OCD_STOP
, &status
, &pktlen
);
377 if (!(status
& OCD_FLAG_BDM
))
378 error ("Can't stop target via BDM");
381 static volatile int ocd_interrupt_flag
;
383 /* Send ^C to target to halt it. Target will respond, and send us a
387 ocd_interrupt (signo
)
390 /* If this doesn't work, try more severe steps. */
391 signal (signo
, ocd_interrupt_twice
);
394 printf_unfiltered ("ocd_interrupt called\n");
401 ocd_put_packet (buf
, 1);
402 ocd_interrupt_flag
= 1;
406 static void (*ofunc
)();
408 /* The user typed ^C twice. */
410 ocd_interrupt_twice (signo
)
413 signal (signo
, ofunc
);
417 signal (signo
, ocd_interrupt
);
420 /* Ask the user what to do when an interrupt is received. */
425 target_terminal_ours ();
427 if (query ("Interrupted while waiting for the program.\n\
428 Give up (and stop debugging it)? "))
430 target_mourn_inferior ();
431 return_to_top_level (RETURN_QUIT
);
434 target_terminal_inferior ();
437 /* If nonzero, ignore the next kill. */
438 static int kill_kludge
;
440 /* Wait until the remote machine stops, then return,
441 storing status in STATUS just as `wait' would.
442 Returns "pid" (though it's not clear what, if anything, that
443 means in the case of this target). */
449 int error_code
, status
;
452 ocd_interrupt_flag
= 0;
454 /* Target may already be stopped by the time we get here. */
456 /* if (!(last_run_status & OCD_FLAG_BDM)) */
458 /* Loop until we've dropped back into BDM mode */
459 while (!(last_run_status
& OCD_FLAG_BDM
))
461 ofunc
= (void (*)()) signal (SIGINT
, ocd_interrupt
);
463 p
= ocd_get_packet (OCD_AYT
, &pktlen
, -1);
465 signal (SIGINT
, ofunc
);
468 error ("Truncated response packet from OCD device");
470 last_run_status
= p
[1];
474 ocd_error ("target_wait:", error_code
);
476 if (last_run_status
& OCD_FLAG_PWF
)
477 error ("OCD device lost VCC at BDM interface.");
478 else if (last_run_status
& OCD_FLAG_CABLE_DISC
)
479 error ("OCD device cable appears to have been disconnected.");
481 if (!(last_run_status
& OCD_FLAG_BDM
))
482 error ("OCD device woke up, but wasn't stopped: 0x%x", status
);
486 if (ocd_interrupt_flag
)
492 /* Read registers from the OCD device. Specify the starting and ending
493 register number. Return the number of regs actually read in *NUMREGS.
494 Returns a pointer to a static array containing the register contents. */
497 ocd_read_bdm_registers (first_bdm_regno
, last_bdm_regno
, reglen
)
502 unsigned char buf
[10];
506 int error_code
, status
;
509 buf
[0] = OCD_READ_REGS
;
510 buf
[1] = first_bdm_regno
>> 8;
511 buf
[2] = first_bdm_regno
& 0xff;
512 buf
[3] = last_bdm_regno
>> 8;
513 buf
[4] = last_bdm_regno
& 0xff;
515 ocd_put_packet (buf
, 5);
516 p
= ocd_get_packet (OCD_READ_REGS
, &pktlen
, remote_timeout
);
522 ocd_error ("read_bdm_registers:", error_code
);
530 error ("Register block size bad: %d", i
);
539 /* Read register BDM_REGNO and returns its value ala read_register() */
542 ocd_read_bdm_register (bdm_regno
)
549 p
= ocd_read_bdm_registers (bdm_regno
, bdm_regno
, ®len
);
550 regval
= extract_unsigned_integer (p
, reglen
);
556 ocd_write_bdm_registers (first_bdm_regno
, regptr
, reglen
)
558 unsigned char *regptr
;
563 int error_code
, status
;
566 buf
= alloca (4 + reglen
);
568 buf
[0] = OCD_WRITE_REGS
;
569 buf
[1] = first_bdm_regno
>> 8;
570 buf
[2] = first_bdm_regno
& 0xff;
572 memcpy (buf
+ 4, regptr
, reglen
);
574 ocd_put_packet (buf
, 4 + reglen
);
575 p
= ocd_get_packet (OCD_WRITE_REGS
, &pktlen
, remote_timeout
);
578 error ("Truncated response packet from OCD device");
584 ocd_error ("ocd_write_bdm_registers:", error_code
);
588 ocd_write_bdm_register (bdm_regno
, reg
)
592 unsigned char buf
[4];
594 store_unsigned_integer (buf
, 4, reg
);
596 ocd_write_bdm_registers (bdm_regno
, buf
, 4);
600 ocd_prepare_to_store ()
604 /* Write memory data directly to the remote machine.
605 This does not inform the data cache; the data cache uses this.
606 MEMADDR is the address in the remote memory space.
607 MYADDR is the address of the buffer in our space.
608 LEN is the number of bytes.
610 Returns number of bytes transferred, or 0 for error. */
612 static int write_mem_command
= OCD_WRITE_MEM
;
615 ocd_write_bytes (memaddr
, myaddr
, len
)
626 buf
[0] = write_mem_command
;
627 buf
[5] = 1; /* Write as bytes */
628 buf
[6] = 0; /* Don't verify */
634 int status
, error_code
;
636 numbytes
= min (len
, 256 - 8);
638 buf
[1] = memaddr
>> 24;
639 buf
[2] = memaddr
>> 16;
640 buf
[3] = memaddr
>> 8;
645 memcpy (&buf
[8], myaddr
, numbytes
);
646 ocd_put_packet (buf
, 8 + numbytes
);
647 p
= ocd_get_packet (OCD_WRITE_MEM
, &pktlen
, remote_timeout
);
649 error ("Truncated response packet from OCD device");
654 if (error_code
== 0x11) /* Got a bus error? */
656 CORE_ADDR error_address
;
658 error_address
= p
[3] << 24;
659 error_address
|= p
[4] << 16;
660 error_address
|= p
[5] << 8;
661 error_address
|= p
[6];
662 numbytes
= error_address
- memaddr
;
670 else if (error_code
!= 0)
671 ocd_error ("ocd_write_bytes:", error_code
);
678 return origlen
- len
;
681 /* Read memory data directly from the remote machine.
682 This does not use the data cache; the data cache uses this.
683 MEMADDR is the address in the remote memory space.
684 MYADDR is the address of the buffer in our space.
685 LEN is the number of bytes.
687 Returns number of bytes transferred, or 0 for error. */
690 ocd_read_bytes (memaddr
, myaddr
, len
)
701 buf
[0] = OCD_READ_MEM
;
702 buf
[5] = 1; /* Read as bytes */
708 int status
, error_code
;
710 numbytes
= min (len
, 256 - 7);
712 buf
[1] = memaddr
>> 24;
713 buf
[2] = memaddr
>> 16;
714 buf
[3] = memaddr
>> 8;
719 ocd_put_packet (buf
, 7);
720 p
= ocd_get_packet (OCD_READ_MEM
, &pktlen
, remote_timeout
);
722 error ("Truncated response packet from OCD device");
727 if (error_code
== 0x11) /* Got a bus error? */
729 CORE_ADDR error_address
;
731 error_address
= p
[3] << 24;
732 error_address
|= p
[4] << 16;
733 error_address
|= p
[5] << 8;
734 error_address
|= p
[6];
735 numbytes
= error_address
- memaddr
;
743 else if (error_code
!= 0)
744 ocd_error ("ocd_read_bytes:", error_code
);
746 memcpy (myaddr
, &p
[4], numbytes
);
753 return origlen
- len
;
756 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
757 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
758 nonzero. Returns length of data written or read; 0 for error. */
762 ocd_xfer_memory (memaddr
, myaddr
, len
, should_write
, target
)
767 struct target_ops
*target
; /* ignored */
769 return dcache_xfer_memory (ocd_dcache
, memaddr
, myaddr
, len
, should_write
);
773 ocd_files_info (ignore
)
774 struct target_ops
*ignore
;
776 puts_filtered ("Debugging a target over a serial line.\n");
779 /* Stuff for dealing with the packets which are part of this protocol.
780 See comment at top of file for details. */
782 /* Read a single character from the remote side, handling wierd errors. */
790 ch
= SERIAL_READCHAR (ocd_desc
, timeout
);
795 error ("Remote connection closed");
797 perror_with_name ("Remote communication error");
805 /* Read a character from the data stream, dequoting as necessary. SYN is
806 treated special. Any SYNs appearing in the data stream are returned as the
807 distinct value RAW_SYN (which has a value > 8 bits and therefore cannot be
808 mistaken for real data). */
811 get_quoted_char (timeout
)
816 ch
= readchar (timeout
);
821 error ("Timeout in mid-packet, aborting");
825 ch
= readchar (timeout
);
834 static unsigned char pkt
[256 * 2 + 10], *pktp
; /* Worst case */
845 if (SERIAL_WRITE (ocd_desc
, pkt
, pktp
- pkt
))
846 perror_with_name ("output_packet: write failed");
851 /* Output a quoted character. SYNs and DLEs are quoted. Everything else goes
852 through untouched. */
869 /* Send a packet to the OCD device. The packet framed by a SYN character,
870 a byte count and a checksum. The byte count only counts the number of
871 bytes between the count and the checksum. A count of zero actually
872 means 256. Any SYNs within the packet (including the checksum and
873 count) must be quoted. The quote character must be quoted as well.
874 Quoting is done by replacing the character with the two-character sequence
875 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
879 stu_put_packet (buf
, len
)
883 unsigned char checksum
;
886 if (len
== 0 || len
> 256)
887 abort (); /* Can't represent 0 length packet */
893 put_quoted_char (RAW_SYN
);
907 put_quoted_char (-checksum
& 0xff);
914 /* Send a packet to the OCD device. The packet framed by a SYN character,
915 a byte count and a checksum. The byte count only counts the number of
916 bytes between the count and the checksum. A count of zero actually
917 means 256. Any SYNs within the packet (including the checksum and
918 count) must be quoted. The quote character must be quoted as well.
919 Quoting is done by replacing the character with the two-character sequence
920 DLE, {char} | 0100. Note that the quoting mechanism has no effect on the
924 ocd_put_packet (buf
, len
)
928 unsigned char checksum
;
930 unsigned char *packet
, *packet_ptr
;
932 packet
= alloca (len
+ 1 + 1); /* packet + SYN + checksum */
937 *packet_ptr
++ = 0x55;
947 *packet_ptr
++ = -checksum
;
948 if (SERIAL_WRITE (ocd_desc
, packet
, packet_ptr
- packet
))
949 perror_with_name ("output_packet: write failed");
954 /* Get a packet from the OCD device. Timeout is only enforced for the
955 first byte of the packet. Subsequent bytes are expected to arrive in
956 time <= remote_timeout. Returns a pointer to a static buffer containing
957 the payload of the packet. *LENP contains the length of the packet.
960 static unsigned char *
961 stu_get_packet (cmd
, lenp
, timeout
)
967 static unsigned char buf
[256 + 10], *p
;
968 unsigned char checksum
;
972 ch
= get_quoted_char (timeout
);
975 error ("get_packet (readchar): %d", ch
);
980 found_syn
: /* Found the start of a packet */
985 len
= get_quoted_char (remote_timeout
);
995 len
++; /* Include checksum */
999 ch
= get_quoted_char (remote_timeout
);
1011 error ("Response phase error. Got 0x%x, expected 0x%x", buf
[0], cmd
);
1013 *lenp
= p
- buf
- 1;
1019 /* Get a packet from the OCD device. Timeout is only enforced for the
1020 first byte of the packet. Subsequent bytes are expected to arrive in
1021 time <= remote_timeout. Returns a pointer to a static buffer containing
1022 the payload of the packet. *LENP contains the length of the packet.
1025 static unsigned char *
1026 ocd_get_packet (cmd
, lenp
, timeout
)
1033 static unsigned char packet
[512];
1034 unsigned char *packet_ptr
;
1035 unsigned char checksum
;
1039 ch
= readchar (timeout
);
1042 error ("ocd_get_packet (readchar): %d", ch
);
1047 /* Found the start of a packet */
1049 packet_ptr
= packet
;
1052 /* Read command char. That sort of tells us how long the packet is. */
1054 ch
= readchar (timeout
);
1057 error ("ocd_get_packet (readchar): %d", ch
);
1064 ch
= readchar (timeout
);
1067 error ("ocd_get_packet (readchar): %d", ch
);
1071 /* Get error code. */
1073 ch
= readchar (timeout
);
1076 error ("ocd_get_packet (readchar): %d", ch
);
1080 switch (ch
) /* Figure out length of packet */
1082 case 0x7: /* Write verify error? */
1083 len
= 8; /* write address, value read back */
1085 case 0x11: /* Bus error? */
1086 /* write address, read flag */
1087 case 0x15: /* Internal error */
1088 len
= 5; /* error code, vector */
1090 default: /* Error w/no params */
1093 case 0x0: /* Normal result */
1096 case OCD_AYT
: /* Are You There? */
1097 case OCD_SET_BAUD_RATE
: /* Set Baud Rate */
1098 case OCD_INIT
: /* Initialize OCD device */
1099 case OCD_SET_SPEED
: /* Set Speed */
1100 case OCD_SET_FUNC_CODE
: /* Set Function Code */
1101 case OCD_SET_CTL_FLAGS
: /* Set Control Flags */
1102 case OCD_SET_BUF_ADDR
: /* Set Register Buffer Address */
1103 case OCD_RUN
: /* Run Target from PC */
1104 case OCD_RUN_ADDR
: /* Run Target from Specified Address */
1105 case OCD_STOP
: /* Stop Target */
1106 case OCD_RESET_RUN
: /* Reset Target and Run */
1107 case OCD_RESET
: /* Reset Target and Halt */
1108 case OCD_STEP
: /* Single Step */
1109 case OCD_WRITE_REGS
: /* Write Register */
1110 case OCD_WRITE_MEM
: /* Write Memory */
1111 case OCD_FILL_MEM
: /* Fill Memory */
1112 case OCD_MOVE_MEM
: /* Move Memory */
1113 case OCD_WRITE_INT_MEM
: /* Write Internal Memory */
1114 case OCD_JUMP
: /* Jump to Subroutine */
1115 case OCD_ERASE_FLASH
: /* Erase flash memory */
1116 case OCD_PROGRAM_FLASH
: /* Write flash memory */
1117 case OCD_EXIT_MON
: /* Exit the flash programming monitor */
1118 case OCD_ENTER_MON
: /* Enter the flash programming monitor */
1119 case OCD_LOG_FILE
: /* Make Wigglers.dll save Wigglers.log */
1120 case OCD_SET_CONNECTION
: /* Set type of connection in Wigglers.dll */
1123 case OCD_GET_VERSION
: /* Get Version */
1126 case OCD_GET_STATUS_MASK
: /* Get Status Mask */
1129 case OCD_GET_CTRS
: /* Get Error Counters */
1130 case OCD_READ_REGS
: /* Read Register */
1131 case OCD_READ_MEM
: /* Read Memory */
1132 case OCD_READ_INT_MEM
: /* Read Internal Memory */
1136 fprintf_filtered (gdb_stderr
, "Unknown packet type 0x%x\n", ch
);
1141 if (len
== 257) /* Byte stream? */
1142 { /* Yes, byte streams contain the length */
1143 ch
= readchar (timeout
);
1146 error ("ocd_get_packet (readchar): %d", ch
);
1154 while (len
-- >= 0) /* Do rest of packet and checksum */
1156 ch
= readchar (timeout
);
1159 error ("ocd_get_packet (readchar): %d", ch
);
1167 if (cmd
!= -1 && cmd
!= packet
[0])
1168 error ("Response phase error. Got 0x%x, expected 0x%x", packet
[0], cmd
);
1170 *lenp
= packet_ptr
- packet
- 1; /* Subtract checksum byte */
1175 /* Execute a simple (one-byte) command. Returns a pointer to the data
1176 following the error code. */
1178 static unsigned char *
1179 ocd_do_command (cmd
, statusp
, lenp
)
1184 unsigned char buf
[100], *p
;
1185 int status
, error_code
;
1188 unsigned char logbuf
[100];
1192 ocd_put_packet (buf
, 1); /* Send command */
1193 p
= ocd_get_packet (*buf
, lenp
, remote_timeout
);
1196 error ("Truncated response packet from OCD device");
1201 if (error_code
!= 0)
1203 sprintf (errbuf
, "ocd_do_command (0x%x):", cmd
);
1204 ocd_error (errbuf
, error_code
);
1207 if (status
& OCD_FLAG_PWF
)
1208 error ("OCD device can't detect VCC at BDM interface.");
1209 else if (status
& OCD_FLAG_CABLE_DISC
)
1210 error ("BDM cable appears to be disconnected.");
1214 logbuf
[0] = OCD_LOG_FILE
;
1215 logbuf
[1] = 3; /* close existing WIGGLERS.LOG */
1216 ocd_put_packet (logbuf
, 2);
1217 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1219 logbuf
[0] = OCD_LOG_FILE
;
1220 logbuf
[1] = 2; /* append to existing WIGGLERS.LOG */
1221 ocd_put_packet (logbuf
, 2);
1222 ocd_get_packet (logbuf
[0], &logpktlen
, remote_timeout
);
1230 /* For some mysterious reason, wait_for_inferior calls kill instead of
1231 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1235 target_mourn_inferior ();
1239 /* Don't wait for it to die. I'm not really sure it matters whether
1241 target_mourn_inferior ();
1247 unpush_target (current_ops
);
1248 generic_mourn_inferior ();
1251 /* All we actually do is set the PC to the start address of exec_bfd, and start
1252 the program at that point. */
1255 ocd_create_inferior (exec_file
, args
, env
)
1260 if (args
&& (*args
!= '\000'))
1261 error ("Args are not supported by BDM.");
1263 clear_proceed_status ();
1264 proceed (bfd_get_start_address (exec_bfd
), TARGET_SIGNAL_0
, 0);
1268 ocd_load (args
, from_tty
)
1272 generic_load (args
, from_tty
);
1276 /* This is necessary because many things were based on the PC at the time that
1277 we attached to the monitor, which is no longer valid now that we have loaded
1278 new code (and just changed the PC). Another way to do this might be to call
1279 normal_stop, except that the stack may not be valid, and things would get
1280 horribly confused... */
1282 clear_symtab_users ();
1285 /* This should be defined for each target */
1286 /* But we want to be able to compile this file for some configurations
1287 not yet supported fully */
1289 #define BDM_BREAKPOINT {0x0,0x0,0x0,0x0} /* For ppc 8xx */
1290 /* #define BDM_BREAKPOINT {0x4a,0xfa} /* BGND insn used for CPU32 */
1292 /* BDM (at least on CPU32) uses a different breakpoint */
1295 ocd_insert_breakpoint (addr
, contents_cache
)
1297 char *contents_cache
;
1299 static char break_insn
[] = BDM_BREAKPOINT
;
1302 val
= target_read_memory (addr
, contents_cache
, sizeof (break_insn
));
1305 val
= target_write_memory (addr
, break_insn
, sizeof (break_insn
));
1311 ocd_remove_breakpoint (addr
, contents_cache
)
1313 char *contents_cache
;
1315 static char break_insn
[] = BDM_BREAKPOINT
;
1318 val
= target_write_memory (addr
, contents_cache
, sizeof (break_insn
));
1324 bdm_command (args
, from_tty
)
1328 error ("bdm command must be followed by `reset'");
1332 bdm_reset_command (args
, from_tty
)
1339 error ("Not connected to OCD device.");
1341 ocd_do_command (OCD_RESET
, &status
, &pktlen
);
1342 dcache_flush (ocd_dcache
);
1343 registers_changed ();
1347 bdm_restart_command (args
, from_tty
)
1354 error ("Not connected to OCD device.");
1356 ocd_do_command (OCD_RESET_RUN
, &status
, &pktlen
);
1357 last_run_status
= status
;
1358 clear_proceed_status ();
1359 wait_for_inferior ();
1363 /* Temporary replacement for target_store_registers(). This prevents
1364 generic_load from trying to set the PC. */
1367 noop_store_registers (regno
)
1373 bdm_update_flash_command (args
, from_tty
)
1378 struct cleanup
*old_chain
;
1379 void (*store_registers_tmp
) PARAMS ((int));
1382 error ("Not connected to OCD device.");
1385 error ("Must specify file containing new OCD code.");
1387 /* old_chain = make_cleanup (flash_cleanup, 0);*/
1389 ocd_do_command (OCD_ENTER_MON
, &status
, &pktlen
);
1391 ocd_do_command (OCD_ERASE_FLASH
, &status
, &pktlen
);
1393 write_mem_command
= OCD_PROGRAM_FLASH
;
1394 store_registers_tmp
= current_target
.to_store_registers
;
1395 current_target
.to_store_registers
= noop_store_registers
;
1397 generic_load (args
, from_tty
);
1399 current_target
.to_store_registers
= store_registers_tmp
;
1400 write_mem_command
= OCD_WRITE_MEM
;
1402 ocd_do_command (OCD_EXIT_MON
, &status
, &pktlen
);
1404 /* discard_cleanups (old_chain);*/
1408 bdm_read_register_command (args
, from_tty
)
1412 /* XXX repeat should go on to the next register */
1415 error ("Not connected to OCD device.");
1418 error ("Must specify BDM register number.");
1423 _initialize_remote_ocd ()
1425 extern struct cmd_list_element
*cmdlist
;
1426 static struct cmd_list_element
*ocd_cmd_list
= NULL
;
1428 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1429 var_integer
, (char *)&remote_timeout
,
1430 "Set timeout value for remote read.\n", &setlist
),
1433 add_prefix_cmd ("ocd", class_obscure
, bdm_command
, "", &ocd_cmd_list
, "ocd ",
1436 add_cmd ("reset", class_obscure
, bdm_reset_command
, "", &ocd_cmd_list
);
1437 add_cmd ("restart", class_obscure
, bdm_restart_command
, "", &ocd_cmd_list
);
1438 add_cmd ("update-flash", class_obscure
, bdm_update_flash_command
, "", &ocd_cmd_list
);
1439 /* add_cmd ("read-register", class_obscure, bdm_read_register_command, "", &ocd_cmd_list);*/