diff options
| author | mergify[bot] | 2022-10-06 21:26:30 +0000 |
|---|---|---|
| committer | GitHub | 2022-10-06 21:26:30 +0000 |
| commit | b72cc42f4f23906db0f201b1d9543a64accbc2ec (patch) | |
| tree | d602090543769f613bfc3ae292c17a6de78c0c37 /core/src/main/scala/chisel3 | |
| parent | cb1bb67194ccf4c17d76f5ad2e8b1e8818c252b8 (diff) | |
Update toPrintable for Enums (#2707) (#2763)
(cherry picked from commit 0ff99ca8d573e3487ef496a21c95d962689c3cba)
Co-authored-by: Aditya Naik <91489422+adkian-sifive@users.noreply.github.com>
Diffstat (limited to 'core/src/main/scala/chisel3')
| -rw-r--r-- | core/src/main/scala/chisel3/StrongEnum.scala | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/core/src/main/scala/chisel3/StrongEnum.scala b/core/src/main/scala/chisel3/StrongEnum.scala index 6d8ceb2f..3c9f4105 100644 --- a/core/src/main/scala/chisel3/StrongEnum.scala +++ b/core/src/main/scala/chisel3/StrongEnum.scala @@ -251,7 +251,28 @@ abstract class EnumType(private[chisel3] val factory: EnumFactory, selfAnnotatin protected def enumTypeName: String = factory.enumTypeName - def toPrintable: Printable = FullName(this) // TODO: Find a better pretty printer + def toPrintable: Printable = { + implicit val sourceInfo = UnlocatableSourceInfo + implicit val compileOptions = ExplicitCompileOptions.Strict + val allNames = factory.allNames.zip(factory.all) + val nameSize = allNames.map(_._1.length).max + def leftPad(str: String): String = { + str.reverse.padTo(nameSize, ' ').reverse + } + val allNamesPadded = allNames.map { case (name, value) => leftPad(name) -> value } + + val result = Wire(Vec(nameSize, UInt(8.W))).suggestName(s"_${enumTypeName}Printable") + result.foreach(_ := '?'.U) + + for ((name, value) <- allNamesPadded) { + when(this === value) { + for ((r, c) <- result.zip(name)) { + r := c.toChar.U + } + } + } + result.map(Character(_)).foldLeft(p"")(_ + _) + } } abstract class EnumFactory { @@ -284,6 +305,8 @@ abstract class EnumFactory { def getWidth: Int = width.get def all: Seq[Type] = enumInstances + /* Accessor for Seq of names in enumRecords */ + def allNames: Seq[String] = enumNames private[chisel3] def nameOfValue(id: BigInt): Option[String] = { enumRecords.find(_.inst.litValue == id).map(_.name) |
