设为首页 加入收藏

TOP

【HDLBits刷题笔记】09 Latches and Flip-Flops(一)
2023-07-23 13:25:53 】 浏览:300
Tags:HDLBits Latches and Flip-Flops

Dff

这一节终于开始时序电路了。首先是一个用的最多的D触发器。

module top_module (
    input clk,    // Clocks are used in sequential circuits
    input d,
    output reg q );//

    // Use a clocked always block
    //   copy d to q at every positive edge of clk
    //   Clocked always blocks should use non-blocking assignments
    always@(posedge clk)
    begin
       q <= d; 
    end

endmodule

Dff8

8位D触发器,写法和上一题是一样的。

module top_module (
    input clk,
    input [7:0] d,
    output [7:0] q
);
    always@(posedge clk)
    begin
        q <= d;
    end

endmodule

Dff8r

带同步高电平复位的D触发器。

module top_module (
    input clk,
    input reset,            // Synchronous reset
    input [7:0] d,
    output [7:0] q
);
    always@(posedge clk)
    begin
        if(reset)
            q <= 'd0;
           else
            q <= d;
    end

endmodule

Dff8p

注意这道题触发器是下降沿触发。

module top_module (
    input clk,
    input reset,
    input [7:0] d,
    output [7:0] q
);
    always@(negedge clk)
    begin
        if(reset)
            q <= 'h34;
        else
            q <= d;        
    end

endmodule

Dff8ar

这道题是异步复位,把areset写在敏感事件列表即可。

module top_module (
    input clk,
    input areset,   // active high asynchronous reset
    input [7:0] d,
    output [7:0] q
);
    always@(posedge clk or posedge areset)
    begin
        if(areset)
            q <= 'd0;
        else
            q <= d;
    end
endmodule

Dff16e

这题多了一个字节使能信号,只有对应的字节使能才能写入,否则维持当前的值。

module top_module (
    input clk,
    input resetn,
    input [1:0] byteena,
    input [15:0] d,
    output [15:0] q
);
    always@(posedge clk)
    begin
        if(~resetn)
            q <= 'd0;
        else
            q <= {byteena[1]?d[15:8]:q[15:8],byteena[0]?d[7:0]:q[7:0]};        
    end
endmodule

Exams/m2014 q4a

这题要求使用一个latch,latch是电平触发的触发器,当ena信号为高电平时输入会传递给输出,这样的缺点是毛刺(glitch)会逐级传递,所以应尽量避免综合出不必要的latch,这一点在前面if和case语句中提到过。

提示中告诉了这里应该使用非阻塞赋值,因为其仍然是时序电路。

module top_module (
    input d, 
    input ena,
    output q);
    always@(*)
    begin
        if(ena)
            q<=d;
    end
endmodule

Exams/m2014 q4b

module top_module (
    input clk,
    input d, 
    input ar,   // asynchronous reset
    output q);
    always@(posedge clk or posedge ar)
    begin
        if(ar)
            q <= 'd0;
        else
            q <= d;
    end
endmodule

Exams/m2014 q4c

总放些重复的题有点浪费时间。。。

module top_module (
    input clk,
    input d, 
    input r,   // synchronous reset
    output q);
    always@(posedge clk)
    begin
        if(r)
            q <= 'd0;
        else
            q <= d;
    end
endmodule

Exams/m2014 q4d

module top_module (
    input clk,
    input in, 
    output out);
    always@(posedge clk)
    begin
        out <= in^out;
    end
endmodule

Mt2015 muxdff

这道题只要写出一个子模块即可。

module top_module (
    input clk,
    input L,
    input r_in,
    input q_in,
    output reg Q);
    always@(posedge clk)
    begin
        Q <= L?r_in:q_in;
    end
endmodule

Exams/2014 q4a

同样也是写一个子模块。

module top_module (
    input clk,
    input w, R, E, L,
    output Q
);
    always@(posedge clk)
    begin
        Q <= L?R:(E?w:Q);
    end
endmodule

Exams/ece241 2014 q4

根据RTL视图直接写代码就可以了。

module top_module (
    input clk,
    input x,
    output z
); 
    reg [2:0]Q=3'd0;
    always@(posedge clk)
    begin
        Q[0] <= x^Q[0];
        Q[1] <= x&~Q[1];
        Q[2] <= x|~Q[2];
    end
    assign z=~|Q;
endmodule

Exams/ece241 2013 q7

使用verilog实现一个JK触发器。

module top_module (
    input clk,
    input j,
    input k,
    output reg Q); 
    always@(posedge clk)
    begin
        case({j,k})
            2'b00:Q<=Q;
            2'b01:Q<=1'b0;
            2'b10:Q<=1'b1;
            2'b11:Q<=~Q;
        endcase
    end
endmodule

Edgedetect

用了两个always,其实和答案是一样的。

module top_module (
    input clk,
    input [7:0] in,
    output reg[7:0] pedge
);
    reg [7:0] in_r;
    always@(posedge clk)
    begin
        in_r <= in;
    end
    always@(posedge clk)
    pedge <= in&~in_r;
endmodule

Edgedetect2

和上一题思路是一样的,区别是逻辑改为异或。

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目