设为首页 加入收藏

TOP

【HDLBits刷题笔记】09 Latches and Flip-Flops(二)
2023-07-23 13:25:53 】 浏览:317
Tags:HDLBits Latches and Flip-Flops
ut
clk, input [7:0] in, output reg[7:0] anyedge ); reg [7:0] in_r; always@(posedge clk) begin in_r <= in; anyedge <= in_r^in; end endmodule

Edgecapture

capture和detect区别:capture会保持1,直到reset。

out <= (~in&in_r)|out;代表只有1会传递到out,从而达到保持的作用。

module top_module (
    input clk,
    input reset,
    input [31:0] in,
    output reg[31:0] out
);
    reg [31:0] in_r;
    always@(posedge clk)
    begin
        in_r <= in;
        if(reset)
            out <= 'd0;
        else if(~in&in_r)
            out <= (~in&in_r)|out;
    end

endmodule

Dualedge

要求写一个双边沿触发器,题目已经告诉了@(posedge clk or negedge clk)的写法是不被允许的。

这题确实没想到好的方法,我的写法可能会产生毛刺,因为在边沿的时候q_p和q_n需要时间跳变,这个时候输出就可能有问题,当然,这样仿真还是能通过的,但实际电路中不能这样写。

题目给的答案非常巧妙,上升沿时p变为d^n,所以输出q = (p^n) = (d^n^n) = d,下降沿同理。

我的答案:

module top_module (
    input clk,
    input d,
    output q
);
    reg q_p,q_n;
    always@(posedge clk)
        q_p <= d;
    always@(negedge clk)
        q_n <= d;
    assign q = clk?q_p:q_n;
endmodule

标准答案:

module top_module(
    input clk,
    input d,
    output q);
    
    reg p, n;
    
    // A positive-edge triggered flip-flop
    always @(posedge clk)
        p <= d ^ n;
        
    // A negative-edge triggered flip-flop
    always @(negedge clk)
        n <= d ^ p;
    
    // Why does this work? 
    // After posedge clk, p changes to d^n. Thus q = (p^n) = (d^n^n) = d.
    // After negedge clk, n changes to d^p. Thus q = (p^n) = (p^d^p) = d.
    // At each (positive or negative) clock edge, p and n FFs alternately
    // load a value that will cancel out the other and cause the new value of d to remain.
    assign q = p ^ n;
    
    
    // Can't synthesize this.
    /*always @(posedge clk, negedge clk) begin
        q <= d;
    end*/
    
    
endmodule

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【HDLBits刷题笔记】06 Basic Gat.. 下一篇【HDLBits刷题笔记】05 More Veri..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目