同步加法器

这个例子创建了一个由时序逻辑组成的 Component ,它对 3 个操作数进行了一些简单的算术运算。

测试平台执行 100 次以下步骤:

  • a, b, 和 c 初始化为 0..255 范围内的随机整数。

  • 激励 DUT 匹配 a, b, c 的输入。

  • 等待直到仿真再次对 DUT 的信号进行采样。

  • 检查输出是否正确。

此示例与异步加法器 Asynchronous adder 示例之间的主要区别在于,此 Component 必须使用 forkStimulus 来生成时钟信号,因为它在内部使用顺序逻辑。

import spinal.core._
import spinal.core.sim._

import scala.util.Random


object SimSynchronousExample {
  class Dut extends Component {
    val io = new Bundle {
      val a, b, c = in UInt (8 bits)
      val result = out UInt (8 bits)
    }
    io.result := RegNext(io.a + io.b - io.c) init(0)
  }

  def main(args: Array[String]): Unit = {
    SimConfig.withWave.compile(new Dut).doSim{ dut =>
      dut.clockDomain.forkStimulus(period = 10)

      var resultModel = 0
      for(idx <- 0 until 100){
        dut.io.a #= Random.nextInt(256)
        dut.io.b #= Random.nextInt(256)
        dut.io.c #= Random.nextInt(256)
        dut.clockDomain.waitSampling()
        assert(dut.io.result.toInt == resultModel)
        resultModel = (dut.io.a.toInt + dut.io.b.toInt - dut.io.c.toInt) & 0xFF
      }
    }
  }
}