1.D flip-flop(D触发器)
module top_module (
input clk, // Clocks are used in sequential circuits
input d,
output reg q );
always @ (posedge clk) begin
q <= d;
end
endmodule
2.D flip-flops
创建8位的D触发器,时钟信号在上升沿有效。
module top_module (
input clk,
input [7:0] d,
output [7:0] q
);
always @ (posedge clk) begin
q <= d;
end
endmodule
3.DFF with reset
创建8位具有同步复位功能的D触发器,时钟信号在上升沿有效。
module top_module (
input clk,
input reset, // Synchronous reset
input [7:0] d,
output [7:0] q
);
always @ (posedge clk) begin
if(reset == 1'b1)
q <= 0;
else
q <= d;
end
endmodule
4.DFF with reset value
创建8位具有有效高同步复位功能的 D 触发器,触发器必须重置为0x34而不是零。所有 DFF 都应由 clk 的下降沿触发。
module top_module (
input clk,
input reset,
input [7:0] d,
output [7:0] q
);
always @ (negedge clk) begin
if(reset == 1'b1)
q <= 8'h0x34;
else
q <= d;
end
endmodule
5.DFF with asynchronous reset
创建异步复位功能的8位D 触发器。所有 DFF 都应由时钟的上升沿触发。
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'b1)
q <= 8'b0;
else
q <= d;
end
endmodule
6.DFF with byte enable
创建16位D触发器,部分情况下,只需要多路触发器中的一部分触发器工作,此时可以通过 ena 使能端进行控制。使能端 ena 信号有效时,触发器在时钟上升沿工作。
byteena 使能信号以 byte 为单位管理 8 路触发器在时钟边沿触发与否。byteena [1] 作为 d[15:8] 高位字节的使能端,byteena [0] 则控制 d 的低位字节。
resetn 为同步,低电平有效复位信号。
所有的触发器在时钟上升沿被触发。
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'b0)
q <= 16'b0;
else begin
q[15:8]<=(byteena[1])?d[15:8]:q[15:8];
q[7:0]<=(byteena[0])?d[7:0]:q[7:0];
end
end
endmodule
7.D Latch
module top_module (
input d,
input ena,
output q);
always @ (*) begin
if(ena)
q <= d;
else
q <= q;
end
endmodule
8.DFF
module top_module (
input clk,
input d,
input ar, // asynchronous reset 异步复位
output q);
always @ (posedge clk or posedge ar) begin
if(ar)
q <= 1'b0;
else
q <= d;
end
endmodule
9.DFF
module top_module (
input clk,
input d,
input r, // synchronous reset 同步复位
output q);
always @ (posedge clk) begin
if(r)
q <= 1'b0;
else
q <= d;
end
endmodule
10.DFF+gate
module top_module (
input clk,
input in,
output out);
wire a;
xor(a,in,out); //异或门
always @ (posedge clk) begin
out <= a;
end
endmodule
11.MUX and DFF
module top_module (
input clk,
input L,
input r_in,
input q_in,
output reg Q);
always @ (posedge clk) begin
if(L)
Q <= r_in;
else
Q <= q_in;
end
endmodule
12.MUX and DFF
module top_module (
input clk,
input w, R, E, L,
output Q
);
wire in;
assign in = E ? w : Q;
always @ (posedge clk) begin
if (L)
Q <= R;
else
Q <= in;
end
endmodule
13.DFFs and gates
module top_module (
input clk,
input x,
output z
);
wire d1,d2,d3,q1,q2,q3;
xor (d1,x,q1);
and(d2,x,~q2);
or(d3,x,~q3);
always @ (posedge clk) begin
q1 <= d1;
q2 <= d2;
q3 <= d3;
end
nor(z,q1,q2,q3);
endmodule
14.Creat circuit from truth table
module top_module (
input clk,
input j,
input k,
output Q);
always @ (posedge clk) begin
Q <= (j & ~Q) | (~k & Q);
end
endmodule
15.Detect an edge(边沿检测)
对于8位向量中的每个位,检测输入信号何时从一个时钟周期的0变为下一时钟周期的1(类似于上升沿检测)。在发生从0到1的跳变后,应将输出位设置为周期。这个题检测的是上升沿,而且是检测到之后,延迟一个周期拉高输出。这是一道经典的题目,思路就是打一拍之后(用触发器就行),把输入取反,之后再与输入相与就可以了。
module top_module (
input clk,
input [7:0] in,
output [7:0] pedge
);
reg [7:0] temp;
always @ (posedge clk) begin
temp <= in;
pedge <= ~temp & in;
end
endmodule
16.Detect both edges(边沿检测)
检测输入信号的所有跳变(0到1,1到0),需要一个寄存器state保存状态值。
module top_module (
input clk,
input [7:0] in,
output [7:0] anyedge
);
reg [7:0] state;
always @ (posedge clk) begin
state <= in;
anyedge <= state ^ in; //异或
end
endmodule
17.Edge capture register
检测下降沿,检测到后out一直为高,当reset =1时,out为0。
module top_module (
input clk,
input reset,
input [31:0] in,
output [31:0] out
);
reg [31:0] state;
always @ (posedge clk) begin
state <= in;
if(reset==1'b1)
out <= 32'b0;
else begin
out <= ~in & state | out;
end
end
endmodule
18.Dual-edge triggered flip-flop(双边沿触发)
module top_module (
input clk,
input d,
output q
);
reg state1,state2;
always @ (posedge clk) begin
state1 <= d ^ state2;
end
always @ (negedge clk) begin
state2 <= d ^ state1;
end
assign q = state1 ^ state2;
endmodule