Advertisement
tariq786

lcbbc_verilog_code

Oct 28th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //AUTHOR: Tariq Bashir
  2. //tariqbashir@gmail.com
  3. //for unambiguous codes (lcbbc)
  4. //released into the public under GPL
  5.  
  6. `timescale 1ns/1ps
  7.  
  8. module lcbbc
  9.   #(parameter N = 3,
  10.     parameter depth=1024,
  11.     parameter width=N,   //same as N
  12.     parameter addr_sz=$clog2(depth)
  13.     )
  14.   (input clk,
  15.    input              rst, //positive reset
  16.    input [N-1:0]      n,
  17.    input [N/2-1:0]      min_hd,
  18.    input              start,
  19.    output reg [N-1:0] codes
  20.    );
  21.  
  22.  
  23.   localparam [2:0]
  24.     IDLE = 0,
  25.     CALC_HD = 1,
  26.     CLOCK_CALC_HD = 2,
  27.     FIND_CODE = 3,
  28.     RD_MEM = 4,
  29.     OUTPUT_CODES = 5,
  30.     CLOCK_OUTPUT_CODES = 6,
  31.     DONE = 7;
  32.    
  33.  
  34.   reg [2:0]           state, nxt_state;
  35.   reg [N-1:0]         counterl, nxt_counterl, counterh, nxt_counterh;
  36.  
  37.   reg [addr_sz-1:0]   codelength, nxt_codelength;
  38.   reg [addr_sz-1:0]   count, nxt_count;
  39.  
  40.   //sequential block
  41.   always @(posedge clk)
  42.     if(rst)
  43.       begin
  44.         state         <= #1 IDLE;
  45.         counterl      <= #1 0;
  46.         codelength    <= #1 0;
  47.     count         <= #1 0;
  48.         counterh      <= #1 0;
  49.       end
  50.     else
  51.       begin
  52.         state         <= #1 nxt_state;
  53.         counterl      <= #1 nxt_counterl;
  54.         counterh      <= #1 nxt_counterh;
  55.         codelength    <= #1 nxt_codelength;
  56.     count         <= #1 nxt_count;
  57.       end
  58.  
  59.  
  60.  
  61.  
  62.   reg [N-1:0]         summation;
  63.   reg [N-1:0]         candidate;
  64.  
  65.   reg                 wr_en, rd_en;
  66.   reg [width-1:0]     d_in;
  67.   reg [addr_sz-1:0]   wr_addr;
  68.   reg [width-1:0]     d_out;
  69.   reg [addr_sz-1:0]   rd_addr, r_addr;
  70.  
  71.   reg [width-1:0]     code_array[0:depth-1];
  72.  
  73.  
  74.   //memory
  75.   always @(posedge clk)
  76.     begin
  77.       if (wr_en)
  78.         begin
  79.           code_array[wr_addr] <= #1 d_in;
  80.         end
  81.       else if (rd_en)
  82.         begin
  83.           d_out <= #1 code_array[rd_addr];
  84.         end
  85.     end // always @ (posedge clk)
  86.  
  87.  
  88.   //Finite STATE MACHINE
  89.   always@*
  90.     begin
  91.       //defaults
  92.       nxt_state         = state;
  93.       nxt_counterl      = counterl;
  94.       nxt_counterh      = counterh;
  95.       nxt_count         = count;
  96.       nxt_codelength    = codelength;
  97.      
  98.       case(state)
  99.         IDLE:
  100.           begin
  101.             if(start)
  102.               begin
  103.                 wr_addr = 0;
  104.                 wr_en = 1;
  105.                 d_in = 0;
  106.                 nxt_codelength = 1;
  107.                 nxt_state = CALC_HD;
  108.               end
  109.           end
  110.        
  111.         CALC_HD:
  112.           begin
  113.             wr_en = 0;
  114.             if( (counterh == 1 ) || (codelength == depth -1) )
  115.               nxt_state = OUTPUT_CODES;
  116.             else
  117.               begin
  118.                 if(counterl == 2**N-1)
  119.                   nxt_counterh = counterh + 1;
  120.                
  121.                 summation = sum(counterl ^ counterh); //function call
  122.         if( summation >= min_hd )
  123.           begin
  124.             //HD[counterl] = counterl;
  125.             candidate = counterl;
  126.             nxt_counterl = counterl + 1;
  127.             nxt_state = FIND_CODE;
  128.           end
  129.         else
  130.           begin
  131.             nxt_counterl = counterl + 1;
  132.                     nxt_state = CLOCK_CALC_HD;
  133.                   end
  134.           end // else: !if( (counterh == 1 ) || (codelength == depth -1) )
  135.            
  136.           end // case: CALC_HD
  137.  
  138.         CLOCK_CALC_HD:
  139.           begin
  140.             nxt_state = CALC_HD;
  141.           end
  142.        
  143.         FIND_CODE:
  144.           begin
  145.             if(count < codelength)
  146.               begin
  147.                 rd_en = 1;
  148.                 rd_addr = count;
  149.                 nxt_state = RD_MEM;
  150.               end
  151.         else if(count == codelength)
  152.           begin
  153.         wr_en = 1;
  154.         d_in = candidate;
  155.         wr_addr = codelength;
  156.         nxt_codelength = codelength + 1;
  157.         nxt_count = 0;
  158.         nxt_state = CALC_HD;
  159.           end
  160.           end  //FIND_CODE
  161.  
  162.        
  163.         RD_MEM:
  164.           begin
  165.             rd_en = 0;
  166.             summation = sum(d_out ^ candidate);
  167.             if (summation >= min_hd)
  168.               begin
  169.                 nxt_count = count + 1;
  170.         nxt_state = FIND_CODE;
  171.               end
  172.         else
  173.           begin
  174.                 nxt_count = 0;
  175.         nxt_state = CALC_HD;
  176.           end
  177.           end
  178.        
  179.         OUTPUT_CODES:
  180.           begin
  181.             if( count < codelength)
  182.               begin
  183.         rd_en = 1;
  184.         rd_addr = count;
  185.         nxt_count = count + 1;
  186.         nxt_state = CLOCK_OUTPUT_CODES;
  187.           end
  188.             else
  189.               nxt_state = DONE;
  190.           end // case: OUTPUT_CODE
  191.        
  192.        
  193.         CLOCK_OUTPUT_CODES:
  194.           begin
  195.         rd_en = 0;
  196.         codes = d_out;
  197.             nxt_state = OUTPUT_CODES;
  198.           end
  199.  
  200.         DONE:
  201.           begin
  202.             $display($time,": Done \n");
  203.             $finish;
  204.           end
  205.  
  206.       endcase // case (state)
  207.      
  208.     end // always@ *
  209.  
  210.  
  211.  
  212.  
  213.   function [N-1:0] sum(input [N-1:0] input_vector);
  214.     integer       k;
  215.     reg [N-1:0]   temp;
  216.    
  217.     begin
  218.       temp = 0;
  219.       for(k=0; k < N; k=k+1)
  220.         begin
  221.           temp = temp + input_vector[k];
  222.         end
  223.       sum = temp;
  224.     end
  225.    
  226.   endfunction // sum
  227.  
  228.  
  229.  
  230.  
  231.  
  232. endmodule // lcbbc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement