Last active
September 28, 2016 18:33
-
-
Save oyvindrobertsen/382c6f7e33103f7fd6801a387bd741ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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