不管是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. 6:django 通用视图

    上一节我们介绍了django视图函数里面几个常用的函数,这节我们来看一下django为我们提供的一些通用视图吧 在最后面有我自己的示例代码,html部分太多了就不贴了 “简单”视图函数 正如名字所言, ...

  2. linux命令(27):cat命令

    实例一:把 log2012.log 的文件内容加上行号后输入 log2013.log 这个文件里 cat -n log2012.log log2013.log 实例二:把 log2012.log 和 ...

  3. scala中常用特殊符号

    参考资料: scala中常用但其他语言不常见的符号含义 Scala学习六:Scala中的特殊字符 =>(匿名函数) 参考文档:scala => 用法 匿名函数 => 匿名函数,在Sp ...

  4. Search a 2D Matrix——两度二分查找

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. Android学习之Android studio篇-Android Studio快捷键总结(mac)

    原文:http://blog.csdn.net/hudfang/article/details/52117065 符号代表键盘按键:⌘(command).⌥(option).⇧(shift).⇪(ca ...

  6. win10的VMware虚机host-only模式下,虚拟机无法ping通物理机,而物理机能ping通虚机

    1.打开控制面板—->Windows防火墙(win10操作系统) 2.点击最上面的”允许应用或功能通过xxxxx” 3.勾上上图的“文件和打印机共享” 然后点确定.

  7. 如何开启apache的PHP-FPM实例

    PHP-FPM 作为 FastCGI 进程管理器而广为熟知,它是PHPFastCGI 实现的改进,带有更为有用的功能,用于处理高负载的服务器和网站.下面列出其中一些功能: 新功能 拥有具有优雅(gra ...

  8. 【ASP.NET】IHttpHandler和IHttpModule

    上篇文章我们主要讲了HttpApplicatiion管道事件,那么我么如何处理这些管道事件呢,以及请求在ASP.NET是如何执行的呢,我们来了解一下IHttpHandler和IHttpModule 引 ...

  9. 训练continue

    11.16 树状数组 http://codeforces.com/contest/1070/problem/C digit sum+% dp http://codeforces.com/contest ...

  10. 中国石油大学(华东)暑期集训--二进制(BZOJ5294)【线段树】

    问题 C: 二进制 时间限制: 1 Sec  内存限制: 128 MB提交: 8  解决: 2[提交] [状态] [讨论版] [命题人:] 题目描述 pupil发现对于一个十进制数,无论怎么将其的数字 ...