module Dff(Q,D,CLK);
input Q,CLK;
output D;
reg D;
always@(posedge CLK)
D <= Q;
endmodule
代码解释说明
testbench
module Dff_tb;
reg CLK,Q;
wire D;
Dff u1(Q,D,CLK);
always
#5 CLK = ~CLK;
initial
begin
CLK = 0;
Q = 0;
#10 Q = 1;
#10 Q = 0;
#10 Q = 1;
#10 Q = 0;
#5 Q = 1;
#5 Q = 0;
end
endmodule
波形
波形图解释:由图可知,在第一个时钟上升沿到来时,输入端Q = 0输出端D = 0,并持续保持此状态;在第二个时钟上升沿到来时,输入端Q = 1输出端D = 1,此时输出不在保持上一个状态的输入值,而改变为此上升沿状态时的输入值。实现了D触发器,与代码解释保持一致
逻辑框图
源代码
module comp2bit(Q, clk, rst);
output Q;
input clk, rst;
reg Q;
always@(posedge clk or negedge rst)
if (!rst)
Q <= 1'b0;
else
Q <= ~Q;
endmodule
实现思路: 首先确定十一进制计数器需要才有4位来进行计数,但4位可以表示16种数据,所以需要在第11个状态跳回初始状态;由于使用的方法是反馈清零,故当计数器计到第11个状态时,后一个状态直接清零跳到初始状态,即可实现反馈清零的十一进制计数器
源代码
module comp11(count, clk, rst);
output [3:0] count;
input clk, rst;
reg [3:0] count;
always@(posedge clk)
if (!rst)
count <= 4'b0000;
else
if (count == 4'b1010)
count <= 4'b0000;
else
count <= count + 1;
endmodule
实现思路: 在移位寄存器中,要求每来一个时钟脉冲,寄存器中的数据就会按顺序向左或者向右移动一位。因此,在构成移位寄存器时,必须采用主-从触发器或者边沿触发器,而不能采用电平触发器
数据输入移位寄存器的方式有串行输入和并行输入两种。下图是串行输入移位寄存器。在时钟的作用下,输入数据进入移位寄存器最左位,同时,将已存入寄存器的数据右移一位。并行输入方式是把全部输入数据同时存入寄存器。
串行输入移位寄存器逻辑框图
源代码
module yiwei(clk,en,din,dout);
input en,clk;
input [7:0] din;
output [7:0] dout;
reg [7:0] dout;
reg [7:0] temp;
always @(posedge clk)
begin
if(en == 0)
temp <= din;
else
//dout <= {temp[0], temp[7:1]};
temp <= {temp[0], temp[7:1]};
dout <= temp;
end
endmodule
module yiwei_tb;
reg [7:0] din;
reg clk;
reg en;
wire [7:0]dout;
wire [7:0] lout [7:0];
yiwei u1(clk,en,din,dout);
LED uu0(dout[0],lout[0]);
LED uu1(dout[1],lout[1]);
LED uu2(dout[2],lout[2]);
LED uu3(dout[3],lout[3]);
LED uu4(dout[4],lout[4]);
LED uu5(dout[5],lout[5]);
LED uu6(dout[6],lout[6]);
LED uu7(dout[7],lout[7]);
always #10 clk = ~clk;
initial
begin
clk = 0;
en = 1'b1;
din = 8'b11100010;
#20 en = 1'b0;
#20 en=1'b1;
end
endmodule
源代码与测试代码
波形图
moore型
moore型逻辑框图
mealy型
mealy型逻辑框图
状态设计
状态转移图
moore型源代码
module moore111(clk,clr,in,out);
input clk,clr,in;
output out;
reg out;
reg [1:0] present_state,next_state;
parameter s0 = 2'b00,s1 = 2'b01,s2 = 2'b10,s3 = 2'b11;
always @(posedge clk or posedge clr)
if(clr)
present_state <= s0;
else
present_state <= next_state;
always @(*)
begin
case(present_state)
s0: if(in) begin next_state = s1; out = 0; end
else begin next_state = s0; out = 0; end
s1: if(in) begin next_state = s2; out = 0; end
else begin next_state = s0; out = 0; end
s2: if(in) begin next_state = s3; out = 0; end
else begin next_state = s0; out = 0; end
s3: if(in) begin next_state = s3; out = 1; end
else begin next_state = s0; out = 1; end
default: begin next_state = 0; out = 0; end
endcase
end
endmodule
module mealy111(
input wire clk,
input wire clr,
input wire din,
output reg dout);
reg [1:0] present_state,next_state;
parameter s0=2'b00,s1=2'b01,s2=2'b11;
always @ (posedge clk or posedge clr)
begin
if(clr==1) present_state<=s0;
else present_state<=next_state;
end
always @(*)
begin
case(present_state)
s0:if(din==1) begin next_state=s1;dout=0;end
else begin next_state=s0;dout=0;end
s1:if(din==1) begin next_state=s2;dout=0;end
else begin next_state=s0;dout=0;end
s2:if(din==1) begin next_state=s2;dout=1;end
else begin next_state=s0;dout=0;end
default:next_state=s0;
endcase
end
endmodule
module seq111_tb;
reg clk;
reg clr;
reg din;
wire dout;
mealy111 u1(clk,clr,din,dout);
moore111 u2(clk,clr,din,dout);
always #100 clk=~clk;
initial begin
clk=0;clr=1;din=0;
#500 clr=0;
#200 din=1;
#200 din=0;
#200 din=1;
#400 din=0;
#400 din=1;
#600 din=0;
#400 din=1;
end
endmodule
源文件与测试文件
波形图
module moore1111(clk,rst,in,out);
input clk,rst,in;
output out;
reg out;
reg [2:0] present_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)
if(!rst)
present_state <= s0;
else
present_state <= next_state;
always @(*)
begin
case(present_state)
s0: if(in) begin next_state = s1;out = 0; end
else begin next_state = s0;out = 0; end
s1: if(in) begin next_state = s2;out = 0; end
else begin next_state = s0;out = 0; end
s2: if(in) begin next_state = s3;out = 0; end
else begin next_state = s0;out = 0; end
s3: if(in) begin next_state = s4;out = 0; end
else begin next_state = s0;out = 0; end
s4: if(in) begin next_state = s4;out = 1; end
else begin next_state = s0;out = 1; end
default: begin next_state = s0;out = 0; end
endcase
end
endmodule
module mealy1111(
input wire clk,
input wire rst,
input wire din,
output reg dout
);
reg [1:0] present_state,next_state;
parameter s0=2'b00,s1=2'b01,s2=2'b10,s3=2'b11;
always @ (posedge clk or negedge rst)
if (!rst)
present_state <= s0;
else
present_state <= next_state;
always @ (*)
case (present_state)
s0: if (din == 1) begin next_state = s1;dout = 0; end
else begin next_state = s0;dout = 0; end
s1: if (din == 1) begin next_state = s2;dout = 0; end
else begin next_state = s0;dout = 0; end
s2: if (din == 1) begin next_state = s3;dout = 0; end
else begin next_state = s0;dout = 0; end
s3: if (din == 1) begin next_state = s3;dout = 1; end
else begin next_state = s0;dout = 0; end
default: next_state = s0;
endcase
endmodule
module seq1111_tb;
reg in;
reg clk;
reg rst;
wire out;
mealy1111 u1(clk,rst,in,out);
moore1111 u2(clk,rst,in,out);
always #100 clk=~clk;
initial begin
clk=0;
rst=0;
in=0;
#300 rst=1;
#200 in=0;
#200 in=1;
#200 in=0;
#200 in=1;
#800 in=0;
#400 in=1;
#600 in=0;
#400 in=1;
#1000 in=0;
#200 in=0;
end
endmodule
源文件与测试文件
波形图
module moore1011(input wire clk,input wire clr,input wire din,output reg dout);
reg [2:0] present_state,next_state;
parameter s0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;
always @ (posedge clk or posedge clr)
if(clr==1) present_state<=s0;
else present_state<=next_state;
always @(*)
begin
case(present_state)
s0:if(din==1) next_state=s1;
else next_state=s0;
s1:if(din==1) next_state=s1;
else next_state=s2;
s2:if(din==1) next_state=s3;
else next_state=s0;
s3:if(din==1) next_state=s4;
else next_state=s2;
s4:if(din==1) next_state=s1;
else next_state=s2;
default:next_state=s0;
endcase
end
always @(*)
begin
if(present_state==s4) dout=1;
else dout=0;
end
endmodule
module mealy1011(
input wire clk,
input wire clr,
input wire din,
output reg dout);
reg [1:0] present_state,next_state;
parameter s0=2'b00,s1=2'b01,s2=2'b10,s3=2'b11;
always @ (posedge clk or posedge clr)
begin
if(clr==1) present_state<=s0;
else present_state<=next_state;
end
always @(*)
begin
case(present_state)
s0:if(din==1) begin next_state=s1;dout=0;end
else begin next_state=s0;dout=0;end
s1:if(din==1) begin next_state=s1;dout=0;end
else begin next_state=s2;dout=0;end
s2:if(din==1) begin next_state=s3;dout=0;end
else begin next_state=s0;dout=0;end
s3:if(din==1) begin next_state=s1;dout=1;end
else begin next_state=s2;dout=0;end
default:next_state=s0;
endcase
end
endmodule
module seq1011_tb;
reg clk;
reg clr;
reg din;
wire dout;
mealy1011 u1(clk,clr,din,dout);
moore1011 u2(clk,clr,din,dout);
always #100 clk=~clk;
initial begin
clk=0;clr=1;din=0;
#300 clr=0;
#200 din=1;
#200 din=0;
#200 din=1;
#200 din=1;
#400 din=1;
#600 din=0;
#400 din=1;
end
endmodule
源文件与测试文件
波形图