xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 321934c75b13e4fd90c75bcc9464844b802b2442)
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
1745c767e3SLinJiaweipackage top
1845c767e3SLinJiawei
198891a219SYinan Xuimport org.chipsalliance.cde.config.{Config, Parameters}
2045c767e3SLinJiaweiimport system.SoCParamsKey
2134ab1ae9SJiawei Linimport xiangshan.{DebugOptionsKey, XSTileKey}
22*321934c7SKunlin Youimport difftest.DifftestModule
2345c767e3SLinJiawei
2445c767e3SLinJiaweiimport scala.annotation.tailrec
2545c767e3SLinJiaweiimport scala.sys.exit
2645c767e3SLinJiawei
2745c767e3SLinJiaweiobject ArgParser {
2845c767e3SLinJiawei  // TODO: add more explainations
2945c767e3SLinJiawei  val usage =
3045c767e3SLinJiawei    """
3145c767e3SLinJiawei      |XiangShan Options
3245c767e3SLinJiawei      |--xs-help                  print this help message
3345c767e3SLinJiawei      |--config <ConfigClassName>
3445c767e3SLinJiawei      |--num-cores <Int>
3545c767e3SLinJiawei      |--with-dramsim3
361545277aSYinan Xu      |--fpga-platform
371545277aSYinan Xu      |--enable-difftest
381545277aSYinan Xu      |--enable-log
39839e5512SZifei Zhang      |--with-chiseldb
40839e5512SZifei Zhang      |--with-rollingdb
4145c767e3SLinJiawei      |--disable-perf
4245c767e3SLinJiawei      |""".stripMargin
4345c767e3SLinJiawei
4445c767e3SLinJiawei  def getConfigByName(confString: String): Parameters = {
4545c767e3SLinJiawei    var prefix = "top." // default package is 'top'
4645c767e3SLinJiawei    if(confString.contains('.')){ // already a full name
4745c767e3SLinJiawei      prefix = ""
4845c767e3SLinJiawei    }
4945c767e3SLinJiawei    val c = Class.forName(prefix + confString).getConstructor(Integer.TYPE)
5045c767e3SLinJiawei    c.newInstance(1.asInstanceOf[Object]).asInstanceOf[Parameters]
5145c767e3SLinJiawei  }
5251e45dbbSTang Haojin  def parse(args: Array[String]): (Parameters, Array[String], Array[String]) = {
5345c767e3SLinJiawei    val default = new DefaultConfig(1)
5445c767e3SLinJiawei    var firrtlOpts = Array[String]()
55b665b650STang Haojin    var firtoolOpts = Array[String]()
5645c767e3SLinJiawei    @tailrec
5745c767e3SLinJiawei    def nextOption(config: Parameters, list: List[String]): Parameters = {
5845c767e3SLinJiawei      list match {
5945c767e3SLinJiawei        case Nil => config
6045c767e3SLinJiawei        case "--xs-help" :: tail =>
6145c767e3SLinJiawei          println(usage)
6245c767e3SLinJiawei          if(tail == Nil) exit(0)
6345c767e3SLinJiawei          nextOption(config, tail)
6445c767e3SLinJiawei        case "--config" :: confString :: tail =>
6545c767e3SLinJiawei          nextOption(getConfigByName(confString), tail)
6645c767e3SLinJiawei        case "--num-cores" :: value :: tail =>
6745c767e3SLinJiawei          nextOption(config.alter((site, here, up) => {
6834ab1ae9SJiawei Lin            case XSTileKey => (0 until value.toInt) map{ i =>
6934ab1ae9SJiawei Lin              up(XSTileKey).head.copy(HartId = i)
7034ab1ae9SJiawei Lin            }
7145c767e3SLinJiawei          }), tail)
7245c767e3SLinJiawei        case "--with-dramsim3" :: tail =>
7345c767e3SLinJiawei          nextOption(config.alter((site, here, up) => {
7445c767e3SLinJiawei            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
7545c767e3SLinJiawei          }), tail)
76b8890d17SZifei Zhang        case "--with-chiseldb" :: tail =>
77b8890d17SZifei Zhang          nextOption(config.alter((site, here, up) => {
78b8890d17SZifei Zhang            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableChiselDB = true)
79b8890d17SZifei Zhang          }), tail)
80839e5512SZifei Zhang        case "--with-rollingdb" :: tail =>
81839e5512SZifei Zhang          nextOption(config.alter((site, here, up) => {
82839e5512SZifei Zhang            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableRollingDB = true)
83839e5512SZifei Zhang          }), tail)
84047e34f9SMaxpicca-Li        case "--with-constantin" :: tail =>
85047e34f9SMaxpicca-Li          nextOption(config.alter((site, here, up) => {
86047e34f9SMaxpicca-Li            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableConstantin = true)
87047e34f9SMaxpicca-Li          }), tail)
881545277aSYinan Xu        case "--fpga-platform" :: tail =>
8945c767e3SLinJiawei          nextOption(config.alter((site, here, up) => {
901545277aSYinan Xu            case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true)
911545277aSYinan Xu          }), tail)
921545277aSYinan Xu        case "--enable-difftest" :: tail =>
931545277aSYinan Xu          nextOption(config.alter((site, here, up) => {
941545277aSYinan Xu            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true)
951545277aSYinan Xu          }), tail)
961545277aSYinan Xu        case "--enable-log" :: tail =>
971545277aSYinan Xu          nextOption(config.alter((site, here, up) => {
981545277aSYinan Xu            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true)
9945c767e3SLinJiawei          }), tail)
10045c767e3SLinJiawei        case "--disable-perf" :: tail =>
10145c767e3SLinJiawei          nextOption(config.alter((site, here, up) => {
10245c767e3SLinJiawei            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
10345c767e3SLinJiawei          }), tail)
104a5b77de4STang Haojin        case "--xstop-prefix" :: value :: tail if chisel3.BuildInfo.version != "3.6.0" =>
105a5b77de4STang Haojin          nextOption(config.alter((site, here, up) => {
106a5b77de4STang Haojin            case SoCParamsKey => up(SoCParamsKey).copy(XSTopPrefix = Some(value))
107a5b77de4STang Haojin          }), tail)
108b665b650STang Haojin        case "--firtool-opt" :: option :: tail =>
10951e45dbbSTang Haojin          firtoolOpts ++= option.split(" ").filter(_.nonEmpty)
110b665b650STang Haojin          nextOption(config, tail)
11145c767e3SLinJiawei        case option :: tail =>
11245c767e3SLinJiawei          // unknown option, maybe a firrtl option, skip
11345c767e3SLinJiawei          firrtlOpts :+= option
11445c767e3SLinJiawei          nextOption(config, tail)
11545c767e3SLinJiawei      }
11645c767e3SLinJiawei    }
117*321934c7SKunlin You    val newArgs = DifftestModule.parseArgs(args)
118*321934c7SKunlin You    var config = nextOption(default, newArgs.toList)
11951e45dbbSTang Haojin    (config, firrtlOpts, firtoolOpts)
12045c767e3SLinJiawei  }
12145c767e3SLinJiawei}
122