ucb-bar / chisel2-deprecated

chisel.eecs.berkeley.edu
387 stars 90 forks source link

A gotcha bug for user code #575

Open da-steve101 opened 8 years ago

da-steve101 commented 8 years ago

Just found a bug in something I was writing which took a while to spot. Essentially came down to:

class UserMod extends Module {
  val io = new Bundle {
    val inA = UInt(INPUT, 4)
    val inB = UInt(INPUT, 4)
    val sel = Bool(INPUT)
    val out = UInt(OUTPUT, 4)
  }
  val tmpA = RegNext(io.inA)
  val tmpSel = tmpA
  when ( io.sel ) {
    tmpSel := io.inB
  }
  io.out := tmpSel
}
class UserTests(c : UserMod) extends Tester(c) {
  poke(c.io.inA, 1)
  poke(c.io.inB, 2)
  poke(c.io.sel, false)
  step(1)
  expect(c.io.out, 1)
  poke(c.io.sel, true)
  expect(c.io.out, 2)
}
launchCppTester( (c : UserMod) => new UserTests(c) )

This fails because tmpSel is actually referring to the register rather than a new node and hence the input to the register is unintentionally changed. It would be nice to be able to catch this and print a warning but I can't think of how to do it. Perhaps a warning if two vals point to the same node? But this could be annoying in loops. Any ideas? Perhaps should put something in the manual to tell people to be on the lookout