天天看點

Chisel實驗筆記(三)

在《Chisel實驗筆記(一)》中我們得到了verilog檔案,在《Chisel實驗筆記(二)》中我們使用Icarus Verilog、GtkWave對的道德verilog檔案進行了仿真測試,實際上,時歐諾個Chisel可以得到對應的C++檔案,進而可以直接進行仿真,本文就介紹産生C++檔案,進行測試的實驗過程。

1、修改Max2.scala檔案如下:

import Chisel._

class Max2 extends Module {
  val io = new Bundle {
    val in0 = UInt(INPUT,  8)
    val in1 = UInt(INPUT,  8)
    val out = UInt(OUTPUT, 8)
  }
  io.out := Mux(io.in0 > io.in1, io.in0, io.in1)
  
}

class Max2Tests(c: Max2) extends Tester(c) {
  for (i <- 0 until 10) {
    // FILL THIS IN HERE
    val in0 = rnd.nextInt(1 << 8)
    val in1 = rnd.nextInt(1 << 8)
    poke(c.io.in0, in0)
    poke(c.io.in1, in1)
    // FILL THIS IN HERE
    step(1)
    expect(c.io.out, if(in0 > in1) in0 else in1)
  }
}

object max2 {
  def main(args: Array[String]) : Unit={
      val margs=Array("--backend","c","--genHarness","--compile","--test")
      chiselMainTest(margs, () => Module(new Max2())){c => new Max2Tests(c)}
  }
}
           

相比《Chisel實驗筆記(一)》中的Max2.scala代碼,其中增加了Max2Tests類,這是專門用來測試的,相當于verilog中的testbench,在這個Max2Test類中,進行了10次循環,每次測試,都随機得到兩個8位的數,使用poke将這兩個随機數分别指派給Max2的in0、in1,然後使用expect判斷Max2的輸出out是否是是in0、in1中較大的那個值。

除了增加Max2Test類,還修改了main中的margs,将其中的“v”改為了“c”,還增加了參數“--genHarness”、“--test”。

然後打開終端,進入chisel_max的目錄,輸入sbt,再輸入run,可以得到如下結果:

> run
[info] Compiling 1 Scala source to /home/riscv/Documents/chisel_max/target/scala-2.10/classes...
[warn] there were 8 feature warning(s); re-run with -feature for details
[warn] one warning found
[info] Running max2 
CPP elaborate
[info] [0.025] // COMPILING < (class Max2)>(0)
[info] [0.028] giving names
[info] [0.034] executing custom transforms
[info] [0.034] adding clocks and resets
[info] [0.038] inferring widths
[info] [0.042] checking widths
[info] [0.043] lowering complex nodes to primitives
[info] [0.043] removing type nodes
[info] [0.043] compiling 5 nodes
[info] [0.044] computing memory ports
[info] [0.044] resolving nodes to the components
[info] [0.049] creating clock domains
[info] [0.050] pruning unconnected IOs
[info] [0.051] checking for combinational loops
[info] [0.052] NO COMBINATIONAL LOOP FOUND
[info] [0.058] populating clock domains
CppBackend::elaborate: need 0, redundant 0 shadow registers
[info] [0.067] generating cpp files
CppBackend: createCppFile Max2.cpp
[info] [1.729] g++ -c -o ./Max2-emulator.o  -I../ -Inull/csrc/  ./Max2-emulator.cpp RET 0
[info] [2.155] g++ -c -o ./Max2.o  -I../ -Inull/csrc/  ./Max2.cpp RET 0
[info] [2.302] g++  -o ./Max2 ./Max2.o ./Max2-emulator.o RET 0
SEED 1432871199400
STARTING ./Max2
RESET 5
  POKE Max2.io_in0 <-  0x6b
  POKE Max2.io_in1 <-  0x99
STEP 1 -> 1
  PEEK Max2.io_out -> 0x99
EXPECT Max2.io_out <- 153 == 153 PASS
  POKE Max2.io_in0 <-  0xc
  POKE Max2.io_in1 <-  0xa7
STEP 1 -> 2
  PEEK Max2.io_out -> 0xa7
EXPECT Max2.io_out <- 167 == 167 PASS
  POKE Max2.io_in0 <-  0x51
  POKE Max2.io_in1 <-  0x80
STEP 1 -> 3
  PEEK Max2.io_out -> 0x80
EXPECT Max2.io_out <- 128 == 128 PASS
  POKE Max2.io_in0 <-  0xc7
  POKE Max2.io_in1 <-  0x8f
STEP 1 -> 4
  PEEK Max2.io_out -> 0xc7
EXPECT Max2.io_out <- 199 == 199 PASS
  POKE Max2.io_in0 <-  0x21
  POKE Max2.io_in1 <-  0x60
STEP 1 -> 5
  PEEK Max2.io_out -> 0x60
EXPECT Max2.io_out <- 96 == 96 PASS
  POKE Max2.io_in0 <-  0xd6
  POKE Max2.io_in1 <-  0xaf
STEP 1 -> 6
  PEEK Max2.io_out -> 0xd6
EXPECT Max2.io_out <- 214 == 214 PASS
  POKE Max2.io_in0 <-  0x96
  POKE Max2.io_in1 <-  0x98
STEP 1 -> 7
  PEEK Max2.io_out -> 0x98
EXPECT Max2.io_out <- 152 == 152 PASS
  POKE Max2.io_in0 <-  0xa4
  POKE Max2.io_in1 <-  0x6b
STEP 1 -> 8
  PEEK Max2.io_out -> 0xa4
EXPECT Max2.io_out <- 164 == 164 PASS
  POKE Max2.io_in0 <-  0xe6
  POKE Max2.io_in1 <-  0x6c
STEP 1 -> 9
  PEEK Max2.io_out -> 0xe6
EXPECT Max2.io_out <- 230 == 230 PASS
  POKE Max2.io_in0 <-  0x7b
  POKE Max2.io_in1 <-  0x3c
STEP 1 -> 10
  PEEK Max2.io_out -> 0x7b
EXPECT Max2.io_out <- 123 == 123 PASS
RAN 10 CYCLES PASSED
PASSED
[success] Total time: 3 s, completed May 29, 2015 11:46:41 AM
           

在終端中輸出了10次模拟結果,PASS表示通過測試。

繼續閱讀