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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [ziprun.cpp] - Blame information for rev 117

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

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    ziprun.cpp
4
//
5
// Project:     XuLA2 board
6
//
7
// Purpose:     To load a program for the ZipCPU into memory.
8
//
9
//      Steps:
10
//              1. Halt and reset the CPU
11
//              2. Load memory
12
//              3. Clear the cache
13
//              4. Clear any registers
14
//              5. Set the PC to point to the FPGA local memory
15
//      THIS DOES NOT START THE PROGRAM!!  The CPU is left in the halt state.
16
//      To actually start the program, execute a ./wbregs cpu 0.  (Actually,
17
//      any value between 0x0 and 0x1f will work, the difference being what
18
//      register you will be able to inspect while the CPU is running.)
19
//
20
//
21
// Creator:     Dan Gisselquist, Ph.D.
22
//              Gisselquist Technology, LLC
23
//
24
////////////////////////////////////////////////////////////////////////////////
25
//
26 91 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
27 5 dgisselq
//
28
// This program is free software (firmware): you can redistribute it and/or
29
// modify it under the terms of  the GNU General Public License as published
30
// by the Free Software Foundation, either version 3 of the License, or (at
31
// your option) any later version.
32
//
33
// This program is distributed in the hope that it will be useful, but WITHOUT
34
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
35
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36
// for more details.
37
//
38
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
////////////////////////////////////////////////////////////////////////////////
43
//
44
//
45
//
46
#include <stdio.h>
47
#include <stdlib.h>
48 29 dgisselq
#include <sys/types.h>
49
#include <sys/stat.h>
50
#include <fcntl.h>
51 5 dgisselq
#include <unistd.h>
52
#include <strings.h>
53
#include <ctype.h>
54
#include <string.h>
55
#include <signal.h>
56
#include <assert.h>
57
 
58
#include "usbi.h"
59
#include "port.h"
60
#include "regdefs.h"
61 29 dgisselq
#include "flashdrvr.h"
62 5 dgisselq
 
63 29 dgisselq
bool    iself(const char *fname) {
64
        FILE    *fp;
65
        bool    ret = true;
66
        fp = fopen(fname, "rb");
67
 
68
        if (!fp)        return false;
69
        if (0x7f != fgetc(fp))  ret = false;
70
        if ('E'  != fgetc(fp))  ret = false;
71
        if ('L'  != fgetc(fp))  ret = false;
72
        if ('F'  != fgetc(fp))  ret = false;
73
        fclose(fp);
74
        return  ret;
75
}
76
 
77
long    fgetwords(FILE *fp) {
78
        // Return the number of words in the current file, and return the 
79
        // file as though it had never been adjusted
80
        long    fpos, flen;
81
        fpos = ftell(fp);
82
        if (0 != fseek(fp, 0l, SEEK_END)) {
83
                fprintf(stderr, "ERR: Could not determine file size\n");
84
                perror("O/S Err:");
85
                exit(-2);
86
        } flen = ftell(fp);
87
        if (0 != fseek(fp, fpos, SEEK_SET)) {
88
                fprintf(stderr, "ERR: Could not seek on file\n");
89
                perror("O/S Err:");
90
                exit(-2);
91
        } flen /= sizeof(FPGA::BUSW);
92
        return flen;
93
}
94
 
95 5 dgisselq
FPGA    *m_fpga;
96 29 dgisselq
class   SECTION {
97
public:
98
        unsigned        m_start, m_len;
99
        FPGA::BUSW      m_data[1];
100
};
101 5 dgisselq
 
102 29 dgisselq
SECTION **singlesection(int nwords) {
103
        fprintf(stderr, "NWORDS = %d\n", nwords);
104
        size_t  sz = (2*(sizeof(SECTION)+sizeof(SECTION *))
105
                +(nwords-1)*(sizeof(FPGA::BUSW)));
106
        char    *d = (char *)malloc(sz);
107
        SECTION **r = (SECTION **)d;
108
        memset(r, 0, sz);
109
        r[0] = (SECTION *)(&d[2*sizeof(SECTION *)]);
110
        r[0]->m_len   = nwords;
111
        r[1] = (SECTION *)(&r[0]->m_data[r[0]->m_len]);
112
        r[0]->m_start = 0;
113
        r[1]->m_start = 0;
114
        r[1]->m_len   = 0;
115
 
116
        return r;
117
}
118
 
119
SECTION **rawsection(const char *fname) {
120
        SECTION         **secpp, *secp;
121
        unsigned        num_words;
122
        FILE            *fp;
123
        int             nr;
124
 
125
        fp = fopen(fname, "r");
126
        if (fp == NULL) {
127
                fprintf(stderr, "Could not open: %s\n", fname);
128
                exit(-1);
129
        }
130
 
131
        if ((num_words=fgetwords(fp)) > MEMWORDS) {
132
                fprintf(stderr, "File overruns Block RAM\n");
133
                exit(-1);
134
        }
135
        secpp = singlesection(num_words);
136
        secp = secpp[0];
137
        secp->m_start = RAMBASE;
138
        secp->m_len = num_words;
139
        nr= fread(secp->m_data, sizeof(FPGA::BUSW), num_words, fp);
140
        if (nr != (int)num_words) {
141
                fprintf(stderr, "Could not read entire file\n");
142
                perror("O/S Err:");
143
                exit(-2);
144
        } assert(secpp[1]->m_len == 0);
145
 
146
        return secpp;
147
}
148
 
149
unsigned        byteswap(unsigned n) {
150
        unsigned        r;
151
 
152
        r = (n&0x0ff); n>>= 8;
153
        r = (r<<8) | (n&0x0ff); n>>= 8;
154
        r = (r<<8) | (n&0x0ff); n>>= 8;
155
        r = (r<<8) | (n&0x0ff); n>>= 8;
156
 
157
        return r;
158
}
159
 
160
// #define      CHEAP_AND_EASY
161
#ifdef  CHEAP_AND_EASY
162
#else
163
#include <libelf.h>
164
#include <gelf.h>
165
 
166
void    elfread(const char *fname, unsigned &entry, SECTION **&sections) {
167
        Elf     *e;
168
        int     fd, i;
169
        size_t  n;
170
        char    *id;
171
        Elf_Kind        ek;
172
        GElf_Ehdr       ehdr;
173
        GElf_Phdr       phdr;
174
        const   bool    dbg = false;
175
 
176
        if (elf_version(EV_CURRENT) == EV_NONE) {
177
                fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
178
                perror("O/S Err:");
179
                exit(EXIT_FAILURE);
180
        } if ((fd = open(fname, O_RDONLY, 0)) < 0) {
181
                fprintf(stderr, "Could not open %s\n", fname);
182
                perror("O/S Err:");
183
                exit(EXIT_FAILURE);
184
        } if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
185
                fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
186
                exit(EXIT_FAILURE);
187
        }
188
 
189
        ek = elf_kind(e);
190
        if (ek == ELF_K_ELF) {
191
                ; // This is the kind of file we should expect
192
        } else if (ek == ELF_K_AR) {
193
                fprintf(stderr, "Cannot run an archive!\n");
194
                exit(EXIT_FAILURE);
195
        } else if (ek == ELF_K_NONE) {
196
                ;
197
        } else {
198
                fprintf(stderr, "Unexpected ELF file kind!\n");
199
                exit(EXIT_FAILURE);
200
        }
201
 
202
        if (gelf_getehdr(e, &ehdr) == NULL) {
203
                fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
204
                exit(EXIT_FAILURE);
205
        } if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
206
                fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
207
                exit(EXIT_FAILURE);
208
        } if ((id = elf_getident(e, NULL)) == NULL) {
209
                fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
210
                exit(EXIT_FAILURE);
211
        } if (i != ELFCLASS32) {
212
                fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
213
                exit(EXIT_FAILURE);
214
        }
215
 
216
        if (dbg) {
217
        printf("    %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
218
        printf("    %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
219
        printf("    %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
220
        printf("    %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
221
        printf("    %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
222
        printf("    %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
223
        printf("    %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
224
        printf("    %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
225
        printf("    %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
226
        printf("    %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
227
        printf("\n");
228
        }
229
 
230
 
231
        // Check whether or not this is an ELF file for the ZipCPU ...
232
        if (ehdr.e_machine != 0x0dadd) {
233
                fprintf(stderr, "This is not a ZipCPU ELF file\n");
234
                exit(EXIT_FAILURE);
235
        }
236
 
237
        // Get our entry address
238
        entry = ehdr.e_entry;
239
 
240
 
241
        // Now, let's go look at the program header
242
        if (elf_getphdrnum(e, &n) != 0) {
243
                fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
244
                exit(EXIT_FAILURE);
245
        }
246
 
247
        unsigned total_octets = 0, current_offset=0, current_section=0;
248
        for(i=0; i<(int)n; i++) {
249
                total_octets += sizeof(SECTION *)+sizeof(SECTION);
250
 
251
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
252
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
253
                        exit(EXIT_FAILURE);
254
                }
255
 
256
                if (dbg) {
257
                printf("    %-20s 0x%x\n", "p_type",   phdr.p_type);
258
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
259
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
260
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
261
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
262
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
263
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
264
 
265
                if (phdr.p_flags & PF_X)        printf(" Execute");
266
                if (phdr.p_flags & PF_R)        printf(" Read");
267
                if (phdr.p_flags & PF_W)        printf(" Write");
268
                printf("]\n");
269
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
270
                }
271
 
272
                total_octets += phdr.p_memsz;
273
        }
274
 
275 30 dgisselq
        char    *d = (char *)malloc(total_octets + sizeof(SECTION)+sizeof(SECTION *));
276 29 dgisselq
        memset(d, 0, total_octets);
277
 
278
        SECTION **r = sections = (SECTION **)d;
279
        current_offset = (n+1)*sizeof(SECTION *);
280
        current_section = 0;
281
 
282
        for(i=0; i<(int)n; i++) {
283
                r[i] = (SECTION *)(&d[current_offset]);
284
 
285
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
286
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
287
                        exit(EXIT_FAILURE);
288
                }
289
 
290
                if (dbg) {
291
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
292
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
293
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
294
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
295
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
296
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
297
 
298
                if (phdr.p_flags & PF_X)        printf(" Execute");
299
                if (phdr.p_flags & PF_R)        printf(" Read");
300
                if (phdr.p_flags & PF_W)        printf(" Write");
301
                printf("]\n");
302
 
303
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
304
                }
305
 
306
                current_section++;
307
 
308
                r[i]->m_start = phdr.p_vaddr;
309
                r[i]->m_len   = phdr.p_filesz/ sizeof(FPGA::BUSW);
310
 
311
                current_offset += phdr.p_memsz + sizeof(SECTION);
312
 
313
                // Now, let's read in our section ...
314
                if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
315
                        fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
316
                        perror("O/S Err:");
317
                        exit(EXIT_FAILURE);
318
                } if (phdr.p_filesz > phdr.p_memsz)
319
                        phdr.p_filesz = 0;
320
                if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
321
                        fprintf(stderr, "Didnt read entire section\n");
322
                        perror("O/S Err:");
323
                        exit(EXIT_FAILURE);
324
                }
325
 
326
                // Next, we need to byte swap it from big to little endian
327
                for(unsigned j=0; j<r[i]->m_len; j++)
328
                        r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
329
 
330
                if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
331
                        fprintf(stderr, "ADR[%04x] = %08x\n", r[i]->m_start+j,
332
                        r[i]->m_data[j]);
333
        }
334
 
335 30 dgisselq
        r[i] = (SECTION *)(&d[current_offset]);
336 29 dgisselq
        r[current_section]->m_start = 0;
337
        r[current_section]->m_len   = 0;
338
 
339
        elf_end(e);
340
        close(fd);
341
}
342
#endif
343
 
344
void    usage(void) {
345
        printf("USAGE: ziprun [-hmprux] <zip-program-file>\n");
346
        printf("\n"
347
"\t-h\tDisplay this usage statement\n"
348
"\t-m\tClear unused memory locations.  Note this only applies to SDRAM\n"
349
"\t\t(if used) and block ram, not flash.\n"
350
"\t-p [PORT]\tConnect to the XuLA device across a network access\n"
351
"\t\tconnection using port PORT, rather than attempting a USB\n"
352
"\t\tconnection.  If PORT is not given, %s:%d will be\n"
353
"\t\tassumed as a default.\n"
354
"\t-u\tAccess the XuLA board via the USB connector [DEFAULT]\n"
355
"\t-x\tClear all of the ZipCPU registers to a known initial state\n\n",
356
        FPGAHOST,FPGAPORT);
357
}
358
 
359 5 dgisselq
int main(int argc, char **argv) {
360 29 dgisselq
        int             skp=0, port = FPGAPORT;
361
        bool            use_usb = true, permit_raw_files = false;
362
        unsigned        entry = RAMBASE;
363
        bool            clear_registers = false, clear_memory = false;
364
        FLASHDRVR       *flash = NULL;
365 5 dgisselq
 
366 29 dgisselq
        if (argc < 2) {
367
                usage();
368
                exit(EXIT_SUCCESS);
369
        }
370
 
371 5 dgisselq
        skp=1;
372
        for(int argn=0; argn<argc-skp; argn++) {
373
                if (argv[argn+skp][0] == '-') {
374 29 dgisselq
                        switch(argv[argn+skp][1]) {
375
                        case 'h':
376
                                usage();
377
                                exit(EXIT_SUCCESS);
378
                        case 'm':
379
                                clear_memory = true;
380
                                fprintf(stderr, "Clear memory feature not yet implemented\n");
381
                                exit(EXIT_FAILURE);
382
                                break;
383
                        case 'p':
384 5 dgisselq
                                use_usb = false;
385
                                if (isdigit(argv[argn+skp][2]))
386
                                        port = atoi(&argv[argn+skp][2]);
387 29 dgisselq
                                break;
388
                        case 'r':
389
                                permit_raw_files = true;
390
                                break;
391
                        case 'u':
392
                                use_usb = true;
393
                                break;
394
                        case 'x':
395
                                clear_registers = true;
396
                                break;
397
                        } skp++; argn--;
398 5 dgisselq
                } else
399
                        argv[argn] = argv[argn+skp];
400
        } argc -= skp;
401
 
402
        if (use_usb)
403
                m_fpga = new FPGA(new USBI());
404
        else
405
                m_fpga = new FPGA(new NETCOMMS(FPGAHOST, port));
406
 
407
        if ((argc<=0)||(access(argv[0],R_OK)!=0)) {
408
                printf("Usage: ziprun obj-file\n");
409
                printf("\n"
410
"\tziprun loads the object file into memory, resets the CPU, and leaves it\n"
411
"\tin a halted state ready to start running the object file.\n");
412
                exit(-1);
413 29 dgisselq
        } const char *codef = argv[0];
414 5 dgisselq
 
415
        printf("Halting the CPU\n");
416 11 dgisselq
        m_fpga->usleep(5);
417 5 dgisselq
        m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT);
418
 
419
        try {
420 29 dgisselq
                SECTION **secpp = NULL, *secp;
421 5 dgisselq
 
422 29 dgisselq
                if(iself(codef)) {
423
#ifndef CHEAP_AND_EASY
424
                        // zip-readelf will help with both of these ...
425
                        elfread(codef, entry, secpp);
426 5 dgisselq
 
427 30 dgisselq
                        /*
428 29 dgisselq
                        fprintf(stderr, "Secpp = %08lx\n", (unsigned long)secpp);
429
                        for(int i=0; secpp[i]->m_len; i++) {
430
                                secp = secpp[i];
431
                                fprintf(stderr, "Sec[%2d] - %08x - %08x\n",
432
                                        i, secp->m_start,
433
                                        secp->m_start+secp->m_len);
434 30 dgisselq
                        } */
435 29 dgisselq
#else
436
                        char    tmpbuf[TMP_MAX], cmdbuf[256];
437
                        int     unused_fd;
438
 
439
                        strcpy(tmpbuf, "/var/tmp/ziprunXXXX");
440
 
441
                        // Make a temporary file
442
                        unused_fd = mkostemp(tmpbuf, O_CREAT|O_TRUNC|O_RDWR);
443
                        // Close it immediately, since we won't be writing to it
444
                        // ourselves
445
                        close(unused_fd);
446
 
447
                        // Now we write to it, as part of calling objcopy
448
                        // 
449
                        sprintf(cmdbuf, "zip-objcopy -S -O binary --reverse-bytes=4 %s %s", codef, tmpbuf);
450
 
451
                        if (system(cmdbuf) != 0) {
452
                                unlink(tmpbuf);
453
                                fprintf(stderr, "ZIPRUN: Could not comprehend ELF binary\n");
454
                                exit(-2);
455
                        }
456
 
457
                        secpp = rawsection(tmpbuf);
458
                        unlink(tmpbuf);
459
                        entry = RAMBASE;
460 5 dgisselq
#endif
461 29 dgisselq
                } else if (permit_raw_files) {
462
                        secpp = rawsection(codef);
463
                        entry = RAMBASE;
464 53 dgisselq
                } else {
465
                        fprintf(stderr, "ERR: %s is not in ELF format\n", codef);
466
                        exit(EXIT_FAILURE);
467 29 dgisselq
                }
468 5 dgisselq
 
469 29 dgisselq
                // assert(secpp[1]->m_len = 0);
470
                for(int i=0; secpp[i]->m_len; i++) {
471
                        bool    valid = false;
472
                        secp=  secpp[i];
473
                        if ((secp->m_start >= RAMBASE)&&(secp->m_start+secp->m_len <= RAMBASE+MEMWORDS))
474
                                valid = true;
475
                        else if ((secp->m_start >= SDRAMBASE)&&(secp->m_start+secp->m_len <= SDRAMBASE+SDRAMWORDS))
476
                                valid = true;
477
                        else if ((secp->m_start >= SPIFLASH)&&(secp->m_start+secp->m_len <= SPIFLASH+FLASHWORDS))
478
                                valid = true;
479
                        if (!valid) {
480
                                fprintf(stderr, "No such memory on board: 0x%08x - %08x\n",
481
                                        secp->m_start, secp->m_start+secp->m_len);
482
                                exit(-2);
483
                        }
484
                }
485 5 dgisselq
 
486 29 dgisselq
                if (clear_memory) for(int i=0; secpp[i]->m_len; i++) {
487
                        secp = secpp[i];
488
                        if ((secp->m_start >= RAMBASE)
489
                                        &&(secp->m_start+secp->m_len
490
                                                        <= RAMBASE+MEMWORDS)) {
491 30 dgisselq
                                printf("Clearing Block ram\n");
492 29 dgisselq
                                FPGA::BUSW      zbuf[128], a;
493
                                memset(zbuf, 0, 128*sizeof(FPGA::BUSW));
494
                                for(a=RAMBASE; a<RAMBASE+MEMWORDS; a+=128)
495
                                        m_fpga->writei(a, 128, zbuf);
496
                                break;
497
                        }
498 30 dgisselq
                } m_fpga->readio(R_VERSION); // Check for buserrors
499 29 dgisselq
 
500
                if (clear_memory) for(int i=0; secpp[i]->m_len; i++) {
501
                        secp = secpp[i];
502
                        if ((secp->m_start >= SDRAMBASE)
503
                                        &&(secp->m_start+secp->m_len
504
                                                <= SDRAMBASE+SDRAMWORDS)) {
505
                                FPGA::BUSW      zbuf[128], a;
506 30 dgisselq
                                printf("Clearing SDRam\n");
507 29 dgisselq
                                memset(zbuf, 0, 128*sizeof(FPGA::BUSW));
508
                                for(a=SDRAMBASE; a<SDRAMBASE+SDRAMWORDS; a+=128)
509
                                        m_fpga->writei(a, 128, zbuf);
510
                                break;
511
                        }
512 30 dgisselq
                } m_fpga->readio(R_VERSION); // Check for buserrors
513
 
514 91 dgisselq
                printf("Loading memory\n");
515 29 dgisselq
                for(int i=0; secpp[i]->m_len; i++) {
516
                        bool    inflash=false;
517 11 dgisselq
 
518 29 dgisselq
                        secp = secpp[i];
519
                        if ((secp->m_start >= SPIFLASH)
520
                                        &&(secp->m_start+secp->m_len
521
                                                        <= SPIFLASH+FLASHWORDS))
522
                                inflash = true;
523
                        if (inflash) {
524
                                if (!flash)
525
                                        flash = new FLASHDRVR(m_fpga);
526
                                flash->write(secp->m_start, secp->m_len, secp->m_data, true);
527 91 dgisselq
                        } else if (secp->m_len < (1<<16)) {
528 29 dgisselq
                                m_fpga->writei(secp->m_start, secp->m_len, secp->m_data);
529 91 dgisselq
                        } else {
530
                                // The load amount is so big, we'd like to let
531
                                // the user know where we're at along the way.
532
                                for(unsigned k=0; k<secp->m_len; k+=(1<<16)) {
533
                                        unsigned ln = (1<<16),
534
                                                st = secp->m_start+k;
535
                                        if (st+ln > secp->m_start+secp->m_len)
536
                                                ln = (secp->m_start+secp->m_len-st);
537
                                        if (ln <= 0) break;
538
                                        printf("Loading MEM[%08x]-MEM[%08x] ...\r",
539
                                                st,st+ln-1); fflush(stdout);
540
                                        m_fpga->writei(st, ln, &secp->m_data[k]);
541
                                        m_fpga->readio(R_VERSION); // Check for buserrors
542
                                } printf("Loaded  MEM[%08x]-MEM[%08x]      \n",
543
                                        secp->m_start,
544
                                        secp->m_start+secp->m_len-1);
545
                                fflush(stdout);
546
                        }
547
                        printf("%08x - %08x\n", secp->m_start, secp->m_start+secp->m_len);
548 5 dgisselq
                }
549 30 dgisselq
                m_fpga->readio(R_ZIPCTRL); // Check for bus errors
550 5 dgisselq
 
551 29 dgisselq
                // Clear any buffers
552
                printf("Clearing the cache\n");
553
                m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT|CPU_CLRCACHE);
554 30 dgisselq
                m_fpga->readio(R_VERSION);
555 29 dgisselq
 
556
                if (clear_registers) {
557
                        printf("Clearing all registers to zero\n");
558
                        // Clear all registers to zero
559
                        for(int i=0; i<32; i++) {
560
                                m_fpga->writeio(R_ZIPCTRL, CPU_HALT|i);
561
                                m_fpga->writeio(R_ZIPDATA, 0);
562
                        }
563 30 dgisselq
                } m_fpga->readio(R_VERSION); // Check for bus errors
564 29 dgisselq
 
565
                // Start in interrupt mode
566
                m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sCC);
567 5 dgisselq
                m_fpga->writeio(R_ZIPDATA, 0x000);
568
 
569 29 dgisselq
                // Set our entry point into our code
570 5 dgisselq
                m_fpga->writeio(R_ZIPCTRL, CPU_HALT|CPU_sPC);
571 29 dgisselq
                m_fpga->writeio(R_ZIPDATA, entry);
572 30 dgisselq
 
573
                printf("The CPU should be fully loaded, you may now start\n");
574
                printf("it.  To start the CPU, type wbregs cpu 0\n");
575 29 dgisselq
        } catch(BUSERR a) {
576 91 dgisselq
                fprintf(stderr, "\nXULA-BUS error @0x%08x\n", a.addr);
577 29 dgisselq
                m_fpga->writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT|CPU_CLRCACHE);
578
                exit(-2);
579 5 dgisselq
        }
580
 
581
        delete  m_fpga;
582
}
583
 

powered by: WebSVN 2.1.0

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