xerpi

SystemVerilog hamming_enc

Oct 19th, 2018
636
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module hamming_enc #(
  2.     parameter NUM_PARITY = 3,
  3.     localparam NUM_DATA = 2 ** NUM_PARITY - NUM_PARITY - 1,
  4.     localparam NUM_TOTAL = 2 ** NUM_PARITY - 1
  5. ) (
  6.     input logic [NUM_DATA - 1 : 0] data,
  7.     output logic [NUM_PARITY - 1 : 0] parity
  8. );
  9.     logic [NUM_PARITY - 1 : 0][NUM_DATA - 1 : 0] matrix;
  10.  
  11.     function automatic parity_covers(input integer p, input integer d);
  12.         integer i = 0, j = 0;
  13.         while (i < NUM_TOTAL && j < d) begin
  14.             i++;
  15.             if (i != (i & ~(i - 1))) /* is data bit */
  16.                 j++;
  17.         end
  18.         return i[p - 1];
  19.     endfunction
  20.  
  21.     generate
  22.         genvar i, j;
  23.         for (i = 0; i < NUM_PARITY; i++) begin
  24.             for (j = 0; j < NUM_DATA; j++) begin
  25.                 if (parity_covers(i + 1, j + 1))
  26.                     assign matrix[i][j] = data[j];
  27.                 else
  28.                     assign matrix[i][j] = 0;
  29.             end
  30.             assign parity[i] = ^matrix[i];
  31.         end
  32.     endgenerate
  33. 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.

×