/*************************************************************************************** * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences * Copyright (c) 2020-2021 Peng Cheng Laboratory * * XiangShan is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * * See the Mulan PSL v2 for more details. ***************************************************************************************/ package top import chisel3._ import chisel3.util._ import xiangshan._ import utils._ import system._ import device._ import chisel3.stage.ChiselGeneratorAnnotation import chipsalliance.rocketchip.config._ import device.{AXI4Plic, DebugModule, TLTimer} import freechips.rocketchip.diplomacy._ import freechips.rocketchip.tilelink._ import freechips.rocketchip.amba.axi4._ import freechips.rocketchip.devices.tilelink._ import freechips.rocketchip.diplomaticobjectmodel.logicaltree.GenericLogicalTreeNode import freechips.rocketchip.interrupts._ import freechips.rocketchip.jtag.JTAGIO import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, XLen} import freechips.rocketchip.tilelink import freechips.rocketchip.util.{ElaborationArtefacts, HasRocketChipStageUtils, UIntToOH1} import huancun.debug.TLLogger import huancun.{HCCacheParamsKey, HuanCun} import freechips.rocketchip.devices.debug.{DebugIO, ResetCtrlIO} abstract class BaseXSSoc()(implicit p: Parameters) extends LazyModule with BindingScope { val misc = LazyModule(new SoCMisc()) lazy val dts = DTS(bindingTree) lazy val json = JSON(bindingTree) } class XSTop()(implicit p: Parameters) extends BaseXSSoc() with HasSoCParameter { ResourceBinding { val width = ResourceInt(2) val model = "freechips,rocketchip-unknown" Resource(ResourceAnchors.root, "model").bind(ResourceString(model)) Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev")) Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc")) Resource(ResourceAnchors.root, "width").bind(width) Resource(ResourceAnchors.soc, "width").bind(width) Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1)) def bindManagers(xbar: TLNexusNode) = { ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager => manager.resources.foreach(r => r.bind(manager.toResource)) } } bindManagers(misc.l3_xbar.asInstanceOf[TLNexusNode]) bindManagers(misc.peripheralXbar.asInstanceOf[TLNexusNode]) } println(s"FPGASoC cores: $NumCores banks: $L3NBanks block size: $L3BlockSize bus size: $L3OuterBusWidth") val core_with_l2 = soc.cores.map(coreParams => LazyModule(new XSTile()(p.alterPartial({ case XSCoreParamsKey => coreParams }))) ) for (i <- 0 until NumCores) { core_with_l2(i).clint_int_sink := misc.clint.intnode core_with_l2(i).plic_int_sink := misc.plic.intnode core_with_l2(i).debug_int_sink := misc.debugModule.debug.dmOuter.dmOuter.intnode misc.plic.intnode := core_with_l2(i).beu_int_source misc.peripheral_ports(i) := core_with_l2(i).uncache misc.core_to_l3_ports(i) :=* core_with_l2(i).memory_port } val l3cacheOpt = soc.L3CacheParamsOpt.map(l3param => LazyModule(new HuanCun()(new Config((_, _, _) => { case HCCacheParamsKey => l3param }))) ) l3cacheOpt match { case Some(l3) => misc.l3_out :*= l3.node :*= misc.l3_in case None => } lazy val module = new LazyRawModuleImp(this) { ElaborationArtefacts.add("dts", dts) ElaborationArtefacts.add("graphml", graphML) ElaborationArtefacts.add("json", json) ElaborationArtefacts.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) val dma = IO(Flipped(misc.dma.cloneType)) val peripheral = IO(misc.peripheral.cloneType) val memory = IO(misc.memory.cloneType) misc.dma <> dma peripheral <> misc.peripheral memory <> misc.memory val io = IO(new Bundle { val clock = Input(Bool()) val reset = Input(Bool()) val sram_config = Input(UInt(5.W)) val osc_clock = Input(Bool()) val pll_output = Output(UInt(14.W)) val extIntrs = Input(UInt(NrExtIntr.W)) val systemjtag = new Bundle { val jtag = Flipped(new JTAGIO(hasTRSTn = false)) val reset = Input(Bool()) // No reset allowed on top val mfr_id = Input(UInt(11.W)) val part_number = Input(UInt(16.W)) val version = Input(UInt(4.W)) } }) io.pll_output := DontCare dontTouch(io.sram_config) dontTouch(io.osc_clock) dontTouch(io.pll_output) childClock := io.clock.asClock withClockAndReset(childClock, io.reset) { val resetGen = Module(new ResetGen(1, !debugOpts.FPGAPlatform)) resetGen.suggestName("top_reset_gen") childReset := resetGen.io.out | misc.module.debug_module_io.debugIO.ndreset } withClockAndReset(childClock, childReset) { misc.module.ext_intrs := io.extIntrs for (i <- 0 until NumCores) { val core_reset_gen = Module(new ResetGen(1, !debugOpts.FPGAPlatform)) core_reset_gen.suggestName(s"core_${i}_reset_gen") core_with_l2(i).module.reset := core_reset_gen.io.out core_with_l2(i).module.io.hartId := i.U } if (l3cacheOpt.nonEmpty) { val l3_reset_gen = Module(new ResetGen(1, !debugOpts.FPGAPlatform)) l3_reset_gen.suggestName("l3_reset_gen") l3cacheOpt.get.module.reset := l3_reset_gen.io.out } misc.module.debug_module_io.resetCtrl.hartIsInReset.foreach {x => x := childReset.asBool() } misc.module.debug_module_io.clock := io.clock misc.module.debug_module_io.reset := io.reset misc.module.debug_module_io.debugIO.reset := io.systemjtag.reset // TODO: use synchronizer? misc.module.debug_module_io.debugIO.clock := childClock misc.module.debug_module_io.debugIO.dmactiveAck := misc.module.debug_module_io.debugIO.dmactive // TODO: delay 3 cycles? // jtag connector misc.module.debug_module_io.debugIO.systemjtag.foreach { x => x.jtag <> io.systemjtag.jtag x.reset := io.systemjtag.reset x.mfr_id := io.systemjtag.mfr_id x.part_number := io.systemjtag.part_number x.version := io.systemjtag.version } } } } object TopMain extends App with HasRocketChipStageUtils { override def main(args: Array[String]): Unit = { val (config, firrtlOpts) = ArgParser.parse(args) val soc = DisableMonitors(p => LazyModule(new XSTop()(p)))(config) XiangShanStage.execute(firrtlOpts, Seq( ChiselGeneratorAnnotation(() => { soc.module }) )) ElaborationArtefacts.files.foreach{ case (extension, contents) => writeOutputFile("./build", s"XSTop.${extension}", contents()) } } }