key44.v
上传用户:shenghui
上传日期:2022-08-09
资源大小:328k
文件大小:4k
源码类别:

VHDL/FPGA/Verilog

开发平台:

Others

  1. module key44(
  2.        code      ,
  3.        col       ,
  4.        valid     ,
  5.        row       ,
  6.        sys_clk       ,
  7.        rst       
  8.                 );
  9.  output  [3:0]  col     ;
  10.  output         valid   ;
  11.  output  [3:0]  code    ;
  12.  
  13.  input   [3:0]  row     ;
  14.  input          sys_clk,rst ;
  15.  
  16.  reg     [3:0]  col,code;
  17.  reg     [5:0]  state,next_state;
  18.  
  19.  parameter  S_0 = 6'b000001,
  20.             S_1 = 6'b000010,
  21.             S_2 = 6'b000100,
  22.             S_3 = 6'b001000,
  23.             S_4 = 6'b010000,
  24.             S_5 = 6'b100000;
  25.  reg       S_row ; 
  26.  reg [3:0] count,row_reg,col_reg;
  27.  reg       clk2,clk4;
  28.  
  29.  
  30.  reg [24:0] Mega_cnt;
  31.  wire       clk;
  32. /**************************************/
  33. always @(posedge sys_clk or negedge rst)
  34. begin
  35. if(!rst)
  36.   begin
  37.   Mega_cnt<=0;
  38.   end
  39. else
  40.   begin
  41.   Mega_cnt<=Mega_cnt+1;
  42.   end
  43. end
  44. assign clk = Mega_cnt[4];
  45. /**************************************/ 
  46.  
  47.  always @ (posedge clk)
  48.  clk2 <= ~clk2;
  49.  
  50.  always @ (posedge clk2)
  51.  clk4 <= ~clk4;
  52.            
  53.  always @ (posedge clk4 or negedge rst)
  54.  if(!rst)
  55.      begin
  56.          count <= 0;
  57.          S_row <= 0;
  58.      end
  59.  else
  60.      begin
  61.          if(!(row[0]&row[1]&row[2]&row[3]))
  62.              begin
  63.                  if(count < 'd4)
  64.                      count <= count +1 ;
  65.                  else
  66.                      S_row <= 1;
  67.              end
  68.          else if(state[5]||state[0])
  69.             begin
  70.                 count <= 0;
  71.                 S_row <= 0;
  72.             end
  73.      end
  74.      
  75.  assign valid = ((state == S_1)||(state == S_2)||(state == S_3)||(state == S_4)) &&  (!(row[3]&row[2]&row[1]&row[0])) ;
  76.  
  77.  always @ (negedge clk)
  78.  if(valid)
  79.      begin
  80.         row_reg <= row ;
  81.         col_reg <= col ;
  82.      end
  83.   else
  84.      begin
  85.         row_reg <= row_reg ;
  86.         col_reg <= col_reg ;
  87.      end
  88.      
  89.  always @ (row_reg or col_reg or clk)
  90.      case({row_reg,col_reg})
  91.         8'b1110_1110: code = 13;
  92.         8'b1110_1101: code = 9 ;
  93.         8'b1110_1011: code = 5 ;
  94.         8'b1110_0111: code = 1 ;
  95.         
  96.         8'b1101_1110: code = 14;
  97.         8'b1101_1101: code = 10;
  98.         8'b1101_1011: code = 6 ;
  99.         8'b1101_0111: code = 2 ;
  100.         
  101.         8'b1011_1110: code = 15 ;
  102.         8'b1011_1101: code = 11 ;
  103.         8'b1011_1011: code = 7;
  104.         8'b1011_0111: code = 3;
  105.         
  106.         8'b0111_1110: code = 0;
  107.         8'b0111_1101: code = 12;
  108.         8'b0111_1011: code = 8;
  109.         8'b0111_0111: code = 4;              
  110.     endcase
  111.  
  112.  always @ (posedge clk4 or negedge rst)
  113.  if(!rst)
  114.       state <= S_0 ;
  115.  else
  116.       state <= next_state ;
  117.     
  118.  always @ ( state or row or S_row)
  119.  begin
  120.    //  next_state = state ; 
  121.      col =0;
  122.      case(state)
  123.         S_0 :  begin
  124.                    col = 4'b0000;
  125.                    if(S_row)
  126.                        next_state = S_1;
  127.                    else
  128.                        next_state = S_0;
  129.                end
  130.         S_1 :  begin
  131.                    col = 4'b1110;
  132.                    if(row!='hf)
  133.                        next_state = S_5;
  134.                    else
  135.                        next_state = S_2;
  136.                end
  137.         S_2 :  begin
  138.                    col = 4'b1101;
  139.                    if(row!='hf)
  140.                        next_state = S_5;
  141.                    else
  142.                        next_state = S_3;
  143.                end 
  144.         S_3 :  begin
  145.                    col = 4'b1011;
  146.                    if(row!='hf)
  147.                        next_state = S_5;
  148.                    else
  149.                        next_state = S_4;
  150.                end  
  151.         S_4 :  begin
  152.                    col = 4'b0111;
  153.                    if(row!='hf)
  154.                        next_state = S_5;
  155.                    else
  156.                        next_state = S_0;
  157.                end  
  158.         S_5 :  begin
  159.                    col = 4'b0000;
  160.                    if(row == 4'b1111) 
  161.                        next_state = S_0;
  162.                    else 
  163.                        next_state = S_5;
  164.                end
  165.         default: next_state = S_0;
  166.   endcase
  167.   end
  168. endmodule