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