折騰一下發現還是IntelliJ更好用一些,所以接下來采用IntelliJ IDEA學習SpinalHDL
安裝環境不再贅述
建立工程
建立工程官方推薦的辦法是從SpinalTemplateSbt創建。
下載好之后,解壓到文件夾
unzip SpinalTemplateSbt-master.zip -d test_m/
在IntelliJ IDEA中導入工程
選擇build.sbt之后,點擊open as project導入工程
在src/main/scala/mylib
中會包含MyTopLevel
和MyTopLevelSim
對應這Spinal描述文件和仿真文件,可以再這個基礎上更改,或者新建一組文件。
創建一個電路
我們選擇在mylib中再創建一個文件,MyTopLevel
和MyTopLevelSim
就用過參考
創建一個電路來檢測輸入信號上升沿個數。
package mylib
import spinal.core._
import spinal.lib._
class RiseCounter extends Component{
val io=new Bundle{
val sigIn=in Bool
val clear=in Bool
val cnt=out UInt (32 bits)
}
val counter=new Area{
val cnt=Counter(32 bits,io.sigIn.rise(False))
when(io.clear){
cnt.value.clearAll()
}
io.cnt:=cnt.value
}
}
//Generate the RiseCounter's Verilog
object RiseCounterVerilog {
def main(args: Array[String]) {
SpinalVerilog(new RiseCounter)
}
}
點擊運行之后,最后在工程的根目錄生成對應的Verilog代碼
仿真
參考MyTopLevelSim
編寫RiseCounterSim
package mylib
import spinal.core._
import spinal.sim._
import spinal.core.sim._
import scala.util.Random
object RiseCounterSim {
def main(args: Array[String]) {
// 建立仿真 其中RiseCounter 來創建代碼
SimConfig.withWave.doSim(new RiseCounter) { dut =>
// 創建時鍾
//Fork a process to generate the reset and the clock on the dut
dut.clockDomain.forkStimulus(period = 10)
// 注意 io的電平賦值用 #=
dut.io.clear #= true
// 等待三個時鍾周期,不知道是否有更好的寫法
for(idx <- 0 to 3){
dut.clockDomain.waitRisingEdge()
}
dut.io.clear #= false
for(idx <- 0 to 99){
//Drive the dut inputs with random values
dut.io.sigIn #= Random.nextBoolean()
//Wait a rising edge on the clock
dut.clockDomain.waitRisingEdge()
}
}
}
}
之后點擊運行之后,就可以自動啟動仿真,並輸出波形
波形路徑在simWorkspace/RiseCounter/test.vcd
可以使用GTKWave打開