RisingEdge示例不适用于Chisel3中的模块输入信号

时间:2019-09-10 07:41:32

标签: hdl chisel icarus cocotb

在Chisel文档中,我们有一个定义如下的上升沿检测方法的示例:

      def risingedge(x: Bool) = x && !RegNext(x)

所有示例代码都可以在我的github project blp上找到。

如果在声明如下的输入信号上使用它:

class RisingEdge extends Module {
  val io = IO(new Bundle{
    val sclk = Input(Bool())
    val redge = Output(Bool())
    val fedge = Output(Bool())
  })

  // seems to not work with icarus + cocotb
  def risingedge(x: Bool) = x && !RegNext(x)
  def fallingedge(x: Bool) = !x && RegNext(x)
  // works with icarus + cocotb
  //def risingedge(x: Bool) = x && !RegNext(RegNext(x))
  //def fallingedge(x: Bool) = !x && RegNext(RegNext(x))

  io.redge :=  risingedge(io.sclk)
  io.fedge := fallingedge(io.sclk)
}

有了这个icarus / cocotb测试台:

class RisingEdge(object):
    def __init__(self, dut, clock):
        self._dut = dut
        self._clock_thread = cocotb.fork(clock.start())

    @cocotb.coroutine
    def reset(self):
        short_per = Timer(100, units="ns")
        self._dut.reset <= 1
        self._dut.io_sclk <= 0
        yield short_per
        self._dut.reset <= 0
        yield short_per

@cocotb.test()
def test_rising_edge(dut):
    dut._log.info("Launching RisingEdge test")
    redge = RisingEdge(dut, Clock(dut.clock, 1, "ns")) 
    yield redge.reset()
    cwait = Timer(10, "ns")
    for i in range(100):
        dut.io_sclk <= 1
        yield cwait
        dut.io_sclk <= 0
        yield cwait

我永远不会在io.redge和io.fedge上收到上升脉冲。为了获得脉冲,我必须更改上升沿的定义,如下所示:

  def risingedge(x: Bool) = x && !RegNext(RegNext(x))

使用双RegNext(): Dual RegNext screenshot

使用简单的RegNext(): Simple RegNext screenshot

这是正常行为吗?

[编辑:我使用上面给出的github示例修改了源示例]

2 个答案:

答案 0 :(得分:3)

我不确定Icarus,但使用默认的Treadle模拟器进行此类测试。

class RisingEdgeTest extends FreeSpec {
  "debug should toggle" in {
    iotesters.Driver.execute(Array("-tiwv"), () => new SlaveSpi) { c =>
      new PeekPokeTester(c) {
        for (i <- 0 until 10) {
          poke(c.io.csn, i % 2)
          println(s"debug is ${peek(c.io.debug)}")
          step(1)
        }
      }
    }
  }
}

我看到了输出

[info] [0.002] debug is 0
[info] [0.002] debug is 1
[info] [0.002] debug is 0
[info] [0.003] debug is 1
[info] [0.003] debug is 0
[info] [0.003] debug is 1
[info] [0.004] debug is 0
[info] [0.004] debug is 1
[info] [0.005] debug is 0
[info] [0.005] debug is 1

波形看起来像 enter image description here

您能解释一下您的想法吗?

答案 1 :(得分:1)

请勿在时钟的上升沿更改模块输入值。

好的,我发现了我的错误。在cocotb测试台中,我在同步时钟的同一沿上切换了输入值。如果这样做,则在D-Latch的设置时间内完全修改了输入,那么行为是不确定的!

然后,问题出在cocotb测试台错误,而不是Chisel错误。为了解决这个问题,我们只需要更改时钟沿以切换类似的值即可:

@cocotb.test()
def test_rising_edge(dut):
    dut._log.info("Launching RisingEdge test")
    redge = RisingEdge(dut, Clock(dut.clock, 1, "ns")) 
    yield redge.reset()
    cwait = Timer(4, "ns")
    yield FallingEdge(dut.clock) #   <--- 'synchronize' on falling edge
    for i in range(5):
        dut.io_sclk <= 1
        yield cwait
        dut.io_sclk <= 0
        yield cwait