xref: /XiangShan/src/test/scala/top/SimTop.scala (revision a0938898e1b6ff239c643d50979b81a67de974f9)
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
172225d46eSJiawei Linpackage top
182225d46eSJiawei Lin
1988ca983fSYinan Xuimport chipsalliance.rocketchip.config.Parameters
202225d46eSJiawei Linimport chisel3._
2188ca983fSYinan Xuimport chisel3.util._
2288ca983fSYinan Xuimport chisel3.stage.ChiselGeneratorAnnotation
23d4aca96cSlqreimport device.{AXI4RAMWrapper, SimJTAG}
24*a0938898SLinJiaweiimport freechips.rocketchip.diplomacy.{DisableMonitors, LazyModule, LazyModuleImp}
25*a0938898SLinJiaweiimport utils.GTimer
26*a0938898SLinJiaweiimport xiangshan.{DebugOptions, DebugOptionsKey}
27a3e87608SWilliam Wangimport difftest._
2888ca983fSYinan Xuimport freechips.rocketchip.diplomacy.{DisableMonitors, LazyModule}
297ba24bbcSJiawei Linimport freechips.rocketchip.util.ElaborationArtefacts
30*a0938898SLinJiaweiimport huancun.utils.ChiselDB
317ba24bbcSJiawei Linimport top.TopMain.writeOutputFile
3288ca983fSYinan Xuimport utils.GTimer
3388ca983fSYinan Xuimport xiangshan.DebugOptionsKey
342225d46eSJiawei Lin
352225d46eSJiawei Linclass SimTop(implicit p: Parameters) extends Module {
362225d46eSJiawei Lin  val debugOpts = p(DebugOptionsKey)
372225d46eSJiawei Lin  val useDRAMSim = debugOpts.UseDRAMSim
382225d46eSJiawei Lin
3973be64b3SJiawei Lin  val l_soc = LazyModule(new XSTop())
402225d46eSJiawei Lin  val soc = Module(l_soc.module)
413a62c537SYinan Xu  // Don't allow the top-level signals to be optimized out,
423a62c537SYinan Xu  // so that we can re-use this SimTop for any generated Verilog RTL.
433a62c537SYinan Xu  dontTouch(soc.io)
442225d46eSJiawei Lin
4598c71602SJiawei Lin  l_soc.module.dma <> 0.U.asTypeOf(l_soc.module.dma)
4673be64b3SJiawei Lin
4773be64b3SJiawei Lin  val l_simMMIO = LazyModule(new SimMMIO(l_soc.misc.peripheralNode.in.head._2))
482225d46eSJiawei Lin  val simMMIO = Module(l_simMMIO.module)
4973be64b3SJiawei Lin  l_simMMIO.io_axi4 <> soc.peripheral
502225d46eSJiawei Lin
512225d46eSJiawei Lin  if(!useDRAMSim){
522225d46eSJiawei Lin    val l_simAXIMem = LazyModule(new AXI4RAMWrapper(
5373be64b3SJiawei Lin      l_soc.misc.memAXI4SlaveNode, 8L * 1024 * 1024 * 1024, useBlackBox = true
542225d46eSJiawei Lin    ))
552225d46eSJiawei Lin    val simAXIMem = Module(l_simAXIMem.module)
5673be64b3SJiawei Lin    l_simAXIMem.io_axi4 <> soc.memory
572225d46eSJiawei Lin  }
582225d46eSJiawei Lin
5973be64b3SJiawei Lin  soc.io.clock := clock.asBool
6073be64b3SJiawei Lin  soc.io.reset := reset.asBool
61b6a21a24SYinan Xu  soc.io.extIntrs := simMMIO.io.interrupt.intrVec
628130d625Srvcoresjw  soc.io.sram_config := 0.U
6398c71602SJiawei Lin  soc.io.pll0_lock := true.B
6498c71602SJiawei Lin  soc.io.cacheable_check := DontCare
65c4b44470SGuokai Chen  soc.io.riscv_rst_vec.foreach(_ := 0x10000000L.U)
6688ca983fSYinan Xu
6788ca983fSYinan Xu  // soc.io.rtc_clock is a div100 of soc.io.clock
689e56439dSHazard  val rtcClockDiv = 100
6988ca983fSYinan Xu  val rtcTickCycle = rtcClockDiv / 2
7088ca983fSYinan Xu  val rtcCounter = RegInit(0.U(log2Ceil(rtcTickCycle + 1).W))
7188ca983fSYinan Xu  rtcCounter := Mux(rtcCounter === (rtcTickCycle - 1).U, 0.U, rtcCounter + 1.U)
7288ca983fSYinan Xu  val rtcClock = RegInit(false.B)
7388ca983fSYinan Xu  when (rtcCounter === 0.U) {
7488ca983fSYinan Xu    rtcClock := ~rtcClock
7588ca983fSYinan Xu  }
7688ca983fSYinan Xu  soc.io.rtc_clock := rtcClock
772225d46eSJiawei Lin
78d4aca96cSlqre  val success = Wire(Bool())
79cc358710SLinJiawei  val jtag = Module(new SimJTAG(tickDelay=3)(p)).connect(soc.io.systemjtag.jtag, clock, reset.asBool, !reset.asBool, success)
80d4aca96cSlqre  soc.io.systemjtag.reset := reset
81d4aca96cSlqre  soc.io.systemjtag.mfr_id := 0.U(11.W)
82d4aca96cSlqre  soc.io.systemjtag.part_number := 0.U(16.W)
83d4aca96cSlqre  soc.io.systemjtag.version := 0.U(4.W)
84d4aca96cSlqre
852225d46eSJiawei Lin  val io = IO(new Bundle(){
862225d46eSJiawei Lin    val logCtrl = new LogCtrlIO
872225d46eSJiawei Lin    val perfInfo = new PerfInfoIO
882225d46eSJiawei Lin    val uart = new UARTIO
8973be64b3SJiawei Lin    val memAXI = if(useDRAMSim) soc.memory.cloneType else null
902225d46eSJiawei Lin  })
912225d46eSJiawei Lin
922225d46eSJiawei Lin  simMMIO.io.uart <> io.uart
932225d46eSJiawei Lin
942225d46eSJiawei Lin  if(useDRAMSim){
9573be64b3SJiawei Lin    io.memAXI <> soc.memory
962225d46eSJiawei Lin  }
972225d46eSJiawei Lin
981545277aSYinan Xu  if (!debugOpts.FPGAPlatform && (debugOpts.EnableDebug || debugOpts.EnablePerfDebug)) {
992225d46eSJiawei Lin    val timer = GTimer()
1002225d46eSJiawei Lin    val logEnable = (timer >= io.logCtrl.log_begin) && (timer < io.logCtrl.log_end)
1012225d46eSJiawei Lin    ExcitingUtils.addSource(logEnable, "DISPLAY_LOG_ENABLE")
1022225d46eSJiawei Lin    ExcitingUtils.addSource(timer, "logTimestamp")
1032225d46eSJiawei Lin  }
1042225d46eSJiawei Lin
1051545277aSYinan Xu  if (!debugOpts.FPGAPlatform && debugOpts.EnablePerfDebug) {
1062225d46eSJiawei Lin    val clean = io.perfInfo.clean
1072225d46eSJiawei Lin    val dump = io.perfInfo.dump
1082225d46eSJiawei Lin    ExcitingUtils.addSource(clean, "XSPERF_CLEAN")
1092225d46eSJiawei Lin    ExcitingUtils.addSource(dump, "XSPERF_DUMP")
1102225d46eSJiawei Lin  }
1112225d46eSJiawei Lin
1122225d46eSJiawei Lin  // Check and dispaly all source and sink connections
1132225d46eSJiawei Lin  ExcitingUtils.fixConnections()
1142225d46eSJiawei Lin  ExcitingUtils.checkAndDisplay()
1152225d46eSJiawei Lin}
1162225d46eSJiawei Lin
1172225d46eSJiawei Linobject SimTop extends App {
1182225d46eSJiawei Lin  override def main(args: Array[String]): Unit = {
1191545277aSYinan Xu    // Keep this the same as TopMain except that SimTop is used here instead of XSTop
120cc358710SLinJiawei    val (config, firrtlOpts, firrtlComplier) = ArgParser.parse(args)
121cc358710SLinJiawei    Generator.execute(
122cc358710SLinJiawei      firrtlOpts,
123cc358710SLinJiawei      DisableMonitors(p => new SimTop()(p))(config),
124cc358710SLinJiawei      firrtlComplier
125cc358710SLinJiawei    )
126*a0938898SLinJiawei    ChiselDB.addToElaborationArtefacts
127*a0938898SLinJiawei    ElaborationArtefacts.files.foreach{
128*a0938898SLinJiawei      case (extension, contents) =>
129*a0938898SLinJiawei        val prefix = extension match {
130*a0938898SLinJiawei          case "h" | "cpp" => "chisel_db"
131*a0938898SLinJiawei          case _ => "XSTop"
132*a0938898SLinJiawei        }
133*a0938898SLinJiawei        writeOutputFile("./build", s"$prefix.${extension}", contents())
1347ba24bbcSJiawei Lin    }
1352225d46eSJiawei Lin  }
1362225d46eSJiawei Lin}
137