Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Chisel Ripple Carry Adder Syntax

im trying to design the following Ripple Carry Adder made of Fulladers. I tried a lot so far, but I'm struggling with Chisel Syntax. Could someone help me out and point out what I'm doing wrong? This is my Code below:

class RcaAdder(val n:Int) extends Module {
  val io = IO(new Bundle {
   val a    = Input(UInt(n.W))
   val b    = Input(UInt(n.W))
   val cin  = Input(UInt(1.W))
   val sum  = Output(UInt(n.W))
   val cout = Output(UInt(1.W))
  })


  //For loop
  for(i <- 0 to n){

   val fulladder = Module(new FullAdder())
   fulladder.io.a := io.a(i)
   fulladder.io.b := io.b(i)

   if(i == 0){
     fulladder.io.cin := io.cin    
   }else{
     fulladder.io.cin := io.cout
   }

   io.cout := fulladder.io.cout
   io.sum(i) := fulladder.io.sum
  }
}

Which gets me the following error:

Exception in thread "main" chisel3.internal.ChiselException: Cannot reassign to read-only Bool(OpResult in RcaAdder)

I assume it has something to do with the " io.sum(i) := .. "

Please help me out! Thank you so much!

like image 430
Robert Avatar asked May 21 '26 14:05

Robert


1 Answers

You are very close to getting it working. One problem you are having is that you cannot assign to a bit subset on the left hand side of :=. One way of getting around this is to create a Vec of UInt(1.W) and then use that as the RHS as a single as a single assignment. I think you have a problem with your ifs, I'd recommend using foldLeft instead of for because it provides a mechanism of accessing the previous elements. Put that all together and I think what you want is something like this.

class RcaAdder(n: Int) extends Module {
  val io = IO(new Bundle {
    val a    = Input(UInt(n.W))
    val b    = Input(UInt(n.W))
    val cin  = Input(UInt(1.W))
    val sum  = Output(UInt(n.W))
    val cout = Output(UInt(1.W))
  })

  val outBits = Wire(Vec(n, UInt(1.W)))

  io.cout := (0 until n).foldLeft(io.cin) { case (carry, index) =>
    val fullAdder = Module(new FullAdder)
    fullAdder.io.a := io.a(index)
    fullAdder.io.b := io.b(index)
    fullAdder.io.cin := carry
    outBits(index) := fullAdder.io.sum
    fullAdder.io.cout.    // This will be passed as carry to the next interation
  }
  io.sum := outBits.asUInt()
}

I've added a working test example here on scastie. Good luck and welcome to Chisel

like image 62
Chick Markley Avatar answered May 23 '26 09:05

Chick Markley