xref: /XiangShan/src/main/scala/device/AXI4Flash.scala (revision 8891a219bbc84f568e1d134854d8d5ed86d6d560)
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 org.chipsalliance.cde.config.Parameters
22import chisel3.experimental.ExtModule
23import freechips.rocketchip.diplomacy.AddressSet
24import utils._
25import utility._
26
27class FlashHelper extends ExtModule with HasExtModuleInline {
28  val clk = IO(Input(Clock()))
29  val ren = IO(Input(Bool()))
30  val data = IO(Output(UInt(64.W)))
31  val addr = IO(Input(UInt(32.W)))
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.clk := clock
69    flash.ren := in.ar.fire
70    flash.addr := Cat(0.U(16.W), getOffset(raddr))
71
72    in.r.bits.data := flash.data
73  }
74}
75