June 27, 2007

Coding even-parity checking logic

Filed under: ASIC Design — paritycheck @ 6:38 pm

For some, there are times when one knows how a logic function is supposed to work but forgets how to code it in Verilog even though one has done it countless times in many projects.  Here is one such function that tends to be taken for granted:

Checking 8-bit (byte) data protected with even-parity. 

Using Verilog, parity checking can be coded with the help of the “^” character.  It is a symbol for an Exclusive-OR logic gate.  Exclusive-OR essentially means if two bits are different, then generate a 1, if they are the same generate a 0.  It is great at finding out whether there are an odd or even number of bits on a bus.  In this article, I won’t go into much detail about what parity is used for since it is probably much easier to use a search engine to get the 411 about it.  Instead, I’ll just provide a couple of code snippets that can be used as a template in your own modules.

Snippet 1:

In this example, data is contained in data[7:0] and is protected by even-parity.  parity[0] contains the expected parity bit and is compared against an Exclusive-OR of all the data bits(data[7:0]).  If the Exclusive-OR of data bits equals the expected value, then no error was detected.  Otherwise, an error is detected.

always @(*) begin
  if (^data[7:0] == parity[0]) begin
    parity_error = 1'b0;
else begin
    parity_error = 1'b1; // parity error detected

Snippet 2: For a bus that is composed of many bytes and uses even-byte parity, Verilog code could look something like this: 

always @(*) begin

  if (

    (^data[7:0] == parity[0])

    & (^data[15:8] == parity[1])

    & (^data[23:16] == parity[2])

    & (^data[31:24] == parity[3])

    ) begin

    parity_error = 1'b0; // no error detected


  else begin

    parity_error = 1'b1; // parity error detected




Caveat  Please note that there is a rare possibility that a data error has occurred but is not detected.  This event could happen if data[7:0] or parity[0] is altered in such a way that their comparison will equal.  For instance, say data[7:0] originally contained 01101110 and parity[0] contained 1.  However, if during data[7:0]'s transit from one logic circuit to another and data[7:0] was corrupted and ended up becoming 10011110, the parity checker won't detect that such an error occurred.  This is because the checker only verifies whether an even or odd number of 1 bits are on the data bus. Are there other Verilog code examples you'd like to see?  Feel free to send me a note...


  1. very interesting.
    i’m adding in RSS Reader

    Comment by music — January 8, 2008 @ 10:05 pm

  2. thanks for the simple verilog code….

    Comment by Tom — May 26, 2010 @ 1:15 pm

  3. very good one but how to stop/see the info line by line in a file ..while searching..

    Comment by Murali — December 10, 2010 @ 9:26 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: