OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [tools/] [bootldr.c] - Blame information for rev 395

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 291 rhoads
/*--------------------------------------------------------------------
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 142 rhoads
#include "plasma.h"
13
 
14
#define MemoryRead(A) (*(volatile unsigned long*)(A))
15
#define MemoryWrite(A,V) *(volatile unsigned long*)(A)=(V)
16
 
17 336 rhoads
extern int putchar(int ch);
18 267 rhoads
extern int puts(const char *string);
19
extern int getch(void);
20
extern int kbhit(void);
21
extern int DdrInit(void);
22 142 rhoads
 
23
typedef void (*FuncPtr)(void);
24 291 rhoads
typedef unsigned long uint32;
25
typedef unsigned short uint16;
26 142 rhoads
 
27 291 rhoads
 
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 142 rhoads
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 291 rhoads
 
79 142 rhoads
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 291 rhoads
 
116 142 rhoads
int main(void)
117
{
118
   int i, j, ch;
119
   unsigned long address, value, count;
120
   FuncPtr funcPtr;
121
   unsigned char *ptr1;
122
 
123 267 rhoads
   DdrInit();  //Harmless if SDRAM instead of DDR
124
 
125 142 rhoads
   puts("\nGreetings from the bootloader ");
126
   puts(__DATE__);
127
   puts(" ");
128
   puts(__TIME__);
129
   puts(":\n");
130 291 rhoads
   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 142 rhoads
   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 291 rhoads
      puts("F. Copy 128KB from DDR to flash\n");
152 142 rhoads
      puts("> ");
153
      ch = getch();
154
      address = 0;
155 183 rhoads
      if('0' <= ch && ch <= '9')
156 142 rhoads
      {
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 291 rhoads
      case 'F':
236 336 rhoads
         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 291 rhoads
         break;
244 142 rhoads
      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
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.