user2466860 user2466860 - 11 months ago 82
Scala Question

Bit vector comparison in Chisel

I have a vector of 4 bits, where I want to check if a single bit from the vector from the vector is a 1. So, basically I want to have loop with 4 iterations, where the first iteration will check if the first bit from the vector is a 1, and then in the next iteration check if the second bit is a 1 and so on.

I have tried with a switch statement shown below, but it doesn't work like I want it to.

for(i <- 0 until ways) { //ways = 3
way_dout(i) :=*line_size-1,i*line_size)

Thank you in advance.

Answer Source

FYI, to help us give you the best answers, you should show us your instantiations of your variables (i.e., what type is current_way, way_dout, and ways?).

There are a number of approaches here. One idea is this:

val ways_vector = Wire(Vec(num_ways, Bool())
val way_dout = Wire(init=Vec.fill(num_ways){UInt(0, width = line_size)})
// set values of ways_vector...
for (i <- 0 until num_ways) {
  when (ways_vector(i)) {

But for loops are ugly. Maps show your intent more clearly:

val way_dout ={ case (w, i) => 
    Mux(w,*line_size-1, i*line_size),

zipWithIndex is a scala-ism, and it brings a lot of awesome power to hardware design.