Chisel Tutorial(八)——執行與測試
阿新 • • 發佈:2018-12-25
以下內容依據2015-7-10版的Chisel 2.2 Tutorial整理
前面我們已經定義了模組,本節討論如何執行和測試一個電路。Chisel可以翻譯得到C++或者Verilog。為了構建一個電路我們需要呼叫chiselMain,如下:
object tutorial{
def main(args : Array[String]) = {
chiselMain( args, () => Module(new Mux2()))
}
}
測試是電路設計中很重要的一步,Chisel採用測試向量的機制來測試電路,測試向量所在的類是Tester的子類。
class Tester[T <: Module](val c: T,val isTrace: Boolean = true){ var t: Int var ok: Boolean val rnd: Random def int(x: Boolean): BigInt def int(x: Int): BigInt def int(x: Bits):BigInt def reset(n: Int = 1) def step(n: Int): Int def pokeAt(data: Mem[T], index: Int, x: BigInt) def poke(data: Bits, x: BigInt) def poke(data: Aggregate, x: Array[BigInt]) def peekAt(data: Mem[T], index: Int) def peek(data: Bits): BigInt def peek(data: Aggregate): Array[BigInt] def expect(good: Boolean, msg: String): Boolean def expect(data: Bits, target: BigInt): Boolean }
測試類繼承自Tester,然後連線到指定的模組,就可以向該模組輸入測試向量了,具體來說,有如下方法:
- poke用來設定埠的值,和state的值
- step用來表示執行一個時鐘
- peek用來讀取埠的值,和state的值
- expect用來判斷讀取的埠值是否是預期的值
使用者使用如下物件連線待測試模組和測試類:
object chiselMainTest{
def apply[T <: Module](args: Array[String], comp: ()=> T)(tester: T => Tester[T]): T
}
--test要作為一個引數傳遞給chiselMainTest
class Mux2Tests(c: Mux2) extends Tester(c){ val n = pow(2, 3).toInt for(s <- 0 until 2){ for(i0 <- 0 until 2){ for(i1 <- 0 until 2){ poke(c.io.sel, s) poke(c.io.in1, i1) poke(c.io.in0, i0) step(1) expect(c.io.out, (if( s == 1) i1 else i0 )) } } } }
上述測試類很簡單,首先使用poke為Mux2的輸入賦值,然後使用expect測試Mux2的輸出是否是預期的值。最後,需要在chiselMainTest中使用該測試類,如下:
chiselMainTest(args + “--test”, () => Module(new Mux2())){
c => new Mux2Tests(c)
}
args中可以加入其他引數,如下:
比如加入--backend v 就可以得到verilog程式碼。在之前的幾篇部落格“Chisel實驗筆記”中已經用到過。