1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* 4* XiangShan is licensed under Mulan PSL v2. 5* You can use this software according to the terms and conditions of the Mulan PSL v2. 6* You may obtain a copy of Mulan PSL v2 at: 7* http://license.coscl.org.cn/MulanPSL2 8* 9* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12* 13* See the Mulan PSL v2 for more details. 14***************************************************************************************/ 15 16package device 17 18import chisel3._ 19import chisel3.util._ 20import chipsalliance.rocketchip.config.Parameters 21import freechips.rocketchip.diplomacy.AddressSet 22import utils._ 23 24class FlashHelper extends BlackBox with HasBlackBoxInline { 25 val io = IO(new Bundle { 26 val clk = Input(Clock()) 27 val ren = Input(Bool()) 28 val data = Output(UInt(64.W)) 29 val addr = Input(UInt(32.W)) 30 }) 31 32 setInline("FlashHelper.v", 33 s""" 34 |import "DPI-C" function void flash_read 35 |( 36 | input int addr, 37 | output longint data 38 |); 39 | 40 |module FlashHelper ( 41 | input clk, 42 | input [31:0] addr, 43 | input ren, 44 | output reg [63:0] data 45 |); 46 | 47 | always @(posedge clk) begin 48 | if (ren) flash_read(addr, data); 49 | end 50 | 51 |endmodule 52 """.stripMargin) 53} 54 55 56class AXI4Flash 57( 58 address: Seq[AddressSet] 59)(implicit p: Parameters) 60 extends AXI4SlaveModule(address, executable = false) 61{ 62 63 override lazy val module = new AXI4SlaveModuleImp(this){ 64 def getOffset(addr: UInt) = addr(15,0) 65 66 val flash = Module(new FlashHelper) 67 flash.io.clk := clock 68 flash.io.ren := in.ar.fire() 69 flash.io.addr := Cat(0.U(16.W), getOffset(raddr)) 70 71 in.r.bits.data := flash.io.data 72 } 73} 74