|
这是书上的原程序:<br>
module seqdet(x,z,clk,rst);<br>
input x,clk,rst;<br>
output z;<br>
<br>
reg[2:0] state; //reg state<br>
wire z;<br>
<br>
parameter idle=3'd0,<br>
a=3'd1,<br>
b=3'd2,<br>
c=3'd3,<br>
d=3'd4,<br>
e=3'd5,<br>
f=3'd6,<br>
g=3'd7;<br>
assign z=(state==d&&x==0)?1:0;<br>
<br>
always@(posedge clk or negedge rst)<br>
if(!rst)<br>
begin<br>
state<=idle;<br>
end<br>
else<br>
casex(state)<br>
idle:if(x==1)<br>
state<=a;<br>
else state<=idle;<br>
a: if(x==0)<br>
state<=b;<br>
else state<=a;<br>
b: if(x==0)<br>
state<=c;<br>
else state<=f;<br>
c: if(x==1)<br>
state<=d;<br>
else state<=g;<br>
d: if(x==0)<br>
state<=e;<br>
else state<=a;<br>
e: if(x==0)<br>
state<=c;<br>
else state<=a;<br>
f: if(x==1)<br>
state<=a;<br>
else state<=b;<br>
g: if(x==1)<br>
state<=f;<br>
else state<=g;<br>
default: state<=idle;<br>
endcase<br>
endmodule<br>
testbench:<br>
`timescale 1ns/1ns<br>
module t;<br>
reg clk,rst;<br>
reg[23:0] data;<br>
wire z,x;<br>
assign x=data[23];<br>
<br>
initial<br>
begin<br>
clk=0;<br>
rst=1;<br>
#2 rst=0;<br>
#30 rst=1;<br>
data=20'b1100_1001_0000_1001_0100;<br>
end<br>
always #10 clk=~clk;<br>
always@(posedge clk)<br>
data={data[22:0],data[23]};<br>
seqdet m(.x(x),.z(z),.clk(clk),.rst(rst));<br>
endmodule |
|