对于一个时序电路,可以把它分为一部分组合逻辑和一部分存储逻辑
module dff(clk,clr,rst,d,q);
input clk,clr,rst,d;
output q;
reg q;
always @(posedge clk or posedge clr)
if(clr==1'b1) q<=1'b0;
else if(rst==1'b1) q<=1'b1;
else q<=d;
endmodule
module dff_tb;
reg clk,clr,rst,d;
wire q;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
clr=1'b0;
rst=1'b0;clr=1'b0;d=1'b0;
#10 rst=1'b1;clr=1'b0;d=1'b0;
#10 clr=1'b1;rst=1'b1;d=1'b1;
#10 clr=1'b0;rst=1'b0;d=1'b1;
#20 d=1'b0;
#20 d=1'b1;
end
dff u1(clk,clr,rst,d,q);
endmodule
8位D触发器
module eight_register(d,clk,q);
input [7:0] d;
input clk;
output [7:0] q;
reg [7:0] q;
always @(posedge clk)
q<=d;
endmodule
module dff8_tb;
reg [7:0] d;
reg clk;
wire [7:0] q;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
d=8'b00000000;
#10 d=8'b00000011;
#10 d=8'b00000000;
#10 d=8'b00000111;
#20 d=8'b00001111;
#20 d=8'b00011111;
#20 d=8'b00111111;
end
dff8 u1(.d(d),.clk(clk),.q(q));
endmodule
module jk_trigger(clk,j,k,q,qb);
input clk,j,k;
output q,qb;
reg q;
always@(posedge clk)
begin
case({j,k})
2'b00:q<=q;
2'b01:q<=1'b0;
2'b10:q<=1'b1;
2'b11:q<=~q;
default: q<=q;
endcase
end
assign qb=~q;
endmodule
module jk_trigger_tb;
reg clk,j,k;
wire q,qb;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;j=1'b0;k=1'b0;
#10 j=1'b0;k=1'b0;
#20 j=1'b0;k=1'b1;
#20 j=1'b1;k=1'b0;
#20 j=1'b1;k=1'b1;
#20 j=1'b1;k=1'b0;
end
jk_trigger u1(clk,j,k,q,qb);
endmodule
module t_trigger(clk,rst,T,dout);
input clk,rst,T;
output dout;
reg dout;
always @(posedge clk or posedgerst)
if(rst==1) dout<=1'b0;
else if(T==1) dout<=~dout;
endmodule
module t_trigger_tb;
reg clk,rst,T;
wire dout;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
rst=1'b0;T=1'b0;
#10 rst=1'b1;T=1'b1;
#10 rst=1'b0;T=1'b0;
#20 T=1'b1;
#20 T=1'b0;
#20 T=1'b1;
end
t_trigger u1(clk,rst,T,dout);
endmodule
module register_right(clk,din,dout);
input clk;
input din;
output [15:0] dout;
reg [15:0] dout;
always @(posedge clk)
dout<={din,dinout[15:1]};
endmodule
module register_right_tb;
reg clk;
reg din;
wire [15:0] dout;
always
begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
din=1'b0;
#10 din=1'b1;
#20 din=1'b0;
#20 din=1'b1;
#100;
end
register_right u1(clk,din,dout);
endmodule
module register_left(clk,din,dout);
input clk;
input [15:0] din;
output [15:0] dout;
reg [15:0] dout;
always @(posedge clk)
dout<={din[14:0],din[15]};
endmodule
module register_left_tb;
reg clk;
reg [15:0] din;
wire [15:0] dout;
always
begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
din=16'b0000000000000000;
#10 din=16'b0000000000000001;
#20 din=16'b0000000000000011;
#20 din=16'b0000000000000101;
end
register_left u1(clk,din,dout);
endmodule
module count12(clk,rst_n,co,dout);
input clk,rst;
output co;
output [3:0] dout;
reg [3:0] dout;
always @ (posedge clk)
begin
if(rst_n==1'b0) dout=4'b0000;
else if(dout==4'b1011) dout<=4'b0000;
else dout<=dout+1'b1;
end
assign co=dout[3]&&dout[1]&&dout[0];
endmodule
module count12_tb;
reg clk,rst_n;
wire oc;
wire [3:0] dout;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
rst_n=1'b0;
#20 rst_n=1'b1;
#100;
end
count12 u1(clk,rst_n,oc,dout);
endmodule
module count10(clk,rst,load,din,dout);
input clk,rst,load;
input [3:0] din;
output [3:0] dout;
reg [3:0] dout;
always @ (posedge clk or posedge rst)
begin
if(rst==1'b1) dout=4'b0000;
else if(load==1'b 1) dout=4'b1001;
else if(dout==4'b0000) dout<=4'b1001;
else dout<=dout-1'b1;
end
endmodule
module count10_tb;
reg clk,rst,load;
reg [3:0] din;
wire [3:0] dout;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
rst=1'b0;load=1'b0;
#10 rst=1'b1;
#10 rst=1'b0;
#50 load=1'b1;
#10 load=1'b0;
#100;
end
count10 u1(clk,rst,load,din,dout);
endmodule
(3)计数器级联
module count10(clk,rst_n,en,dout,co);
input clk,rst_n,en;
output[3:0] dout;
output co;
reg [3:0] dout;
always @ (posedge clk or negedge rst_n)
begin
if(rst_n ==1'b 0) dout<=4'b0000;
else if(en==1'b1)
if(dout==4'b1001) dout<=4'b0000;
else dout<=dout+1'b1;
else dout<=dout;
end
assign co=dout[0]&dout[3];
endmodule
module count6(clk,rst_n,en,dout,co);
input clk,rst_n,en;
output[3:0] dout;
output co;
reg [3:0] dout;
always @ (posedge clk or negedge rst_n)
begin
if(rst_n ==1'b 0) dout<=4'b0000;
else if(en==1'b1)
if(dout==4'b0101) dout<=4'b0000;
else dout<=dout+1'b1;
else dout<=dout;
end
assign co=dout[0]&dout[2];
endmodule
module count60(clk,rst_n,en,dout,co);
input clk,rst,en;
output[7:0] dout;
output co;
wire c010,co6;
wire[3:0] dout10,dout6;
counter_10 u1(.clk(clk),.rst_n(rst_n),.en(en),.dout(dout10),.co(co10));
counter_6 u2(.clk(clk),.rst_n(rst_n),.en(co10),.dout(dout6),.co(co6));
and u3(co,co10,co6);
assign dout={dout6,dout10};
endmodule
module count60_tb ;
reg clk,rst_n,en ;
wire [7:0] dout ;
wire co ;
always
begin
#1 clk=1'b1 ;
#1 clk=1'b0 ;
end
initial
begin
clk=1'b0 ;
rst_n=1'b1 ;
en=1'b1;
#1 rst_n=1'b0 ;
#1 rst_n=1'b1 ;
end
count60 U1(.clk(clk),.rst_n(rst_n),.en(en),.dout(dout),.co(co));
endmodule
扭环计数器
module twisted_counter(clk,rst_n,dout);
input clk,rst_n;
output[3:0] dout;
reg [3:0] dout;
always @(posedge clk or negedge rst_n)
if(!rst_n) dout<=4'b0000;
else begin
dout<={dout[2:0],~dout[3] };
endmodule
module twisted_counter_tb;
reg clk,rst_n;
wire [3:0] dout;
always begin
#10 clk=1'b1;
#10 clk=1'b0;
end
initial begin
clk=1'b0;
rst_n=1'b1;
#10 rst_n=1'b0;
#10 rst_n=1'b1;
#100;
end
twisted_counter u1(clk,rst_n,dout);
endmodule
(1)最大循环长度序列码,M=2n。
(2)最长线形序列码(m序列码),M=2n-1。
(3)任意循环长度序列码,M<2n。
module sequence_signal_fsm(clk,rst_n,dout);
input clk,rst_n;
output dout;
reg dout;
reg [2:0] pre_state,next_state;
parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100,s5=3'b101;
always @(posedge clk or negedge rst_n)
if(rst_n ==1'b0)
pre_state<=s0;
else
pre_state<=next_state;
always @(pre_state)
case(pre_state)
s0:
begin
dout<=1'b0;
next_state<=s1;
end
s1:
begin
dout<=1'b0;
next_state<=s2;
end
s2:
begin
dout<=1'b1;
next_state<=s3;
end
s3:
begin
dout<=1'b0;
next_state<=s4;
end
s4:
begin
dout<=1'b1;
next_state<=s5;
end
s5:
begin
dout<=1'b1;
next_state<=s0;
end
default next_state<=s0;
endcase
endmodule
module sequence_signal_fsm_tb;
reg clk,rst_n;
wire dout;
sequence_signal_fsm u1(clk,rst_n,dout);
always #10 clk=~clk;
initial
begin
clk=1'b0;
rst_n=1'b1;
#5 rst_n=1'b0;
#5 rst_n=1'b1;
end
endmodule
module signal_generator_shifter_reg(clk,rst,din,dout);
input clk,rst;
input [5:0] din;
output dout;
reg dout;
reg [5:0] temp;
always @(posedge clk)
begin
if(rst==1'b1) temp<=din;
else begin
dout<=temp[5];
temp<={temp[4:0],temp[5]};
end
end
endmodule
module signal_generator_shifter_reg_tb;
reg clk,rst;
reg [5:0] din;
wire dout;
signal_generator_shifter_reg u1(.clk(clk),.rst(rst),.din(din),.dout(dout));
always #10 clk=~clk;
initial
begin
clk=1'b0;
rst=1'b0;
#10 rst=1'b1;din=6'b001011;
#20 rst=1'b0;
end
endmodule
module counter_sequence(clk,rst,dout);
input clk,rst;
output dout;
reg [2:0] counter;
always @(posedge clk)
if(rst==1'b1)
counter<=3'b000;
else if(counter==3'b101)
counter<=3'b000;
else
counter<=counter+1'b1;
assign dout=((~counter[0])&counter[1])|counter[2];
endmodule
module counter_sequence_tb;
reg clk,rst;
wire dout;
counter_sequence u1(.clk(clk),.rst(rst),.dout(dout));
always #10 clk=~clk;
initial
begin
clk=1'b0;
rst=1'b0;
#10 rst=1'b1;
#20 rst=1'b0;
end
endmodule
(1)根据给定的序列信号的循环长度M,确定寄存器位数n,2n-1
(2)确定移位寄存器的M个独立状态。
将给定的序列码按照移位规律每n位一组,划分为M个状态。若M个状态中出现重复现象,则应增加移位寄存器位数。用n+1位再重复上述过程,直到划分为M个独立状态为止。
(3)根据M个不同的状态列出移位寄存器的态序表和反馈函数表,求出反馈函数F的表达式。
(4)检查自启动性能。
设计一个移位寄存器加组合逻辑电路类型的010011序列信号发生器。
对于“010011”序列的信号发生器
module signal_shifter_feedback(clk,load,dout,D);
input clk,load;
input [2:0]D;
output dout;
reg [2:0] Q;
wire F;
always @(posedge clk)
if(load==1'b1) Q<=D;
else Q<={Q[1:0],F};
assign F=((~Q[0])&(~Q[2]))|((~Q[1])&Q[2]);
assign dout=Q[2];
endmodule
module signal_shifter_feedback_tb;
reg clk,load;
reg[2:0] D;
wire dout;
signal_shifter_feedback u1(.clk(clk),.load(load),.dout(dout),.D(D));
always #10 clk=~clk;
initial
begin
clk=1'b0;
D=3'b001;
load=1'b0;
#10 load=1'b1;
#20 load=1'b0;
end
endmodule
设计一个m序列信号产生器的Verilog HDL描述,该程序中n=5
module m_sequence(clk,en,dout,D);
input clk,en;
input[4:0] D;
output dout;
reg[4:0] Q;
wire F;
always @ (posedge clk)
if(en==1'b0) Q<=D;
else Q<={Q[3:0],F};
assign F=(Q[0]^Q[2])|((~Q[4])&(~Q[3])&(~Q[2])&(~Q[1])&(~Q[0]));
assign dout=Q[4];
endmodule
module m_sequence_tb;
reg clk,en;
reg[4:0] D;
wire dout;
m_sequence u1(.clk(clk),.en(en),.dout(dout),.D(D));
always #10 clk=~clk;
initial
begin
clk=1'b0;
D=5'b10000;
en=1'b0;
#20 en=1'b1;
end
endmodule
用两段式描述的5进制同步加法计数器
module couter5_fsm(clk,rst_n,Z);
input clk,rst_n;
utput Z;
reg Z;
reg[2:0] pre_state,next_state;
parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;
//第一个过程,同步时序always模块,描述状态转移方程
always @(posedge clk or negedge rst_n)
if(!rst_n)
pre_state<=s0;
else
pre_state<=next_state;
//第二个过程,组合逻辑always模块,描述激励方程和输出方程
always @(pre_state)
begin
next_state=3'bxxx;
Z=1'b0;
case (pre_state)
s0:
begin
next_state=s1;
Z=1'b0;
end
s1:
begin
next_state=s2;
Z=1'b0;
end
s2:
begin
next_state=s3;
Z=1'b0;
end
s3:
begin
next_state=s4;
Z=1'b0;
end
s4:
begin
next_state=s0;
Z=1'b1;
end
default:
begin
next_state=s0;
end
endcase
end
endmodule
用三段式描述5进制加法计数器
module couter5_fsm(clk,rst_n,Z);
input clk,rst_n;
output Z;
reg Z;
reg[2:0] pre_state,next_state;
parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;
//第一个过程,同步时序always模块,描述状态转移方程
always @(posedge clk or negedge rst_n)
if(!rst_n)
pre_state<=s0;
else
pre_state<=next_state;
//第二个过程,组合逻辑always模块,描述激励方程
always @(pre_state)
begin
next_state=3'bxxx;
case (pre_state)
s0:next_state=s1;
s1:next_state=s2;
s2:next_state=s3;
s3:next_state=s4;
s4:next_state=s0;
default:next_state=s0;
endcase
end
//第三个过程,同步时序always模块,格式化描述输出方程
always @(pre_state)
begin
Z=1'b0;
case (pre_state)
s0: Z=1'b0;
s1: Z=1'b0;
s2: Z=1'b0;
s3: Z=1'b0;
s4: Z=1'b1;
default: Z=1'b0;
endcase
end
endmodule
module couter5_fsm_tb;
reg clk,rst_n;
wire Z;
couter5_fsm u1(clk,rst_n,Z);
always #10 clk=~clk;
initial
begin
clk=1'b0;
rst_n=1'b1;
#10 rst_n=1'b0;
#10 rst_n=1'b1;
end
endmodule
module detector_Mealy(clk,rst_n,din,dout);
input clk,rst_n,din;
output dout;
reg dout;
reg [1:0] pr_state,next_state;
parameter s0=2'b00,s1=2'b01,s2=2'b10,s3=2'b11;
always @(posedge clk or negedge rst_n)
begin
if(rst_n ==1'b0)
pr_state<=s0;
else
pr_state<=next_state;
end
always@(pr_state or din)
begin
next_state<=2'bxx;
dout<=1'b0;
case(pr_state)
s0: //the state is 1;
begin
if(din==1'b1)
begin
next_state<=s0;
dout<=1'b0;
end
else
begin
next_state<=s1;
dout<=1'b0;
end
end
s1://the state is 0;
begin
if(din==1'b1)
begin
next_state<=s2;
dout<=1'b0;
end
else
next_state<=s1;
end
s2: //the state is 01;
begin
if(din==1'b1)
begin
next_state<=s0;
dout<=1'b0;
end
else
next_state<=s3;
end
s3: //the state is 010;
begin
if(din==1'b1)
begin
next_state<=s2;
dout<=1'b1;
end
else
next_state<=s1;
end
endcase
end
endmodule
module detector_Mealy_tb;
reg clk,rst_n,din;
wire dout;
detector_Mealy u1(.clk(clk),.rst_n(rst_n),.din(din),.dout(dout));
always #50 clk=~clk;
initial
begin
clk=1'b1;
rst_n=1'b0;
#100 rst_n=1'b1; din=1'b0;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b0;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
end
endmodule
module detector_Moore(clk,rst_n,din,dout);
input clk,rst_n,din;
output dout;
reg dout;
reg [2:0] pr_state,next_state;
parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;
always @(posedge clk or negedge rst_n)
begin
if(rst_n ==1'b0)
pr_state<=s0;
else
pr_state<=next_state;
end
always@(pr_state or din)
begin
next_state<=2'bxx;
dout<=1'b0;
case(pr_state)
s0://the state is 1;
begin
dout<=1'b0;
if(din==1'b1)
next_state<=s0;
else
next_state<=s1;
end
s1://the state is 0;
begin
dout<=1'b0;
if(din==1'b1)
next_state<=s2;
else
next_state<=s1;
s2: //the state is 01;
begin
dout<=1'b0;
if(din==1'b1)
next_state<=s0;
else
next_state<=s3;
end
s3: //the state is 010;
begin
dout<=1'b0;
if(din==1'b1)
next_state<=s4;
else
next_state<=s1;
end
s4: //the state is 0101;
begin
dout<=1'b1;
if(din==1'b1)
next_state<=s0;
else
next_state<=s3;
end
endcase
end
endmodule
module detector_Moore_tb;
reg clk,rst_n,din;
wire dout;
detector_Moore u1(.clk(clk),.rst_n(rst_n),.din(din),.dout(dout));
always #50 clk=~clk;
initial
begin
clk=1'b1;
rst_n=1'b0;
#100 rst_n=1'b1; din=1'b0;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b0;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
#100 din=1'b1;
#100 din=1'b0;
end
endmodule
计数器控制的状态机。该程序为计数器控制的状态机模型,0—16数值分别对应了不同的格雷码和约翰逊码,当计数器进行计数时,计数到每一个不同状态,相应地输出该状态所对应的格雷码和约翰逊码。
module counter_fsm(clk,rst,dout_gray,dout_johnson);
input clk,rst;
output [3:0] dout_gray;
output [7:0] dout_johnson;
reg [3:0] dout_gray;
reg [7:0] dout_johnson;
reg [3:0] counter;
always @(posedge clk)
if(rst==1'b1)
counter=4'b0000;
else
counter=counter+4'b0001;
always @(counter)
case(counter)
4'b0000:
begin
dout_gray=4'b0000;
dout_johnson=8'b0000_0000;
end
4'b0001:
begin
dout_gray=4'b0001;
dout_johnson=8'b0000_0001;
end
4'b0010:
begin
dout_gray=4'b0011;
dout_johnson=8'b0000_0011;
end
4'b0011:
begin
dout_gray=4'b0010;
dout_johnson=8'b0000_0111;
end
4'b0100:
begin
dout_gray=4'b0110;
dout_johnson=8'b0000_1111;
end
4'b0101:
begin
dout_gray=4'b0111;
dout_johnson=8'b0001_1111;
end
4'b0110:
begin
dout_gray=4'b0101;
dout_johnson=8'b0011_1111;
end
4'b0111:
begin
dout_gray=4'b0100;
dout_johnson=8'b0111_1111;
end
4'b1000:
begin
dout_gray=4'b1100;
dout_johnson=8'b1111_1111;
end
4'b1001:
begin
dout_gray=4'b1101;
dout_johnson=8'b1111_1110;
end
4'b1010:
begin
dout_gray=4'b1111;
dout_johnson=8'b1111_1100;
end
4'b1011:
begin
dout_gray=4'b1110;
dout_johnson=8'b1111_1000;
end
4'b1100:
begin
dout_gray=4'b1010;
dout_johnson=8'b1111_0000;
end
4'b1101:
begin
dout_gray=4'b1011;
dout_johnson=8'b1110_0000;
end
4'b1110:
begin
dout_gray=4'b1001;
dout_johnson=8'b1100_0000;
end
4'b1111:
begin
dout_gray=4'b1000;
dout_johnson=8'b1000_0000;
end
endcase
endmodule
module counter_fsm_tb;
reg clk,rst;
wire [3:0] dout_gray;
wire [7:0] dout_johnson;
counter_fsm u1(.clk(clk),.rst(rst),.dout_gray(dout_gray),.dout_johnson(dout_johnson));
always #10 clk=~clk;
initial
begin
clk=1'b0;
rst=1'b0;
#10 rst=1'b1;
#20 rst=1'b0;
end
endmodule