Chisel3 - Tutorial - FullAdder
https://mp.weixin.qq.com/s/Aye-SrUUuIP6_o67Rlt5OQ
import chisel3._ class FullAdder extends Module { val io = IO(new Bundle { val a = Input(UInt(1.W)) val b = Input(UInt(1.W)) val cin = Input(UInt(1.W)) val sum = Output(UInt(1.W)) val cout = Output(UInt(1.W)) }) // Generate the sum val a_xor_b = io.a ^ io.b io.sum := a_xor_b ^ io.cin // Generate the carry val a_and_b = io.a & io.b val b_and_cin = io.b & io.cin val a_and_cin = io.a & io.cin io.cout := a_and_b | b_and_cin | a_and_cin } object Main { def main(args: Array[String]): Unit = { chisel3.Driver.execute(Array("--target-dir", "generated/FullAdder"), () => new FullAdder) // chisel3.Driver.execute(args, () => new FullAdder) } }
import chisel3.iotesters.{PeekPokeTester, Driver, ChiselFlatSpec} class FullAdderTester(c: FullAdder) extends PeekPokeTester(c) { for (t <- 0 until 4) { val a = rnd.nextInt(2) val b = rnd.nextInt(2) val cin = rnd.nextInt(2) val res = a + b + cin val sum = res & 1 val cout = (res >> 1) & 1 poke(c.io.a, a) poke(c.io.b, b) poke(c.io.cin, cin) step(1) expect(c.io.sum, sum) expect(c.io.cout, cout) } } object FullAdderTester { def main(args: Array[String]): Unit = { chisel3.iotesters.Driver(() => new FullAdder)(c => new FullAdderTester(c)) } }