xref: /XiangShan/src/main/scala/top/Top.scala (revision 57bb43b5f11c3f1e89ac52f232fe73056b35d9bd)
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 top
18
19import chisel3._
20import chisel3.util._
21import xiangshan._
22import utils._
23import system._
24import device._
25import chisel3.stage.ChiselGeneratorAnnotation
26import chipsalliance.rocketchip.config._
27import device.{AXI4Plic, DebugModule, TLTimer}
28import freechips.rocketchip.diplomacy._
29import freechips.rocketchip.tilelink._
30import freechips.rocketchip.amba.axi4._
31import freechips.rocketchip.devices.tilelink._
32import freechips.rocketchip.diplomaticobjectmodel.logicaltree.GenericLogicalTreeNode
33import freechips.rocketchip.interrupts._
34import freechips.rocketchip.jtag.JTAGIO
35import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, XLen}
36import freechips.rocketchip.tilelink
37import freechips.rocketchip.util.{ElaborationArtefacts, HasRocketChipStageUtils, UIntToOH1}
38import huancun.debug.TLLogger
39import huancun.{HCCacheParamsKey, HuanCun}
40import freechips.rocketchip.devices.debug.{DebugIO, ResetCtrlIO}
41
42abstract class BaseXSSoc()(implicit p: Parameters) extends LazyModule
43  with BindingScope
44{
45  val misc = LazyModule(new SoCMisc())
46  lazy val dts = DTS(bindingTree)
47  lazy val json = JSON(bindingTree)
48}
49
50class XSTop()(implicit p: Parameters) extends BaseXSSoc() with HasSoCParameter
51{
52  ResourceBinding {
53    val width = ResourceInt(2)
54    val model = "freechips,rocketchip-unknown"
55    Resource(ResourceAnchors.root, "model").bind(ResourceString(model))
56    Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev"))
57    Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc"))
58    Resource(ResourceAnchors.root, "width").bind(width)
59    Resource(ResourceAnchors.soc, "width").bind(width)
60    Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1))
61    def bindManagers(xbar: TLNexusNode) = {
62      ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager =>
63        manager.resources.foreach(r => r.bind(manager.toResource))
64      }
65    }
66    bindManagers(misc.l3_xbar.asInstanceOf[TLNexusNode])
67    bindManagers(misc.peripheralXbar.asInstanceOf[TLNexusNode])
68  }
69
70  println(s"FPGASoC cores: $NumCores banks: $L3NBanks block size: $L3BlockSize bus size: $L3OuterBusWidth")
71
72  val core_with_l2 = tiles.map(coreParams =>
73    LazyModule(new XSTile()(p.alterPartial({
74      case XSCoreParamsKey => coreParams
75    })))
76  )
77
78  val l3cacheOpt = soc.L3CacheParamsOpt.map(l3param =>
79    LazyModule(new HuanCun()(new Config((_, _, _) => {
80      case HCCacheParamsKey => l3param
81    })))
82  )
83
84  for (i <- 0 until NumCores) {
85    core_with_l2(i).clint_int_sink := misc.clint.intnode
86    core_with_l2(i).plic_int_sink :*= misc.plic.intnode
87    core_with_l2(i).debug_int_sink := misc.debugModule.debug.dmOuter.dmOuter.intnode
88    misc.plic.intnode := IntBuffer() := core_with_l2(i).beu_int_source
89    misc.peripheral_ports(i) := core_with_l2(i).uncache
90    misc.core_to_l3_ports(i) :=* core_with_l2(i).memory_port
91  }
92
93  l3cacheOpt.map(_.ctlnode.map(_ := misc.peripheralXbar))
94  l3cacheOpt.map(_.intnode.map(int => {
95    misc.plic.intnode := IntBuffer() := int
96  }))
97
98  val core_rst_nodes = if(l3cacheOpt.nonEmpty && l3cacheOpt.get.rst_nodes.nonEmpty){
99    l3cacheOpt.get.rst_nodes.get
100  } else {
101    core_with_l2.map(_ => BundleBridgeSource(() => Bool()))
102  }
103
104  core_rst_nodes.zip(core_with_l2.map(_.core_reset_sink)).foreach({
105    case (source, sink) =>  sink := source
106  })
107
108  l3cacheOpt match {
109    case Some(l3) =>
110      misc.l3_out :*= l3.node :*= TLBuffer.chainNode(2) :*= misc.l3_banked_xbar
111    case None =>
112  }
113
114  lazy val module = new LazyRawModuleImp(this) {
115    ElaborationArtefacts.add("dts", dts)
116    ElaborationArtefacts.add("graphml", graphML)
117    ElaborationArtefacts.add("json", json)
118    ElaborationArtefacts.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader())
119
120    val dma = IO(Flipped(misc.dma.cloneType))
121    val peripheral = IO(misc.peripheral.cloneType)
122    val memory = IO(misc.memory.cloneType)
123
124    misc.dma <> dma
125    peripheral <> misc.peripheral
126    memory <> misc.memory
127
128    val io = IO(new Bundle {
129      val clock = Input(Bool())
130      val reset = Input(Bool())
131      val sram_config = Input(UInt(16.W))
132      val extIntrs = Input(UInt(NrExtIntr.W))
133      val pll0_lock = Input(Bool())
134      val pll0_ctrl = Output(Vec(6, UInt(32.W)))
135      val systemjtag = new Bundle {
136        val jtag = Flipped(new JTAGIO(hasTRSTn = false))
137        val reset = Input(Bool()) // No reset allowed on top
138        val mfr_id = Input(UInt(11.W))
139        val part_number = Input(UInt(16.W))
140        val version = Input(UInt(4.W))
141      }
142      val debug_reset = Output(Bool())
143      val cacheable_check = new TLPMAIO()
144    })
145    // override LazyRawModuleImp's clock and reset
146    childClock := io.clock.asClock
147    childReset := io.reset
148
149    // output
150    io.debug_reset := misc.module.debug_module_io.debugIO.ndreset
151
152    // input
153    dontTouch(dma)
154    dontTouch(io)
155    dontTouch(peripheral)
156    dontTouch(memory)
157    misc.module.ext_intrs := io.extIntrs
158    misc.module.pll0_lock := io.pll0_lock
159    misc.module.cacheable_check <> io.cacheable_check
160
161    io.pll0_ctrl <> misc.module.pll0_ctrl
162
163    for ((core, i) <- core_with_l2.zipWithIndex) {
164      core.module.io.hartId := i.U
165    }
166
167    if(l3cacheOpt.isEmpty || l3cacheOpt.get.rst_nodes.isEmpty){
168      // tie off core soft reset
169      for(node <- core_rst_nodes){
170        node.out.head._1 := false.B
171      }
172    }
173
174    misc.module.debug_module_io.resetCtrl.hartIsInReset := core_with_l2.map(_.module.reset.asBool)
175    misc.module.debug_module_io.clock := io.clock
176    misc.module.debug_module_io.reset := io.reset
177
178    // TODO: use synchronizer?
179    misc.module.debug_module_io.debugIO.reset := io.systemjtag.reset
180    misc.module.debug_module_io.debugIO.clock := io.clock.asClock
181    // TODO: delay 3 cycles?
182    misc.module.debug_module_io.debugIO.dmactiveAck := misc.module.debug_module_io.debugIO.dmactive
183    // jtag connector
184    misc.module.debug_module_io.debugIO.systemjtag.foreach { x =>
185      x.jtag        <> io.systemjtag.jtag
186      x.reset       := io.systemjtag.reset
187      x.mfr_id      := io.systemjtag.mfr_id
188      x.part_number := io.systemjtag.part_number
189      x.version     := io.systemjtag.version
190    }
191
192    withClockAndReset(io.clock.asClock, io.reset) {
193      // Modules are reset one by one
194      // reset ----> SYNC --> {SoCMisc, L3 Cache, Cores}
195      val resetChain = Seq(Seq(misc.module) ++ l3cacheOpt.map(_.module) ++ core_with_l2.map(_.module))
196      ResetGen(resetChain, io.reset, !debugOpts.FPGAPlatform)
197    }
198
199  }
200}
201
202object TopMain extends App with HasRocketChipStageUtils {
203  override def main(args: Array[String]): Unit = {
204    val (config, firrtlOpts) = ArgParser.parse(args)
205    val soc = DisableMonitors(p => LazyModule(new XSTop()(p)))(config)
206    XiangShanStage.execute(firrtlOpts, Seq(
207      ChiselGeneratorAnnotation(() => {
208        soc.module
209      })
210    ))
211    ElaborationArtefacts.files.foreach{ case (extension, contents) =>
212      writeOutputFile("./build", s"XSTop.${extension}", contents())
213    }
214  }
215}
216