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];
38 #ifndef SIMULATOR /* allow non archos platforms to display output */
44 #if CONFIG_CPU == SH7034 /* these are still very SH-oriented */
48 SSR1
&= ~(SCI_RDRF
| SCI_ORER
| SCI_PER
| SCI_FER
);
50 /* This enables the serial Rx interrupt, to be able to exit into the
51 debugger when you hit CTRL-C */
54 IPRE
|= 0xf000; /* Set to highest priority */
57 static int debug_tx_ready(void)
59 return (SSR1
& SCI_TDRE
);
62 static void debug_tx_char(char ch
)
64 while (!debug_tx_ready())
70 * Write data into TDR and clear TDRE
76 static void debug_handle_error(char ssr
)
79 SSR1
&= ~(SCI_ORER
| SCI_PER
| SCI_FER
);
82 static int debug_rx_ready(void)
86 ssr
= SSR1
& ( SCI_PER
| SCI_FER
| SCI_ORER
);
88 debug_handle_error ( ssr
);
89 return SSR1
& SCI_RDRF
;
92 static char debug_rx_char(void)
97 while (!debug_rx_ready())
105 ssr
= SSR1
& (SCI_PER
| SCI_FER
| SCI_ORER
);
108 debug_handle_error (ssr
);
113 static const char hexchars
[] = "0123456789abcdef";
115 static char highhex(int x
)
117 return hexchars
[(x
>> 4) & 0xf];
120 static char lowhex(int x
)
122 return hexchars
[x
& 0xf];
125 static void putpacket (const char *buffer
)
127 register int checksum
;
129 const char *src
= buffer
;
131 /* Special debug hack. Shut off the Rx IRQ during I/O to prevent the debug
132 stub from interrupting the message */
142 /* Do run length encoding */
143 for (runlen
= 0; runlen
< 100; runlen
++)
145 if (src
[0] != src
[runlen
] || runlen
== 99)
150 /* Got a useful amount */
151 debug_tx_char (*src
);
155 checksum
+= (encode
= runlen
+ ' ' - 4);
156 debug_tx_char (encode
);
161 debug_tx_char (*src
);
172 debug_tx_char (highhex(checksum
));
173 debug_tx_char (lowhex(checksum
));
175 /* Wait for the '+' */
178 /* Special debug hack. Enable the IRQ again */
183 /* convert the memory, pointed to by mem into hex, placing result in buf */
184 /* return a pointer to the last char put in buf (null) */
185 static char *mem2hex (const char *mem
, char *buf
, int count
)
189 for (i
= 0; i
< count
; i
++)
192 *buf
++ = highhex (ch
);
193 *buf
++ = lowhex (ch
);
199 static void debug(const char *msg
)
203 mem2hex(msg
, &debugbuf
[1], strlen(msg
));
206 #elif defined(HAVE_GDB_API)
207 static void *get_api_function(int n
)
209 struct gdb_api
*api
= (struct gdb_api
*)GDB_API_ADDRESS
;
210 if (api
->magic
== GDB_API_MAGIC
)
216 void breakpoint(void)
218 void (*f
)(void) = get_api_function(0);
222 static void debug(char *msg
)
224 void (*f
)(char *) = get_api_function(1);
228 void debug_init(void)
231 #else /* !SH7034 && !HAVE_GDB_API */
232 void debug_init(void)
236 static inline void debug(char *msg
)
242 #endif /* end of DEBUG section */
245 void debugf(const char *fmt
, ...)
252 vsnprintf(debugmembuf
, sizeof(debugmembuf
), fmt
, ap
);