1 /*--------------------------------------------------------------------
2 * TITLE: Plasma Bootloader
3 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4 * DATE CREATED: 12/17/05
6 * PROJECT: Plasma CPU core
7 * COPYRIGHT: Software placed into the public domain by the author.
8 * Software 'as is' without warranty. Author liable for nothing.
11 *--------------------------------------------------------------------*/
14 #define MemoryRead(A) (*(volatile unsigned long*)(A))
15 #define MemoryWrite(A,V) *(volatile unsigned long*)(A)=(V)
17 extern int putchar(int ch);
18 extern int puts(const char *string);
19 extern int getch(void);
20 extern int kbhit(void);
21 extern int DdrInit(void);
23 typedef void (*FuncPtr)(void);
24 typedef unsigned long uint32;
25 typedef unsigned short uint16;
28 void FlashRead(uint16 *dst, uint32 byteOffset, int bytes)
30 volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
31 *ptr = 0xff; //read mode
34 *dst++ = (uint16)*ptr++;
40 void FlashWrite(uint16 *src, uint32 byteOffset, int bytes)
42 volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
45 *ptr = 0x40; //write mode
46 *ptr++ = *src++; //write data
47 while((*ptr & 0x80) == 0) //check status
54 void FlashErase(uint32 byteOffset)
56 volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
57 *ptr = 0x20; //erase block
58 *ptr = 0xd0; //confirm
59 while((*ptr & 0x80) == 0) //check status
64 char *xtoa(unsigned long num)
69 for (i = 7; i >= 0; --i)
72 buf[i] = digit + (digit < 10 ? '0' : 'A' - 10);
79 unsigned long getnum(void)
82 unsigned long ch, ch2, value=0;
86 if(ch == '\n' || ch == '\r')
88 if('0' <= ch && ch <= '9')
90 else if('A' <= ch && ch <= 'Z')
92 else if('a' <= ch && ch <= 'z')
107 value = (value << 4) + ch;
119 unsigned long address, value, count;
123 DdrInit(); //Harmless if SDRAM instead of DDR
125 puts("\nGreetings from the bootloader ");
130 MemoryWrite(FLASH_BASE, 0xff); //read mode
131 if((MemoryRead(GPIOA_IN) & 1) && (MemoryRead(FLASH_BASE) & 0xffff) == 0x3c1c)
133 puts("Boot from flash\n");
134 FlashRead((uint16*)RAM_EXTERNAL_BASE, 0, 1024*128);
135 funcPtr = (FuncPtr)RAM_EXTERNAL_BASE;
140 puts("\nWaiting for binary image linked at 0x10000000\n");
141 puts("Other Menu Options:\n");
142 puts("1. Memory read word\n");
143 puts("2. Memory write word\n");
144 puts("3. Memory read byte\n");
145 puts("4. Memory write byte\n");
146 puts("5. Jump to address\n");
147 puts("6. Raw memory read\n");
148 puts("7. Raw memory write\n");
149 puts("8. Checksum\n");
151 puts("F. Copy 128KB from DDR to flash\n");
155 if('0' <= ch && ch <= '9')
158 puts("\nAddress in hex> ");
167 value = MemoryRead(address);
172 puts("\nValue in hex> ");
175 MemoryWrite(address, value);
178 value = *(unsigned char*)address;
183 puts("\nValue in hex> ");
186 *(unsigned char*)address = value;
189 funcPtr = (FuncPtr)address;
193 puts("\nCount in hex> ");
195 for(i = 0; i < count; ++i)
197 ch = *(unsigned char*)(address + i);
202 puts("\nCount in hex> ");
204 for(i = 0; i < count; ++i)
207 *(unsigned char*)(address+i) = ch;
211 puts("\nCount in hex> ");
214 for(i = 0; i < count; ++i)
216 value += *(unsigned char*)(address+i);
222 puts("\nCount in hex> ");
225 for(i = 0; i < count; i += 4)
229 value = *(unsigned long*)(address+i);
236 puts("\nConfirm with 12345678> ");
238 if(value == 0x12345678)
241 FlashWrite((uint16*)RAM_EXTERNAL_BASE, 0, 1024*128);
244 case 0x3c: //raw test.bin file
245 ptr1 = (unsigned char*)0x10000000;
246 for(i = 0; i < 1024*1024; ++i)
248 ptr1[i] = (unsigned char)ch;
249 for(j = 0; j < 10000; ++j)
255 break; //assume end of file
258 funcPtr = (FuncPtr)0x10000000;