classPassthroughGenerator(width: Int) extendsModule{ val io = IO(newBundle { val in = Input(UInt(width.W)) val out = Output(UInt(width.W)) }) io.out := io.in }
val testResult = Driver(() => newPassthrough()) { c => newPeekPokeTester(c) { poke(c.io.in, 0) // Set our input to value 0 expect(c.io.out, 0) // Assert that the output correctly has 0 poke(c.io.in, 1) // Set our input to value 1 expect(c.io.out, 1) // Assert that the output correctly has 1 poke(c.io.in, 2) // Set our input to value 2 expect(c.io.out, 2) // Assert that the output correctly has 2 } } assert(testResult) // Scala Code: if testResult == false, will throw an error println("SUCCESS!!") // Scala Code: if we get here, our tests passed!
classPrintingModuleextendsModule{ val io = IO(newBundle { val in = Input(UInt(4.W)) val out = Output(UInt(4.W)) }) io.out := io.in
printf("Print during simulation: Input is %d\n", io.in) // chisel printf has its own string interpolator too printf(p"Print during simulation: IO is $io\n")
println(s"Print during generation: Input is ${io.in}") }
classPrintingModuleTester(c: PrintingModule) extendsPeekPokeTester(c) { poke(c.io.in, 3) step(5) // circuit will print println(s"Print during testing: Input is ${peek(c.io.in)}") } chisel3.iotesters.Driver( () => newPrintingModule ) { c => newPrintingModuleTester(c) }
[info] [0.002] Elaborating design... Print during generation: Input is chisel3.core.UInt@7 [info] [0.115] Done elaborating. Total FIRRTL Compile Time: 176.6 ms Total FIRRTL Compile Time: 14.3 ms End of dependency graph Circuit state created [info] [0.001] SEED 1536679256727 Print during simulation: Input is 3 Print during simulation: IO is Bundle(in -> 3, out -> 3) Print during simulation: Input is 3 Print during simulation: IO is Bundle(in -> 3, out -> 3) Print during simulation: Input is 3 Print during simulation: IO is Bundle(in -> 3, out -> 3) Print during simulation: Input is 3 Print during simulation: IO is Bundle(in -> 3, out -> 3) Print during simulation: Input is 3 Print during simulation: IO is Bundle(in -> 3, out -> 3) [info] [0.007] Print during testing: Input is 3 test cmd2WrapperHelperPrintingModule Success: 0 tests passed in 10 cycles taking 0.033230 seconds [info] [0.008] RAN 5 CYCLES PASSED
数字组合逻辑电路
多路选择与级联
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classMyOperatorsTwoextendsModule{ val io = IO(newBundle { val in = Input(UInt(4.W)) val out_mux = Output(UInt(4.W)) val out_cat = Output(UInt(4.W)) })
val s = true.B io.out_mux := Mux(s, 3.U, 0.U) // should return 3.U, since s is true io.out_cat := Cat(2.U, 1.U) // concatenates 2 (b10) with 1 (b1) to give 5 (101) }
classMACextendsModule{ val io = IO(newBundle { val in_a = Input(UInt(4.W)) val in_b = Input(UInt(4.W)) val in_c = Input(UInt(4.W)) val out = Output(UInt(8.W)) })
io.out := io.in_a * io.in_b + io.in_c } classMACTester(c: MAC) extendsPeekPokeTester(c) { val cycles = 100 import scala.util.Random for (i <- 0 until cycles) { val in_a = Random.nextInt(16) val in_b = Random.nextInt(16) val in_c = Random.nextInt(16) poke(c.io.in_a, in_a) poke(c.io.in_b, in_b) poke(c.io.in_c, in_c) expect(c.io.out, in_a*in_b+in_c) } } assert(Driver(() => newMAC) {c => newMACTester(c)}) println("SUCCESS!!")
// Max3 returns the max of its 3 arguments classMax3extendsModule{ val io = IO(newBundle { val in1 = Input(UInt(16.W)) val in2 = Input(UInt(16.W)) val in3 = Input(UInt(16.W)) val out = Output(UInt(16.W)) }) when(io.in1 > io.in2 && io.in1 > io.in3) { io.out := io.in1 }.elsewhen(io.in2 > io.in1 && io.in2 > io.in3) { io.out := io.in2 }.otherwise { io.out := io.in3 } }
// verify that the max of the three inputs is correct classMax3Tester(c: Max3) extendsPeekPokeTester(c) { poke(c.io.in1, 6) poke(c.io.in2, 4) poke(c.io.in3, 2) expect(c.io.out, 6) // input 1 should be biggest poke(c.io.in2, 7) expect(c.io.out, 7) // now input 2 is poke(c.io.in3, 11) expect(c.io.out, 11) // and now input 3 poke(c.io.in3, 3) expect(c.io.out, 7) // show that decreasing an input works as well }
// Test Max3 val works = Driver(() => newMax3) { c => newMax3Tester(c) } assert(works) // Scala Code: if works == false, will throw an error println("SUCCESS!!") // Scala Code: if we get here, our tests passed!
/** Sort4 sorts its 4 inputs to its 4 outputs */ classSort4extendsModule{ val io = IO(newBundle { val in0 = Input(UInt(16.W)) val in1 = Input(UInt(16.W)) val in2 = Input(UInt(16.W)) val in3 = Input(UInt(16.W)) val out0 = Output(UInt(16.W)) val out1 = Output(UInt(16.W)) val out2 = Output(UInt(16.W)) val out3 = Output(UInt(16.W)) })
val row10 = Wire(UInt(16.W)) val row11 = Wire(UInt(16.W)) val row12 = Wire(UInt(16.W)) val row13 = Wire(UInt(16.W))
when(io.in0 < io.in1) { row10 := io.in0 // preserve first two elements row11 := io.in1 }.otherwise { row10 := io.in1 // swap first two elements row11 := io.in0 }
when(io.in2 < io.in3) { row12 := io.in2 // preserve last two elements row13 := io.in3 }.otherwise { row12 := io.in3 // swap last two elements row13 := io.in2 }
val row21 = Wire(UInt(16.W)) val row22 = Wire(UInt(16.W))
when(row11 < row12) { row21 := row11 // preserve middle 2 elements row22 := row12 }.otherwise { row21 := row12 // swap middle two elements row22 := row11 }
val row20 = Wire(UInt(16.W)) val row23 = Wire(UInt(16.W)) when(row10 < row13) { row20 := row10 // preserve middle 2 elements row23 := row13 }.otherwise { row20 := row13 // swap middle two elements row23 := row10 }
when(row20 < row21) { io.out0 := row20 // preserve first two elements io.out1 := row21 }.otherwise { io.out0 := row21 // swap first two elements io.out1 := row20 }
when(row22 < row23) { io.out2 := row22 // preserve first two elements io.out3 := row23 }.otherwise { io.out2 := row23 // swap first two elements io.out3 := row22 } }
// verify the all possible ordering of 4 numbers are sorted classBetterSort4Tester(c: Sort4) extendsPeekPokeTester(c) { List(1, 2, 3, 4).permutations.foreach { case i0 :: i1 :: i2 :: i3 :: Nil => println(s"Sorting $i0$i1$i2$i3") poke(c.io.in0, i0) poke(c.io.in1, i1) poke(c.io.in2, i2) poke(c.io.in3, i3) expect(c.io.out0, 1) expect(c.io.out1, 2) expect(c.io.out2, 3) expect(c.io.out3, 4) } }
// Here's the tester val works = iotesters.Driver(() => newSort4) { c => newBetterSort4Tester(c) } assert(works) // Scala Code: if works == false, will throw an error println("SUCCESS!!") // Scala Code: if we get here, our tests passed!