xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 0ffeff0dfd0776be463415a25137f43b59b336e4)
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 org.chipsalliance.cde.config.{Config, Parameters}
20import system.SoCParamsKey
21import xiangshan.{DebugOptionsKey, XSTileKey}
22import difftest.DifftestModule
23
24import scala.annotation.tailrec
25import scala.sys.exit
26
27object ArgParser {
28  // TODO: add more explainations
29  val usage =
30    """
31      |XiangShan Options
32      |--xs-help                  print this help message
33      |--config <ConfigClassName>
34      |--num-cores <Int>
35      |--with-dramsim3
36      |--fpga-platform
37      |--enable-difftest
38      |--enable-log
39      |--with-chiseldb
40      |--with-rollingdb
41      |--disable-perf
42      |""".stripMargin
43
44  def getConfigByName(confString: String): Parameters = {
45    var prefix = "top." // default package is 'top'
46    if(confString.contains('.')){ // already a full name
47      prefix = ""
48    }
49    val c = Class.forName(prefix + confString).getConstructor(Integer.TYPE)
50    c.newInstance(1.asInstanceOf[Object]).asInstanceOf[Parameters]
51  }
52  def parse(args: Array[String]): (Parameters, Array[String], Array[String]) = {
53    val default = new DefaultConfig(1)
54    var firrtlOpts = Array[String]()
55    var firtoolOpts = Array[String]()
56    @tailrec
57    def nextOption(config: Parameters, list: List[String]): Parameters = {
58      list match {
59        case Nil => config
60        case "--xs-help" :: tail =>
61          println(usage)
62          if(tail == Nil) exit(0)
63          nextOption(config, tail)
64        case "--config" :: confString :: tail =>
65          nextOption(getConfigByName(confString), tail)
66        case "--num-cores" :: value :: tail =>
67          nextOption(config.alter((site, here, up) => {
68            case XSTileKey => (0 until value.toInt) map{ i =>
69              up(XSTileKey).head.copy(HartId = i)
70            }
71          }), tail)
72        case "--with-dramsim3" :: tail =>
73          nextOption(config.alter((site, here, up) => {
74            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
75          }), tail)
76        case "--with-chiseldb" :: tail =>
77          nextOption(config.alter((site, here, up) => {
78            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableChiselDB = true)
79          }), tail)
80        case "--with-rollingdb" :: tail =>
81          nextOption(config.alter((site, here, up) => {
82            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableRollingDB = true)
83          }), tail)
84        case "--with-constantin" :: tail =>
85          nextOption(config.alter((site, here, up) => {
86            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableConstantin = true)
87          }), tail)
88        case "--fpga-platform" :: tail =>
89          nextOption(config.alter((site, here, up) => {
90            case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true)
91          }), tail)
92        case "--enable-difftest" :: tail =>
93          nextOption(config.alter((site, here, up) => {
94            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true)
95          }), tail)
96        case "--disable-always-basic-diff" :: tail =>
97          nextOption(config.alter((site, here, up) => {
98            case DebugOptionsKey => up(DebugOptionsKey).copy(AlwaysBasicDiff = false)
99          }), tail)
100        case "--enable-log" :: tail =>
101          nextOption(config.alter((site, here, up) => {
102            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true)
103          }), tail)
104        case "--disable-perf" :: tail =>
105          nextOption(config.alter((site, here, up) => {
106            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
107          }), tail)
108        case "--xstop-prefix" :: value :: tail if chisel3.BuildInfo.version != "3.6.0" =>
109          nextOption(config.alter((site, here, up) => {
110            case SoCParamsKey => up(SoCParamsKey).copy(XSTopPrefix = Some(value))
111          }), tail)
112        case "--firtool-opt" :: option :: tail =>
113          firtoolOpts ++= option.split(" ").filter(_.nonEmpty)
114          nextOption(config, tail)
115        case option :: tail =>
116          // unknown option, maybe a firrtl option, skip
117          firrtlOpts :+= option
118          nextOption(config, tail)
119      }
120    }
121    val newArgs = DifftestModule.parseArgs(args)
122    var config = nextOption(default, newArgs.toList)
123    (config, firrtlOpts, firtoolOpts)
124  }
125}
126