ucb-bar / chisel-tutorial

chisel tutorial exercises and answers
Other
688 stars 196 forks source link

not found: type SwitchContext #166

Closed YongkangLi closed 3 years ago

YongkangLi commented 3 years ago

When I was doing the Mux4, I wanted to use switch-is. But an error occurred during compilation. I know that the referential solution uses two Mux2. I just want to figure out what is wrong doing so. PS: the error message is as follows.

[error] .../chisel-tutorial/src/main/scala/problems/Mux4.scala:38:19: not found: type SwitchContext

[error] switch (io.sel) { [error] ^ [error] one error found

class Mux4 extends Module {
  val io = IO(new Bundle {
    val in0 = Input(UInt(1.W))
    val in1 = Input(UInt(1.W))
    val in2 = Input(UInt(1.W))
    val in3 = Input(UInt(1.W))
    val sel = Input(UInt(2.W))
    val out = Output(UInt(1.W))
  })

  //Implement below ----------
  switch (io.sel) {
    is (0.U) {
      io.out := io.in0
    }
    is (1.U) {
      io.out := io.in1
    }
    is (2.U) {
      io.out := io.in2
    }
    is (3.U) {
      io.out := io.in3
    }
  }

  // make the compile process happy, needs to be substituted by the output of the Mux
  //io.out := 1.U
  //Implement above ----------
}
jackkoenig commented 3 years ago

There's a minor bug in the implementation of switch that assumes you're doing a wildcard import import chisel3.util._. You should be able to workaround by using the wildcard, or you can import SwitchContext directly, import chisel3.util.SwitchContext. This is dumb and an easy fix though so I'll have a PR shortly.

YongkangLi commented 3 years ago

Thanks for explanation and help. It seems that this was a stupid question. Oops!

jackkoenig commented 3 years ago

It seems that this was a stupid question. Oops!

Not at all, SwitchContext is supposed to be hidden from you, this is our mistake 😉