IDEA中SpinalHDL+Verilator仿真进程完成后无法自动退出
问题描述
在IDEA中运行基于Verilator的SpinalHDL仿真程序时,仿真实际已完成(控制台显示[Done] Simulation done),但进程始终无法自动退出,不会输出Process finished with exit code 0,只能通过关闭IDEA来终止进程。该问题仅在本地PC出现,其他设备可正常结束进程。
相关代码
import spinal.core._ import spinal.sim._ import spinal.core.sim._ import scala.util.Random //MyTopLevel's testbench object MyTopLevelSim { def main(args: Array[String]) { SimConfig.withWave.doSim(new MyTopLevel){dut => //Fork a process to generate the reset and the clock on the dut dut.clockDomain.forkStimulus(period = 10) var modelState = 0 for(idx <- 0 to 99){ //Drive the dut inputs with random values dut.io.cond0 #= Random.nextBoolean() dut.io.cond1 #= Random.nextBoolean() //Wait a rising edge on the clock dut.clockDomain.waitRisingEdge() //Check that the dut values match with the reference model ones val modelFlag = modelState == 0 || dut.io.cond1.toBoolean assert(dut.io.state.toInt == modelState) assert(dut.io.flag.toBoolean == modelFlag) //Update the reference model value if(dut.io.cond0.toBoolean) { modelState = (modelState + 1) & 0xFF } } } } }
控制台输出
Connected to the target VM, address: '127.0.0.1:57888', transport: 'socket' [Runtime] SpinalHDL v1.6.0 git head : 73c8d8e2b86b45646e9d0b2e729291f2b65e6be3 [Runtime] JVM max memory : 4070.0MiB [Runtime] Current date : 2023.06.21 15:11:03 [Progress] at 0.000 : Elaborate components [Progress] at 0.611 : Checks and transforms [Progress] at 0.685 : Generate Verilog [Done] at 0.755 [Progress] Simulation workspace in C:\Users\xxx\spinalhdl_home\test_new\.\simWorkspace\MyTopLevel [Progress] Verilator compilation started [Progress] Verilator compilation done in 4973.795 ms [Progress] Start MyTopLevel test simulation with seed 835369353 done1 [Done] Simulation done in 20.747 ms
解决方法
1. 手动添加JVM退出指令
在仿真逻辑完成后,强制JVM正常退出。修改代码,在doSim闭包的末尾(循环结束后)添加System.exit(0):
object MyTopLevelSim { def main(args: Array[String]) { SimConfig.withWave.doSim(new MyTopLevel){dut => // ... 原有仿真代码 ... for(idx <- 0 to 99){ // ... 原有循环逻辑 ... } // 仿真结束后强制触发JVM退出 System.exit(0) } } }
该方法直接解决JVM因后台线程残留无法自动退出的问题,是最直接的临时解决方案。
2. 对齐Verilator版本
本地PC的Verilator版本可能与其他正常设备存在差异,导致兼容性问题。查看其他设备的Verilator版本(通过verilator --version命令),在本地安装相同版本,避免版本不一致引发的线程残留。
3. 清理仿真工作区
删除本地仿真工作区目录(控制台中显示的C:\Users\xxx\spinalhdl_home\test_new\.\simWorkspace\MyTopLevel),重新运行仿真,避免缓存的编译文件或临时文件导致异常。
4. 检查波形查看器状态
如果启用了withWave,本地的GTKWave可能未正常关闭,导致进程挂起。可以暂时去掉withWave参数,运行仿真验证是否能正常退出:
SimConfig.doSim(new MyTopLevel){dut => // ... 仿真代码 ... }
如果去掉后能正常退出,说明是GTKWave的问题,可尝试手动关闭波形窗口,或更新GTKWave版本。
5. 调整IDEA的JVM配置
在IDEA的Run Configuration中,修改VM options,添加-Dspinal.sim.exitAfterSim=true(部分SpinalHDL版本支持该参数自动触发退出),或切换JDK版本(比如从OpenJDK切换到Oracle JDK),排除JVM本身的问题。
内容的提问来源于stack exchange,提问作者Johnhave

