不管是RISC-V, MIPS, Nios II, MicroBlaze, MSP430, 8051, OpenRISC, OpenSPARC, LEON2/LEON3等等软核处理器,在FPGA上实现的时候我们通常需要一部分片上RAM存储bootloader,可以使用gcc的objcopy把bootloader的text, exception vector, rodata, data or bss等你需要的内容copy出来,可以通过-O binary生成二进制文件,可以通过-j .section提取你要的section,当然也可以通过--gap-fill 0x00 参数指定默认各sections之间空白填充的数据。通过以下C程序可以生成符合Altera (Intel)、Xilinx和Verilog中$readmemh调用的内存初始化数据。可以根据不同处理器的大小端字节序做修改,下面的程序为小端序设计。

/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* copyright @ Lyu Yang
*/ #include <stdio.h>
#include <stdlib.h> int main(int argc, char** argv)
{
int i, j, FileLen = 0, address = 0, maxaddr;
FILE * FileIn, * FileOut; if (argc != 3) {
printf("Arguments Error!\n");
printf("\nUsage: bin2fpga.exe Depth InputFile\n");
return 0;
}
else { FileIn = fopen(*(argv + 2), "rb");
if (FileIn == NULL){
printf("File does not exist!\n");
return 0;
} fseek(FileIn, 0L, SEEK_END);
FileLen = ftell(FileIn);
maxaddr = atoi(*(argv + 1)); unsigned char * buf = (unsigned char*)malloc(sizeof(char)* FileLen);
if(buf == NULL) {
printf("Memory Allocate Error!\n");
return 0;
} // For altera fpga mif file
FileOut = fopen("./altera.mif", "w"); if (FileOut == NULL) {
printf("Output File Create Error!\n");
return 0;
} fprintf(FileOut, "DEPTH = %d;\nWIDTH = 32;\nADDRESS_RADIX = DEC;\nDATA_RADIX = HEX;\nCONTENT\nBEGIN\n", maxaddr); fseek(FileIn, 0L, SEEK_SET);
fread(buf, FileLen, 1, FileIn); for (i = 0; i < FileLen; i += 4) {
fprintf(FileOut, "%d: ", address); fprintf(FileOut, "%02x", buf[i + 3]);
fprintf(FileOut, "%02x", buf[i + 2]);
fprintf(FileOut, "%02x", buf[i + 1]);
fprintf(FileOut, "%02x", buf[i + 0]); fprintf(FileOut, ";\n");
address++;
} fprintf(FileOut, "[%d..%d]: 0;\n", address, maxaddr - 1); fprintf(FileOut, "\n\nEND;\n"); fclose(FileOut); // For xilinx fpga mif file
FileOut = fopen("./xilinx.mif", "w"); if (FileOut == NULL) {
printf("Output File Create Error!\n");
return 0;
} fseek(FileIn, 0L, SEEK_SET);
fread(buf, FileLen, 1, FileIn); for (i = 0; i < FileLen; i += 4) {
unsigned int word = *((unsigned int*)(buf + i));
int x = 32;
while(x--)
fprintf(FileOut, "%c", (word >> x & 0x1) + '0'); fprintf(FileOut, "\n");
} fclose(FileOut); // For xilinx fpga coe file
FileOut = fopen("./xilinx.coe", "w"); if (FileOut == NULL) {
printf("Output File Create Error!\n");
return 0;
} fprintf(FileOut, "MEMORY_INITIALIZATION_RADIX=16;\nMEMORY_INITIALIZATION_VECTOR=\n"); fseek(FileIn, 0L, SEEK_SET);
fread(buf, FileLen, 1, FileIn); for (i = 0; i < FileLen; i += 4) {
fprintf(FileOut, "%02x", buf[i + 3]);
fprintf(FileOut, "%02x", buf[i + 2]);
fprintf(FileOut, "%02x", buf[i + 1]);
fprintf(FileOut, "%02x", buf[i + 0]); fprintf(FileOut, ",\n");
} fseek(FileOut, -2L, SEEK_END);
fprintf(FileOut, ";\n"); fclose(FileOut); // Generic verilog readmemh file
FileOut = fopen("./readmemh.txt", "w"); if (FileOut == NULL) {
printf("Output File Create Error!\n");
return 0;
} address = 0x0;
fseek(FileIn, 0L, SEEK_SET);
fread(buf, FileLen, 1, FileIn); for (i = 0; i < FileLen; i += 4) {
fprintf(FileOut, "@%08x\n", address); fprintf(FileOut, "%02x", buf[i + 3]);
fprintf(FileOut, "%02x", buf[i + 2]);
fprintf(FileOut, "%02x", buf[i + 1]);
fprintf(FileOut, "%02x", buf[i + 0]); fprintf(FileOut, "\n");
address++;
} free(buf);
fclose(FileIn);
fclose(FileOut);
} return 0;
}

各种软核处理器二进制文件FPGA初始化文件生成程序的更多相关文章

  1. 【小梅哥FPGA进阶教程】MC8051软核在FPGA上的使用

    十.MC8051软核在FPGA上的使用 本教程内容力求以详细的步骤和讲解让读者以最快的方式学会 MC8051 IP core 的应用以及相关设计软件的使用,并激起读者对 SOPC 技术的兴趣.本实验重 ...

  2. wrHDL编译中软核代码初始化及编译耗时长的问题

    问题的提出整个WR的ISE工程比较大,编译时间很长,导致开发效率低.通过分析发现,ISE在综合的时候大量的时间都花在了初始化DPRAM上.调研发现Xilinx提供了BMM文件和DATA2MEM工具,可 ...

  3. [置顶] 基于FPGA的VGA简易显存设计&NIOS ii软核接入

    项目简介 本项目基于Altera公司的Cyclone IV型芯片,利用NIOS II软核,2-port RAM与时序控制模块,实现64*48分辨率的显存(再大的显存板载资源m9k不够用) 实现效果如下 ...

  4. FPGA的软核与硬核

    硬核 zynq和pynq系列的fpga都是双ARM/Cortex-A9构成,这里的ARM处理器为硬核,Cortex-A9部分为FPGA部分.即整体分为两部分:PS/PL.PS部分为A9处理器部分,PL ...

  5. 关于Quartus构建nios软核以及eclipse建立c语言工程以及成功下载到FPGA芯片过程遇到的各种问题以及解决方法详解

    这不是一篇构建nios的教程,而是遇到的各种问题以及解决方法.至于构建教程,网上一大把,我推荐正点原子的FPGA教程,比较新,比较详细,通俗易懂!!! 这里以一个点亮LED灯的Nios软核为例,很明显 ...

  6. 可编程逻辑(FPGA)与硬核处理器(HPS)之间互联的结构

    本周我想进一步探究可编程逻辑(FPGA)与硬核处理器(HPS)之间互联的结构.我发现了三种主要方式,它们是如何映射并处理通信的,哪些组件需要管控时序并且有访问权限. AXI Bridge 为了能够实现 ...

  7. Spartan6上软核系统自定义外设调用AXI Stream FFT经验

    这几天希望能在Spartan系列新品xc6slx16csg324-2运行带有FFT的软核处理系统,基本系统早就搭建好了.需要做的就是建立一个封装有Xilinx提供的FFT IP的自定义外设.由于Xil ...

  8. 【lattice软核】MICO8流程

    The LatticeMico System software is composed of three bundled applications:  Mico System Builder (MS ...

  9. ISE创建Microblaze软核(三)

    第七步 进入SDK开发环境 编译完成后弹出如下对话框,选择SDK的工作目录.在MicroblazeTutor中创建一个Workspace文件夹,并选择该文件夹为SDK的工作目录. 进入SDK主界面. ...

随机推荐

  1. POJ 2387 Til the Cows Come Home(dijkstra裸题)

    题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...

  2. LeetCode解题报告—— Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  3. hdu 2389(二分图hk算法模板)

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  4. 关于在ASP.NET MVC 中使用EF的Code First的方式来读取数据库时的Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    今天在做一个小网站的时候遇到很多问题唉,我还是个菜鸟,懂的也不多,今天一个表单的提交按钮用不了,都弄了几个小时唉.不过最后还是搞定了,还有浏览器有开发人员选项,不然我都不知道我还要继续排查多久哦,今天 ...

  5. LintCode 13. Implement strStr()

    LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...

  6. 使用supervisor管理后台进程

    在linux中supervisor是用来管理后台进程的,是一个用python写的进程管理工具,可以让宕机的进程重启.这里我们大概讲一下用他来管理uWSGI. 一.安装supervisor 1.pyth ...

  7. 最短路&生成树&二分图匹配&费用流问题

    最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...

  8. Luogu P2486 染色(树链剖分+线段树)

    题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...

  9. HDU 6118 2017百度之星初赛B 度度熊的交易计划(费用流)

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. ALL运算符

    ALL在英文中的意思是“所有”,ALL运算符要求比较的值需要匹配子查询中的所有值.ALL运算符同样不能单独使用,必须和比较运算符共同使用. 下面的SQL语句用来检索在所有会员入会之前出版的图书: SE ...