1 //-------------------------------------------------------------------
5 //-------------------------------------------------------------------
7 // Copyright (C) 2003, MLB Associates.
9 // Routine to download code into main [external] SRAM and then
12 //-------------------------------------------------------------------
14 //#define DEBUG_DOWNLOAD
16 extern void tty_init(void);
17 extern unsigned char tty_getc(int chan);
18 extern void tty_putc(int chan, char c);
19 extern void tty_puts(int chan, char *c);
20 extern void tty_puthex(int chan, unsigned long val, int width);
26 // Validate a hex character
27 __inline__ static bool
30 return (((c >= '0') && (c <= '9')) ||
31 ((c >= 'A') && (c <= 'F')) ||
32 ((c >= 'a') && (c <= 'f')));
35 // Convert a single hex nibble
41 if ((c >= '0') && (c <= '9')) {
43 } else if ((c >= 'a') && (c <= 'f')) {
44 ret = (c - 'a' + 0x0a);
45 } else if ((c >= 'A') && (c <= 'F')) {
46 ret = (c - 'A' + 0x0A);
53 // Process the next hex value from a string
59 unsigned char *cp = *_cp;
62 while (_is_hex(*cp)) {
63 val = (val << 4) | _from_hex(*cp++);
70 // Simple GDB protocol handler
71 // Only three commands are handled:
72 // $M<loc>,<len>:<data...>#<cksum>
73 // $M20000000,10:0e0000ea18f09fe518f09fe518f09fe5#59
74 // $P<reg>=<val>#<cksum>
76 // <cksum> is a simple accumulation of all of the ASCII
77 // characters following the "$"
94 unsigned char cksum, _cksum;
95 unsigned char line[64];
101 void (*entry_address)(void);
107 // HACK - '>>' indicates to jump to FLASH/ROM
111 entry_address = (void *)0x60000000;
127 _cksum = _from_hex(c) << 4;
129 _cksum |= _from_hex(c);
130 ok = (cksum == _cksum);
131 tty_putc(0, ok ? '+' : '-');
135 #ifdef DEBUG_DOWNLOAD
136 tty_puts(1, ok ? " = OK\n" : " = BAD\n");
139 // Empty "OK" message
145 loc = (unsigned char *)_hex(&cp);
146 if (*cp++ != ',') continue;
148 if (*cp++ != ':') continue;
150 val = _from_hex(*cp++) << 4;
151 val |= _from_hex(*cp++);
158 if (*cp++ != '=') continue;
159 entry_address = (void *)_hex(&cp);
165 tty_puts(0, "** unknown command: $");
176 // Change 0x6XXXXXXX to use external ROM
177 *(volatile short *)0x4010100C |= (1<<9);
178 tty_puts(0, "Ready to download >>");
179 #ifdef DEBUG_DOWNLOAD
180 tty_puts(1, "... GDB data:\n");
183 return 0; // Never happens!