Write one parameterized block that makes a parity bit for a word of any width, even or odd. It also checks a received word against the same rule.
Hardware context: A 32-bit motion report packed from two signed deltas, a tag and two buttons; and a parity generator and checker at six widths. Write the parity generator and checker for any width, even or odd.
Three links in a chip protect their words with one parity bit each. They carry 5, 16 and 33 bits. Two use odd parity and one uses even. The last project had three hand-written copies, and one of them counted only the low byte.
This is an Implement problem: write the body of parity. The fixture builds six copies with different WIDTH and ODD. It feeds each its own data and received bit, and checks all six together.
Brief: combinational. With ODD = 0 the sent word {p, d} holds an even number of 1 bits. With ODD = 1 it holds an odd number. err is 1 when the received word {rx_p, d} does not follow that rule. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
stimulus | 133 | input of each check |
Full specification
module parity #(
parameter int WIDTH = 8, // data bits, 1 to 64
parameter bit ODD = 1'b0 // 0: even parity, 1: odd parity
) (
input logic [WIDTH-1:0] d, // data to send, and the data received
input logic rx_p, // the parity bit received with d
output logic p, // the parity bit to send with d
output logic err // 1 when {rx_p, d} breaks the parity rule
);
// your design
endmodule
| WIDTH | ODD | d | rx_p | p | err |
|---|---|---|---|---|---|
| 8 | 0 | 0000_0111 | 1 | 1 | 0 |
| 8 | 0 | 0000_0111 | 0 | 1 | 1 |
| 8 | 1 | 0000_0111 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 | 1 | 0 |
The copies are (WIDTH, ODD) = (1, 0), (5, 1), (8, 0), (16, 1), (33, 0), (64, 1). The result lists p and err for each copy. The synthesis check builds the design at its default parameters.
Examples
Example 1
All Zeros- Input
WIDTH=1 even: d=0x0 rx_p=0; WIDTH=5 odd: d=0x0 rx_p=0; WIDTH=8 even: d=0x0 rx_p=0; WIDTH=16 odd: d=0x0 rx_p=0; WIDTH=33 even: d=0x0 rx_p=0; WIDTH=64 odd: d=0x0 rx_p=0
- Output
p_w1 = no, err_w1 = no, p_w5 = yes, err_w5 = yes, p_w8 = no, err_w8 = no, p_w16 = yes, err_w16 = yes, p_w33 = no, err_w33 = no, p_w64 = yes, err_w64 = yes
Zero 1 bits is an even number. Even copies send p = 0 and odd copies send p = 1. rx_p is 0, so only the odd copies report an error.
Example 2
All Ones- Input
WIDTH=1 even: d=0x1 rx_p=1; WIDTH=5 odd: d=0x1f rx_p=1; WIDTH=8 even: d=0xff rx_p=1; WIDTH=16 odd: d=0xffff rx_p=1; WIDTH=33 even: d=0x1ffffffff rx_p=1; WIDTH=64 odd: d=0xffffffffffffffff rx_p=1
- Output
p_w1 = yes, err_w1 = no, p_w5 = no, err_w5 = yes, p_w8 = no, err_w8 = yes, p_w16 = yes, err_w16 = no, p_w33 = yes, err_w33 = no, p_w64 = yes, err_w64 = no
The count of 1 bits is the width itself, so p depends on whether WIDTH is odd or even, and on ODD.
Example 3
Only the Top Bit Set- Input
WIDTH=1 even: d=0x1 rx_p=1; WIDTH=5 odd: d=0x10 rx_p=1; WIDTH=8 even: d=0x80 rx_p=1; WIDTH=16 odd: d=0x8000 rx_p=1; WIDTH=33 even: d=0x100000000 rx_p=1; WIDTH=64 odd: d=0x8000000000000000 rx_p=1
- Output
p_w1 = yes, err_w1 = no, p_w5 = no, err_w5 = yes, p_w8 = yes, err_w8 = no, p_w16 = no, err_w16 = yes, p_w33 = yes, err_w33 = no, p_w64 = no, err_w64 = yes
One 1 bit, in the highest position of each copy. A design that looks at a fixed number of bits misses it in the wide copies.
Constraints and Contract
- Inputs are known 0/1 values. Each case applies one input vector and samples the outputs after a nanosecond.
- Keep the module name, the parameters and the ports.
Hints
Hint 1
First write p for even parity. Which single operator tells you whether d holds an odd number of 1 bits?
Hint 2
Odd parity is even parity with the bit flipped. ODD is one bit, so one more gate turns one rule into the other.
Hint 3
The receiver computes the bit it expects from d and compares it with rx_p. A difference is the error.
What the grader checks: Six directed and twelve seeded cases drive six copies at widths 1 to 64, with both parity rules. An independent model checks p and err of every copy. The synthesis check builds the default copy.
The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.
18 checks on Icarus Verilog + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 steps and the final capstone