Basic logic gates
Map the truth tables for AND, OR, NOT, NAND, NOR and XOR to gates. Then use four-bit inputs to work out the output width of a bitwise operation, a logical test and a reduction.
The collector represents four sensor states in each word. a=1010 has ones on channels three and one, and b=1100 has ones on channels three and two. Finding every channel that is high in both words needs four output bits. Asking whether both words have at least one high channel needs one bit.
Decide whether to operate on each bit or on a whole word
Bitwise AND (a & b) pairs bits at matching positions. Only channel three is high on both sides, so the result is 1000. OR needs either side to be high and gives 1110. XOR requires a difference and gives 0110. NOT flips the bits of A to produce 0101. NAND flips the AND word to 0111. NOR flips the OR word to 0001.
A single-bit truth table gives the result for each position. NOT has one input and flips zero to one or one to zero.
| A | B | AND | OR | XOR | NAND | NOR |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Logical AND (a && b) treats both words as conditions. Each word is nonzero, so it returns the single bit 1. A reduction works on one input word. &a asks whether all four bits are high and gives 0. ^a returns one if the count of high bits is odd. 1010 has two ones, so its parity is 0.
Synthesizable SystemVerilog
module logic_gates4 (
input logic [3:0] a,
input logic [3:0] b,
output logic [3:0] and_bits,
output logic [3:0] or_bits,
output logic [3:0] xor_bits,
output logic [3:0] not_a,
output logic [3:0] nand_bits,
output logic [3:0] nor_bits,
output logic logic_and,
output logic all_a,
output logic parity_a
);
assign and_bits = a & b;
assign or_bits = a | b;
assign xor_bits = a ^ b;
assign not_a = ~a;
assign nand_bits = ~(a & b);
assign nor_bits = ~(a | b);
assign logic_and = a && b;
assign all_a = &a;
assign parity_a = ^a;
endmodule
Check the results against the waveform
The inputs and outputs are shown in binary.
| Change at | a | b | and_bits | logic_and | all_a | parity_a |
|---|---|---|---|---|---|---|
| 0 ns | 1010 | 1100 | 1000 | 1 | 0 | 0 |
| 10 ns | 1010 | 0000 | 0000 | 0 | 0 | 0 |
| 20 ns | 1111 | 0000 | 0000 | 0 | 1 | 0 |
| 30 ns | 0111 | 0011 | 0011 | 1 | 0 | 1 |
With a=1111 and b=0000, reduction AND gives 1 and logical AND remains 0. The first checks every bit of A, and the second checks both words as conditions. Choose the operator according to whether the receiver needs a mask or a flag.
Download the runnable SystemVerilog lab. Icarus checks the 4 rows above and 256 binary input combinations. It reads each directed result 1 ns after changing the inputs. The wait sets an observation time in the testbench. The waveform is functional evidence. Physical delay needs a timing model.
Interactive lab
Change the two four-bit inputs. Compare four-bit bitwise words with one-bit logical and reduction results.
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- NOT A
- NAND
- NOR
- Logical AND
- Reduction AND of A
- Parity of A
Show the last eight observations
Teaching film
Run the RTL yourself
Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.
Download Basic logic gates RTL (.zip)