Skip to content

Instantly share code, notes, and snippets.

@chick
Created March 23, 2017 21:30
Show Gist options
  • Save chick/cdbe02943deabd59c77c595f7b12a421 to your computer and use it in GitHub Desktop.
Save chick/cdbe02943deabd59c77c595f7b12a421 to your computer and use it in GitHub Desktop.
How to initialize a bundle
class MyBundle extends Bundle {
val x = Bool()
val y = UInt(8.W)
val z = Bool()
}
object MyBundle {
/**
* initialize x and y but not z,
* @return
*/
def init1: MyBundle = {
val wire = Wire(new MyBundle)
wire.x := true.B()
wire.y := 7.U
wire
}
/**
* This init takes an existing bundle to preserver unspecified values, in this case wire.x
* @param current
* @return
*/
def init2(current: MyBundle): MyBundle = {
val wire = Wire(new MyBundle)
wire := current
wire.y := 5.U
wire.z := false.B
wire
}
}
class UseMyBundle extends Module {
val io = IO(new Bundle{
val something = Input(Bool())
val outa = Output(new MyBundle)
val outb = Output(new MyBundle)
})
// Use Case #1
val reg_a = RegInit(MyBundle.init1)
// Use Case #2
val reg_b = Reg(new MyBundle)
when (io.something) {
reg_b := MyBundle.init1
}.otherwise {
reg_b := MyBundle.init2(reg_b)
}
io.outa := reg_a
io.outb := reg_b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment