Add a get_offset API call to make codec_advance_buffer_loc_callback work.
[Rockbox.git] / firmware / debug.c
blob6b638c18c2bd65ab7825baa55a5f706a8406ef27
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include "config.h"
24 #include "cpu.h"
25 #ifdef HAVE_GDB_API
26 #include "gdb_api.h"
27 #endif
29 #ifdef DEBUG
30 static char debugmembuf[200];
31 #if CONFIG_CPU == SH7034
32 static char debugbuf[400];
33 #endif
34 #endif
36 #ifndef SIMULATOR /* allow non archos platforms to display output */
37 #include "kernel.h"
38 #include "system.h"
40 #ifdef DEBUG
41 #if CONFIG_CPU == SH7034 /* these are still very SH-oriented */
42 void debug_init(void)
44 /* Clear it all! */
45 SSR1 &= ~(SCI_RDRF | SCI_ORER | SCI_PER | SCI_FER);
47 /* This enables the serial Rx interrupt, to be able to exit into the
48 debugger when you hit CTRL-C */
49 SCR1 |= 0x40;
50 SCR1 &= ~0x80;
51 IPRE |= 0xf000; /* Set to highest priority */
54 static int debug_tx_ready(void)
56 return (SSR1 & SCI_TDRE);
59 static void debug_tx_char(char ch)
61 while (!debug_tx_ready())
67 * Write data into TDR and clear TDRE
69 TDR1 = ch;
70 SSR1 &= ~SCI_TDRE;
73 static void debug_handle_error(char ssr)
75 (void)ssr;
76 SSR1 &= ~(SCI_ORER | SCI_PER | SCI_FER);
79 static int debug_rx_ready(void)
81 char ssr;
83 ssr = SSR1 & ( SCI_PER | SCI_FER | SCI_ORER );
84 if ( ssr )
85 debug_handle_error ( ssr );
86 return SSR1 & SCI_RDRF;
89 static char debug_rx_char(void)
91 char ch;
92 char ssr;
94 while (!debug_rx_ready())
99 ch = RDR1;
100 SSR1 &= ~SCI_RDRF;
102 ssr = SSR1 & (SCI_PER | SCI_FER | SCI_ORER);
104 if (ssr)
105 debug_handle_error (ssr);
107 return ch;
110 static const char hexchars[] = "0123456789abcdef";
112 static char highhex(int x)
114 return hexchars[(x >> 4) & 0xf];
117 static char lowhex(int x)
119 return hexchars[x & 0xf];
122 static void putpacket (const char *buffer)
124 register int checksum;
126 const char *src = buffer;
128 /* Special debug hack. Shut off the Rx IRQ during I/O to prevent the debug
129 stub from interrupting the message */
130 SCR1 &= ~0x40;
132 debug_tx_char ('$');
133 checksum = 0;
135 while (*src)
137 int runlen;
139 /* Do run length encoding */
140 for (runlen = 0; runlen < 100; runlen ++)
142 if (src[0] != src[runlen] || runlen == 99)
144 if (runlen > 3)
146 int encode;
147 /* Got a useful amount */
148 debug_tx_char (*src);
149 checksum += *src;
150 debug_tx_char ('*');
151 checksum += '*';
152 checksum += (encode = runlen + ' ' - 4);
153 debug_tx_char (encode);
154 src += runlen;
156 else
158 debug_tx_char (*src);
159 checksum += *src;
160 src++;
162 break;
168 debug_tx_char ('#');
169 debug_tx_char (highhex(checksum));
170 debug_tx_char (lowhex(checksum));
172 /* Wait for the '+' */
173 debug_rx_char();
175 /* Special debug hack. Enable the IRQ again */
176 SCR1 |= 0x40;
180 /* convert the memory, pointed to by mem into hex, placing result in buf */
181 /* return a pointer to the last char put in buf (null) */
182 static char *mem2hex (const char *mem, char *buf, int count)
184 int i;
185 int ch;
186 for (i = 0; i < count; i++)
188 ch = *mem++;
189 *buf++ = highhex (ch);
190 *buf++ = lowhex (ch);
192 *buf = 0;
193 return (buf);
196 static void debug(const char *msg)
198 debugbuf[0] = 'O';
200 mem2hex(msg, &debugbuf[1], strlen(msg));
201 putpacket(debugbuf);
203 #endif /* SH7034 */
205 #ifdef 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)
210 return api->func[n];
211 else
212 return NULL;
215 void breakpoint(void)
217 void (*f)(void) = get_api_function(0);
218 if (f) (*f)();
221 static void debug(char *msg)
223 void (*f)(char *) = get_api_function(1);
224 if (f) (*f)(msg);
227 void debug_init(void)
231 #endif /* HAVE_GDB_API */
232 #endif /* end of DEBUG section */
234 #ifdef __GNUC__
235 void debugf(const char *fmt, ...)
236 #endif
238 #ifdef DEBUG
239 va_list ap;
241 va_start(ap, fmt);
242 vsnprintf(debugmembuf, sizeof(debugmembuf), fmt, ap);
243 va_end(ap);
244 debug(debugmembuf);
245 #else
246 (void)fmt;
247 #endif
250 #endif