Skip to content

Instantly share code, notes, and snippets.

@oyvindrobertsen
Last active September 28, 2016 18:33
Show Gist options
  • Save oyvindrobertsen/382c6f7e33103f7fd6801a387bd741ad to your computer and use it in GitHub Desktop.
Save oyvindrobertsen/382c6f7e33103f7fd6801a387bd741ad to your computer and use it in GitHub Desktop.
package plan9
import Chisel._
object EnumeratedUInt {
def apply(dir: IODirection = NODIR, enum: Map[Symbol, UInt]): EnumeratedUInt = {
val res = new EnumeratedUInt()
res.create(dir, enum)
return res
}
}
class EnumeratedUInt extends UInt {
var enum: Map[Symbol, UInt] = _
def create(dir: IODirection, enum: Map[Symbol, UInt]) {
val width = log2Ceil(enum.size)
this.enum = enum
super.create(dir, width)
}
def :=(that: Symbol) = this colonEquals that
protected def colonEquals(that: Symbol) {
if (!enum.contains(that)) {
ChiselError.error("Invalid assignment to EnumeratedUInt")
} else {
this := enum(that)
}
}
}
package plan9
import Chisel._
class SomeModule extends Module {
val io = new Bundle {
val a = EnumeratedUInt(Enum(UInt(), List('a, 'b)))
}
a := 'c // Results in error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment