敏捷硬件開發(fā)語言Chisel與數(shù)字系統(tǒng)設(shè)計(jì) 課件 第5章 Verilog HDL與基本測(cè)試_第1頁
敏捷硬件開發(fā)語言Chisel與數(shù)字系統(tǒng)設(shè)計(jì) 課件 第5章 Verilog HDL與基本測(cè)試_第2頁
敏捷硬件開發(fā)語言Chisel與數(shù)字系統(tǒng)設(shè)計(jì) 課件 第5章 Verilog HDL與基本測(cè)試_第3頁
敏捷硬件開發(fā)語言Chisel與數(shù)字系統(tǒng)設(shè)計(jì) 課件 第5章 Verilog HDL與基本測(cè)試_第4頁
敏捷硬件開發(fā)語言Chisel與數(shù)字系統(tǒng)設(shè)計(jì) 課件 第5章 Verilog HDL與基本測(cè)試_第5頁
已閱讀5頁,還剩30頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

5.VerilogHDL與基本測(cè)試一、生成VerilogHDL二、增加參數(shù)的方法三、編寫與運(yùn)行chisel-iotesters四、編寫與運(yùn)行chiseltest目

錄2一、生成VerilogHDL3生成VerilogHDL1.1應(yīng)用場(chǎng)景小明在對(duì)Chisel語言學(xué)習(xí)一段時(shí)間之后,終于寫出了自己設(shè)計(jì)的電路,于是迫不及待地想去仿真驗(yàn)證一下電路的功能。但在找驗(yàn)證工具的時(shí)候,卻出現(xiàn)了問題…支持Chisel的仿真驗(yàn)證工具去哪兒找呀???4生成VerilogHDL1.execute()函數(shù)2.emitVerilog()函數(shù)1.2生成方式由Chisel代碼生成Verilog代碼:關(guān)系:

emitVerilog()函數(shù)本身是通過調(diào)用execute()函數(shù)來實(shí)現(xiàn)生成Verilog代碼的,可以將emitVerilog()理解成execute()的簡(jiǎn)化參數(shù)版。5生成VerilogHDL1.2生成方式第一個(gè)參數(shù):字符串?dāng)?shù)組args第二個(gè)參數(shù):initialAnnotationsexecute()函數(shù)定義:參數(shù):finaldefexecute(args:Array[String],annotations:AnnotationSeq):AnnotationSeq6生成VerilogHDL實(shí)例:全加器Chisel代碼:packagechapter05

importchisel3._

//定義全加器的輸入輸出端口

classFullAdderextendsModule{

valio=IO(newBundle{

vala=Input(UInt(1.W))

valb=Input(UInt(1.W))

valcin=Input(UInt(1.W))

vals=Output(UInt(1.W))

valcout=Output(UInt(1.W))

})

//根據(jù)輸入a,b,cin得到本位和s

io.s:=io.a^io.b^io.cin

//根據(jù)輸入a,b,cin得到高位進(jìn)位輸出cout

io.cout:=(io.a&io.b)|((io.a|io.b)&io.cin)

}7packagechapter05

importchisel3.stage.ChiselGeneratorAnnotation

objectFullAdderGenextendsApp{new(chisel3.stage.ChiselStage).execute(Array("--target-dir","./generated/chapter05/FullAdderGen"),Seq(ChiselGeneratorAnnotation(()=>newFullAdder)))}生成VerilogHDL實(shí)例:全加器主函數(shù)里可以包括多個(gè)execute函數(shù),也可以包含其它代碼。還有一點(diǎn)要注意的是,建議把設(shè)計(jì)文件和主函數(shù)放在一個(gè)包里,比如這里的“packagechapter05”,這樣省去了編寫路徑的麻煩。8生成VerilogHDL實(shí)例:全加器運(yùn)行主函數(shù):

1.使用IDEA軟件直接點(diǎn)擊代碼旁邊綠色箭頭運(yùn)行;2.如果使用sbt命令行運(yùn)行,則需要在build.sbt文件所在的路徑下打開終端,然后執(zhí)行命令:如果設(shè)計(jì)文件沒有錯(cuò)誤,即可生成如下三個(gè)文件:sbt"test:runMainchapter05.FullAdderGen"9objectFullAdderGenextendsApp{

(newchisel3.stage.ChiselStage).

emitVerilog(newFullAdder,Array("--target-dir","./generated/chapter05/FullAdderGen1"))}生成VerilogHDL第一個(gè)參數(shù):傳入一個(gè)無入?yún)⒎祷豏awMoudle類型的函數(shù)第二個(gè)參數(shù):與execute第一個(gè)參數(shù)類似,也可以定義一個(gè)Array指定路徑emitVerilog()函數(shù)參數(shù):1.2生成方式10objectFullAdderGenextendsApp{

//將生成的字符串形式的Verilog代碼賦值給變量

valverilogString=(newchisel3.stage.ChiselStage).emitVerilog(newFullAdder,Array("--target-dir","./generated/chapter05/FullAdderGen2"))

//終端打印輸出Verilog文件

println(verilogString)

}生成VerilogHDL1.2生成方式

運(yùn)行完主函數(shù)之后,同樣會(huì)生成三個(gè)文件,生成的Verilog文件和使用execute函數(shù)一樣。

注意emitVerilog返回值是String,String內(nèi)容是生成的Verilog。把String打印到終端直接顯示出來:11二、增加參數(shù)的方法12增加參數(shù)的方法

2.1

給Firrtl傳遞參數(shù)在運(yùn)行主函數(shù)時(shí),可以給剛才的命令后面繼續(xù)增加可選的參數(shù):增加參數(shù)“--help”查看幫助菜單objectFullAdderGenextendsApp{

//添加參數(shù)“--help”用于得到幫助菜單

new(chisel3.stage.ChiselStage).execute(Array("--target-dir","./generated/chapter05/FullAdderGen","--help"),Seq(ChiselGeneratorAnnotation(()=>newFullAdder)))

}13objectFullAdderGenextendsApp{

new(chisel3.stage.ChiselStage).execute(args,Seq(ChiselGeneratorAnnotation(()=>newFullAdder)))

}增加參數(shù)的方法

因?yàn)閮煞N方式同時(shí)使用時(shí),會(huì)以scala代碼中指定的路徑為準(zhǔn),命令行指定的路徑無效。2.1

給Firrtl傳遞參數(shù)在命令行時(shí)傳入?yún)?shù):sbt'test:runMainchapter05.FullAdderGen-td./generated/chapter05/FullAdderGen'NOTE:

如果使用這種傳參方式,在scala代碼中主函數(shù)應(yīng)該寫成下面這樣:14增加參數(shù)的方法

假設(shè)要構(gòu)建一個(gè)n位的加法器,具體位寬不確定,根據(jù)需要而定。那么,就可以把端口位寬參數(shù)化,實(shí)例化時(shí)傳入想要的參數(shù)即可。2.2

給主函數(shù)傳遞參數(shù)Chisel的模塊可以接收參數(shù):classNBitsAdder(n:Int)extendsModule{

valio=IO(newBundle{

vala=Input(UInt(n.W))

valb=Input(UInt(n.W))

vals=Output(UInt(n.W))

valcout=Output(UInt(1.W))

})}

//根據(jù)輸入a,b得到n位位寬和s

io.s:=(io.a+&io.b)(n-1,0)

//使用+&實(shí)現(xiàn)拓展一位加法,最高位為進(jìn)位輸出

io.cout:=(io.a+&io.b)(n)

}15objectNBitsAdderGenextendsApp{

new(chisel3.stage.ChiselStage).execute(Array("--target-dir","./generated/chapter05/NBitsAdderGen"),

Seq(ChiselGeneratorAnnotation(()=>newNBitsAdder(8))))

}增加參數(shù)的方法2.2

給主函數(shù)傳遞參數(shù)Scala代碼中直接寫直接將‘8’傳入‘NBitsAdder’得到八位加法器16packagechapter05

importchisel3.stage.ChiselGeneratorAnnotation

objectNBitsAdderGenextendsApp{

new(chisel3.stage.ChiselStage).execute(Array("--target-dir","./generated/chapter05/NBitsAdderGen1"),

Seq(ChiselGeneratorAnnotation(()=>newNBitsAdder(args(0).toInt))))

}增加參數(shù)的方法終端運(yùn)行:sbt‘test:runMainchapter05.NBitsAdderGen8’2.2

給主函數(shù)傳遞參數(shù)使用命令行傳參17增加參數(shù)的方法2.3多參數(shù)傳遞當(dāng)傳遞參數(shù)的數(shù)量很多,類型也不同時(shí),在命令行以字符串傳遞參數(shù),模塊內(nèi)對(duì)字符串進(jìn)行處理實(shí)現(xiàn)指定功能。18加法器:(1)兩個(gè)輸入數(shù)據(jù)位寬可參數(shù)化(2)根據(jù)“Unsigned”或“Signed”實(shí)現(xiàn)無符號(hào)數(shù)加法或者有符號(hào)數(shù)加法實(shí)例:增加參數(shù)的方法2.3多參數(shù)傳遞19classMutiParaAdder(paraA:Int,paraB:Int,paraC:String)extendsModule{

assert((paraC=="Unsigned")|(paraC=="Signed"))

valio=IO(newBundle{

vala=Input(UInt(paraA.W))

valb=Input(UInt(paraB.W))

vals=Output(UInt((if(paraA>paraB)paraA+1elseparaB+1).W))

})

if(paraC=="Unsigned")

io.s:=io.a+&io.b

else

io.s:=(io.a.asSInt()+&io.b.asSInt()).asUInt()

}Chisel代碼:增加參數(shù)的方法2.3多參數(shù)傳遞20objectMutiParaAdderGenextendsApp{

new(chisel3.stage.ChiselStage).execute(Array("--target-dir","./generated/chapter05/MutiParaAdderGen"),

Seq(ChiselGeneratorAnnotation(()=>newMutiParaAdder(6,8,"Unsigned"))))

}objectMutiParaAdderGenextendsApp{

new(chisel3.stage.ChiselStage).execute(Array("--target-dir","./generated/chapter05/MutiParaAdderGen2"),

Seq(ChiselGeneratorAnnotation(()=>newMutiParaAdder(args(0).toInt,args(1).toInt,args(2)))))

}終端執(zhí)行:sbt"test:runMainchapter05.MutiParaAdderGen268Signed"Scala代碼中傳參命令行傳參三、編寫與運(yùn)行chisel-iotesters21編寫chisel-iotesters對(duì)于超大型的系統(tǒng)級(jí)電路,最好還是生成Verilog,交由成熟的EDA工具,用UVM進(jìn)行驗(yàn)證。3.1

Chisel的測(cè)試方式利用Scala的測(cè)試來驗(yàn)證Chisel級(jí)別的代碼邏輯有沒有錯(cuò)誤。(復(fù)雜)利用Chisel庫里的peek和poke函數(shù),給模塊的端口加激勵(lì)、查看信號(hào)值,并交由下游的Verilator來仿真、產(chǎn)生波形。(適合小型電路的驗(yàn)證)22編寫chisel-iotesters

"edu.berkeley.cs"%%"chisel-iotesters"%"1.5.2"3.2具體步驟編寫一個(gè)chisel-iotesters,需要在.sbt庫依賴添加一句:定義一個(gè)類這個(gè)類繼承自PeekPokeTester類測(cè)試類內(nèi)部有四種方法

①“poke(端口,激勵(lì)值)”方法②“peek(端口)”方法③“expect(端口,期望值)”方法④“step(n)”方法23importscala.util._

importchisel3.iotesters._

classNBitsAdderTest(c:NBitsAdder)extendsPeekPokeTester(c){

valrandNum=newRandom

for(i<-0until10){

vala=randNum.nextInt(256)

valb=randNum.nextInt(256)

poke(c.io.a,a)

poke(c.io.b,b)

step(1)

expect(c.io.s,(a+b)&0xff)

expect(c.io.cout,((a+b)&0x100)>>8)

}

}編寫chisel-iotesters實(shí)例:8位加法器的testbench第一個(gè)包scala.util里包含了Scala生成偽隨機(jī)數(shù)的類Random;第二個(gè)包c(diǎn)hisel3.iotesters包含了測(cè)試類PeekPokeTester;24運(yùn)行chisel-iotesters

運(yùn)行這個(gè)主函數(shù),執(zhí)行成功后,就能在相應(yīng)文件夾里看到一個(gè)新生成的文件夾,里面是仿真生成的文件。其中,“NBitsAdder.vcd”文件就是波形文件,使用GTKWave軟件打開就能查看。運(yùn)行測(cè)試,需要通過主函數(shù)://NbitsAdderTest.scala

objectNBitsAdderTestMainextendsApp{

//指定后端使用verilator仿真

chisel3.iotesters.Driver.execute(Array("--target-dir","./generated/chapter05/NBitsAdderTest","--backend-name","verilator"),

()=>newNBitsAdder(8))(c=>newNBitsAdderTest(c))

}25運(yùn)行chisel-iotesters

那么終端就會(huì)顯示如下信息:[info][0.002]SEED1550906002475[info][0.005]POKEio_a<-184[info][0.006]POKEio_b<-142[info][0.006]STEP0->1[info][0.007]EXPECTAT1io_sgot70expected70PASS……[info][0.024]EXPECTAT10io_coutgot0expected0PASStestAdderSuccess:20testspassedin15cyclesin0.047415seconds316.36Hz[info][0.025]RAN10CYCLESPASSED[success]Totaltime:7s,completedFeb23,20193:13:26PM如果只想在終端查看仿真運(yùn)行的信息,則執(zhí)行以下主函數(shù):objectNBitsAdderTestMainextendsApp{

chisel3.iotesters.Driver.execute(Array("--target-dir","./generated/chapter05/NBitsAdderTest","--is-verbose"),()=>newNBitsAdder(8))(c=>newNBitsAdderTest(c))

}26四、編寫與運(yùn)行chiseltest27編寫chiseltest4.1

chiseltest定義:

是用于基于Chisel的RTL設(shè)計(jì)的測(cè)試工具,目前支持定向測(cè)試(手動(dòng)指定的所有測(cè)試激勵(lì))。特點(diǎn):

chiseltest強(qiáng)調(diào)輕量級(jí)(最小化樣板代碼)、易于讀寫(可理解性)和可復(fù)用(更好地重用測(cè)試代碼)的測(cè)試。核心功能:1.poke,expect,step2.fork和join類似chisel-iotesters,但語法不同:tester.poke(wire,value)是使用Scala數(shù)值,在chiseltest中,wire.poke(value)使用Chisel字面值編寫。28編寫chiseltest1.該項(xiàng)目旨在取代chisel-testers,最終可能成為chisel3

的默認(rèn)部分。其支持

chisel-testers

中的所有功能,并提供附加功能。2.用chisel-iotesters編寫的測(cè)試用例不能直接用在chiseltest中,因?yàn)檎Z法明顯不同。在Chisel項(xiàng)目的指定路徑下創(chuàng)建一個(gè)新文件,例如BasicTest.scala。然后在這個(gè)文件中:4.2

chiseltest作用4.3實(shí)現(xiàn)步驟29編寫chiseltest添加必要的導(dǎo)入: importorg.scalatest._ importchiseltest._ importchisel3._創(chuàng)建一個(gè)測(cè)試類:classBasicTestextendsFlatSpecwithChiselScalatestTesterwithMatchers{behaviorof"MyModule"http://testclassbodyhere}在測(cè)試類中,定義一個(gè)測(cè)試用例:itshould"dosomething"in{//testcasebodyhere}30編寫chiseltest在測(cè)試用例中,定義被測(cè)試的模塊: test(newMyModule){c=>//testbodyhere}在測(cè)試體中,使用poke、step和expect操作來編寫測(cè)試:

c.in.poke(0.U)c.out.expect(0.U)

c.in.poke(42.U)

c.out.expect(42.U)

測(cè)試用例完成后,可以通過調(diào)用

ScalaTest來運(yùn)行項(xiàng)目中的所有測(cè)試用例;

如果使用的是sbt,則可以從命令行sbttest或test(sbt控制臺(tái))運(yùn)行;

testOnly也可用于運(yùn)行特定測(cè)試;31classNBitsAdderChiselTestextendsFlatSpecwithChiselScalatestTesterwithMatchers{

behaviorof"NBitsAdder"

itshould"Addtwonumbers"in{

test(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論