Advertisement
JadonChan

digital_lock.sv

May 30th, 2024
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ns
  2.  
  3. typedef enum logic [2:0] {
  4.     S0,
  5.     S1,
  6.     S2,
  7.     S3,
  8.     V0,
  9.     V1,
  10.     V2,
  11.     V3
  12. } state_t;
  13.  
  14. module digital_lock(
  15.     input wire clock,
  16.     input wire reset,
  17.     input wire [3:0] code,
  18.     input wire mode,
  19.  
  20.     output wire unlock,
  21.     output wire error,
  22.     output wire [1:0] index_output,
  23.     output wire [1:0] count_output,
  24.     output wire status_output
  25.     );
  26.  
  27.     reg unlock_reg = 1'b0;
  28.     reg error_reg = 1'b0;
  29.     reg dead = 1'b0;
  30.     reg [3:0] sudo_code [3:0] = {4'b0000, 4'b0000, 4'b0000, 4'b0000};
  31.     reg [3:0] user_code [3:0] = {4'b0000, 4'b0000, 4'b0000, 4'b0000};
  32.     reg [3:0] input_reg [3:0];
  33.     state_t state;
  34.     reg [1:0] index;
  35.     reg [1:0] count = 0;
  36.  
  37.     always_ff @(posedge clock or posedge reset) begin
  38.         if (reset) begin
  39.             index <= 2'b00;
  40.             unlock_reg <= 1'b0;
  41.             if (state == V0 || state == V1 || state == V2 || state == V3) begin
  42.                 state <= V0;
  43.                 if (~dead) begin
  44.                     error_reg <= 1'b0;
  45.                 end
  46.             end
  47.             else begin
  48.                 state <= S0;
  49.             end
  50.         end
  51.         else if (mode == 0 && state <= S3) begin
  52.             state <= V1;
  53.             index <= 2'b01;
  54.             input_reg[0] <= code;
  55.         end
  56.         else if (mode == 1 && state >= V0) begin
  57.             state <= S1;
  58.             index <= 2'b01;
  59.             input_reg[0] <= code;
  60.             if (state == V0) begin
  61.                 if (~dead) begin
  62.                     unlock_reg <= 1'b0;
  63.                     error_reg <= 1'b0;
  64.                 end
  65.             end
  66.         end
  67.         else if (state >= V0) begin
  68.             input_reg[index] <= code;
  69.             index <= index + 1;
  70.             if (state == V3) begin
  71.                 state <= V0;
  72.                 if (input_reg[2:0] == sudo_code[2:0] && code == sudo_code[3]) begin
  73.                     unlock_reg <= 1'b1;
  74.                     error_reg <= 1'b0;
  75.                     dead <= 1'b0;
  76.                     count <= 0;
  77.                 end
  78.                 else if (~dead && input_reg[2:0] == user_code[2:0] && code == user_code[3]) begin
  79.                     unlock_reg <= 1'b1;
  80.                     error_reg <= 1'b0;
  81.                     count <= 0;
  82.                 end
  83.                 else begin
  84.                     unlock_reg <= 1'b0;
  85.                     error_reg <= 1'b1;
  86.                     if (~dead) begin
  87.                         count <= count + 1;
  88.                     end
  89.                     if (count == 2) begin
  90.                         dead <= 1'b1;
  91.                     end
  92.                 end
  93.             end
  94.             else if (state == V0) begin
  95.                 state <= state_t'(state + 1);
  96.                 if (~dead) begin
  97.                     unlock_reg <= 1'b0;
  98.                     error_reg <= 1'b0;
  99.                 end
  100.             end
  101.             else begin
  102.                 state <= state_t'(state + 1);
  103.             end
  104.         end
  105.         else begin
  106.             input_reg[index] <= code;
  107.             index <= index + 1;
  108.             state <= state_t'(state + 1);
  109.             if (state == S3) begin
  110.                 state <= S0;
  111.                 user_code[2:0] <= input_reg[2:0];
  112.                 user_code[3] <= code;
  113.             end
  114.         end
  115.     end
  116.  
  117.     assign unlock = unlock_reg;
  118.     assign error = error_reg;
  119.     assign index_output = index;
  120.     assign count_output = count;
  121.     assign status_output = state <= S3 ? 1'b1 : 1'b0;
  122.  
  123. endmodule
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement