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, Array[String]) = { 51 val default = new DefaultConfig(1) 52 var firrtlOpts = Array[String]() 53 var firrtlCompiler: FirrtlCompiler = SFC 54 var firtoolOpts = Array[String]() 55 @tailrec 56 def nextOption(config: Parameters, list: List[String]): Parameters = { 57 list match { 58 case Nil => config 59 case "--xs-help" :: tail => 60 println(usage) 61 if(tail == Nil) exit(0) 62 nextOption(config, tail) 63 case "--config" :: confString :: tail => 64 nextOption(getConfigByName(confString), tail) 65 case "--num-cores" :: value :: tail => 66 nextOption(config.alter((site, here, up) => { 67 case XSTileKey => (0 until value.toInt) map{ i => 68 up(XSTileKey).head.copy(HartId = i) 69 } 70 }), tail) 71 case "--with-dramsim3" :: tail => 72 nextOption(config.alter((site, here, up) => { 73 case DebugOptionsKey => up(DebugOptionsKey).copy(UseDRAMSim = true) 74 }), tail) 75 case "--with-chiseldb" :: tail => 76 nextOption(config.alter((site, here, up) => { 77 case DebugOptionsKey => up(DebugOptionsKey).copy(EnableChiselDB = true) 78 }), tail) 79 case "--with-constantin" :: tail => 80 nextOption(config.alter((site, here, up) => { 81 case DebugOptionsKey => up(DebugOptionsKey).copy(EnableConstantin = true) 82 }), tail) 83 case "--fpga-platform" :: tail => 84 nextOption(config.alter((site, here, up) => { 85 case DebugOptionsKey => up(DebugOptionsKey).copy(FPGAPlatform = true) 86 }), tail) 87 case "--enable-difftest" :: tail => 88 nextOption(config.alter((site, here, up) => { 89 case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDifftest = true) 90 }), tail) 91 case "--enable-log" :: tail => 92 nextOption(config.alter((site, here, up) => { 93 case DebugOptionsKey => up(DebugOptionsKey).copy(EnableDebug = true) 94 }), tail) 95 case "--disable-perf" :: tail => 96 nextOption(config.alter((site, here, up) => { 97 case DebugOptionsKey => up(DebugOptionsKey).copy(EnablePerfDebug = false) 98 }), tail) 99 case "--mfc" :: tail => 100 firrtlCompiler = MFC 101 nextOption(config, tail) 102 case "--firtool-opt" :: option :: tail => 103 firtoolOpts :+= option 104 nextOption(config, tail) 105 case option :: tail => 106 // unknown option, maybe a firrtl option, skip 107 firrtlOpts :+= option 108 nextOption(config, tail) 109 } 110 } 111 var config = nextOption(default, args.toList) 112 (config, firrtlOpts, firrtlCompiler, firtoolOpts) 113 } 114} 115