xref: /XiangShan/src/main/scala/device/AXI4Flash.scala (revision 510ae4ee6820204606a64d649eefa05784ab6021)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
4c6d43980SLemover*
5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2.
6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at:
8c6d43980SLemover*          http://license.coscl.org.cn/MulanPSL2
9c6d43980SLemover*
10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13c6d43980SLemover*
14c6d43980SLemover* See the Mulan PSL v2 for more details.
15c6d43980SLemover***************************************************************************************/
16c6d43980SLemover
17b4cc98d2SZihao Yupackage device
18b4cc98d2SZihao Yu
19b4cc98d2SZihao Yuimport chisel3._
20b4cc98d2SZihao Yuimport chisel3.util._
21956d83c0Slinjiaweiimport chipsalliance.rocketchip.config.Parameters
22*510ae4eeSJiuyang Liuimport chisel3.experimental.ExtModule
23956d83c0Slinjiaweiimport freechips.rocketchip.diplomacy.AddressSet
24b4cc98d2SZihao Yuimport utils._
25b4cc98d2SZihao Yu
26*510ae4eeSJiuyang Liuclass FlashHelper extends ExtModule with HasExtModuleInline {
27*510ae4eeSJiuyang Liu  val clk = IO(Input(Clock()))
28*510ae4eeSJiuyang Liu  val ren = IO(Input(Bool()))
29*510ae4eeSJiuyang Liu  val data = IO(Output(UInt(64.W)))
30*510ae4eeSJiuyang Liu  val addr = IO(Input(UInt(32.W)))
314c494e36SJay
324c494e36SJay  setInline("FlashHelper.v",
334c494e36SJay    s"""
344c494e36SJay       |import "DPI-C" function void flash_read
354c494e36SJay       |(
364c494e36SJay       |  input int addr,
374c494e36SJay       |  output longint data
384c494e36SJay       |);
394c494e36SJay       |
404c494e36SJay       |module FlashHelper (
414c494e36SJay       |  input clk,
424c494e36SJay       |  input [31:0] addr,
434c494e36SJay       |  input ren,
444c494e36SJay       |  output reg [63:0] data
454c494e36SJay       |);
464c494e36SJay       |
474c494e36SJay       |  always @(posedge clk) begin
484c494e36SJay       |    if (ren) flash_read(addr, data);
494c494e36SJay       |  end
504c494e36SJay       |
514c494e36SJay       |endmodule
524c494e36SJay     """.stripMargin)
534c494e36SJay}
544c494e36SJay
554c494e36SJay
56956d83c0Slinjiaweiclass AXI4Flash
57956d83c0Slinjiawei(
58a2e9bde6SAllen  address: Seq[AddressSet]
59956d83c0Slinjiawei)(implicit p: Parameters)
60956d83c0Slinjiawei  extends AXI4SlaveModule(address, executable = false)
61956d83c0Slinjiawei{
62956d83c0Slinjiawei
63956d83c0Slinjiawei  override lazy val module = new AXI4SlaveModuleImp(this){
644c494e36SJay    def getOffset(addr: UInt) = addr(15,0)
65b4cc98d2SZihao Yu
664c494e36SJay    val flash = Module(new FlashHelper)
67*510ae4eeSJiuyang Liu    flash.clk := clock
68*510ae4eeSJiuyang Liu    flash.ren := in.ar.fire()
69*510ae4eeSJiuyang Liu    flash.addr := Cat(0.U(16.W), getOffset(raddr))
70b4cc98d2SZihao Yu
71*510ae4eeSJiuyang Liu    in.r.bits.data := flash.data
72b4cc98d2SZihao Yu  }
73956d83c0Slinjiawei}
74