top of page

ECC detection

In modern day systems, ECC is used extensively to detect errors. Let us today design this for ourselves with verilog. Please note, that these kind of problems are simple to write, but tricky to test. Hence, it gives us deep-dive into good brain storming. 

​

Verilog code for ECC detection:

module ECC_Detection (
  input [7:0] data,
  input parity,
  output reg error_detected
);

  always @(data or parity)
  begin
    // Calculate the number of 1's in the data word
    reg [3:0] count;
    reg i;

    count = 0;
    for (i = 0; i < 8; i = i + 1)
      count = count + data[i];

    // Check if the number of 1's is odd and parity bit is set to 1
    if ((count % 2 == 1) && (parity == 1))
      error_detected = 1;
    else
      error_detected = 0;
  end

endmodule

 

In this example, the ECC_Detection module takes an 8-bit data word (data) and a parity bit (parity) as inputs. It provides an output signal (error_detected) indicating whether an error has been detected.

The module calculates the number of 1's in the data word using a for loop and an internal register (count). If the count of 1's is odd (count % 2 == 1) and the parity bit is set to 1 (parity == 1), it indicates that an error has occurred. In such cases, the error_detected signal is set to 1. Otherwise, if the count of 1's is even or the parity bit is not set to 1, the error_detected signal is set to 0.

Please note that this code assumes a simple parity-based ECC scheme. Depending on the ECC scheme you are using, the logic and calculations for error detection may vary.

​

Testbench : 

module ECC_Detection_TB;

  // Testbench signals
  reg [7:0] data;
  reg parity;
  wire error_detected;

  // Instantiate the module under test
  ECC_Detection uut (
    .data(data),
    .parity(parity),
    .error_detected(error_detected)
  );

  // Test stimulus
  initial begin
    // Test case 1: Even parity with no errors
    data = 8'b01100101;
    parity = 0;
    #10;

    // Test case 2: Even parity with error (parity bit set to 1)
    data = 8'b01100101;
    parity = 1;
    #10;

    // Test case 3: Odd parity with no errors
    data = 8'b01010101;
    parity = 1;
    #10;

    // Test case 4: Odd parity with error (parity bit set to 0)
    data = 8'b01010101;
    parity = 0;
    #10;

    // Finish simulation
    $finish;
  end

  // Assertion to check error detection behavior
  always @(error_detected)
  begin
    if (error_detected)
      $display("Error detected!");
  end

endmodule

 

In this testbench code, the ECC_Detection_TB module is defined. It instantiates the ECC_Detection module (uut) and connects the testbench signals (data, parity, and error_detected) to the module under test.

The testbench includes a set of test cases in the initial block. Each test case assigns specific values to the data and parity signals and delays for a certain time to allow the module to detect errors. After each test case, the simulation continues with the next test case until all the test cases have been executed.

An assertion block is included to check if an error has been detected, and it displays a message when the error_detected signal goes high.

​

bottom of page