Verilog基础(五):时序逻辑

时序逻辑 (Sequential Login)

锁存器与触发器
- D-触发器(D flip-flops)

D-触发器可以存储一个bit数据并根据时钟信号周期的更新数据,一般是由正边沿触发.
D-触发器由逻辑合成器(Logic synthesizer)在使用"Always block"时创建(参见AlwaysBlock2).D-触发器是"组合逻辑块之后连接触发器"的最简单形式,其中组合逻辑部分只是一个wire类型变量.

dff

创建一个D-触发器.

  • Module Declaration
module top_module (
    input clk,
    input [7:0] d,
    output [7:0] q
);
  • Solution
module top_module (
    input clk,    // Clocks are used in sequential circuits
    input d,
    output reg q );//

    // Use a clocked always block   使用Always block
    //   copy d to q at every positive edge of clk  在时钟信号上升沿时将q复制给d  
    //   Clocked always blocks should use non-blocking assignments  
    /*在Always block内永远都不要使用赋值(Assignment),即assign关键字*/
    always @(posedge clk) begin
        q = d;
    end

endmodule
- D触发器组合

创建8个D触发器,所有的D触发器均由时钟上升沿触发.

  • Module Declaration
module top_module (
    input clk,
    input [7:0] d,
    output [7:0] q
);
  • Solution
module top_module (
    input clk,
    input [7:0] d,
    output [7:0] q
);
    always @(posedge clk) begin
        q = d;
    end
endmodule
- 可复位(Reset)的DFF

使用主动高位同步复位信号(Reset)创建8个D-触发器.

所有的触发器均由时钟上升沿信号触发.

  • Module Declaration
module top_module (
    input clk,
    input reset,            // Synchronous reset
    input [7:0] d,
    output [7:0] q
);
  • Solution
module top_module (
    input clk,
    input reset,            // Synchronous reset
    input [7:0] d,
    output [7:0] q
);
    always @(posedge clk) begin
        q <= d & {8{~reset}};
    end
endmodule
- 可复位为特殊值的D-触发器

使用主动高位同步复位信号(Reset)创建8个D-触发器.

触发器必须重置为0x34而不是零.

所有DFF都应该由CLK的下降沿触发.

  • Module Declaration
module top_module (
    input clk,
    input reset,
    input [7:0] d,
    output [7:0] q
);
  • Solution
module top_module (
    input clk,
    input reset,
    input [7:0] d,
    output [7:0] q
);
    always @(negedge clk) begin
        if(reset) begin
            q <= {8{6'h34}};
        end
        else begin
            q <= d;
        end
    end
endmodule
- 可异步复位的触发器(Asynchronous Reset)

使用主动高位异步复位信号创建8个D-触发器.

所有DFF都应该由时钟上升沿触发.

  • Module Declaration
module top_module (
    input clk,
    input areset,   // active high asynchronous reset
    input [7:0] d,
    output [7:0] q
);
  • Solution
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 == 1) begin
            q <= 0;
        end
        else begin
            q <= d;
        end
    end
endmodule
- 有开关的触发器

创建16个D-触发器.有时我们希望只修改触发器组的部分值.输入的开关字节控制16个寄存器的每个字节是否应在该循环中写入.

byteena[1]控制上字节d[15:8],而byteena[0]控制下字节d[7:0].

resetn是一个同步的、主动的低重置.

所有DFF都应该由时钟的上升沿触发.

  • Module Delclaration
module top_module (
    input clk,
    input resetn,
    input [1:0] byteena,
    input [15:0] d,
    output [15:0] q
);
  • Solution
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 == 1) begin
            if(byteena[0] == 1) begin
                q[7:0] <= d[7:0];
            end
            if(byteena[1] == 1) begin
                q[15:8] <= d[15:8];
            end
        end
        else if(resetn == 0) begin
            q <= 0;
        end
    end
endmodule
- D-锁存器(D Latch)

实现如下电路:

D Latch

请注意,这是一个锁存器,因此Quartus会报锁存警告

  • Module Declaration
module top_module (
    input d, 
    input ena,
    output q);
module top_module (
    input d, 
    input ena,
    output q);
    wire in,out1,out2;
    always @(*) begin
        out1 = ~(out2 | (~d)&ena);
        out2 = ~(out1 | d&ena);
        q = out1;
    end
endmodule
- DFF练习1

请实现如下电路:

  • Module Declaration
module top_module (
    input clk,
    input d, 
    input ar,   // asynchronous reset
    output q);
  • Solution
module top_module (
    input clk,
    input d, 
    input ar,   // asynchronous reset
    output q);
    wire out1,out2;
    always @(posedge clk or posedge ar) begin
        if(ar == 1) begin
            q <= 0;
        end
        else if(ar == 0) begin
            q <= d;
        end
    end
endmodule
- DFF2

实现如下电路:
DFF2

  • Module Declaration
module top_module (
    input clk,
    input d, 
    input r,   // synchronous reset
    output q);
  • Solution
module top_module (
    input clk,
    input d, 
    input r,   // synchronous reset
    output q);
    always @(posedge clk) begin
        if(r) begin
            q <= 0;
        end
        else begin
            q = d;
        end
    end
endmodule
- DFF和gate

实现如下电路:
DFF and gage

  • Module Declaration

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TrustZone_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值