## 访问仿真信号(Accessing signals of the simulation) ### 一、读写信号(Read and write signals) 每个顶层的接口信号都可以通过Scala读写: | 语句 | 描述 | | :------------------------------------: | :---------------------------------------------------------: | | `Bool.toBoolean` | 读出硬件`Bool`信号作为Scala`Boolean`值 | | `Bits`/`UInt`/`SInt.toInt` | 读出硬件`BitVector`信号作为Scala`Int`值 | | `Bits`/`UInt`/`SInt.toLong` | 读出硬件`BitVector`信号作为Scala`Long`值 | | `Bits`/`UInt`/`SInt.toBigInt` | 读出硬件`BitVector`信号作为Scala`BigInt`值 | | `SpinalEnumCraft.toEnum` | 读出硬件`SpinalEnumCraft`信号作为Scala`SpinalEnumElement`值 | | `Bool #= Boolean` | 用Scala`Boolean`值赋值给硬件`Bool`信号 | | `Bits`/`UInt`/`SInt #= Int` | 用Scala`Int`值赋值给硬件`BitVector`信号 | | `Bits`/`UInt`/`SInt #= Long` | 用Scala`Long`值赋值给硬件`BitVector`信号 | | `Bits`/`UInt`/`SInt #= BigInt` | 用Scala`BigInt`值赋值给硬件`BitVector`信号 | | `SpinalEnumCraft #= SpinalEnumElement` | 用Scala`SpinalEnumElement`值赋值给硬件`SpinalEnumCraft`信号 | | `Data.randomize()` | 给SpinalHDL值赋随机值 | ```Scala dut.io.a #= 42 dut.io.a #= 42l dut.io.a #= BigInt("101010", 2) dut.io.a #= BigInt("0123456789ABCDEF", 16) println(dut.io.b.toInt) ``` ### 二、在模块层次访问信号(Accessing signals inside the component's hierarchy) 为了访问在模块层次内部的信号, 你应该先把信号设置成`simPublic`。 你可以直接在硬件描述中增加`simPublic`标签: ```Scala object SimAccessSubSignal { import spinal.core.sim._ class TopLevel extends Component { val counter = Reg(UInt(8 bits)) init(0) simPublic() //这里给counter寄存器增加simPublic标签让其可被访问 counter := counter + 1 } def main(args: Array[String]) { SimConfig.compile(new TopLevel).doSim{dut => dut.clockDomain.forkStimulus(10) for(i <- 0 to 3) { dut.clockDomain.waitSampling() println(dut.counter.toInt) } } } } ``` 或者你可以在完成对顶层例化后, 在仿真时增加标签 ```Scala object SimAccessSubSignal { import spinal.core.sim._ class TopLevel extends Component { val counter = Reg(UInt(8 bits)) init(0) counter := counter + 1 } def main(args: Array[String]) { SimConfig.compile { val dut = new TopLevel dut.counter.simPublic() dut }.doSim{dut => dut.clockDomain.forkStimulus(10) for(i <- 0 to 3) { dut.clockDomain.waitSampling() println(dut.counter.toInt) } } } } ```