各種軟核處理器二進位檔案FPGA初始設定檔案產生程式

來源:互聯網
上載者:User

標籤:

  不管是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、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 ----------------------------2015-10-02 */#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);        printf("\nInput File length is %d Bytes.\n", FileLen);        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 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", maxaddr);        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 data file        FileOut = fopen("./data.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初始設定檔案產生程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.