xerpi

hamming

Oct 19th, 2018
649
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module hamming_gen #(
  2.     parameter NUM_PARITY = 3
  3. ) (
  4.     input  logic [  NUM_DATA - 1 : 0] data,
  5.     output logic [NUM_PARITY - 1 : 0] parity
  6. );
  7.     localparam NUM_DATA = 2 ** NUM_PARITY - NUM_PARITY - 1;
  8.     localparam NUM_TOTAL = 2 ** NUM_PARITY - 1;
  9.  
  10.     function parity_covers(input integer p, input integer d);
  11.         integer i;
  12.         integer j = 0;
  13.         for (i = 1; i <= NUM_TOTAL && j < d; i++)
  14.             if (i != (i & ~(i - 1))) /* Data bit */
  15.                 j++;
  16.         return ((i - 1) >> (p - 1)) & 1;
  17.     endfunction
  18.  
  19.     function integer foo(input integer d);
  20.         integer i;
  21.         integer j = 0;
  22.         for (i = 1; i <= NUM_TOTAL && j < d; i++)
  23.             if (i != (i & ~(i - 1))) /* Data bit */
  24.                 j++;
  25.         return i - 1;
  26.     endfunction
  27.  
  28.     generate
  29.         genvar i, j;
  30.         for (i = 0; i < NUM_PARITY; i++) begin
  31.             for (j = 0; j < NUM_DATA; j++) begin
  32.                 if (parity_covers(i + 1, j + 1)) begin
  33.                     assign parity[i] = ^data[j];
  34.                 end
  35.                 /*if ((j & ~(j - 1)) != j) begin
  36.                     assign parity[i] = ^data[j - (j & ~(j - 1))];
  37.                 end*/
  38.             end
  39.         end
  40.     endgenerate
  41.  
  42.         initial begin
  43.         integer i, j;
  44.  
  45.         for (i = 1; i <= NUM_DATA; i++)
  46.             $display("%d -> %d", i, foo(i));
  47.  
  48.         for (i = 1; i <= NUM_PARITY; i++)
  49.             for (j = 1; j <= NUM_DATA; j++)
  50.                 $display("parity_covers(%d, %d): %d", i, j, parity_covers(i, j));
  51.                 /*$display("parity: %d\n  data: %d\n total: %d",
  52.             NUM_PARITY, NUM_DATA, NUM_PARITY + NUM_DATA);*/
  53.         end
  54. endmodule
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×