1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
32 static char debugmembuf
[200];
33 #if CONFIG_CPU == SH7034
34 static char debugbuf
[400];
43 #if CONFIG_CPU == SH7034 /* these are still very SH-oriented */
47 SSR1
&= ~(SCI_RDRF
| SCI_ORER
| SCI_PER
| SCI_FER
);
49 /* This enables the serial Rx interrupt, to be able to exit into the
50 debugger when you hit CTRL-C */
53 IPRE
|= 0xf000; /* Set to highest priority */
56 static int debug_tx_ready(void)
58 return (SSR1
& SCI_TDRE
);
61 static void debug_tx_char(char ch
)
63 while (!debug_tx_ready())
69 * Write data into TDR and clear TDRE
75 static void debug_handle_error(char ssr
)
78 SSR1
&= ~(SCI_ORER
| SCI_PER
| SCI_FER
);
81 static int debug_rx_ready(void)
85 ssr
= SSR1
& ( SCI_PER
| SCI_FER
| SCI_ORER
);
87 debug_handle_error ( ssr
);
88 return SSR1
& SCI_RDRF
;
91 static char debug_rx_char(void)
96 while (!debug_rx_ready())
104 ssr
= SSR1
& (SCI_PER
| SCI_FER
| SCI_ORER
);
107 debug_handle_error (ssr
);
112 static const char hexchars
[] = "0123456789abcdef";
114 static char highhex(int x
)
116 return hexchars
[(x
>> 4) & 0xf];
119 static char lowhex(int x
)
121 return hexchars
[x
& 0xf];
124 static void putpacket (const char *buffer
)
126 register int checksum
;
128 const char *src
= buffer
;
130 /* Special debug hack. Shut off the Rx IRQ during I/O to prevent the debug
131 stub from interrupting the message */
141 /* Do run length encoding */
142 for (runlen
= 0; runlen
< 100; runlen
++)
144 if (src
[0] != src
[runlen
] || runlen
== 99)
149 /* Got a useful amount */
150 debug_tx_char (*src
);
154 checksum
+= (encode
= runlen
+ ' ' - 4);
155 debug_tx_char (encode
);
160 debug_tx_char (*src
);
171 debug_tx_char (highhex(checksum
));
172 debug_tx_char (lowhex(checksum
));
174 /* Wait for the '+' */
177 /* Special debug hack. Enable the IRQ again */
182 /* convert the memory, pointed to by mem into hex, placing result in buf */
183 /* return a pointer to the last char put in buf (null) */
184 static char *mem2hex (const char *mem
, char *buf
, int count
)
188 for (i
= 0; i
< count
; i
++)
191 *buf
++ = highhex (ch
);
192 *buf
++ = lowhex (ch
);
198 static void debug(const char *msg
)
202 mem2hex(msg
, &debugbuf
[1], strlen(msg
));
205 #elif defined(HAVE_GDB_API)
206 static void *get_api_function(int n
)
208 struct gdb_api
*api
= (struct gdb_api
*)GDB_API_ADDRESS
;
209 if (api
->magic
== GDB_API_MAGIC
)
215 void breakpoint(void)
217 void (*f
)(void) = get_api_function(0);
221 static void debug(char *msg
)
223 void (*f
)(char *) = get_api_function(1);
227 void debug_init(void)
230 #else /* !SH7034 && !HAVE_GDB_API */
231 void debug_init(void)
235 static inline void debug(char *msg
)
241 #endif /* end of DEBUG section */
244 void debugf(const char *fmt
, ...)
251 vsnprintf(debugmembuf
, sizeof(debugmembuf
), fmt
, ap
);