1.Mux and DFF_1
只写一个子模块,别想复杂了
module top_module (
input clk,
input L,
input r_in,
input q_in,
output reg Q);
always@(posedge clk)
begin
case(L)
1'b0: Q<=q_in;
1'b1: Q<=r_in;
endcase
end
endmodule
2.Mux and DFF_2
module top_module (
input clk,
input w, R, E, L,
output Q
);
wire p;
always@(posedge clk)
begin
case(E)
1'b0: p=Q; //采用阻塞赋值
1'b1: p=w;
endcase
case(L)
1'b0: Q=p;
1'b1: Q=R;
endcase
end
endmodule
3.DFFs and gates
module top_module (
input clk,
input x,
output z
);
wire q1,q2,q3;
always@(posedge clk)
begin
q1<=x^q1;
q2<=x&(~q2);
q3<=x|(~q3);
end
assign z=~(q1|q2|q3);//不受时钟影响
endmodule
4.Create circuit from truth table
module top_module (
input clk,
input j,
input k,
output Q);
always@(posedge clk)
begin
case({j,k})
2'b00: Q<=Q;
2'b01: Q<=0;
2'b10: Q<=1;
2'b11: Q<=~Q;
endcase
end
endmodule
5.Detect an edge
module top_module (
input clk,
input [7:0] in,
output [7:0] pedge
);
reg [7:0] p;
always@(posedge clk)
begin
p<=in;
pedge<=~p∈ //原来为0,跳变为1后,输出位才为1.即~0&1=1
end
endmodule
6.Detect both edges
module top_module (
input clk,
input [7:0] in,
output [7:0] anyedge
);
reg [7:0] p;
always@(posedge clk)
begin
p<=in;
anyedge<=p^in;
end
endmodule
7.Edge capture register
module top_module (
input clk,
input reset,
input [31:0] in,
output [31:0] out
);
reg[31:0] p;
always@(posedge clk)
begin
p<=in;
end
always@(posedge clk)
begin
if(reset)
out<=0;
else
begin
out<=(p&~in)|out; //捕获后,将一直保持1,只有复位才能置为0
end
end
endmodule
8.Dual-edge triggered
module top_module (
input clk,
input d,
output q
);
reg p1,p2;
always@(posedge clk)
begin
p1<=d;
end
always@(negedge clk)
begin
p2<=d;
end
assign q=clk ? p1 : p2;
endmodule