Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

synchronization - Proper way for signal edge detection in Verilog

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...