Skip to content

Instantly share code, notes, and snippets.

@chick
Created August 5, 2019 17:23
Show Gist options
  • Save chick/6a0053c59fe7f99e4602da6dd1b98ec0 to your computer and use it in GitHub Desktop.
Save chick/6a0053c59fe7f99e4602da6dd1b98ec0 to your computer and use it in GitHub Desktop.
Illustrates toUInt conversion and Cat behavior
package bit_examples
import chisel3._
import chisel3.experimental.MultiIOModule
import chisel3.iotesters.PeekPokeTester
import chisel3.util.Cat
import org.scalatest.{FreeSpec, Matchers}
//scalastyle:off magic.number
// Class and companion Object to support instantiation and initialization of
// state due to the need to have subword assignment for vectors > 64-bits
//
// Fields in the bundle enumerate from to low. When converting this bundle
// to UInt it is equivalent to Cat(Word1, Word0) or if subword assignment
// was supported:
//
// state(127,64) := word1
// state(63,0) := word0
//
class State_Class extends Bundle {
val word1 = UInt(64.W)
val word0 = UInt(64.W)
}
object State_Class {
def init: State_Class = {
val wire = Wire(new State_Class)
wire.word1 := BigInt("a"*16, 16).U
wire.word0 := BigInt("b"*16, 16).U
wire
}
}
// Class and companion Object to support instantiation and initialization of
// key due to the need to have subword assignment for vectors > 64-bits
//
class Key_Class extends Bundle {
val word2 = UInt(64.W)
val word1 = UInt(64.W)
val word0 = UInt(64.W)
}
object Key_Class {
def init: Key_Class = {
val wire = Wire(new Key_Class)
wire.word2 := BigInt("c"*16, 16).U
wire.word1 := BigInt("d"*16, 16).U
wire.word0 := BigInt("e"*16, 16).U
wire
}
}
class Example extends MultiIOModule {
val stateOut = IO(Output((UInt((new State_Class).getWidth.W))))
val keyOut = IO(Output((UInt((new Key_Class).getWidth.W))))
val combinedOut = IO(Output((UInt((stateOut.getWidth + keyOut.getWidth).W))))
val state = RegInit(State_Class.init)
val key = RegInit(Key_Class.init)
stateOut := state.asUInt
keyOut := key.asUInt
combinedOut := Cat(stateOut, keyOut)
}
class BitTests extends FreeSpec with Matchers {
"example of uint conversion and and ordering of cat" in {
iotesters.Driver.execute(Array(), () => new Example) { c =>
new PeekPokeTester(c) {
step(1)
peek(c.stateOut).toString(16) should be ("a"*16 + "b"*16)
peek(c.keyOut).toString(16) should be ("c"*16 + "d"*16 + "e"*16)
peek(c.combinedOut).toString(16) should be ("a"*16 + "b"*16 + "c"*16 + "d"*16 + "e"*16)
println(s"stateOut ${peek(c.stateOut).toString(16)}")
println(s"keyOut ${peek(c.keyOut).toString(16)}")
println(s"combined ${peek(c.combinedOut).toString(16)}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment