verilog 学习笔记 —— 时序逻辑 Sequential Logics (Latches and Flip-Flops 锁存器和触发器)

这篇具有很好参考价值的文章主要介绍了verilog 学习笔记 —— 时序逻辑 Sequential Logics (Latches and Flip-Flops 锁存器和触发器)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. D flip-flop D触发器

门控制d锁存器的verilog测试代码,学习,笔记

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

2. D flip-flop  D触发器

门控制d锁存器的verilog测试代码,学习,笔记

module top_module (
    input clk,
    input [7:0] d,
    output [7:0] q
);
    // Because q is a vector, this creates multiple DFFs.
    always@(posedge clk)
        begin
            q <= d ;
        end

endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

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

endmodule

4. 带复位值的D触发器

门控制d锁存器的verilog测试代码,学习,笔记

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

endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

module top_module(
	input clk,
	input [7:0] d,
	input areset,
	output reg [7:0] q);
	
	// The only difference in code compared to synchronous reset is in the sensitivity list.
	always @(posedge clk, posedge areset)
		if (areset)
			q <= 0;
		else
			q <= d;


	// In Verilog, the sensitivity list looks strange. The FF's reset is sensitive to the
	// *level* of areset, so why does using "posedge areset" work?
	// To see why it works, consider the truth table for all events that change the input 
	// signals, assuming clk and areset do not switch at precisely the same time:
	
	//  clk		areset		output
	//   x		 0->1		q <= 0; (because areset = 1)   上升沿触发  复位
	//   x		 1->0		no change (always block not triggered)
	//  0->1	   0		q <= d; (not resetting)        上升沿触发  复位
	//  0->1	   1		q <= 0; (still resetting, q was 0 before too)    复位
	//  1->0	   x		no change (always block not triggered)
	
endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

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 <= 16'b0;
            else
                begin
                    if(byteena[0])
                        q[7:0] <= d[7:0];
                    if(byteena[1])   
                        q[15:8] <=d[15:8];
                end
        end


endmodule

7. D Latch  D锁存器

门控制d锁存器的verilog测试代码,学习,笔记

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

endmodule

8. DFF

门控制d锁存器的verilog测试代码,学习,笔记

module top_module (
    input clk,
    input d, 
    input ar,   // asynchronous reset  异步复位
    output q);
    always@(posedge clk, posedge ar)  //异步复位,always语句条件需要注明
        begin
            if(ar)
                q<=1'b0;
            else
                q<=d;
        end

endmodule

 9. DFF

门控制d锁存器的verilog测试代码,学习,笔记

 

module top_module (
    input clk,
    input d, 
    input r,   // synchronous reset  同步复位
    output q);
    always@(posedge clk)
        begin
            if(r)
                q<=0;
            else
                q<=d;
        end

endmodule

10. DFF+gate

门控制d锁存器的verilog测试代码,学习,笔记

 

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

endmodule

11. Mux and DFF

门控制d锁存器的verilog测试代码,学习,笔记

 门控制d锁存器的verilog测试代码,学习,笔记

module top_module (
	input clk,
	input L,
	input r_in,
	input q_in,
	output reg Q);
    wire D;
    assign D=L?r_in:q_in;   //多路复用器
    always@(posedge clk)    //触发器
        begin
            Q<=D;
        end

endmodule

12. DFFs and gates

门控制d锁存器的verilog测试代码,学习,笔记

 

module top_module (
    input clk,
    input w, R, E, L,
    output Q
);
    wire W,D;             //多路复用器
    assign W=E?w:Q;
    assign D=L?R:W;
    always@(posedge clk)  //触发器
        begin
            Q<=D;
        end

endmodule

13.  DFFs and gates

门控制d锁存器的verilog测试代码,学习,笔记

 

module top_module (
    input clk,
    input x,
    output z
); 
    wire Q1,Q2,Q3;
    wire D1,D2,D3;
    assign D1=x^Q1;
    assign D2=x&~Q2;
    assign D3=x|~Q3;
    assign z=~(Q1|Q2|Q3);
    always@(posedge clk)
        begin
            Q1<=D1;
            Q2<=D2;
            Q3<=D3;
        end
    

endmodule

14. Create circuit from turth table 

门控制d锁存器的verilog测试代码,学习,笔记

module top_module (
    input clk,
    input j,
    input k,
    output Q); 
    wire D;
    assign D=j&~Q | ~k&Q;
    always@(posedge clk)
        begin
            Q<=D;
        end
endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

module top_module (
    input clk,
    input [7:0] in,
    output [7:0] pedge
);
    reg [7:0] mid;
    always@(posedge clk)
        begin
            mid<=in;  //把上一周期的in值赋给mid
            pedge<=~mid&in;  //上一周期in为0,这一周期in为1,pedge才会有值
        end

endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

 

module top_module (
    input clk,
    input [7:0] in,
    output [7:0] anyedge
);
    reg [7:0]mid;

    always@(posedge clk)
        begin 
            mid<=in;        //把上一周期的In值赋给mid
            anyedge <= ~mid&in | mid&~in ;  //上一周期0现在1  或者  上一周期1现在0  
        end
        

endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

module top_module (
    input clk,
    input reset,
    input [31:0] in,
    output [31:0] out
);
    reg [31:0] mid;
    always@(posedge clk)
        begin
            mid <= in;
            if(reset)
                out <= 4'h0;   //复位前保持高电平,复位后变为低电平
            else
                out <= mid&~in | out ;   //   |out  表示    会保持高电平,直到收到复位信号
        end
endmodule

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

门控制d锁存器的verilog测试代码,学习,笔记

 

module top_module (
    input clk,
    input d,
    output q
);
    reg q0,q1;
    always@(posedge clk)
        begin 
            q0 <= d;    //q0为上升沿触发的输出
        end
    always@(negedge clk)
        begin
            q1 <= d;    //q1为下降沿触发的输出
        end
    assign q = clk?q0:q1;  //根据时钟clk决定,最终使用哪一段 输出
 
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

 文章来源地址https://www.toymoban.com/news/detail-765546.html

到了这里,关于verilog 学习笔记 —— 时序逻辑 Sequential Logics (Latches and Flip-Flops 锁存器和触发器)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【HDLBits 刷题 6】Circuits(2)Sequential Logic---Latches and Filp Flops

    目录   写在前面 Latches and Filp Flops Dff Dff8 Dff8r Dff8p Dff8ar Dff16e D Latch DFF1 DFF2 DFF gate Mux and DFF1 Mux and DFF2 DFFs and gates creat circuit Edgedetect Edgedetect2 Edgecapture Dualedge 总结   本篇博客对 Circuits 部分的组合逻辑前两节做答案和部分解析,一些比较简单的题目就直接给出答案,有些难度

    2024年02月03日
    浏览(29)
  • OUC数字逻辑Verilog实验二 用Verilog实现4位计数器(时序逻辑)

    clk为模拟的脉冲,reset为重置信号,如果reset为0,则把init的值作为初始值赋值给out,enable为使能端,如果enable为1,则在上升沿根据mode的值,如果mode为1,为加计数,mode为0,为减计数。 仿真图像中, 第1个脉冲,reset为0,为out赋值输入的初始值0010。 第2~6个脉冲,enable为1,

    2024年01月17日
    浏览(35)
  • Verilog学习笔记(3):Verilog数字逻辑电路设计方法

    例:用Verilog设计模256(8bits)计数器 (a)可综合程序描述方式 (b)常见的错误描述方式 同时Verilog的电路描述方式具有多样性,这也决定了对于电路设计的多样性。 例:用Verilog设计数字多路选择器 (a)采用真值表形式的代码 (b)采用逻辑表达式形式的代码 (c)采用结

    2023年04月08日
    浏览(103)
  • 【Verilog刷题篇】硬件工程师从0到入门3|组合逻辑复习+时序逻辑入门

    硬件工程师近年来也开始慢慢吃香,校招进大厂年薪总包不下30-40w的人数一大把!而且大厂人数并没有饱和! - 本期是【Verilog刷题篇】硬件工程师从0到入门3|组合逻辑复习+时序逻辑入门,有不懂的地方可以评论进行讨论! 推荐给大家一款刷题、面试的神器 ,我也是用这一款

    2024年02月03日
    浏览(26)
  • Verilog Tutorial(5)使用always块实现时序逻辑电路

    在自己准备写verilog教程之前,参考了许多资料----FPGA Tutorial网站的这套verilog教程即是其一。这套教程写得不错,只是没有中文,在下只好斗胆翻译过来(加了自己的理解)分享给大家。 这是网站原文:https://fpgatutorial.com/verilog/ 这是系列导航:Verilog教程系列文章导航 这篇文

    2023年04月21日
    浏览(28)
  • (数字逻辑笔记)用Verilog实现一个简单ALU(组合逻辑)

    实验描述: 输入 :两个4位二进制数,代表两个操作数A,B;一个3位控制信号operation,代表ALU要进行的运算。本实验中,ALU可以实现8种运算: 输出 :4位结果,1位进位 operation | F 000 | A + B 001 | A - B 010 | B + 1 011 | B - 1 100 | NOT A 101 | A XOR B 110 | A AND B 111 | A OR B 实现代码: TestBen

    2024年02月04日
    浏览(37)
  • [从零开始学习FPGA编程-32]:进阶篇 - 基本时序电路-D触发器(Verilog语言)

    作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客 本文网址:  目录 第1章 什么是时序电路 1.1 时序电路 1.2 什么是触发器

    2023年04月08日
    浏览(35)
  • 组合逻辑、时序逻辑的适用场合、数字逻辑电路的时序分析

    组合逻辑: 组合逻辑是一类逻辑电路,其输出仅仅取决于当前的输入信号状态,而不考虑过去的信号状态。 组合逻辑电路的输出完全由输入决定,没有时钟信号的概念,因此输出是输入的函数。 例子包括逻辑门(AND、OR、NOT等)和其他不带存储元件(如触发器)的电路。 时

    2024年02月03日
    浏览(32)
  • bupt数字逻辑时序逻辑实验

    实验一 序列检测器 实验内容 设计一个序列检测器检测序列1110010。 设计思路 每输入一个序列1110010则会在输出端输出一个1,其余时间为0。 首先写出状态转移图,再利用case语句,根据状态转移图写出状态的转移及输出。 检测序列为七位,所以可以设状态机状态数为8个; 输

    2024年02月09日
    浏览(43)
  • 时序逻辑电路一——数字逻辑实验

    (1)熟悉触发器的逻辑功能及特性。 (2)掌握集成D和JK触发器的应用。 (3)掌握时序逻辑电路的分析和设计方法。 用D触发器(74LS74)组成二分频器、四分频器 74LS74是双D触发器(上升沿触发的D触发器),其管脚图和功能表如下: 每个74LS74芯片有两个D触发器,每个D触发器

    2024年02月06日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包