| 1 |
11 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: zipload.cpp
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: CMod S6 System on a Chip, ZipCPU demonstration project
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose: To load the flash--both a the two configurations and the
|
| 8 |
|
|
// a program for the ZipCPU into (flash) memory.
|
| 9 |
|
|
//
|
| 10 |
|
|
// Steps:
|
| 11 |
|
|
// 1. Reboot the CMod into the alternate/debug/command mode
|
| 12 |
|
|
// 2. Load flash memory
|
| 13 |
|
|
// 3. Reload (reboot) the CMod configuration into ZipCPU mode
|
| 14 |
|
|
// 4. Program should start on its own.
|
| 15 |
|
|
//
|
| 16 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 17 |
|
|
// Gisselquist Technology, LLC
|
| 18 |
|
|
//
|
| 19 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 20 |
|
|
//
|
| 21 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
| 22 |
|
|
//
|
| 23 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 24 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 25 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 26 |
|
|
// your option) any later version.
|
| 27 |
|
|
//
|
| 28 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 29 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 30 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 31 |
|
|
// for more details.
|
| 32 |
|
|
//
|
| 33 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 34 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 35 |
|
|
//
|
| 36 |
|
|
//
|
| 37 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 38 |
|
|
//
|
| 39 |
|
|
//
|
| 40 |
|
|
//
|
| 41 |
|
|
#include <stdio.h>
|
| 42 |
|
|
#include <stdlib.h>
|
| 43 |
|
|
#include <sys/types.h>
|
| 44 |
|
|
#include <sys/stat.h>
|
| 45 |
|
|
#include <fcntl.h>
|
| 46 |
|
|
#include <unistd.h>
|
| 47 |
|
|
#include <strings.h>
|
| 48 |
|
|
#include <ctype.h>
|
| 49 |
|
|
#include <string.h>
|
| 50 |
|
|
#include <signal.h>
|
| 51 |
|
|
#include <assert.h>
|
| 52 |
|
|
|
| 53 |
|
|
#include "devbus.h"
|
| 54 |
|
|
#include "llcomms.h"
|
| 55 |
|
|
#include "deppi.h"
|
| 56 |
|
|
#include "regdefs.h"
|
| 57 |
|
|
#include "flashdrvr.h"
|
| 58 |
|
|
|
| 59 |
|
|
bool iself(const char *fname) {
|
| 60 |
|
|
FILE *fp;
|
| 61 |
|
|
bool ret = true;
|
| 62 |
|
|
|
| 63 |
|
|
if ((!fname)||(!fname[0]))
|
| 64 |
|
|
return false;
|
| 65 |
|
|
|
| 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 |
|
|
FPGA *m_fpga;
|
| 96 |
|
|
class SECTION {
|
| 97 |
|
|
public:
|
| 98 |
|
|
unsigned m_start, m_len;
|
| 99 |
|
|
FPGA::BUSW m_data[1];
|
| 100 |
|
|
};
|
| 101 |
|
|
|
| 102 |
|
|
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)) > FLASHWORDS-(RESET_ADDRESS-SPIFLASH)) {
|
| 132 |
|
|
fprintf(stderr, "File overruns flash memory\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 **§ions) {
|
| 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 |
|
|
char *d = (char *)malloc(total_octets + sizeof(SECTION)+sizeof(SECTION *));
|
| 276 |
|
|
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 |
26 |
dgisselq |
r[i]->m_start = phdr.p_paddr;
|
| 309 |
11 |
dgisselq |
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 |
|
|
r[i] = (SECTION *)(&d[current_offset]);
|
| 336 |
|
|
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 [-h] [<bit-file> [<alt-bit-file>]] <zip-program-file>\n");
|
| 346 |
|
|
printf("\n"
|
| 347 |
|
|
"\t-h\tDisplay this usage statement\n");
|
| 348 |
|
|
}
|
| 349 |
|
|
|
| 350 |
|
|
int main(int argc, char **argv) {
|
| 351 |
|
|
int skp=0;
|
| 352 |
26 |
dgisselq |
bool permit_raw_files = false, debug_only = false;
|
| 353 |
11 |
dgisselq |
unsigned entry = RAMBASE;
|
| 354 |
|
|
FLASHDRVR *flash = NULL;
|
| 355 |
|
|
const char *bitfile = NULL, *altbitfile = NULL;
|
| 356 |
|
|
|
| 357 |
|
|
if (argc < 2) {
|
| 358 |
|
|
usage();
|
| 359 |
|
|
exit(EXIT_SUCCESS);
|
| 360 |
|
|
}
|
| 361 |
|
|
|
| 362 |
|
|
skp=1;
|
| 363 |
|
|
for(int argn=0; argn<argc-skp; argn++) {
|
| 364 |
|
|
if (argv[argn+skp][0] == '-') {
|
| 365 |
|
|
switch(argv[argn+skp][1]) {
|
| 366 |
26 |
dgisselq |
case 'd':
|
| 367 |
|
|
debug_only = true;
|
| 368 |
|
|
break;
|
| 369 |
11 |
dgisselq |
case 'h':
|
| 370 |
|
|
usage();
|
| 371 |
|
|
exit(EXIT_SUCCESS);
|
| 372 |
26 |
dgisselq |
break;
|
| 373 |
11 |
dgisselq |
case 'r':
|
| 374 |
|
|
permit_raw_files = true;
|
| 375 |
|
|
break;
|
| 376 |
26 |
dgisselq |
default:
|
| 377 |
|
|
fprintf(stderr, "Unknown option, -%c\n\n",
|
| 378 |
|
|
argv[argn+skp][0]);
|
| 379 |
|
|
usage();
|
| 380 |
|
|
exit(EXIT_FAILURE);
|
| 381 |
|
|
break;
|
| 382 |
11 |
dgisselq |
} skp++; argn--;
|
| 383 |
|
|
} else { // Check for bit files
|
| 384 |
|
|
int sl = strlen(argv[argn+skp]);
|
| 385 |
|
|
if ((sl>4)&&(strcmp(&argv[argn+skp][sl-4],".bit")==0)) {
|
| 386 |
|
|
if (bitfile == NULL)
|
| 387 |
|
|
bitfile = argv[argn+skp];
|
| 388 |
|
|
else if (altbitfile == NULL)
|
| 389 |
|
|
altbitfile = argv[argn+skp];
|
| 390 |
|
|
else {
|
| 391 |
|
|
fprintf(stderr, "Err: Too many bit files listed\n");
|
| 392 |
|
|
exit(EXIT_FAILURE);
|
| 393 |
|
|
} skp++; argn--;
|
| 394 |
|
|
} else
|
| 395 |
|
|
argv[argn] = argv[argn+skp];
|
| 396 |
|
|
}
|
| 397 |
|
|
} argc -= skp;
|
| 398 |
|
|
|
| 399 |
|
|
|
| 400 |
|
|
if ((bitfile)&&(access(bitfile,R_OK)!=0)) {
|
| 401 |
|
|
fprintf(stderr, "Cannot open bitfile, %s\n", bitfile);
|
| 402 |
|
|
exit(EXIT_FAILURE);
|
| 403 |
|
|
} if ((altbitfile)&&(access(altbitfile,R_OK)!=0)) {
|
| 404 |
|
|
fprintf(stderr, "Cannot open alternate bitfile, %s\n",
|
| 405 |
|
|
altbitfile);
|
| 406 |
|
|
exit(EXIT_FAILURE);
|
| 407 |
|
|
} if(((!bitfile)&&(argc<=0)) || ((argc>0)&&(access(argv[0],R_OK)!=0))) {
|
| 408 |
|
|
// If there's no code file, or the code file cannot be opened
|
| 409 |
|
|
if (argc>0)
|
| 410 |
|
|
fprintf(stderr, "Cannot open executable, %s\n", argv[0]);
|
| 411 |
|
|
else
|
| 412 |
|
|
usage();
|
| 413 |
|
|
exit(EXIT_FAILURE);
|
| 414 |
|
|
}
|
| 415 |
|
|
|
| 416 |
|
|
const char *codef = (argc>0)?argv[0]:NULL;
|
| 417 |
|
|
DEVBUS::BUSW *fbuf = new DEVBUS::BUSW[FLASHWORDS];
|
| 418 |
|
|
|
| 419 |
|
|
// Set the flash buffer to all ones
|
| 420 |
|
|
memset(fbuf, -1, FLASHWORDS*sizeof(fbuf[0]));
|
| 421 |
|
|
|
| 422 |
26 |
dgisselq |
if (debug_only) {
|
| 423 |
|
|
m_fpga = NULL;
|
| 424 |
|
|
} else {
|
| 425 |
11 |
dgisselq |
char szSel[64];
|
| 426 |
|
|
strcpy(szSel, "SN:210282768825");
|
| 427 |
|
|
m_fpga = new FPGA(new DEPPI(szSel));
|
| 428 |
|
|
}
|
| 429 |
|
|
|
| 430 |
26 |
dgisselq |
flash = (debug_only)?NULL : new FLASHDRVR(m_fpga);
|
| 431 |
11 |
dgisselq |
|
| 432 |
|
|
// First, see if we need to load a bit file
|
| 433 |
|
|
if (bitfile) {
|
| 434 |
|
|
int len;
|
| 435 |
|
|
FILE *fp = fopen(bitfile, "rb");
|
| 436 |
|
|
|
| 437 |
|
|
fseek(fp, 0x5dl, SEEK_SET);
|
| 438 |
|
|
len = fread(&fbuf[CONFIG_ADDRESS-SPIFLASH],
|
| 439 |
|
|
sizeof(fbuf[0]),
|
| 440 |
|
|
FLASHWORDS-(CONFIG_ADDRESS-SPIFLASH), fp);
|
| 441 |
|
|
assert(len + CONFIG_ADDRESS < ALTCONFIG_ADDRESS);
|
| 442 |
|
|
fclose(fp);
|
| 443 |
|
|
|
| 444 |
|
|
for(int i=0; i<4; i++) {
|
| 445 |
|
|
// printf("0x%08x\n", fbuf[i]);
|
| 446 |
|
|
assert(fbuf[i] == 0x0ffffffff);
|
| 447 |
|
|
} // printf("0x%08x\n", fbuf[4]);
|
| 448 |
|
|
assert(fbuf[4] == 0x0665599aa);
|
| 449 |
|
|
|
| 450 |
|
|
printf("Loading: %s\n", bitfile);
|
| 451 |
26 |
dgisselq |
if ((flash)&&(!flash->write(CONFIG_ADDRESS, len, &fbuf[CONFIG_ADDRESS-SPIFLASH], true))) {
|
| 452 |
11 |
dgisselq |
fprintf(stderr, "Could not write primary bitfile\n");
|
| 453 |
|
|
exit(EXIT_FAILURE);
|
| 454 |
|
|
}
|
| 455 |
|
|
} if (altbitfile) {
|
| 456 |
|
|
int len;
|
| 457 |
|
|
FILE *fp = fopen(altbitfile, "rb");
|
| 458 |
|
|
|
| 459 |
|
|
// The alternate configuration follows the first configuration
|
| 460 |
|
|
len = fread(&fbuf[ALTCONFIG_ADDRESS-SPIFLASH],
|
| 461 |
|
|
sizeof(fbuf[0]),
|
| 462 |
|
|
FLASHWORDS-(ALTCONFIG_ADDRESS-SPIFLASH), fp);
|
| 463 |
|
|
assert(len + ALTCONFIG_ADDRESS < RESET_ADDRESS);
|
| 464 |
|
|
fclose(fp);
|
| 465 |
|
|
printf("Loading: %s\n", altbitfile);
|
| 466 |
|
|
|
| 467 |
26 |
dgisselq |
if ((flash)&&(!flash->write(ALTCONFIG_ADDRESS, len, &fbuf[ALTCONFIG_ADDRESS-SPIFLASH], true))) {
|
| 468 |
11 |
dgisselq |
fprintf(stderr, "Could not write alternate bitfile\n");
|
| 469 |
|
|
exit(EXIT_FAILURE);
|
| 470 |
|
|
}
|
| 471 |
|
|
}
|
| 472 |
|
|
|
| 473 |
|
|
if (codef) try {
|
| 474 |
|
|
SECTION **secpp = NULL, *secp;
|
| 475 |
|
|
|
| 476 |
|
|
if(iself(codef)) {
|
| 477 |
|
|
// zip-readelf will help with both of these ...
|
| 478 |
|
|
elfread(codef, entry, secpp);
|
| 479 |
|
|
assert(entry == RESET_ADDRESS);
|
| 480 |
|
|
} else if (permit_raw_files) {
|
| 481 |
|
|
secpp = rawsection(codef);
|
| 482 |
|
|
entry = RESET_ADDRESS;
|
| 483 |
|
|
} else {
|
| 484 |
|
|
fprintf(stderr, "ERR: %s is not in ELF format\n", codef);
|
| 485 |
|
|
exit(EXIT_FAILURE);
|
| 486 |
|
|
}
|
| 487 |
|
|
|
| 488 |
|
|
printf("Loading: %s\n", codef);
|
| 489 |
|
|
// assert(secpp[1]->m_len = 0);
|
| 490 |
|
|
for(int i=0; secpp[i]->m_len; i++) {
|
| 491 |
|
|
bool valid = false;
|
| 492 |
|
|
secp= secpp[i];
|
| 493 |
|
|
if ((secp->m_start >= RESET_ADDRESS)
|
| 494 |
|
|
&&(secp->m_start+secp->m_len
|
| 495 |
|
|
<= SPIFLASH+FLASHWORDS))
|
| 496 |
|
|
valid = true;
|
| 497 |
14 |
dgisselq |
if ((secp->m_start >= RAMBASE)
|
| 498 |
|
|
&&(secp->m_start+secp->m_len
|
| 499 |
|
|
<= RAMBASE+MEMWORDS))
|
| 500 |
|
|
valid = true;
|
| 501 |
11 |
dgisselq |
if (!valid) {
|
| 502 |
|
|
fprintf(stderr, "No such memory on board: 0x%08x - %08x\n",
|
| 503 |
|
|
secp->m_start, secp->m_start+secp->m_len);
|
| 504 |
|
|
exit(EXIT_FAILURE);
|
| 505 |
|
|
}
|
| 506 |
|
|
}
|
| 507 |
|
|
|
| 508 |
19 |
dgisselq |
unsigned startaddr = RESET_ADDRESS, codelen = 0;
|
| 509 |
11 |
dgisselq |
for(int i=0; secpp[i]->m_len; i++) {
|
| 510 |
|
|
secp = secpp[i];
|
| 511 |
14 |
dgisselq |
if ((secp->m_start >= RAMBASE)
|
| 512 |
|
|
&&(secp->m_start+secp->m_len
|
| 513 |
|
|
<= RAMBASE+MEMWORDS)) {
|
| 514 |
|
|
for(int i=0; (unsigned)i<secp->m_len; i++) {
|
| 515 |
|
|
if (secp->m_data[i] != 0) {
|
| 516 |
|
|
fprintf(stderr, "ERR: Cannot set RAM upon bootup!\n");
|
| 517 |
|
|
fprintf(stderr, "(The bootloaders just not that smart ... yet)\n");
|
| 518 |
19 |
dgisselq |
fprintf(stderr, "Attempting to set %08x - %08x\n", secp->m_start, secp->m_start+secp->m_len-1);
|
| 519 |
|
|
fprintf(stderr, "%08x cannot be set to %08x\n", secp->m_start+i, secp->m_data[i]);
|
| 520 |
14 |
dgisselq |
exit(EXIT_FAILURE);
|
| 521 |
|
|
}
|
| 522 |
|
|
}
|
| 523 |
19 |
dgisselq |
} else {
|
| 524 |
|
|
if (secp->m_start < startaddr) {
|
| 525 |
|
|
codelen += (startaddr-secp->m_start);
|
| 526 |
|
|
startaddr = secp->m_start;
|
| 527 |
|
|
} if (secp->m_start+secp->m_len > startaddr+codelen) {
|
| 528 |
|
|
codelen = secp->m_start+secp->m_len-startaddr;
|
| 529 |
|
|
} memcpy(&fbuf[secp->m_start-SPIFLASH],
|
| 530 |
|
|
secp->m_data,
|
| 531 |
|
|
secp->m_len*sizeof(FPGA::BUSW));
|
| 532 |
11 |
dgisselq |
}
|
| 533 |
|
|
}
|
| 534 |
26 |
dgisselq |
if ((flash)&&(!flash->write(startaddr, codelen, &fbuf[startaddr-SPIFLASH], true))) {
|
| 535 |
19 |
dgisselq |
fprintf(stderr, "ERR: Could not write program to flash\n");
|
| 536 |
|
|
exit(EXIT_FAILURE);
|
| 537 |
26 |
dgisselq |
} else if (!flash)
|
| 538 |
|
|
printf("flash->write(%08x, %d, ... );\n", startaddr,
|
| 539 |
|
|
codelen);
|
| 540 |
|
|
if (m_fpga) m_fpga->readio(R_VERSION); // Check for bus errors
|
| 541 |
11 |
dgisselq |
|
| 542 |
|
|
// Now ... how shall we start this CPU?
|
| 543 |
|
|
printf("The CPU should be fully loaded, you may now start\n");
|
| 544 |
|
|
printf("it. To start the CPU, either toggle power or type\n");
|
| 545 |
|
|
printf("%% wbregs fpgagen1 0 \n");
|
| 546 |
|
|
printf("%% wbregs fpgagen2 0x0300 \n");
|
| 547 |
|
|
printf("%% wbregs fpgacmd 14 \n");
|
| 548 |
|
|
} catch(BUSERR a) {
|
| 549 |
|
|
fprintf(stderr, "XULA-BUS error: %08x\n", a.addr);
|
| 550 |
|
|
exit(-2);
|
| 551 |
|
|
}
|
| 552 |
|
|
|
| 553 |
26 |
dgisselq |
if (m_fpga) delete m_fpga;
|
| 554 |
11 |
dgisselq |
|
| 555 |
|
|
return EXIT_SUCCESS;
|
| 556 |
|
|
}
|
| 557 |
|
|
|