1package device 2 3import chisel3._ 4import chisel3.util._ 5import chipsalliance.rocketchip.config.Parameters 6import freechips.rocketchip.diplomacy.AddressSet 7import utils._ 8 9class FlashHelper extends BlackBox with HasBlackBoxInline { 10 val io = IO(new Bundle { 11 val clk = Input(Clock()) 12 val ren = Input(Bool()) 13 val data = Output(UInt(64.W)) 14 val addr = Input(UInt(32.W)) 15 }) 16 17 setInline("FlashHelper.v", 18 s""" 19 |import "DPI-C" function void flash_read 20 |( 21 | input int addr, 22 | output longint data 23 |); 24 | 25 |module FlashHelper ( 26 | input clk, 27 | input [31:0] addr, 28 | input ren, 29 | output reg [63:0] data 30 |); 31 | 32 | always @(posedge clk) begin 33 | if (ren) flash_read(addr, data); 34 | end 35 | 36 |endmodule 37 """.stripMargin) 38} 39 40 41class AXI4Flash 42( 43 address: Seq[AddressSet] 44)(implicit p: Parameters) 45 extends AXI4SlaveModule(address, executable = false) 46{ 47 48 override lazy val module = new AXI4SlaveModuleImp(this){ 49 def getOffset(addr: UInt) = addr(15,0) 50 51 val flash = Module(new FlashHelper) 52 flash.io.clk := clock 53 flash.io.ren := in.ar.fire() 54 flash.io.addr := Cat(0.U(16.W), getOffset(raddr)) 55 56 in.r.bits.data := flash.io.data 57 } 58} 59