xref: /XiangShan/src/main/scala/device/AXI4Flash.scala (revision f320e0f01bd645f0a3045a8a740e60dd770734a9)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3*f320e0f0SYinan 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
22956d83c0Slinjiaweiimport freechips.rocketchip.diplomacy.AddressSet
23b4cc98d2SZihao Yuimport utils._
24b4cc98d2SZihao Yu
254c494e36SJayclass FlashHelper extends BlackBox with HasBlackBoxInline {
264c494e36SJay  val io = IO(new Bundle {
274c494e36SJay    val clk = Input(Clock())
284c494e36SJay    val ren = Input(Bool())
294c494e36SJay    val data = Output(UInt(64.W))
304c494e36SJay    val addr = Input(UInt(32.W))
314c494e36SJay  })
324c494e36SJay
334c494e36SJay  setInline("FlashHelper.v",
344c494e36SJay    s"""
354c494e36SJay       |import "DPI-C" function void flash_read
364c494e36SJay       |(
374c494e36SJay       |  input int addr,
384c494e36SJay       |  output longint data
394c494e36SJay       |);
404c494e36SJay       |
414c494e36SJay       |module FlashHelper (
424c494e36SJay       |  input clk,
434c494e36SJay       |  input [31:0] addr,
444c494e36SJay       |  input ren,
454c494e36SJay       |  output reg [63:0] data
464c494e36SJay       |);
474c494e36SJay       |
484c494e36SJay       |  always @(posedge clk) begin
494c494e36SJay       |    if (ren) flash_read(addr, data);
504c494e36SJay       |  end
514c494e36SJay       |
524c494e36SJay       |endmodule
534c494e36SJay     """.stripMargin)
544c494e36SJay}
554c494e36SJay
564c494e36SJay
57956d83c0Slinjiaweiclass AXI4Flash
58956d83c0Slinjiawei(
59a2e9bde6SAllen  address: Seq[AddressSet]
60956d83c0Slinjiawei)(implicit p: Parameters)
61956d83c0Slinjiawei  extends AXI4SlaveModule(address, executable = false)
62956d83c0Slinjiawei{
63956d83c0Slinjiawei
64956d83c0Slinjiawei  override lazy val module = new AXI4SlaveModuleImp(this){
654c494e36SJay    def getOffset(addr: UInt) = addr(15,0)
66b4cc98d2SZihao Yu
674c494e36SJay    val flash = Module(new FlashHelper)
684c494e36SJay    flash.io.clk := clock
694c494e36SJay    flash.io.ren := in.ar.fire()
704c494e36SJay    flash.io.addr := Cat(0.U(16.W), getOffset(raddr))
71b4cc98d2SZihao Yu
724c494e36SJay    in.r.bits.data := flash.io.data
73b4cc98d2SZihao Yu  }
74956d83c0Slinjiawei}
75