用 verilog 语言设计一个数字密码锁,需要满足以下基本功能:
1. 密码为 4 位,每位密码为数字 0-5,4 位密码各不相同。
2. 具有使能输入端 en,用于控制密码锁的开启和关闭。密码锁开启,允许输入密码;密码锁关闭,禁止输入密码。
3. 具有清零输入端 clr,用于清除已输入的密码,重新进行密码输入。
4. 可输出显示密码正确与否。
进阶功能:
1. 具有密码修改功能,允许修改预设的四位密码。
2. 密码连续输错 3 次以上,密码锁输出显示锁定,不允许再进行密码输入。
module PasswordLock(
input wire [3:0] password_in,
input wire en,
input wire clr,
output reg correct_password,
output reg locked
);
reg [3:0] preset_password;
reg [3:0] input_password;
reg [2:0] wrong_attempts;
initial begin
preset_password = 4'b0110; // 预设的四位密码(示例为 0110)
input_password = 4'b0000;
wrong_attempts = 0;
correct_password = 0;
locked = 0;
end
always @(posedge en) begin
if (clr) begin
input_password <= 4'b0000;
correct_password <= 0;
end else if (input_password != preset_password) begin
input_password <= password_in;
if (input_password == preset_password) begin
correct_password <= 1;
wrong_attempts <= 0;
end else begin
correct_password <= 0;
wrong_attempts <= wrong_attempts + 1;
end
end
end
always @(posedge en) begin
if (wrong_attempts >= 3) begin
locked <= 1;
end else if (clr) begin
locked <= 0;
end
end
endmodule
鄂ICP备2023011697号-1 | Powered By 91代做