From 4c494e36aed13512118200566faaac5a375afe92 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 31 Mar 2021 11:48:06 +0800 Subject: [PATCH] Use blackbox to rebuild AXI-flash (#726) * AXIFlash: use blackbox to rebuild flash * device.cpp: add init_flash * Add flash.cpp for DPI-C funtion * Flash: use USE_BIN to enable FI from flash * AXIFlash: delete original flash --- Makefile | 5 +++ src/main/scala/device/AXI4Flash.scala | 58 ++++++++++++++++++--------- src/test/csrc/device.cpp | 2 + src/test/csrc/difftest.cpp | 6 ++- src/test/csrc/emu.cpp | 10 +++++ src/test/csrc/flash.cpp | 41 +++++++++++++++++++ src/test/csrc/flash.h | 6 +++ 7 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 src/test/csrc/flash.cpp create mode 100644 src/test/csrc/flash.h diff --git a/Makefile b/Makefile index 66b6add50..264df1c39 100644 --- a/Makefile +++ b/Makefile @@ -107,6 +107,11 @@ ifeq ($(DUALCORE),1) EMU_CXXFLAGS += -DDUALCORE endif +USE_BIN ?= 0 +ifeq ($(USE_BIN),1) +EMU_CXXFLAGS += -DUSE_BIN +endif + # --trace VERILATOR_FLAGS = --top-module $(EMU_TOP) \ +define+VERILATOR=1 \ diff --git a/src/main/scala/device/AXI4Flash.scala b/src/main/scala/device/AXI4Flash.scala index 90b7f3a0a..ebe2d933d 100644 --- a/src/main/scala/device/AXI4Flash.scala +++ b/src/main/scala/device/AXI4Flash.scala @@ -6,6 +6,38 @@ import chipsalliance.rocketchip.config.Parameters import freechips.rocketchip.diplomacy.AddressSet import utils._ +class FlashHelper extends BlackBox with HasBlackBoxInline { + val io = IO(new Bundle { + val clk = Input(Clock()) + val ren = Input(Bool()) + val data = Output(UInt(64.W)) + val addr = Input(UInt(32.W)) + }) + + setInline("FlashHelper.v", + s""" + |import "DPI-C" function void flash_read + |( + | input int addr, + | output longint data + |); + | + |module FlashHelper ( + | input clk, + | input [31:0] addr, + | input ren, + | output reg [63:0] data + |); + | + | always @(posedge clk) begin + | if (ren) flash_read(addr, data); + | end + | + |endmodule + """.stripMargin) +} + + class AXI4Flash ( address: Seq[AddressSet] @@ -14,23 +46,13 @@ class AXI4Flash { override lazy val module = new AXI4SlaveModuleImp(this){ - val jmpToDramInstr1 = "h0010029b".U // addiw t0,zero,1 - val jmpToDramInstr2 = "h01f29293".U // slli t0,t0,0x1f - val jmpToDramInstr3 = "h00028067".U // jr t0 - - val mapping = Map( - RegMap(0x0, jmpToDramInstr1, RegMap.Unwritable), - RegMap(0x4, jmpToDramInstr2, RegMap.Unwritable), - RegMap(0x8, jmpToDramInstr3, RegMap.Unwritable) - ) - def getOffset(addr: UInt) = addr(12,0) - - val rdata = Wire(Vec(2,UInt(32.W))) - (0 until 2).map{ i => - RegMap.generate(mapping, getOffset(raddr + (i * 4).U), rdata(i), - getOffset(waddr), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb)) - } - - in.r.bits.data := rdata.asUInt + def getOffset(addr: UInt) = addr(15,0) + + val flash = Module(new FlashHelper) + flash.io.clk := clock + flash.io.ren := in.ar.fire() + flash.io.addr := Cat(0.U(16.W), getOffset(raddr)) + + in.r.bits.data := flash.io.data } } diff --git a/src/test/csrc/device.cpp b/src/test/csrc/device.cpp index 745c00e84..8ea98f39b 100644 --- a/src/test/csrc/device.cpp +++ b/src/test/csrc/device.cpp @@ -7,6 +7,7 @@ void init_sdl(void); void init_uart(void); extern "C" void init_sd(void); +extern "C" void init_flash(void); static struct timeval boot = {}; @@ -14,6 +15,7 @@ void init_device(void) { init_sdl(); init_uart(); init_sd(); + init_flash(); gettimeofday(&boot, NULL); } diff --git a/src/test/csrc/difftest.cpp b/src/test/csrc/difftest.cpp index 224b49011..07ff878cd 100644 --- a/src/test/csrc/difftest.cpp +++ b/src/test/csrc/difftest.cpp @@ -51,7 +51,11 @@ void init_difftest() { } for (int i = 0; i < NumCore; i++) { - nemu_this_pc[i] = 0x80000000; +#ifdef USE_BIN + nemu_this_pc[i] = 0x10000000; +#else + nemu_this_pc[i] = 0x80000000; +#endif pc_retire_pointer[i] = DEBUG_RETIRE_TRACE_SIZE-1; } } diff --git a/src/test/csrc/emu.cpp b/src/test/csrc/emu.cpp index 7f3f9ced8..308e77af5 100644 --- a/src/test/csrc/emu.cpp +++ b/src/test/csrc/emu.cpp @@ -611,12 +611,22 @@ uint64_t Emulator::execute(uint64_t max_cycle, uint64_t max_instr) { } for (int i = 0; i < NumCore; i++) { +#ifdef USE_BIN +#ifdef DUALCORE + int first_instr_commit = (i == 0) ? dut_ptr->io_difftest_commit && dut_ptr->io_difftest_thisPC == 0x10000000u : + dut_ptr->io_difftest2_commit && dut_ptr->io_difftest2_thisPC == 0x10000000u; +#else + int first_instr_commit = dut_ptr->io_difftest_commit && dut_ptr->io_difftest_thisPC == 0x10000000u; +#endif +#else #ifdef DUALCORE int first_instr_commit = (i == 0) ? dut_ptr->io_difftest_commit && dut_ptr->io_difftest_thisPC == 0x80000000u : dut_ptr->io_difftest2_commit && dut_ptr->io_difftest2_thisPC == 0x80000000u; #else int first_instr_commit = dut_ptr->io_difftest_commit && dut_ptr->io_difftest_thisPC == 0x80000000u; +#endif #endif + if (!hascommit[i] && first_instr_commit) { hascommit[i] = 1; #ifdef DUALCORE diff --git a/src/test/csrc/flash.cpp b/src/test/csrc/flash.cpp new file mode 100644 index 000000000..8896018ec --- /dev/null +++ b/src/test/csrc/flash.cpp @@ -0,0 +1,41 @@ +#include "common.h" +#include "flash.h" + +FILE *flash_fp = NULL; + +extern "C" { + +void flash_read(uint32_t addr, uint64_t *data) { +#ifdef USE_BIN + fseek(flash_fp, addr, SEEK_SET); + fread(data, 8, 1, flash_fp); +#else + uint32_t index = addr & 0x00000fff; + switch(index>>3){ + case 0 : + *data = 0x01f292930010029b; + break; + case 1 : + *data = 0x00028067; + break; + default : + *data = 0; + } +#endif +} + +void init_flash(void) { +#ifdef USE_BIN + + flash_fp = fopen("/home/jy/Project/nexus-am/tests/cputest/build/dummy-riscv64-noop.bin", "r"); + if(!flash_fp) + { + eprintf(ANSI_COLOR_MAGENTA "[warning] flash img not found\n"); + } + printf("use bin as a flash!\n"); +#else + printf("use fixed 3 instructions!\n"); +#endif +} + +} diff --git a/src/test/csrc/flash.h b/src/test/csrc/flash.h new file mode 100644 index 000000000..3fbd4949c --- /dev/null +++ b/src/test/csrc/flash.h @@ -0,0 +1,6 @@ +#ifndef __FLASH_H +#define __FLASH_H + +#include "common.h" + +#endif // __FLASH_H -- GitLab