RTL Foundations / F03

Boolean Decisions: What Does Each Operator Compute?

Build a sensor format check, work through bitwise, logical and reduction operators, and use a waveform to catch a nonzero test that lets invalid codes through.

Lesson film

Film chapters

TranscriptCaptionsDownload film

The sensor in the last lesson sent an eight-bit measurement. The collector now has to decide whether it can use that measurement. We will add a four-bit format field next to the data. The collector supports one code, 4'b0010, and it may use the measurement only while the channel is enabled.

Suppose someone implements that rule as “the channel is enabled and the format field has a value.” The two versions agree when the field holds 0010. What happens when it holds 0100? To find out, we need to distinguish questions about the whole pattern from questions about individual bit positions.

Which row permits the measurement?

Split the requirement into two questions. Is the channel enabled? Does the format field equal 0010? One bit can hold each answer. With channel_en_i and format_ok_o as those two answers, there are four possible combinations.

Channel enabledFormat validaccept_o
000
010
100
111

This is the truth table for AND. It returns 1 only when both inputs are 1. OR returns 1 if at least one input is 1, and NOT reverses one condition. Suppose the panel needs to explain a rejection. It can check !channel_en_i and !format_ok_o separately. For known binary inputs, !channel_en_i || !format_ok_o lights a rejection indicator whenever either reason applies.

The four-bit format is compared with 0010. A one-bit format_ok signal and the one-bit channel_en signal feed AND to produce accept.
The comparator answers the format question. The AND combines its answer with the channel state.
assign format_ok_o = (format_i == 4'b0010);
assign accept_o    = channel_en_i && format_ok_o;

The first line turns a four-bit field into a one-bit comparison result. The second combines that result with the channel state. Both are continuous assignments, so the combinational outputs follow changes in the inputs. Here accept_o means the two conditions currently hold. A later lesson will define the interface and clock edge at which a transfer actually occurs.

Each input contains a 1. Why does AND give 0000?

Set a=4'b0010 and b=4'b0100. Align the two operands and work from the most significant position to the least significant one. The four pairs are 0 & 0, 0 & 1, 1 & 0 and 0 & 0. Each gives 0 because the two operands have no 1 in a shared position. This is a bitwise operation. With these four-bit operands, a & b gives the four-bit result 0000, and a | b gives 0110.

Logical AND asks whether both values are true when taken as conditions. For known binary patterns, zero is false and any nonzero value is true. Both 0010 and 0100 are nonzero, so a && b gives the one-bit result 1. The positions of the two ones make no difference to this question.[1]

Bitwise AND of 0010 and 0100 has no overlapping ones and yields four-bit 0000. Logical AND of the two nonzero values yields one-bit 1.
Bitwise AND produces one result per position. Logical AND treats each whole operand as a condition and gives a single answer.

The same distinction applies to inversion. For a=0010, ~a flips each of the four bits and gives 1101. Logical negation !a asks whether the value is zero and gives 0. If a=0000, the two results are 1111 and 1. Multi-bit operands are legal in logical operations. An explicit comparison such as format_i == 4'b0010 lets the reader see the exact value specified by the interface.[1]

ExpressionResult hereResult width
a & b00004
a && b11
~a11014
!a01

The examples use four-bit declarations. The testbench checks the widths of the expressions themselves with $bits. A wider assignment can affect the evaluation width of a bitwise expression. F05 returns to expression context with arithmetic examples.[1]

Fold four bits into a check bit

The sensor can send an even parity bit so the receiver can check for some kinds of bit flips in transit. Count the ones in the data. Append a 1 if the count is odd, so the total becomes even. Append a 0 if the count is already even. The pattern 0010 contains one 1, so its parity bit is 1.

XOR gives 1 when its inputs differ and 0 when they match. Work through 0010 from left to right and get ((0 ^ 0) ^ 1) ^ 0 = 1. With two operands, ^ is bitwise XOR. Put it before a single operand, as in ^data_i, and it combines every bit of that operand into one result. This is reduction XOR. Reduction AND &data_i asks whether all the bits are 1. Reduction OR |data_i asks whether any bit is 1. Each reduces a group of bits to one result.[1]

assign parity_o = ^data_i;

The receiver reduces the four received bits and XORs the result with the received parity bit. This one-bit check result is called a syndrome. Start with 0010 and parity 1. When all bits arrive intact, the syndrome is 1 ^ 1 = 0. If the least significant data bit flips, the data become 0011 and their recomputed parity changes to 0. The syndrome is now 0 ^ 1 = 1.

Data 0010 with parity 1 gives syndrome 0. One flip gives 0011 and syndrome 1. Two flips give 0100 and bring the syndrome back to 0.
All three rows use the original parity bit of 1. One flip changes the parity of the data. Two flips leave it unchanged.

Now flip positions 2 and 1 of the original data to get 0100. There is still one 1, so the recomputed parity is still 1 and the syndrome returns to 0. Each flipped position toggles the overall parity once. In this model, parity detects any odd number of flipped positions. An even number preserves the original parity. A zero syndrome confirms that the five received bits satisfy the even-parity rule. It gives no evidence about who sent them.

Where do the parentheses go?

For known four-bit unsigned operands, a == b asks whether the patterns match, and a != b asks whether they differ. The expression a < b compares their unsigned values. With 0010 and 0100, equality returns 0 and the less-than comparison returns 1. For signed values, the types and widths of the two operands also affect the comparison. F05 works through that case.[1]

Suppose you want to know whether the two operands have any 1 bits in common. In (a & b) == 4'b0000, the parentheses tell SystemVerilog to compute each bit pair before testing whether all four results are zero. It returns 1. In a & b == 4'b0000, equality has higher precedence than bitwise &. The language evaluates b == 4'b0000 first and gets 0. Then it performs bitwise AND with that zero, making the final result zero as well. The testbench runs both forms and reports bare=0 grouped=1.[1]

How should a test compare X or Z?

Change format_i to 4'b00x0. If the unknown bit is 1, the field equals 0010. If it is 0, the patterns differ. Ordinary equality == has to return X here because the unknown bit decides the answer. If a known bit already differs, as in 010x compared with 0010, equality can return 0.[1]

Case equality === and case inequality !== are useful in a testbench. They compare all four states as symbols, including X and Z, and always return 0 or 1. Every row below compares with 4'b0010.[1]

Format=====!==
0010110
00x0X01
00z0X01

If a test requires an output to be 1, if (actual !== 1'b1) takes the failure branch for 0, X, or Z. With !=, an unknown output can yield X, and the if would skip that failure branch. When a test deliberately expects X in a position, === can check that too. For example, 4'b00x0 === 4'b00x0 gives 1. The X and Z observations come from four-state simulation. The format circuit uses ordinary equality, and the synthesis run processes the two design modules separately from the testbenches.[1]

Send an invalid code through the same test

The wrong version changes the comparison to format_i != 4'b0000. Both versions recognize 0010 and accept it when the channel is enabled. Leave the channel enabled and replace the format in turn with 0100, 0110 and 1111. All three violate the format requirement. All three pass the nonzero test.

In the first 25 nanoseconds, both versions reject 0010 with the channel off and accept 0010 with the channel on. Only the nonzero version accepts the later 0100, 0110 and 1111 codes.
The first five inputs from the correct and wrong VCD traces. Each input holds for 5 ns to make the combinational result visible in simulation.

The expected answers come from the interface requirement. Write 32 rows, one for each of the 16 codes with the channel off and another for each code with it on. Set format_ok_o to 1 for the two rows containing 0010. Set accept_o to 1 only for the enabled row with that code. The testbench reads this table from expected_format.mem and compares the outputs row by row. Copying the nonzero condition from the wrong design into the expected-answer table would let the same error pass twice.

Icarus Verilog 12.0 found that the correct version agreed with all 32 binary rows and passed four directed four-state cases. The wrong version produced 28 format_ok_o mismatches. Each of the 14 invalid nonzero codes was recognized as valid with the channel either on or off. The mismatch reached accept_o only with the channel enabled, giving 14 false accepts. The separate parity test counted ones for all 16 four-bit patterns and reproduced the directed syndrome sequence 0, 1, 0.

Yosys 0.33 produced one $eq and one $logic_and for the format check, plus one $reduce_xor for parity. These are generic word-level cells from the specified pass sequence, before mapping to a target cell library. The cell counts and the simulation stimulus times do not measure physical area or propagation delay.[2]

What if two bits flip together?

Find the even parity bit for 1011. Now flip positions 3 and 0, keeping the transmitted parity bit fixed. Calculate the receiver’s syndrome. Then write down the expected format and acceptance outputs for three cases of your own. Use a valid code with the channel off, a valid code with it on, and an invalid nonzero code with it on. Find those inputs in the downloadable waveforms and check your answers.

Worked answer

There are three ones in 1011, so the parity bit is 1. Flipping positions 3 and 0 gives 0010, which contains one 1. At the receiver, 1 XOR 1 gives a syndrome of 0. For the format check, enable=0 with 0010 gives {format_ok, accept}=10. Enable=1 with 0010 gives 11. Enable=1 with 0100 gives 00.

The outputs in this lesson are one-bit decisions. In F04, a decision like this drives a multiplexer and determines which data reach the output. The selection circuit needs both the conditions and a defined output for every branch.

Sources and further reading

  1. IEEE Std 1800-2017, §11.3.2 on precedence, §11.4.4–11.4.5 on relational and equality operators, §11.4.7–11.4.9 on logical, bitwise and reduction operators, and §11.6 on expression widths. IEEE standard PDF hosted by MIT
  2. Tool documentation. Icarus uses -g2012 to compile the SystemVerilog examples. Yosys uses read_verilog -sv for the design modules. The bundle includes the actual commands and versions. Icarus compiler flags · Yosys Verilog documentation

Try two bit patterns

Start with A = 0010 and B = 0100. Compare the two AND results. Then set A to 0011 and check whether a nonzero test accepts the wrong format.

   

Download RTL, testbench and results

Check your understanding

1. The format is 0010 and the channel is off. What are {format_ok_o, accept_o}?
2. With a=0010 and b=0100, what do a & b and a && b return, in that order?
3. A test expects 1 but observes X. Which condition definitely takes the failure branch?
4. Data 0010 were sent with parity 1. The receiver gets 0100 and parity 1. What is the syndrome?
5. Which test distinguishes exact recognition of 0010 from the nonzero check by observing accept_o?