xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 8891a219bbc84f568e1d134854d8d5ed86d6d560)
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}
22
23import scala.annotation.tailrec
24import scala.sys.exit
25
26object ArgParser {
27  // TODO: add more explainations
28  val usage =
29    """
30      |XiangShan Options
31      |--xs-help                  print this help message
32      |--config <ConfigClassName>
33      |--num-cores <Int>
34      |--with-dramsim3
35      |--fpga-platform
36      |--enable-difftest
37      |--enable-log
38      |--with-chiseldb
39      |--with-rollingdb
40      |--disable-perf
41      |--mfc
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], FirrtlCompiler, Array[String]) = {
53    val default = new DefaultConfig(1)
54    var firrtlOpts = Array[String]()
55    var firrtlCompiler: FirrtlCompiler = SFC
56    var firtoolOpts = Array[String]()
57    @tailrec
58    def nextOption(config: Parameters, list: List[String]): Parameters = {
59      list match {
60        case Nil => config
61        case "--xs-help" :: tail =>
62          println(usage)
63          if(tail == Nil) exit(0)
64          nextOption(config, tail)
65        case "--config" :: confString :: tail =>
66          nextOption(getConfigByName(confString), tail)
67        case "--num-cores" :: value :: tail =>
68          nextOption(config.alter((site, here, up) => {
69            case XSTileKey => (0 until value.toInt) map{ i =>
70              up(XSTileKey).head.copy(HartId = i)
71            }
72          }), tail)
73        case "--with-dramsim3" :: tail =>
74          nextOption(config.alter((site, here, up) => {
75            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
76          }), tail)
77        case "--with-chiseldb" :: tail =>
78          nextOption(config.alter((site, here, up) => {
79            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableChiselDB = true)
80          }), tail)
81        case "--with-rollingdb" :: tail =>
82          nextOption(config.alter((site, here, up) => {
83            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableRollingDB = true)
84          }), tail)
85        case "--with-constantin" :: tail =>
86          nextOption(config.alter((site, here, up) => {
87            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableConstantin = true)
88          }), tail)
89        case "--fpga-platform" :: tail =>
90          nextOption(config.alter((site, here, up) => {
91            case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true)
92          }), tail)
93        case "--enable-difftest" :: tail =>
94          nextOption(config.alter((site, here, up) => {
95            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true)
96          }), tail)
97        case "--enable-log" :: tail =>
98          nextOption(config.alter((site, here, up) => {
99            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true)
100          }), tail)
101        case "--disable-perf" :: tail =>
102          nextOption(config.alter((site, here, up) => {
103            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
104          }), tail)
105        case "--mfc" :: tail =>
106          firrtlCompiler = MFC
107          nextOption(config, tail)
108        case "--firtool-opt" :: option :: tail =>
109          firtoolOpts :+= option
110          nextOption(config, tail)
111        case option :: tail =>
112          // unknown option, maybe a firrtl option, skip
113          firrtlOpts :+= option
114          nextOption(config, tail)
115      }
116    }
117    var config = nextOption(default, args.toList)
118    (config, firrtlOpts, firrtlCompiler, firtoolOpts)
119  }
120}
121