访问仿真信号(Accessing signals of the simulation)

一、读写信号(Read and write signals)

每个顶层的接口信号都可以通过Scala读写:

语句 描述
Bool.toBoolean 读出硬件Bool信号作为ScalaBoolean
Bits/UInt/SInt.toInt 读出硬件BitVector信号作为ScalaInt
Bits/UInt/SInt.toLong 读出硬件BitVector信号作为ScalaLong
Bits/UInt/SInt.toBigInt 读出硬件BitVector信号作为ScalaBigInt
SpinalEnumCraft.toEnum 读出硬件SpinalEnumCraft信号作为ScalaSpinalEnumElement
Bool #= Boolean 用ScalaBoolean值赋值给硬件Bool信号
Bits/UInt/SInt #= Int 用ScalaInt值赋值给硬件BitVector信号
Bits/UInt/SInt #= Long 用ScalaLong值赋值给硬件BitVector信号
Bits/UInt/SInt #= BigInt 用ScalaBigInt值赋值给硬件BitVector信号
SpinalEnumCraft #= SpinalEnumElement 用ScalaSpinalEnumElement值赋值给硬件SpinalEnumCraft信号
Data.randomize() 给SpinalHDL值赋随机值
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标签:

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)
      }
    }
  }
}

或者你可以在完成对顶层例化后, 在仿真时增加标签

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)
      }
    }
  }
}