You are on page 1of 2

CME4456 Reconfigurable Computing – Lab 2

For the following state diagram, write Verilog code and test it with using a waveform.

case(CS)
s1:if(in) Y=1'b0;else Y=1'b0;
s2:if(in) Y=1'b0;else Y=1'b1;
s3:if(in) Y=1'b0;else Y=1'b1;
s4:if(in) Y=1'b0;else Y=1'b1;
default:Y=1'b0;
endcase

EXAMPLE:

module fsm(clk,reset,in,out,Y);
input clk,reset,in;
output Y,out;
reg[1:0] out,CS,NS;
reg Y;
parameter s1=2'b00, s2=2'b01,s3=2'b10,s4=2'b11;
always @(posedge clk or posedge reset)
begin:Register
if(reset)CS=s1;else CS=NS;
end
always@(in or CS)
begin:Transitions
case(CS)
s1:if(in)NS=s1;else NS=s2;
s2:if(in)NS=s1;else NS=s3;
s3:if(in)NS=s4;else NS=s3;
s4:if(in)NS=s1;else NS=s2;
default:NS=s1;
endcase

end
always@(in or CS)
begin:Output
case(CS)
s1:if(in) Y=1'b0;else Y=1'b0;
s2:if(in) Y=1'b0;else Y=1'b1;
s3:if(in) Y=1'b0;else Y=1'b1;
s4:if(in) Y=1'b0;else Y=1'b1;
default:Y=1'b0;
endcase
out= CS;
end
endmodule

You might also like