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 def &&(that: Bool): Bool = { 258 require(this.getWidth == 1, s"Only 1 bit field can use operator &&. The width of left operand is ${this.getWidth}") 259 this.asBool && that 260 } 261 262 def &&(that: CSREnumType): Bool = { 263 require(this.getWidth == 1, s"Only 1 bit field can use operator &&. The width of left operand is ${this.getWidth}") 264 require(that.getWidth == 1, s"Only 1 bit field can use operator &&. The width of right operand is ${that.getWidth}") 265 this.asBool && that.asBool 266 } 267 268 // override cloneType to make ValidIO etc function return CSREnumType not EnumType 269 override def cloneType: this.type = factory.asInstanceOf[CSREnum].makeType.asInstanceOf[this.type] 270} 271 272class CSREnum extends ChiselEnum { 273 protected def apply(rwType: CSRRWType, init: Data = null)(msb: Int, lsb: Int)(factory: ChiselEnum): CSREnumType = { 274 this.msb = msb 275 this.lsb = lsb 276 new CSREnumType(msb, lsb)(rwType, init)(factory) 277 } 278 279 var msb, lsb: Int = 0 280 281 def makeType: CSREnumType = { 282 new CSREnumType(msb, lsb)(RWType())(this) 283 } 284 285 /** 286 * Used to allow 0.U.asTypeOf(CSREnumInstance) convertion 287 */ 288 def addMinValue: Unit = { 289 Value(0.U) 290 } 291 292 /** 293 * A trick to expand the width of Enum to (msb - lsb + 1) 294 */ 295 def addMaxValue: Unit = { 296 Value(((BigInt(1) << (msb - lsb + 1)) - 1).U) 297 } 298 299 /** 300 * 301 * @param value: A new value need to add in Enum set 302 * @return this 303 */ 304 def addNewValue(value: UInt): this.type = { 305 Value(value) 306 this 307 } 308 309 def isLegal(enum: CSREnumType): Bool = true.B 310 311 println(s"A new CSREnum is created, factory: $this") 312} 313 314trait RWApply { self: CSREnum => 315 def apply(msb: Int, lsb: Int): CSREnumType = self 316 .apply(RWType())(msb, lsb)(this) 317 318 def apply(bit: Int): CSREnumType = apply(bit, bit) 319} 320 321trait ROApply { self: CSREnum => 322 def apply(msb: Int, lsb: Int): CSREnumType = self 323 .apply(ROType())(msb, lsb)(this) 324} 325 326trait WARLApply { self: CSREnum => 327 def apply(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 328 .apply(WARLType(wfn, rfn))(msb, lsb)(this) 329 330 def apply(msb: Int, lsb: Int, wfn: CSRWfnType): CSREnumType = self 331 .apply(WARLType(wfn))(msb, lsb)(this) 332 333 def apply(bit: Int, wfn: CSRWfnType): CSREnumType = apply(bit, bit, wfn) 334} 335 336trait WLRLApply { self: CSREnum => 337 def apply(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 338 .apply(WLRLType(wfn, rfn))(msb, lsb)(this) 339} 340 341trait CSRMacroApply { self: CSREnum => 342 def RO(msb: Int, lsb: Int, rfn: CSRRfnType, resetVal: Data = null): CSREnumType = self 343 .apply(ROType(rfn), resetVal)(msb, lsb)(this) 344 345 def RO(msb: Int, lsb: Int): CSREnumType = self 346 .apply(ROType())(msb, lsb)(this) 347 348 def RW(msb: Int, lsb: Int, resetVal: Data = null): CSREnumType = self 349 .apply(RWType(), resetVal)(msb, lsb)(this) 350 351 def WARL(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 352 .apply(WARLType(wfn, rfn))(msb, lsb)(this) 353 354 def WARL(msb: Int, lsb: Int, wfn: CSRWfnType): CSREnumType = self 355 .apply(WARLType(wfn))(msb, lsb)(this) 356 357 def WLRL(msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 358 .apply(WLRLType(wfn, rfn))(msb, lsb)(this) 359 360 def RefRO(ref: CSREnumType, msb: Int, lsb: Int, rfn: CSRRfnType): CSREnumType = self 361 .apply(RefROType(Some(ref) ,rfn))(msb, lsb)(ref.factory) 362 363 def RefRO(ref: CSREnumType, msb: Int, lsb: Int): CSREnumType = self 364 .apply(RefROType(Some(ref)))(msb, lsb)(ref.factory) 365 366 def RefWARL(ref: CSREnumType, msb: Int, lsb: Int, wfn: CSRWfnType, rfn: CSRRfnType): CSREnumType = self 367 .apply(RefWARLType(Some(ref), wfn, rfn))(msb, lsb)(ref.factory) 368 369 def RefWARL(ref: CSREnumType, msb: Int, lsb: Int, wfn: CSRWfnType): CSREnumType = self 370 .apply(RefWARLType(Some(ref), wfn))(msb, lsb)(ref.factory) 371} 372 373object CSREnumTypeImplicitCast { 374 implicit def CSREnumTypeToUInt(field: CSREnumType): UInt = { 375 field.asUInt 376 } 377 378 class BoolField(val value: Bool) { 379 def && (field: CSREnumType): Bool = { 380 this.value && field.asBool 381 } 382 383 def || (field: CSREnumType): Bool = { 384 this.value || field.asBool 385 } 386 } 387 388 implicit def BoolToBoolField(bool: Bool): BoolField = new BoolField(bool) 389} 390 391