时序逻辑 (Sequential Login)
锁存器与触发器
- D-触发器(D flip-flops)
D-触发器可以存储一个bit数据并根据时钟信号周期的更新数据,一般是由正边沿触发.
D-触发器由逻辑合成器(Logic synthesizer)在使用"Always block"时创建(参见AlwaysBlock2).D-触发器是"组合逻辑块之后连接触发器"的最简单形式,其中组合逻辑部分只是一个wire类型变量.
创建一个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)
实现如下电路:
请注意,这是一个锁存器,因此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
实现如下电路:
- 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
实现如下电路:
- Module Declaration