Altera_Forum
Honored Contributor
12 years agoVerilog excess Three no out puts
I have lots of trouble with my code and no one to ask why. The outputs are X(do not cares). I'm not sure why
`timescale 1ns/ 1ns
module excess_three (W,R,clk,Reset);
input W;
input clk;
input Reset;
output R;
reg R;
reg State;
reg NextState;
parameter Start = 3'b000 ,CarryOne= 3'b101, NoCarryOne= 3'b001;
parameter CarryTwo= 3'b011, NoCarryTwo= 3'b111;
parameter CarryThree= 3'b010, NoCarryThree= 3'b110;
always @ (W or State)
begin
case (State)
Start: NextState = (W==1)? CarryOne:NoCarryOne;
Start: R=(W==1)? 1'b1:1'b1;
CarryOne: NextState = (W==1)? CarryTwo:CarryTwo;
CarryOne: R=(W==1)? 1'b1:1'b0;
NoCarryOne: NextState = (W==1)? CarryTwo:NoCarryTwo;
NoCarryOne: R=(W==1)? 1'b0:1'b1;
CarryTwo: NextState = (W==1)? CarryThree:NoCarryThree;
CarryTwo: R=(W==1)? 1'b0:1'b1;
NoCarryTwo: NextState = (W==1)? NoCarryThree:NoCarryThree;
NoCarryTwo: R=(W==1)? 1'b1:1'b0;
CarryThree: NextState = (W==1)? NoCarryThree:3'bxxx;
CarryThree: R=(W==1)? 1'b1:1'b0;
NoCarryThree: NextState = (W==1)? Start:Start;
NoCarryThree: R=(W==1)? 1'b1:1'b0;
endcase
end
always @ (posedge clk or negedge Reset)
begin
if (Reset==0)
State <= Start;
else
State <=NextState;
end
endmodule
this is my test bench
`timescale 1ns/ 1ns
module excess_three_TB;
reg W;
reg clk;
reg Reset;
wire R;
excess_three test(.W(W),.R(R),.clk(clk),.Reset(Reset));
initial
begin# 0 clk=0;# 0 Reset=0;# 0 W=4'b0000;
end
always begin# 5 clk = !clk;# 5 W=4'b0001;# 5 W=4'b0010;# 5 W=4'b0011;# 5 W=4'b0100;# 5 W=4'b0101;# 5 W=4'b0110;# 5 W=4'b0111;# 5 W=4'b1000;# 5 W=4'b1001;# 5 W=4'b1010;# 5 W=4'b1011;# 5 W=4'b1100;# 5 W=4'b1101;# 5 W=4'b1110;# 5 W=4'b1111;
end
initial begin
$display("\t\ttime,\tW\tclk,\tReset,\tR");
$monitor("%d,\t%b,\t%b,\t%b,\t%d",$time,W, clk,Reset,R);
end
initial # 165 $finish;
endmodule