定义为组件输入的寄存器(Register defined as component input)

简介

在SpinalHDL中,用户不被允许定义一个将寄存器作为输入的组件。原因是为了防止用户试图用寄存器信号作为子组件的输入驱动时出现意外。如果确实需要一个寄存器输入,用户可以在 io 线束中先定义一个非寄存器输入, 随后在组件内部对其添加寄存器。

示例

以下代码:

class TopLevel extends Component {
  val io = new Bundle {
    val a = in(Reg(UInt(8 bits)))
  }
}

会报错:

REGISTER DEFINED AS COMPONENT INPUT : (toplevel/io_a : in UInt[8 bits]) is defined as a registered input of the toplevel component, but isn't allowed.
  ***
  Source file location of the toplevel/io_a definition via the stack trace
  ***

一个可能的修复方法是:

class TopLevel extends Component {
  val io = new Bundle {
    val a = in UInt(8 bits)
  }
}

如果需要一个寄存器信号 a ,可以这样做:

class TopLevel extends Component {
  val io = new Bundle {
    val a = in UInt(8 bits)
  }
  val a = RegNext(io.a)
}