Advertisement
markkurvers

Untitled

Jun 30th, 2021
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2.  
  3. /* 3-bit shift register */
  4. module character_recognition (
  5.         // Clock (positive edge polarity)
  6.         input clk,
  7.        
  8.         // Reset (active-high synchronous)
  9.         input reset,
  10.        
  11.         // Update (rising edge synchronous)
  12.         input update,
  13.        
  14.         // Input
  15.         input [2:0] character,
  16.        
  17.         // Outputs
  18.         output finished,
  19.         output valid_sequence
  20.     );    
  21.    
  22.     localparam  start = 3'b000,
  23.                 invalid = 3'b001,
  24.                 square_open = 3'b010,
  25.                 round_open = 3'b011,
  26.                 round_closed = 3'b100,
  27.                 valid = 3'b101;
  28.                
  29.    
  30.     reg [2:0] cur_state;
  31.     reg [2:0] next_state;
  32.     reg prev_update;
  33.    
  34.     always @(posedge clk)
  35.     begin
  36.     cur_state <= next_state;
  37.     prev_update <= update;
  38.     end
  39.    
  40.     always @(*)
  41.     begin
  42.         if(reset) next_state = start;
  43.         else if(update&&prev_update!=update)
  44.         begin
  45.             case(cur_state)
  46.                 start: next_state=(character == 3'b001)?square_open:invalid;
  47.                 square_open: next_state=(character == 3'b010)?round_open:invalid;
  48.                 round_open:begin
  49.                             if(character == 3'b000) next_state = round_open;
  50.                             else if(character == 3'b011) next_state = round_closed;
  51.                             else next_state = invalid;
  52.                         end
  53.                 round_closed: next_state=(character == 3'b100)?valid:invalid;
  54.                 valid: next_state =valid;
  55.                
  56.                 default: next_state = start;
  57.             endcase
  58.         end
  59.     end
  60.    
  61.     assign finished = (cur_state == valid || cur_state == invalid);
  62.     assign valid_sequence = (cur_state == valid);
  63. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement