1package xiangshan.backend.fu.NewCSR 2 3import chisel3._ 4 5import xiangshan.backend.fu.NewCSR.CSRFunc._ 6 7abstract class CSRRWType { 8 val wfn: CSRWfnType 9 val rfn: CSRRfnType 10 val ref: Option[CSREnumType] = None 11 12 def isRO: Boolean = this.isInstanceOf[ROType] || this.isInstanceOf[RefROType] 13 14 def isRW: Boolean = this.isInstanceOf[RWType] || this.isInstanceOf[RefRWType] 15 16 def isWARL: Boolean = this.isInstanceOf[WARLType] || this.isInstanceOf[RefWARLType] 17 18 def isWLRL: Boolean = this.isInstanceOf[WLRLType] || this.isInstanceOf[RefWLRLType] 19 20 def isRef: Boolean = this.isInstanceOf[RefROType] || this.isInstanceOf[RefRWType] || this.isInstanceOf[RefWARLType] || 21 this.isInstanceOf[RefWLRLType] 22 23 override def toString: String = { 24 val typeString = this match { 25 case WARLType(_, _) => "WARL" 26 case ROType(_) => "RO" 27 case WLRLType(_, _) => "WLRL" 28 case RWType() => "RW" 29 } 30 typeString + (if (isRef) " Ref" else "") 31 } 32} 33 34case class WARLType( 35 override val wfn: CSRWfnType, 36 override val rfn: CSRRfnType = null, 37) extends CSRRWType 38 39case class ROType( 40 override val rfn: CSRRfnType = null, 41) extends CSRRWType { 42 override final val wfn: CSRWfnType = wNoEffect 43} 44 45case class WLRLType( 46 override val wfn: CSRWfnType, 47 override val rfn: CSRRfnType, 48) extends CSRRWType 49 50case class RWType() extends CSRRWType { 51 override final val wfn: CSRWfnType = wNoFilter 52 override final val rfn: CSRRfnType = null 53} 54 55trait CheckRef { self: CSRRWType => 56 require(ref.nonEmpty) 57} 58 59case class RefWARLType( 60 override val ref: Option[CSREnumType], 61 override val wfn: CSRWfnType, 62 override val rfn: CSRRfnType = null, 63) extends CSRRWType with CheckRef 64 65case class RefROType( 66 override val ref: Option[CSREnumType], 67 override val rfn: CSRRfnType = null, 68) extends CSRRWType with CheckRef { 69 override final val wfn: CSRWfnType = wNoEffect 70} 71 72case class RefWLRLType( 73 override val ref: Option[CSREnumType], 74 override val wfn: CSRWfnType, 75 override val rfn: CSRRfnType, 76) extends CSRRWType with CheckRef 77 78case class RefRWType( 79 override val ref: Option[CSREnumType], 80) extends CSRRWType with CheckRef { 81 override final val wfn: CSRWfnType = wNoFilter 82 override final val rfn: CSRRfnType = null 83} 84 85object CSRFunc { 86 type CSRWfnType = (UInt, UInt, Seq[Data]) => UInt 87 88 def wNoFilter: CSRWfnType = 89 (newV: UInt, oldV: UInt, _: Seq[Data]) => newV 90 91 def wNoEffectWhen(keepCond: UInt => Bool): CSRWfnType = 92 (newV: UInt, oldV: UInt, _: Seq[Data]) => { 93 Mux(keepCond(newV), oldV, newV) 94 } 95 96 def wNoEffect: CSRWfnType = 97 (_: UInt, oldV: UInt, _: Seq[Data]) => { oldV } 98 99 type CSRRfnType = (UInt, Seq[Data]) => UInt 100 101 def rNoFilter: CSRRfnType = null 102 103 def rWithFilter(rFilter: (UInt, Seq[Data]) => UInt): CSRRfnType = 104 (oriV: UInt, seq: Seq[Data]) => rFilter(oriV, seq) 105 106 def rFixValue(value: UInt): CSRRfnType = { 107 (_, _) => value 108 } 109} 110 111class CSREnumType( 112 val msb: Int, 113 val lsb: Int, 114)( 115 var rwType: CSRRWType, 116 var init: Data = null 117)( 118 override val factory: ChiselEnum 119) extends EnumType(factory) { 120 121 if (factory.all.isEmpty) { 122 factory.asInstanceOf[CSREnum].addMinValue 123 } 124 125 if (this.init != null && !factory.all.exists(_.litValue == this.init.litValue)) { 126 factory.asInstanceOf[CSREnum].addNewValue(init.asUInt) 127 } 128 129 if (!factory.all.exists(_.litValue == ((BigInt(1) << (msb - lsb + 1)) - 1))) { 130 factory.asInstanceOf[CSREnum].addMaxValue 131 } 132 133 if (msb - lsb + 1 > this.getWidth) 134 println( 135 s"[CSRInfo] $this: " + 136 s"the setting range($msb, $lsb) of bitfield is widen than EnumType's width(${this.getWidth}), " + 137 s"the higher bits will be optimized" 138 ) 139 140 def isRef = this.rwType.isRef 141 142 def isRO = this.rwType.isRO 143 144 def isWARL = this.rwType.isWARL 145 146 // Check if the write data is legal that can update the regfield. 147 // Also check if the write field is not Read Only. 148 def isLegal: Bool = this.factory.asInstanceOf[CSREnum].isLegal(this) && (!this.isRO).B 149 150 def needReset: Boolean = init != null 151 152 def rfn: CSRRfnType = rwType.rfn 153 154 def wfn: CSRWfnType = rwType.wfn 155 156 // Check if reset with a enum value in factory.all 157 protected def resetCheck[T <: EnumType](init: T): Unit = { 158 resetCheckRWType 159 require(this.factory.all.contains(init), 160 s""" 161 | The value ${init.litValue} is NOT in ${factory.all}. 162 | Please check if $init is the enum in the $factory") 163 """.stripMargin 164 ) 165 } 166 167 // Check if reset with a enum value in factory.all 168 protected def resetCheck(init: UInt): Unit = { 169 resetCheckRWType 170 require(this.factory.all.exists(_.litValue == init.litValue), 171 s""" 172 |The value ${init.litValue} is not in ${factory.all}. 173 |Please add reset value as the tail of (msb,lsb, HERE) or (bit, HERE), If you need reset field with the value NOT in enum set. 174 | ^ ^ 175 |""".stripMargin 176 ) 177 } 178 179 protected def resetCheckRWType: Unit = { 180 rwType match { 181 case ROType(rfn) => require(rfn == null) 182 case _ => 183 } 184 } 185 186 def withReset[T <: EnumType](init: T): this.type = { 187 resetCheck(init) 188 this.init = init 189 if (!factory.all.exists(_.litValue == ((BigInt(1) << (msb - lsb + 1)) - 1))) { 190 factory.asInstanceOf[CSREnum].addMaxValue 191 } 192 this 193 } 194 195 def withReset(init: UInt): this.type = { 196 resetCheck(init) 197 this.init = this.factory(init) 198 if (!factory.all.exists(_.litValue == ((BigInt(1) << (msb - lsb + 1)) - 1))) { 199 factory.asInstanceOf[CSREnum].addMaxValue 200 } 201 this 202 } 203 204 // Reset using the value not in factory.all 205 def withNonEnumReset(init: UInt): this.type = { 206 resetCheckRWType 207 if (!this.factory.all.exists(_.litValue == init.litValue)) { 208 this.factory.asInstanceOf[CSREnum].addNewValue(init) 209 println(s"[CSR-info] add reset value ${init.litValue} into $this") 210 } 211 if (!factory.all.exists(_.litValue == ((BigInt(1) << (msb - lsb + 1)) - 1))) { 212 factory.asInstanceOf[CSREnum].addMaxValue 213 } 214 this 215 } 216 217 def := (that: UInt): Unit = { 218 this := this.factory(that) 219 } 220 221 def dumpName = { 222 s"${chisel3.reflect.DataMirror.queryNameGuess(this)} ${rwType} [$msb, $lsb] reset($init)" 223 } 224 225 def asBool: Bool = { 226 this.asUInt.asBool 227 } 228 229 private def setRwType(newType: CSRRWType): this.type = { 230 this.rwType = newType 231 this 232 } 233 234 def setRO(rfn: CSRRfnType = null): this.type = { 235 this.setRwType(ROType(rfn)) 236 } 237 238 def setRW(): this.type = { 239 this.setRwType(RWType()) 240 } 241 242 def setWARL(wfn: CSRWfnType): this.type = { 243 this.setRwType(WARLType(wfn)) 244 } 245 246 def ||(that: Bool): Bool = { 247 require(this.getWidth == 1, s"Only 1 bit field can use operator ||. The width of left operand is ${this.getWidth}") 248 this.asBool || that 249 } 250 251 def ||(that: CSREnumType): Bool = { 252 require(this.getWidth == 1, s"Only 1 bit field can use operator ||. The width of left operand is ${this.getWidth}") 253 require(that.getWidth == 1, s"Only 1 bit field can use operator ||. The width of right operand is ${that.getWidth}") 254 this.asBool || that.asBool 255 } 256 257 // override cloneType to make ValidIO etc function return CSREnumType not EnumType 258 override def cloneType: this.type = factory.asInstanceOf[CSREnum].makeType.asInstanceOf[this.type] 259} 260 261class CSREnum extends ChiselEnum { 262 protected def apply(rwType: CSRRWType, init: Data = null)(msb: Int, lsb: Int)(factory: ChiselEnum): CSREnumType = { 263 this.msb = msb 264 this.lsb = lsb 265 new CSREnumType(msb, lsb)(rwType, init)(factory) 266 } 267 268 var msb, lsb: Int = 0 269 270 def makeType: CSREnumType = { 271 new CSREnumType(msb, lsb)(RWType())(this) 272 } 273 274 /** 275 * Used to allow 0.U.asTypeOf(CSREnumInstance) convertion 276 */ 277 def addMinValue: Unit = { 278 Value(0.U) 279 } 280 281 /** 282 * A trick to expand the width of Enum to (msb - lsb + 1) 283 */ 284 def addMaxValue: Unit = { 285 Value(((BigInt(1) << (msb - lsb + 1)) - 1).U) 286 } 287 288 /** 289 * 290 * @param value: A new value need to add in Enum set 291 * @return this 292 */ 293 def addNewValue(value: UInt): this.type = { 294 Value(value) 295 this 296 } 297 298 def isLegal(enum: CSREnumType): Bool = true.B 299 300 println(s"A new CSREnum is created, factory: $this") 301} 302 303trait RWApply { self: CSREnum => 304 def apply(msb: Int, lsb: Int): CSREnumType = self 305 .apply(RWType())(msb, lsb)(this) 306 307 def apply(bit: Int): CSREnumType = apply(bit, bit) 308} 309 310trait ROApply { self: CSREnum => 311 def apply(msb: Int, lsb: Int): CSREnumType = self 312 .apply(ROType())(msb, lsb)(this) 313} 314 315trait WARLApply { self: CSREnum => 316 def apply(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 317 .apply(WARLType(wfn, rfn))(msb, lsb)(this) 318 319 def apply(msb: Int, lsb: Int, wfn: CSRWfnType): CSREnumType = self 320 .apply(WARLType(wfn))(msb, lsb)(this) 321} 322 323trait WLRLApply { self: CSREnum => 324 def apply(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 325 .apply(WLRLType(wfn, rfn))(msb, lsb)(this) 326} 327 328trait CSRMacroApply { self: CSREnum => 329 def RO(msb: Int, lsb: Int, rfn: CSRRfnType, resetVal: Data = null): CSREnumType = self 330 .apply(ROType(rfn), resetVal)(msb, lsb)(this) 331 332 def RO(msb: Int, lsb: Int): CSREnumType = self 333 .apply(ROType())(msb, lsb)(this) 334 335 def RW(msb: Int, lsb: Int, resetVal: Data = null): CSREnumType = self 336 .apply(RWType(), resetVal)(msb, lsb)(this) 337 338 def WARL(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 339 .apply(WARLType(wfn, rfn))(msb, lsb)(this) 340 341 def WARL(msb: Int, lsb: Int, wfn: CSRWfnType): CSREnumType = self 342 .apply(WARLType(wfn))(msb, lsb)(this) 343 344 def WLRL(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 345 .apply(WLRLType(wfn, rfn))(msb, lsb)(this) 346 347 def RefRO(ref: CSREnumType, msb: Int, lsb: Int, rfn: CSRRfnType): CSREnumType = self 348 .apply(RefROType(Some(ref) ,rfn))(msb, lsb)(ref.factory) 349 350 def RefRO(ref: CSREnumType, msb: Int, lsb: Int): CSREnumType = self 351 .apply(RefROType(Some(ref)))(msb, lsb)(ref.factory) 352 353 def RefWARL(ref: CSREnumType, msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 354 .apply(RefWARLType(Some(ref), wfn, rfn))(msb, lsb)(ref.factory) 355 356 def RefWARL(ref: CSREnumType, msb: Int, lsb: Int, wfn: CSRWfnType): CSREnumType = self 357 .apply(RefWARLType(Some(ref), wfn))(msb, lsb)(ref.factory) 358} 359 360object CSREnumTypeImplicitCast { 361 implicit def CSREnumTypeToUInt(field: CSREnumType): UInt = { 362 field.asUInt 363 } 364 365 class BoolField(val value: Bool) { 366 def && (field: CSREnumType): Bool = { 367 this.value && field.asBool 368 } 369 370 def || (field: CSREnumType): Bool = { 371 this.value || field.asBool 372 } 373 } 374 375 implicit def BoolToBoolField(bool: Bool): BoolField = new BoolField(bool) 376} 377 378