xref: /XiangShan/src/main/scala/top/ArgParser.scala (revision 7a2fc509e2d355879c4db3dc3f17a6ccacd3d09e)
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 chipsalliance.rocketchip.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      |--disable-perf
39      |--mfc
40      |""".stripMargin
41
42  def getConfigByName(confString: String): Parameters = {
43    var prefix = "top." // default package is 'top'
44    if(confString.contains('.')){ // already a full name
45      prefix = ""
46    }
47    val c = Class.forName(prefix + confString).getConstructor(Integer.TYPE)
48    c.newInstance(1.asInstanceOf[Object]).asInstanceOf[Parameters]
49  }
50  def parse(args: Array[String]): (Parameters, Array[String], FirrtlCompiler) = {
51    val default = new DefaultConfig(1)
52    var firrtlOpts = Array[String]()
53    var firrtlCompiler: FirrtlCompiler = SFC
54    @tailrec
55    def nextOption(config: Parameters, list: List[String]): Parameters = {
56      list match {
57        case Nil => config
58        case "--xs-help" :: tail =>
59          println(usage)
60          if(tail == Nil) exit(0)
61          nextOption(config, tail)
62        case "--config" :: confString :: tail =>
63          nextOption(getConfigByName(confString), tail)
64        case "--num-cores" :: value :: tail =>
65          nextOption(config.alter((site, here, up) => {
66            case XSTileKey => (0 until value.toInt) map{ i =>
67              up(XSTileKey).head.copy(HartId = i)
68            }
69          }), tail)
70        case "--with-dramsim3" :: tail =>
71          nextOption(config.alter((site, here, up) => {
72            case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true)
73          }), tail)
74        case "--fpga-platform" :: tail =>
75          nextOption(config.alter((site, here, up) => {
76            case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true)
77          }), tail)
78        case "--enable-difftest" :: tail =>
79          nextOption(config.alter((site, here, up) => {
80            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true)
81          }), tail)
82        case "--enable-log" :: tail =>
83          nextOption(config.alter((site, here, up) => {
84            case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true)
85          }), tail)
86        case "--disable-perf" :: tail =>
87          nextOption(config.alter((site, here, up) => {
88            case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false)
89          }), tail)
90        case "--mfc" :: tail =>
91          firrtlCompiler = MFC
92          nextOption(config, tail)
93        case option :: tail =>
94          // unknown option, maybe a firrtl option, skip
95          firrtlOpts :+= option
96          nextOption(config, tail)
97      }
98    }
99    var config = nextOption(default, args.toList)
100    (config, firrtlOpts, firrtlCompiler)
101  }
102}
103