ucb-bar / chisel-tutorial

chisel tutorial exercises and answers
Other
695 stars 197 forks source link

Difference between 'to' and 'until' in for loop #115

Closed pranith closed 6 years ago

pranith commented 6 years ago

For the VecShiftRegisterParam problem, I see that using 'until' in the for loop as follows fails, but using 'to' passes. What is the difference between the two?

class VecShiftRegisterParam(val n: Int, val w: Int) extends Module {
  val io = IO(new Bundle {
    val in  = Input(UInt(w.W))
    val out = Output(UInt(w.W))
  })

  // Implement below ----------
  val values = Seq.fill(n) {0.U(w.W)}
  val regs = RegInit(Vec(values))

  for (i <- 0 until n-2) {
    regs(i) := regs(i+1)
  }

  regs(n-1) := io.in
  io.out := regs(0)
}
edwardcwang commented 6 years ago

Scala to is inclusive while until is exclusive. For example, see https://www.dotnetperls.com/for-scala