]> rtime.felk.cvut.cz Git - fpga/plasma.git/blob - tools/bootldr.c
Local copy of Plasma MIPS project.
[fpga/plasma.git] / tools / bootldr.c
1 /*--------------------------------------------------------------------
2  * TITLE: Plasma Bootloader
3  * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4  * DATE CREATED: 12/17/05
5  * FILENAME: bootldr.c
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.
9  * DESCRIPTION:
10  *    Plasma bootloader.
11  *--------------------------------------------------------------------*/
12 #include "plasma.h"
13
14 #define MemoryRead(A) (*(volatile unsigned long*)(A))
15 #define MemoryWrite(A,V) *(volatile unsigned long*)(A)=(V)
16
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);
22
23 typedef void (*FuncPtr)(void);
24 typedef unsigned long uint32;
25 typedef unsigned short uint16;
26
27
28 void FlashRead(uint16 *dst, uint32 byteOffset, int bytes)
29 {
30    volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
31    *ptr = 0xff;                   //read mode
32    while(bytes > 0)
33    {
34       *dst++ = (uint16)*ptr++;
35       bytes -= 2;
36    }
37 }
38
39
40 void FlashWrite(uint16 *src, uint32 byteOffset, int bytes)
41 {
42    volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
43    while(bytes > 0)
44    {
45       *ptr = 0x40;                //write mode
46       *ptr++ = *src++;            //write data
47       while((*ptr & 0x80) == 0)   //check status
48          ;
49       bytes -= 2;
50    }
51 }
52
53
54 void FlashErase(uint32 byteOffset)
55 {
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
60       ;
61 }
62
63
64 char *xtoa(unsigned long num)
65 {
66    static char buf[12];
67    int i, digit;
68    buf[8] = 0;
69    for (i = 7; i >= 0; --i)
70    {
71       digit = num & 0xf;
72       buf[i] = digit + (digit < 10 ? '0' : 'A' - 10);
73       num >>= 4;
74    }
75    return buf;
76 }
77
78
79 unsigned long getnum(void)
80 {
81    int i;
82    unsigned long ch, ch2, value=0;
83    for(i = 0; i < 16; )
84    {
85       ch = ch2 = getch();
86       if(ch == '\n' || ch == '\r')
87          break;
88       if('0' <= ch && ch <= '9')
89          ch -= '0';
90       else if('A' <= ch && ch <= 'Z')
91          ch = ch - 'A' + 10;
92       else if('a' <= ch && ch <= 'z')
93          ch = ch - 'a' + 10;
94       else if(ch == 8)
95       {
96          if(i > 0)
97          {
98             --i;
99             putchar(ch);
100             putchar(' ');
101             putchar(ch);
102          }
103          value >>= 4;
104          continue;
105       }
106       putchar(ch2);
107       value = (value << 4) + ch;
108       ++i;
109    }
110    putchar('\r');
111    putchar('\n');
112    return value;
113 }
114
115
116 int main(void)
117 {
118    int i, j, ch;
119    unsigned long address, value, count;
120    FuncPtr funcPtr;
121    unsigned char *ptr1;
122
123    DdrInit();  //Harmless if SDRAM instead of DDR
124
125    puts("\nGreetings from the bootloader ");
126    puts(__DATE__);
127    puts(" ");
128    puts(__TIME__);
129    puts(":\n");
130    MemoryWrite(FLASH_BASE, 0xff);  //read mode
131    if((MemoryRead(GPIOA_IN) & 1) && (MemoryRead(FLASH_BASE) & 0xffff) == 0x3c1c)
132    {
133       puts("Boot from flash\n");
134       FlashRead((uint16*)RAM_EXTERNAL_BASE, 0, 1024*128);
135       funcPtr = (FuncPtr)RAM_EXTERNAL_BASE;
136       funcPtr();
137    }
138    for(;;)
139    {
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");
150       puts("9. Dump\n");
151       puts("F. Copy 128KB from DDR to flash\n");
152       puts("> ");
153       ch = getch();
154       address = 0;
155       if('0' <= ch && ch <= '9')
156       {
157          putchar(ch);
158          puts("\nAddress in hex> ");
159          address = getnum();
160          puts("Address = ");
161          puts(xtoa(address));
162          puts("\n");
163       }
164       switch(ch)
165       {
166       case '1':
167          value = MemoryRead(address);
168          puts(xtoa(value));
169          puts("\n");
170          break;
171       case '2':
172          puts("\nValue in hex> ");
173          value = getnum();
174          puts(xtoa(value));
175          MemoryWrite(address, value);
176          break;
177       case '3':
178          value = *(unsigned char*)address;
179          puts(xtoa(value));
180          puts("\n");
181          break;
182       case '4':
183          puts("\nValue in hex> ");
184          value = getnum();
185          puts(xtoa(value));
186          *(unsigned char*)address = value;
187          break;
188       case '5':
189          funcPtr = (FuncPtr)address;
190          funcPtr();
191          break;
192       case '6':
193          puts("\nCount in hex> ");
194          count = getnum();
195          for(i = 0; i < count; ++i)
196          {
197             ch = *(unsigned char*)(address + i);
198             putchar(ch);
199          }
200          break;
201       case '7':
202          puts("\nCount in hex> ");
203          count = getnum();
204          for(i = 0; i < count; ++i)
205          {
206             ch = getch();
207             *(unsigned char*)(address+i) = ch;
208          }
209          break;
210       case '8':
211          puts("\nCount in hex> ");
212          count = getnum();
213          value = 0;
214          for(i = 0; i < count; ++i)
215          {
216             value += *(unsigned char*)(address+i);
217          }
218          puts(xtoa(value));
219          putchar('\n');
220          break;
221       case '9':
222          puts("\nCount in hex> ");
223          count = getnum();
224          value = 0;
225          for(i = 0; i < count; i += 4)
226          {
227             if((i & 15) == 0)
228                puts("\r\n");
229             value = *(unsigned long*)(address+i);
230             puts(xtoa(value));
231             putchar(' ');
232          }
233          puts("\r\n");
234          break;
235       case 'F':
236          puts("\nConfirm with 12345678> ");
237          value = getnum();
238          if(value == 0x12345678)
239          {
240             FlashErase(0);
241             FlashWrite((uint16*)RAM_EXTERNAL_BASE, 0, 1024*128);
242          }
243          break;
244       case 0x3c:   //raw test.bin file
245          ptr1 = (unsigned char*)0x10000000;
246          for(i = 0; i < 1024*1024; ++i)
247          {
248             ptr1[i] = (unsigned char)ch;
249             for(j = 0; j < 10000; ++j)
250             {
251                if(kbhit())
252                   break;
253             }
254             if(j >= 10000)
255                break;       //assume end of file
256             ch = getch();
257          }
258          funcPtr = (FuncPtr)0x10000000;
259          funcPtr();
260          break;
261       }
262    }
263    return 0;
264 }
265