不管是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调用的内存初始化数据。可以根据不同处理器的大小端字节序做修改,下面的程序为小端序设计。

  1. /*
  2. * This program is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * copyright @ Lyu Yang
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. int main(int argc, char** argv)
  22. {
  23. int i, j, FileLen = 0, address = 0, maxaddr;
  24. FILE * FileIn, * FileOut;
  25.  
  26. if (argc != 3) {
  27. printf("Arguments Error!\n");
  28. printf("\nUsage: bin2fpga.exe Depth InputFile\n");
  29. return 0;
  30. }
  31. else {
  32.  
  33. FileIn = fopen(*(argv + 2), "rb");
  34. if (FileIn == NULL){
  35. printf("File does not exist!\n");
  36. return 0;
  37. }
  38.  
  39. fseek(FileIn, 0L, SEEK_END);
  40. FileLen = ftell(FileIn);
  41. maxaddr = atoi(*(argv + 1));
  42.  
  43. unsigned char * buf = (unsigned char*)malloc(sizeof(char)* FileLen);
  44. if(buf == NULL) {
  45. printf("Memory Allocate Error!\n");
  46. return 0;
  47. }
  48.  
  49. // For altera fpga mif file
  50. FileOut = fopen("./altera.mif", "w");
  51.  
  52. if (FileOut == NULL) {
  53. printf("Output File Create Error!\n");
  54. return 0;
  55. }
  56.  
  57. fprintf(FileOut, "DEPTH = %d;\nWIDTH = 32;\nADDRESS_RADIX = DEC;\nDATA_RADIX = HEX;\nCONTENT\nBEGIN\n", maxaddr);
  58.  
  59. fseek(FileIn, 0L, SEEK_SET);
  60. fread(buf, FileLen, 1, FileIn);
  61.  
  62. for (i = 0; i < FileLen; i += 4) {
  63. fprintf(FileOut, "%d: ", address);
  64.  
  65. fprintf(FileOut, "%02x", buf[i + 3]);
  66. fprintf(FileOut, "%02x", buf[i + 2]);
  67. fprintf(FileOut, "%02x", buf[i + 1]);
  68. fprintf(FileOut, "%02x", buf[i + 0]);
  69.  
  70. fprintf(FileOut, ";\n");
  71. address++;
  72. }
  73.  
  74. fprintf(FileOut, "[%d..%d]: 0;\n", address, maxaddr - 1);
  75.  
  76. fprintf(FileOut, "\n\nEND;\n");
  77.  
  78. fclose(FileOut);
  79.  
  80. // For xilinx fpga mif file
  81. FileOut = fopen("./xilinx.mif", "w");
  82.  
  83. if (FileOut == NULL) {
  84. printf("Output File Create Error!\n");
  85. return 0;
  86. }
  87.  
  88. fseek(FileIn, 0L, SEEK_SET);
  89. fread(buf, FileLen, 1, FileIn);
  90.  
  91. for (i = 0; i < FileLen; i += 4) {
  92. unsigned int word = *((unsigned int*)(buf + i));
  93. int x = 32;
  94. while(x--)
  95. fprintf(FileOut, "%c", (word >> x & 0x1) + '0');
  96.  
  97. fprintf(FileOut, "\n");
  98. }
  99.  
  100. fclose(FileOut);
  101.  
  102. // For xilinx fpga coe file
  103. FileOut = fopen("./xilinx.coe", "w");
  104.  
  105. if (FileOut == NULL) {
  106. printf("Output File Create Error!\n");
  107. return 0;
  108. }
  109.  
  110. fprintf(FileOut, "MEMORY_INITIALIZATION_RADIX=16;\nMEMORY_INITIALIZATION_VECTOR=\n");
  111.  
  112. fseek(FileIn, 0L, SEEK_SET);
  113. fread(buf, FileLen, 1, FileIn);
  114.  
  115. for (i = 0; i < FileLen; i += 4) {
  116. fprintf(FileOut, "%02x", buf[i + 3]);
  117. fprintf(FileOut, "%02x", buf[i + 2]);
  118. fprintf(FileOut, "%02x", buf[i + 1]);
  119. fprintf(FileOut, "%02x", buf[i + 0]);
  120.  
  121. fprintf(FileOut, ",\n");
  122. }
  123.  
  124. fseek(FileOut, -2L, SEEK_END);
  125. fprintf(FileOut, ";\n");
  126.  
  127. fclose(FileOut);
  128.  
  129. // Generic verilog readmemh file
  130. FileOut = fopen("./readmemh.txt", "w");
  131.  
  132. if (FileOut == NULL) {
  133. printf("Output File Create Error!\n");
  134. return 0;
  135. }
  136.  
  137. address = 0x0;
  138. fseek(FileIn, 0L, SEEK_SET);
  139. fread(buf, FileLen, 1, FileIn);
  140.  
  141. for (i = 0; i < FileLen; i += 4) {
  142. fprintf(FileOut, "@%08x\n", address);
  143.  
  144. fprintf(FileOut, "%02x", buf[i + 3]);
  145. fprintf(FileOut, "%02x", buf[i + 2]);
  146. fprintf(FileOut, "%02x", buf[i + 1]);
  147. fprintf(FileOut, "%02x", buf[i + 0]);
  148.  
  149. fprintf(FileOut, "\n");
  150. address++;
  151. }
  152.  
  153. free(buf);
  154. fclose(FileIn);
  155. fclose(FileOut);
  156. }
  157.  
  158. return 0;
  159. }

各种软核处理器二进制文件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 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

  2. Matlab处理数据导出Paraview可读的vtk文件(一)

    Paraview是一个开源的可视化软件. 用到matlab子程序从这里下载 或者到博客末尾复制粘贴 子程序名为 vtkwrite 示例1: load mri D = squeeze(D); vtkwr ...

  3. Jmeter+Jenkins持续集成(Ant运行脚本)

    Jmeter接口测试脚本运行后生成的是jtl(xml)格式的文件,这些文件不具备可读性,所以我们要把他转化为可以阅读的html格式报告. Ant是一个功能强大的打包编译工具.我们使用他的目的是将xml ...

  4. CentOS 6.5,SSH安装与配置

    #rpm -qa |grep ssh 检查是否装了SSH包 #yum install openssh-server 没有的话,安装SSH服务 #chkconfig --list sshd 检查SSHD ...

  5. 京东前端:PhantomJS 和NodeJS在网站前端监控平台的最佳实践

    1. 为什么需要一个前端监控系统 通常在一个大型的 Web 项目中有很多监控系统,比如后端的服务 API 监控,接口存活.调用.延迟等监控,这些一般都用来监控后台接口数据层面的信息.而且对于大型网站系 ...

  6. 单断言VS多断言

    STST 想和大家讨论一下,一个测试用例里只做一个断言还是一个用例里做多个相关的断言 比如有一个查询函数Query(id) 返回[姓名,性别,年龄] 那么是在一个测试用例里对这三个属性进行断言好? 还 ...

  7. HDU 5862 Counting Intersections(离散化 + 树状数组)

    Counting Intersections Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. [ARC 066] Tutorial

    Link: ARC 066 传送门 C: 如果存在可行方案则答案为$2^{n/2}$ #include <bits/stdc++.h> using namespace std; #defi ...

  9. 【计算几何】【预处理】【枚举】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem K. Kiwi Trees

    发现由于角的度数和边的长度有限制,那俩圆如果放得下的话,必然是塞在两个角里. 于是预处理n个圆心的位置(注意要判断那个圆会不会和其他的边界相交),然后n^2枚举俩角即可. #include<cs ...

  10. Java学习笔记(7)

    构造函数: 构造函数的作用:给对应的对象进行初始化. 构造函数定义的格式: 修饰符   函数名(形式参数){ 函数体... } 构造函数要注意的细节: 构造函数是没有返回值类型的 构造函数的函数名必须 ...