Verilog 中信号边缘检测的正确方法

发布于 2024-12-20 10:51:09 字数 833 浏览 1 评论 0原文

我想检测信号的上升沿从触发器AABB

                    +----+
  A ----------------|    |----- OUT
        +----+      | BB |
  B ----|    |------|>   |
        | AA |      +----+
clk ----|>   |
        +----+

Verilog代码:

    module edge_detect (
        input A,
        input B,
        input clk,
        output OUT
    );

        reg AA;
        reg BB;

        always @(posedge clk) begin
            AA <= B;
        end

        always @(posedge AA)begin
            BB <= A;
        end

        assign OUT = BB;
    endmodule

AA<的输出/code> 用作 BB 的时钟,表示 AA 已完成其工作,然后 BB 现在可以继续其操作。

我很少看到这段代码。这是一个好的做法吗?

如果没有,是否还有其他正确的方法来检测信号边缘?

I want to detect a rising edge of a signal from a flip-flop AA to BB

                    +----+
  A ----------------|    |----- OUT
        +----+      | BB |
  B ----|    |------|>   |
        | AA |      +----+
clk ----|>   |
        +----+

Verilog code:

    module edge_detect (
        input A,
        input B,
        input clk,
        output OUT
    );

        reg AA;
        reg BB;

        always @(posedge clk) begin
            AA <= B;
        end

        always @(posedge AA)begin
            BB <= A;
        end

        assign OUT = BB;
    endmodule

The output of AA is used as a clock to BB saying that AA has done its job and then BB can now continue its operation.

I rarely see this code. Is this a good practice?

If not, are there any other proper way to detect an edge of a signal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2024-12-27 10:51:10

如果您想在 Verilog 中检测上升沿或下降沿,只需将信号流水线或延迟 1 个时钟脉冲即可。在数字环境中,边缘可以被视为 0 到 1 的转换或 1 到 0 的转换。
因此,您可以检查信号是否转换到任一状态,然后仅在该情况下将输出断言为高电平。

例如 :

output out_flag; 
reg temp;    
reg temp_d;   
always@(posedge clk)
temp_d <= temp;    
always@(posedge clk)
begin
 if (temp && ~temp_d)
   out_flag<= 1'b1;
 else
   out_flag<= 1'b0;
end

If you wanna detect a rising or a falling edge in Verilog, simply pipeline or delay the signal by 1 clock pulse. In a digital environment, an edge can be thought of as a 0 to 1 transition or 1 to 0 transition.
So you can check if the signal made a transition to either state and then assert your output high only for that condition.

For example :

output out_flag; 
reg temp;    
reg temp_d;   
always@(posedge clk)
temp_d <= temp;    
always@(posedge clk)
begin
 if (temp && ~temp_d)
   out_flag<= 1'b1;
 else
   out_flag<= 1'b0;
end
第七度阳光i 2024-12-27 10:51:09

由于各种原因,人们往往不赞成使用数据作为时钟。

就我个人而言,如果我写这篇文章,我会同意:

module edge_detect (
    input A,
    input B,
    input clk,
    output OUT
);

    reg AA;
    reg BB;
    wire enA;

    always @(posedge clk) begin
        BB <= B;
    end

    assign enA = !BB && B;

    always @(posedge clk)begin
       if (enA) begin
            AA <= A;
      end
    end

    assign OUT = AA;
endmodule

                                +----+
  A ----------------------------|D   |----- OUT
                     +---+      | AA |
      /--------------|   |      |    |
      | +----+       |AND|------|E   |
  B ----|    |------o|   |      |    |
        | BB |       +---+      |    |
clk ----|>   |          clk ----|>   |
        +----+                  +----+

不过行为有点不同。

People tend to frown on using data as clocks for various reason.

Personally if I was writing this I'd go with:

module edge_detect (
    input A,
    input B,
    input clk,
    output OUT
);

    reg AA;
    reg BB;
    wire enA;

    always @(posedge clk) begin
        BB <= B;
    end

    assign enA = !BB && B;

    always @(posedge clk)begin
       if (enA) begin
            AA <= A;
      end
    end

    assign OUT = AA;
endmodule

                                +----+
  A ----------------------------|D   |----- OUT
                     +---+      | AA |
      /--------------|   |      |    |
      | +----+       |AND|------|E   |
  B ----|    |------o|   |      |    |
        | BB |       +---+      |    |
clk ----|>   |          clk ----|>   |
        +----+                  +----+

The behaviour is a little different though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文