【chisel】如何用chisel写一个上升沿检测程序

34 篇文章 1 订阅
14 篇文章 0 订阅

自定义上升沿检测

class RisingEdgeDetector extends Module {
  val io = IO(new Bundle {
    val inSig = Input(Bool())
    val outSig = Output(Bool())
    val clock = Input(Clock())
  })

  // 定义 risingedge 函数
  def risingedge(x: Bool): Bool = x && !RegNext(x)

  // 使用 risingedge 函数检测上升沿
  io.outSig := risingedge(io.inSig)

  // Chisel 会为 RegNext 自动生成一个寄存器来保存 inSig 的值
  // 并在下一个时钟周期提供这个值
}

实现主程序

package hello

import chisel3.{Clock, _}
class RisingEdgeDetector extends Module {
  val io = IO(new Bundle {
    val inSig = Input(Bool())
    val outSig = Output(Bool())
  })

  // 定义 risingedge 函数
  def risingedge(x: Bool): Bool = x && !RegNext(x)

  // 使用 risingedge 函数检测上升沿
  io.outSig := risingedge(io.inSig)

  // Chisel 会为 RegNext 自动生成一个寄存器来保存 inSig 的值
  // 并在下一个时钟周期提供这个值
}

class risingedge extends Module {
  val io = IO(new Bundle {
    val detectedRisingEdge  = Output(Bool()) // IO names will be the same
    val someBoolSignal  = Input(Bool())  // (without 'io_' in prefix)
  })
  // 创建 RisingEdgeDetector 模块的实例
  val detector = Module(new RisingEdgeDetector)
  // 连接输入和输出
  detector.io.inSig := io.someBoolSignal // 这里的 someBoolSignal 应该是你的设计中的一个 Bool 类型的信号
  io.detectedRisingEdge := detector.io.outSig // 这将是一个 Bool 类型的信号,表示上升沿的检测结果
}
object risingedgeMain extends App {
  (new chisel3.stage.ChiselStage).emitVerilog(new risingedge(), Array("--target-dir", "generated"))
}

运行以上chisel,生成Verilog如下

module RisingEdgeDetector(
  input   clock,
  input   io_inSig,
  output  io_outSig
);

  reg  _T; // @[risingedge.scala 11:48]
  wire  _T_1 = ~_T; // @[risingedge.scala 11:40]
  assign io_outSig = io_inSig & _T_1; // @[risingedge.scala 14:13]

  always @(posedge clock) begin
    _T <= io_inSig;
  end
endmodule


module risingedge(
  input   clock,
  input   reset,
  output  io_detectedRisingEdge,
  input   io_someBoolSignal
);
  wire  detector_clock; // @[risingedge.scala 26:24]
  wire  detector_io_inSig; // @[risingedge.scala 26:24]
  wire  detector_io_outSig; // @[risingedge.scala 26:24]
  RisingEdgeDetector detector ( // @[risingedge.scala 26:24]
    .clock(detector_clock),
    .io_inSig(detector_io_inSig),
    .io_outSig(detector_io_outSig)
  );
  assign io_detectedRisingEdge = detector_io_outSig; // @[risingedge.scala 29:25]
  assign detector_clock = clock;
  assign detector_io_inSig = io_someBoolSignal; // @[risingedge.scala 28:21]
endmodule

  • 17
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神仙约架

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值