当前位置:   article > 正文

verilog 学习笔记 —— 时序逻辑 Sequential Logics (Latches and Flip-Flops 锁存器和触发器)_门控制d锁存器的verilog测试代码

门控制d锁存器的verilog测试代码

1. D flip-flop D触发器

  1. module top_module (
  2. input clk, // Clocks are used in sequential circuits
  3. input d,
  4. output reg q );//
  5. // Use a clocked always block
  6. // copy d to q at every positive edge of clk
  7. // Clocked always blocks should use non-blocking assignments
  8. always@(posedge clk) //固定格式
  9. begin
  10. q <= d ; // 非阻塞赋值,顺序执行
  11. end
  12. endmodule

2. D flip-flop  D触发器

  1. module top_module (
  2. input clk,
  3. input [7:0] d,
  4. output [7:0] q
  5. );
  6. // Because q is a vector, this creates multiple DFFs.
  7. always@(posedge clk)
  8. begin
  9. q <= d ;
  10. end
  11. endmodule

3. DFF with reset  带复位的D触发器 

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. input [7:0] d,
  5. output [7:0] q
  6. );
  7. always @(posedge clk)
  8. begin
  9. if(reset)
  10. q <= 8'b0;
  11. else
  12. q <= d ;
  13. end
  14. endmodule

4. 带复位值的D触发器

  1. module top_module (
  2. input clk,
  3. input reset,
  4. input [7:0] d,
  5. output [7:0] q
  6. );
  7. always@(negedge clk)
  8. begin
  9. if(reset)
  10. q <= 8'H0x34 ;
  11. else
  12. q<=d;
  13. end
  14. endmodule

5. DFF with asynchronous reset 带异步复位功能的 D触发器

  1. module top_module(
  2. input clk,
  3. input [7:0] d,
  4. input areset,
  5. output reg [7:0] q);
  6. // The only difference in code compared to synchronous reset is in the sensitivity list.
  7. always @(posedge clk, posedge areset)
  8. if (areset)
  9. q <= 0;
  10. else
  11. q <= d;
  12. // In Verilog, the sensitivity list looks strange. The FF's reset is sensitive to the
  13. // *level* of areset, so why does using "posedge areset" work?
  14. // To see why it works, consider the truth table for all events that change the input
  15. // signals, assuming clk and areset do not switch at precisely the same time:
  16. // clk areset output
  17. // x 0->1 q <= 0; (because areset = 1) 上升沿触发 复位
  18. // x 1->0 no change (always block not triggered)
  19. // 0->1 0 q <= d; (not resetting) 上升沿触发 复位
  20. // 0->1 1 q <= 0; (still resetting, q was 0 before too) 复位
  21. // 1->0 x no change (always block not triggered)
  22. endmodule

6. DFF with byte enable   带位启动的触发器

  1. module top_module (
  2. input clk,
  3. input resetn,
  4. input [1:0] byteena,
  5. input [15:0] d,
  6. output [15:0] q
  7. );
  8. always@(posedge clk)
  9. begin
  10. if(!resetn)
  11. q <= 16'b0;
  12. else
  13. begin
  14. if(byteena[0])
  15. q[7:0] <= d[7:0];
  16. if(byteena[1])
  17. q[15:8] <=d[15:8];
  18. end
  19. end
  20. endmodule

7. D Latch  D锁存器

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

8. DFF

  1. module top_module (
  2. input clk,
  3. input d,
  4. input ar, // asynchronous reset 异步复位
  5. output q);
  6. always@(posedge clk, posedge ar) //异步复位,always语句条件需要注明
  7. begin
  8. if(ar)
  9. q<=1'b0;
  10. else
  11. q<=d;
  12. end
  13. endmodule

 9. DFF

 

  1. module top_module (
  2. input clk,
  3. input d,
  4. input r, // synchronous reset 同步复位
  5. output q);
  6. always@(posedge clk)
  7. begin
  8. if(r)
  9. q<=0;
  10. else
  11. q<=d;
  12. end
  13. endmodule

10. DFF+gate

 

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

11. Mux and DFF

 

  1. module top_module (
  2. input clk,
  3. input L,
  4. input r_in,
  5. input q_in,
  6. output reg Q);
  7. wire D;
  8. assign D=L?r_in:q_in; //多路复用器
  9. always@(posedge clk) //触发器
  10. begin
  11. Q<=D;
  12. end
  13. endmodule

12. DFFs and gates

 

  1. module top_module (
  2. input clk,
  3. input w, R, E, L,
  4. output Q
  5. );
  6. wire W,D; //多路复用器
  7. assign W=E?w:Q;
  8. assign D=L?R:W;
  9. always@(posedge clk) //触发器
  10. begin
  11. Q<=D;
  12. end
  13. endmodule

13.  DFFs and gates

 

  1. module top_module (
  2. input clk,
  3. input x,
  4. output z
  5. );
  6. wire Q1,Q2,Q3;
  7. wire D1,D2,D3;
  8. assign D1=x^Q1;
  9. assign D2=x&~Q2;
  10. assign D3=x|~Q3;
  11. assign z=~(Q1|Q2|Q3);
  12. always@(posedge clk)
  13. begin
  14. Q1<=D1;
  15. Q2<=D2;
  16. Q3<=D3;
  17. end
  18. endmodule

14. Create circuit from turth table 

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

15. Detect and edge  输入的单边沿检测

  1. module top_module (
  2. input clk,
  3. input [7:0] in,
  4. output [7:0] pedge
  5. );
  6. reg [7:0] mid;
  7. always@(posedge clk)
  8. begin
  9. mid<=in; //把上一周期的in值赋给mid
  10. pedge<=~mid&in; //上一周期in为0,这一周期in为1,pedge才会有值
  11. end
  12. endmodule

16. Detect both edges  输入的双边沿检测

 

  1. module top_module (
  2. input clk,
  3. input [7:0] in,
  4. output [7:0] anyedge
  5. );
  6. reg [7:0]mid;
  7. always@(posedge clk)
  8. begin
  9. mid<=in; //把上一周期的In值赋给mid
  10. anyedge <= ~mid&in | mid&~in ; //上一周期0现在1 或者 上一周期1现在0
  11. end
  12. endmodule

 17. Edge capture register  边沿捕获寄存器

  1. module top_module (
  2. input clk,
  3. input reset,
  4. input [31:0] in,
  5. output [31:0] out
  6. );
  7. reg [31:0] mid;
  8. always@(posedge clk)
  9. begin
  10. mid <= in;
  11. if(reset)
  12. out <= 4'h0; //复位前保持高电平,复位后变为低电平
  13. else
  14. out <= mid&~in | out ; // |out 表示 会保持高电平,直到收到复位信号
  15. end
  16. endmodule

 18. Dual-edge triggered flip-flop  时钟的双边沿捕获

 

  1. module top_module (
  2. input clk,
  3. input d,
  4. output q
  5. );
  6. reg q0,q1;
  7. always@(posedge clk)
  8. begin
  9. q0 <= d; //q0为上升沿触发的输出
  10. end
  11. always@(negedge clk)
  12. begin
  13. q1 <= d; //q1为下降沿触发的输出
  14. end
  15. assign q = clk?q0:q1; //根据时钟clk决定,最终使用哪一段 输出
  16. endmodule
  1. module top_module(
  2. input clk,
  3. input d,
  4. output q);
  5. reg p, n;
  6. // A positive-edge triggered flip-flop
  7. always @(posedge clk)
  8. p <= d ^ n;
  9. // A negative-edge triggered flip-flop
  10. always @(negedge clk)
  11. n <= d ^ p;
  12. // Why does this work?
  13. // After posedge clk, p changes to d^n. Thus q = (p^n) = (d^n^n) = d.
  14. // After negedge clk, n changes to d^p. Thus q = (p^n) = (p^d^p) = d.
  15. // At each (positive or negative) clock edge, p and n FFs alternately
  16. // load a value that will cancel out the other and cause the new value of d to remain.
  17. assign q = p ^ n;
  18. // Can't synthesize this.
  19. /*always @(posedge clk, negedge clk) begin
  20. q <= d;
  21. end*/
  22. endmodule

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/603061
推荐阅读
相关标签
  

闽ICP备14008679号