HDLBits 系列(14) Latch and Dff and Edge detect

举报
李锐博恩 发表于 2021/07/15 01:51:14 2021/07/15
【摘要】 目录 D Latch DFF + GATE Mux + DFF MUX2 + DFF FSM JK 触发器 Edgedetect(边沿检测) 双边沿检测   D Latch Implement the following circuit: 这是一个锁存器,高电平跟随,低电平保持,于是设计: module top_module ( in...

目录

D Latch

DFF + GATE

Mux + DFF

MUX2 + DFF

FSM

JK 触发器

Edgedetect(边沿检测)

双边沿检测


 

D Latch

Implement the following circuit:

这是一个锁存器,高电平跟随,低电平保持,于是设计:


  
  1. module top_module (
  2. input d,
  3. input ena,
  4. output q);
  5. always@(*)begin
  6. if(ena) q = d;
  7. else ;
  8. end
  9. endmodule

DFF + GATE

Implement the following circuit:


  
  1. module top_module (
  2. input clk,
  3. input in,
  4. output out);
  5. always@(posedge clk) begin
  6. out <= in ^ out;
  7. end
  8. endmodule

Mux + DFF

Taken from ECE253 2015 midterm question 5

Consider the sequential circuit below:

Assume that you want to implement hierarchical Verilog code for this circuit, using three instantiations of a submodule that has a flip-flop and multiplexer in it. Write a Verilog module (containing one flip-flop and multiplexer) named top_module for this submodule.

假设您要使用其中具有触发器和多路复用器的子模块的三个实例化来为此电路实现分层的Verilog代码。 为此子模块编写一个名为top_module的Verilog模块(包含一个触发器和多路复用器)。

要求写出其中一个子模块即可:


  
  1. module top_module (
  2. input clk,
  3. input L,
  4. input r_in,
  5. input q_in,
  6. output reg Q);
  7. wire mid;
  8. assign mid = L ? r_in : q_in;
  9. always@(posedge clk) begin
  10. Q <= mid;
  11. end
  12. endmodule

其实读者应该自己用例化的方式,把整个电路实现了。


MUX2 + DFF

Consider the n-bit shift register circuit shown below:

Write a Verilog module named top_module for one stage of this circuit, including both the flip-flop and multiplexers.


  
  1. module top_module (
  2. input clk,
  3. input w, R, E, L,
  4. output Q
  5. );
  6. reg Q;
  7. wire mid1, mid2;
  8. assign mid1 = E ? w : Q;
  9. assign mid2 = L ? R : mid1;
  10. always@(posedge clk) begin
  11. Q <= mid2;
  12. end
  13. endmodule

FSM

Given the finite state machine circuit as shown, assume that the D flip-flops are initially reset to zero before the machine begins.

Build this circuit.


  
  1. module top_module (
  2. input clk,
  3. input x,
  4. output z
  5. );
  6. reg q1, q2, q3;
  7. always@(posedge clk) begin
  8. q1 <= q1 ^ x;
  9. q2 <= ~q2 & x;
  10. q3 <= ~q3 | x;
  11. end
  12. assign z = ~(q1 | q2 | q3);
  13. endmodule

JK 触发器

A JK flip-flop has the below truth table. Implement a JK flip-flop with only a D-type flip-flop and gates. Note: Qold is the output of the D flip-flop before the positive clock edge.

J K Q
0 0 Qold
0 1 0
1 0 1
1 1 ~Qold

  
  1. module top_module (
  2. input clk,
  3. input j,
  4. input k,
  5. output Q);
  6. always@(posedge clk) begin
  7. if(~j & ~k) Q <= Q;
  8. else if(j&k) Q <= ~Q;
  9. else if(~j & k) Q <= 0;
  10. else Q <= 1;
  11. end
  12. endmodule

Edgedetect(边沿检测)

For each bit in an 8-bit vector, detect when the input signal changes from 0 in one clock cycle to 1 the next (similar to positive edge detection). The output bit should be set the cycle after a 0 to 1 transition occurs.

Here are some examples. For clarity, in[1] and pedge[1] are shown separately.

对于8位向量中的每个位,检测输入信号何时从一个时钟周期的0变为下一时钟周期的1(类似于上升沿检测)。 输出位应在发生0到1转换后的周期内进行设置。

这里有些例子。 为了清楚起见,分别显示了in [1]和pedge [1]。


  
  1. module top_module (
  2. input clk,
  3. input [7:0] in,
  4. output [7:0] pedge
  5. );
  6. reg [7:0] in_r1, in_r2;
  7. always@(posedge clk) begin
  8. in_r1 <= in;
  9. in_r2 <= in_r1;
  10. end
  11. assign pedge = ~in_r2 & in_r1;
  12. endmodule

双边沿检测

For each bit in an 8-bit vector, detect when the input signal changes from one clock cycle to the next (detect any edge). The output bit should be set the cycle after a 0 to 1 transition occurs.

Here are some examples. For clarity, in[1] and anyedge[1] are shown separately


  
  1. module top_module (
  2. input clk,
  3. input [7:0] in,
  4. output [7:0] anyedge
  5. );
  6. reg [7:0] in_r1, in_r2;
  7. always@(posedge clk) begin
  8. in_r1 <= in;
  9. in_r2 <= in_r1;
  10. end
  11. assign anyedge = in_r2 ^ in_r1;
  12. endmodule

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/103226673

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。