Here is the code in Chisel Bootcamp:
Driver(() => new Module {
// Example circuit using Mux1H
val io = IO(new Bundle {
val in_sels = Input(Vec(2, Bool()))
val in_bits = Input(Vec(2, UInt(8.W)))
val out = Output(UInt(8.W))
})
io.out := Mux1H(io.in_sels, io.in_bits)
}) { c => new PeekPokeTester(c) {
poke(c.io.in_bits(0), 10)
poke(c.io.in_bits(1), 20)
// Select index 1
poke(c.io.in_sels(0), 0)
poke(c.io.in_sels(1), 1)
println(s"in_sels=${peek(c.io.in_sels)}, out=${peek(c.io.out)}")
// Select index 0
poke(c.io.in_sels(0), 1)
poke(c.io.in_sels(1), 0)
println(s"in_sels=${peek(c.io.in_sels)}, out=${peek(c.io.out)}")
// Select none (invalid)
poke(c.io.in_sels(0), 0)
poke(c.io.in_sels(1), 0)
println(s"in_sels=${peek(c.io.in_sels)}, out=${peek(c.io.out)}")
// Select both (invalid)
poke(c.io.in_sels(0), 1)
poke(c.io.in_sels(1), 1)
println(s"in_sels=${peek(c.io.in_sels)}, out=${peek(c.io.out)}")
} }
And I got the result like this:
[info] [0.001] in_sels=Vector(0, 1), out=20
[info] [0.001] in_sels=Vector(1, 0), out=10
[info] [0.002] in_sels=Vector(0, 0), out=0
[info] [0.002] in_sels=Vector(1, 1), out=30
Why the result is 30 when select = (1, 1) ? PS: 30 is a random num, not the result 10 + 20
Mux1H
's behavior is undefined when more than one of its selects is high. If you look into the implementation it is using an or
to create the behavior. 30 is the result of the 10 | 20. But, again, because it's undefined you cannot rely on this outcome when multiple selects are high.