Anda di halaman 1dari 4

module passwordlock(KEY, SW, HEX0, HEX1, HEX2, HEX3, LEDG, LEDR, on,off);

input [3:0]KEY; //4 KEYS is used


input [9:0]SW; //9 SWITCHES is used
output reg [0:6]HEX0; //4 7segment is used
output reg [0:6]HEX1;
output reg [0:6]HEX2;
output reg [0:6]HEX3;
output reg [2:7]LEDR;
output reg [0:6]LEDG;
output on;
output off;

reg [2:0]Q1; //3 variables of Q which goes to 7segment


reg [2:0]Q2;
reg [2:0]Q3;
reg [2:0]store1; //3 variables of store which used to saved password
reg [2:0]store2;
reg [2:0]store3;
reg on;
reg off;

initial
begin
HEX3 = 7'b1111111;
on = 1;
off=1;

end //first 7 segment


always@(SW[0], SW[1], SW[2], SW[9])
begin
if((SW[2]==0)&&(SW[1]==0))
begin
if(SW[0])
Q1 <= 3'b001; //BCD is 001=DECIMAL 1
else
Q1 <= 3'b000; //BCD IS 000=DECIMAL 0
end

else if((SW[2]==0)&&(SW[1]))
begin
if(SW[0])
Q1 <= 3'b011; //BCD is 011=DECIMAL 3
else
Q1 <= 3'b010; //BCD is 010=DECIMAL 2
end

else if((SW[2])&&(SW[1]==0))
begin
if(SW[0])
Q1 <= 3'b101; //BCD is 101=DECIMAL 5
else
Q1 <= 3'b100; //BCD is 100=DECIMAL 4
end

else if(SW[2]&&SW[1])
begin
if(SW[0])
Q1 <= 3'b111; //BCD is 111=DECIMAL 7
else
Q1 <= 3'b110; //BCD is 110=DECIMAL 6
end

case(Q1)
3'b000: HEX0 = 7'b0000001;
3'b001: HEX0 = 7'b1001111;
3'b010: HEX0 = 7'b0010010;
3'b011: HEX0 = 7'b0000110;
3'b100: HEX0 = 7'b1001100;
3'b101: HEX0 = 7'b0100100;
3'b110: HEX0 = 7'b0100000;
3'b111: HEX0 = 7'b0001111;
endcase

//retrieve password
if(SW[9])
begin
if((KEY[2]==0)&&(KEY[1]==0))
Q1 <= store1;
end
end //second 7 segment
always@(SW[3], SW[4], SW[5], SW[9])
begin
if((SW[5]==0)&&(SW[4]==0))
begin
if(SW[3])
Q2 <= 3'b001;
else
Q2 <= 3'b000;
end
else if((SW[5]==0)&&(SW[4]))
begin
if(SW[3])
Q2 <= 3'b011;
else
Q2 <= 3'b010;
end

else if((SW[5])&&(SW[4]==0))
begin
if(SW[3])
Q2 <= 3'b101;
else
Q2 <= 3'b100;
end

else if(SW[5]&&SW[4])
begin
if(SW[3])
Q2 <= 3'b111;
else
Q2 <= 3'b110;
end

case(Q2)
3'b000: HEX1 = 7'b0000001;
3'b001: HEX1 = 7'b1001111;
3'b010: HEX1 = 7'b0010010;
3'b011: HEX1 = 7'b0000110;
3'b100: HEX1 = 7'b1001100;
3'b101: HEX1 = 7'b0100100;
3'b110: HEX1 = 7'b0100000;
3'b111: HEX1 = 7'b0001111;
endcase

//retrieve password
if(SW[9])
begin
if((KEY[2]==0)&&(KEY[1]==0))
Q2 <= store2;
end
end

//Third 7 segment
always@(SW[6], SW[7], SW[8], SW[9])
begin
if((SW[8]==0)&&(SW[7]==0))
begin
if(SW[6])
Q3 <= 3'b001;
else
Q3 <= 3'b000;
end
else if((SW[8]==0)&&(SW[7]))
begin
if(SW[6])
Q3 <= 3'b011;
else
Q3 <= 3'b010;
end

else if((SW[8])&&(SW[7]==0))
begin
if(SW[6])
Q3 <= 3'b101;
else
Q3 <= 3'b100;
end

else if(SW[8]&&SW[7])
begin
if(SW[6])
Q3 <= 3'b111;
else
Q3 <= 4'b110;
end

case(Q3)
3'b000: HEX2 = 7'b0000001;
3'b001: HEX2 = 7'b1001111;
3'b010: HEX2 = 7'b0010010;
3'b011: HEX2 = 7'b0000110;
3'b100: HEX2 = 7'b1001100;
3'b101: HEX2 = 7'b0100100;
3'b110: HEX2 = 7'b0100000;
3'b111: HEX2 = 7'b0001111;
endcase
//retrieve password
if(SW[9])
begin
if((KEY[2]==0)&&(KEY[1]==0))
Q3 <= store3;
end
end always@(*)
begin //save password
if(KEY[3]==0) //press the KEY[3] to saved the password
begin
store1 = Q1;
store2 = Q2;
store3 = Q3;

LEDG[0] =1; //LEDG[0] turn on and forth 7segment display S


HEX3 = 7'b0100100; //to indicate the password is saved
end

else if((SW[9]==1)&&(KEY[0]==0)) //all the led and the forth 7 segment are turned
begin //off by turn on switch[9] and pressing KEY[0]
LEDG[0:6] = 6'b000000;
LEDR[2:7] = 6'b000000;
HEX3 = 7'b1111111;
end

else if (KEY[0]==0) //testing the password by pressing KEY[0]


begin
if((Q1==store1)&&(Q2==store2)&&(Q3==store3))//the new entered password is match
begin //with the saved password
LEDG[1:6] = 6'b111111; //6 green led light up
LEDR[2:7] = 6'b000000;
HEX3 = 7'b0110001; //third 7segment show C = Correct
on=1;
off=0;

end

else //the new entered password is not match


begin //with the saved password
LEDR[2:7] = 6'b111111; //6 red led light up
LEDG[1:6] = 6'b000000;
HEX3 = 7'b0111000; //third 7segment show F = False
on=0;
off=1;

end

end
end
endmodule

Anda mungkin juga menyukai