간단한 Adder 만드는 예제이다.
오해의 여지가 없을 정도로, 매우 쉽다.
다만, width를 w로 paramaterize해야 하는 함정이 있다.
// See LICENSE.txt for license details.
package problems
import chisel3._
// Problem:
//
// 'out' should be the sum of 'in0' and 'in1'
// Adder width should be parametrized
//
// Implement below ----------
class Adder(val w: Int) extends Module {
val io = IO(new Bundle {
val in0 = Input(UInt(w.W))
val in1 = Input(UInt(w.W))
val out = Output(UInt(w.W))
})
io.out := io.in0 + io.in1
printf("Accumulator: %d := %d + %d\n", io.out, io.in0, io.in1)
}
// Implement above ----------
// See LICENSE.txt for license details.
package problems
import chisel3.iotesters.PeekPokeTester
class AdderTests(c: Adder) extends PeekPokeTester(c) {
for (i <- 0 until 10) {
val in0 = rnd.nextInt(1 << c.w)
val in1 = rnd.nextInt(1 << c.w)
poke(c.io.in0, in0)
poke(c.io.in1, in1)
step(1)
expect(c.io.out, (in0 + in1)&((1 << c.w)-1))
// printf("hello world!")
// println(s"c.io.in0 ${peek(c.io.in0)}")
// printf("Type of io.input: %d\n", c.io.in0)
// println(s"INPUT: c.io.out(%d) := c.io.in0(%d) + c.io.in1(%d)", c.io.out, c.io.in0, c.io.in1)
}
}
글 잘 봤습니다.