https://mp.weixin.qq.com/s/SEcVjGRL1YloGlEPSoHr3A
import chisel3._ //A n-bit adder with carry in and carry out class Adder(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)) }) //create an Array of FullAdders // NOTE: Since we do all the wiring during elaboration and not at run-time, // i.e., we don't need to dynamically index into the data structure at run-time, // we use an Array instead of a Vec. val FAs = Array.fill(n)(Module(new FullAdder()).io) // val FAs = Array.fill(n)(Module(new FullAdder())) val carry = Wire(Vec(n+1, UInt(1.W))) val sum = Wire(Vec(n, Bool())) //first carry is the top level carry in carry(0) := io.Cin //wire up the ports of the full adders for (i <- 0 until n) { FAs(i).a := io.A(i) FAs(i).a := io.A(i) FAs(i).b := io.B(i) FAs(i).cin := carry(i) carry(i+1) := FAs(i).cout sum(i) := FAs(i).sum.toBool() } // for (i <- 0 until n) { // FAs(i).io.a := io.A(i) // FAs(i).io.a := io.A(i) // FAs(i).io.b := io.B(i) // FAs(i).io.cin := carry(i) // carry(i+1) := FAs(i).io.cout // sum(i) := FAs(i).io.sum.toBool() // } io.Sum := sum.asUInt io.Cout := carry(n) } object AdderMain { def main(args: Array[String]): Unit = { chisel3.Driver.execute(Array("--target-dir", "generated/Adder"), () => new Adder(4)) } }