always @(posedge clk) //LED flash and pause wait
begin
ctmp0 = ntmp0;
ctmp1 = ntmp1;
ctmp2 = ntmp2;
ctmp3 = ntmp3;
cstate = nstate;
error <= ntmp0 + ntmp1 + ntmp2 + ntmp3;
if (ctmp0 ==0 && ctmp1 == 0 && ctmp2 == 0 && ctmp3 == 0)
begin
flag = 0; //combination is right
end
else
begin
flag = 1; //lock
end
if (error > 30)
begin
flag = 2; //pause
if (paustime > 50000000)
begin
paustime = 0;
flag = 1;
end
paustime = paustime+1;
end
end
always @(posedge clk)
begin
if (flag == 2'd0 && switch == 1)
begin
if (ledtime > 10000000)
begin
ledtime = 0;
leds = ~leds;
end
ledtime = ledtime+1;
end
else
leds = 0;
end
always @(posedge clk) //composition of combination
begin
case (button)
4'b0001:
begin
temp_comb = ntmp0;
ntmp0 <= #1 combination(temp_comb);
end
4'b0010:
begin
temp_comb = ntmp1;
ntmp1 <= #1 combination(temp_comb);
end
4'b0100:
begin
temp_comb = ntmp2;
ntmp2 <= #1 combination(temp_comb);
end
4'b1000:
begin
temp_comb = ntmp3;
ntmp3 <= #1 combination(temp_comb);
end
4'b1001:
begin
ntmp0 <= 0;
ntmp1 <= 0;
ntmp2 <= 0;
ntmp3 <= 0;
end
endcase
end
always @(posedge clk) //change of state
begin
case (flag)
2'd0: nstate <= UNLC;
2'd1: nstate <= LOCK;
2'd2: nstate <= PAUS;
endcase
end
//display the result on the 7-segment LEDs dynamically
always @(posedge clk)
begin
count <= count+1;
if (count<=5000)
begin
anodes <= 4'b0111; //an0, k14, most right segment
end
else if (count<=10000)
begin
anodes <= 4'b1011; //an1, m13
end
else if (count<=15000)
begin
anodes <= 4'b1101; //an2. j12
end
else if (count<=20000)
begin
anodes <= 4'b1110; //an2. j12
end
else
count<=0;
end
always @(posedge clk) //displacement of the 7_segment LEDS
begin
if (switch == 0) //show the combinations
begin
cathodes = show_combination(digit);
case(anodes)
4'b0111: digit = ctmp0;
4'b1011: digit = ctmp1;
4'b1101: digit = ctmp2;
4'b1110: digit = ctmp3;
endcase
end
else //show the clock states
begin
case(anodes)
4'b0111:case (cstate) //most right anode
LOCK: cathodes=8'hc6;//show nothing
UNLC: cathodes=8'hc6;//C
PAUS: cathodes=8'h92;//S
endcase
4'b1011:case (cstate)
LOCK: cathodes=8'hc0;//C
UNLC: cathodes=8'hc7;//L
PAUS: cathodes=8'hc1;//U
endcase
4'b1101:case (cstate)
LOCK: cathodes=8'hc7;//O
UNLC: cathodes=8'hab;//n
PAUS: cathodes=8'h88;//A
endcase
4'b1110:case (cstate)
LOCK: cathodes=8'hff;//L
UNLC: cathodes=8'hc1;//U
PAUS: cathodes=8'h8c;//P
endcase
endcase
end
end
function [3:0] combination;
input [3:0] temp_comb;
begin
case (temp_comb)
4'd0: combination=4'd1;
4'd1: combination=4'd2;
4'd2: combination=4'd3;
4'd3: combination=4'd4;
4'd4: combination=4'd5;
4'd5: combination=4'd6;
4'd6: combination=4'd7;
4'd7: combination=4'd8;
4'd8: combination=4'd9;
4'd9: combination=4'd0;
default: combination=8'hcf;
endcase
end
endfunction
function [7:0] show_combination;
input [3:0] digit;
begin
case (digit)
4'd0: show_combination=8'hc0;
4'd1: show_combination=8'hcf;
4'd2: show_combination=8'ha4;
4'd3: show_combination=8'hb0;
4'd4: show_combination=8'h99;
4'd5: show_combination=8'h92;
4'd6: show_combination=8'h82;
4'd7: show_combination=8'hf8;
4'd8: show_combination=8'h80;
4'd9: show_combination=8'h90;
default: show_combination=8'hcf;
endcase
end
endfunction