// a class extends to built-in Chisel class 'Module' classPassthroughextendsModule{ // io: must be called 'io' and be an 'IO' object or instance val io = IO(newBundle { // Type: Uint, Width: 4 val in = Input(UInt(4.W)) val out = Output(UInt(4.W)) }) // ':=' is a Chisel operator (assign) io.out := io.in }
Module Generator
1 2 3 4 5 6 7 8 9 10 11
// parameterize classPassthroughGenerator(width: Int) extendsModule{ val io = IO(newBundle { val in = Input(UInt(width.W)) val out = Output(UInt(width.W)) }) io.out := io.in } // generate module with different widths println(getVerilog(newPassthroughGenerator(10))) println(getVerilog(newPassthroughGenerator(20)))
Tester
poke, expect, peek
1 2 3 4 5 6 7 8
test(newPassthrough()) { c => c.io.in.poke(0.U) // Set our input to value 0 c.io.out.expect(0.U) // Assert that the output correctly has 0 c.io.in.poke(1.U) // Set our input to value 1 c.io.out.expect(1.U) // Assert that the output correctly has 1 } println("SUCCESS!!") println(c.io.out.peek())
classPrintingModuleextendsModule{ val io = IO(newBundle { val in = Input(UInt(4.W)) val out = Output(UInt(4.W)) }) io.out := io.in
// chisel p interpolator // only when simulation, print every setp(1) printf(p"Print during simulation: IO is $io\n") // only when simulation, print every setp(1) printf("Print during simulation: Input is %d\n", io.in) // only when generation // println is a built-in Scala function that prints to the console. // It cannot be used to print during circuit simulation, // because the generated circuit is FIRRTL or Verilog- not Scala. println(s"Print during generation: Input is ${io.in}") }
test(newPrintingModule ) { c => c.io.in.poke(3.U) c.clock.step(2) // circuit will print // only when testing println(s"Print during testing: Input is ${c.io.in.peek()}") }
1 2 3 4 5 6 7 8 9 10 11
Elaborating design... Print during generation: Input is UInt<4>(IO in unelaborated PrintingModule) Done elaborating. Print during simulation: Input is 3 Print during simulation: IO is AnonymousBundle(in -> 3, out -> 3) Print during simulation: Input is 3 Print during simulation: IO is AnonymousBundle(in -> 3, out -> 3) Print during testing: Input is UInt<4>(3) Print during simulation: Input is 0 Print during simulation: IO is AnonymousBundle(in -> 0, out -> 0) test PrintingModule Success: 0 tests passed in 4 cycles in 0.020066 seconds 199.34 Hz