xref: /XiangShan/src/main/scala/device/AXI4Flash.scala (revision dc597826530cb6803c2396d6ab0e5eb176b732e0)
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 Chisel.BlackBox
22import chipsalliance.rocketchip.config.Parameters
23import freechips.rocketchip.diplomacy.AddressSet
24import utils._
25
26class FlashHelper extends BlackBox with HasBlackBoxInline {
27  val io = IO(new Bundle {
28    val clk = Input(Clock())
29    val ren = Input(Bool())
30    val data = Output(UInt(64.W))
31    val addr = Input(UInt(32.W))
32  })
33
34  setInline("FlashHelper.v",
35    s"""
36       |import "DPI-C" function void flash_read
37       |(
38       |  input int addr,
39       |  output longint data
40       |);
41       |
42       |module FlashHelper (
43       |  input clk,
44       |  input [31:0] addr,
45       |  input ren,
46       |  output reg [63:0] data
47       |);
48       |
49       |  always @(posedge clk) begin
50       |    if (ren) flash_read(addr, data);
51       |  end
52       |
53       |endmodule
54     """.stripMargin)
55}
56
57
58class AXI4Flash
59(
60  address: Seq[AddressSet]
61)(implicit p: Parameters)
62  extends AXI4SlaveModule(address, executable = false)
63{
64
65  override lazy val module = new AXI4SlaveModuleImp(this){
66    def getOffset(addr: UInt) = addr(15,0)
67
68    val flash = Module(new FlashHelper)
69    flash.io.clk := clock
70    flash.io.ren := in.ar.fire()
71    flash.io.addr := Cat(0.U(16.W), getOffset(raddr))
72
73    in.r.bits.data := flash.io.data
74  }
75}
76