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 chisel3.experimental.ExtModule 23import freechips.rocketchip.diplomacy.AddressSet 24import utils._ 25 26class FlashHelper extends ExtModule with HasExtModuleInline { 27 val clk = IO(Input(Clock())) 28 val ren = IO(Input(Bool())) 29 val data = IO(Output(UInt(64.W))) 30 val addr = IO(Input(UInt(32.W))) 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.clk := clock 68 flash.ren := in.ar.fire() 69 flash.addr := Cat(0.U(16.W), getOffset(raddr)) 70 71 in.r.bits.data := flash.data 72 } 73} 74