Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Truth tables and logic gates

After this lesson: I can read a truth table and write the gate that makes it.

50 min lesson4 commandsDigital design lab: Verilog, VHDL and yosys on one private Ubuntu VM

A siren with two reasons to sound

A truth table lists every possible input of a circuit and the output each one gives. A logic gate is the small circuit that makes one row of it true or false.

A shop alarm should sound if the door or the window opens, unless the owner has switched it off. That rule is three inputs and one output, and you can write it down as a table before anyone builds it.

By the end of this lesson

Note

I can read a truth table and write the gate that makes it.

Key words

WordWhat it meansExample
Bita value that is 0 or 1
Logic gatea tiny circuit that turns bits into one bitAND
Truth tableevery input pattern with the output it gives
Moduleone circuit, with inputs and outputs
Testbencha module that feeds a circuit and prints what comes out
Simulationrunning a circuit description on a computer

The cells students fill in are left blank in this preview.

A logic gate turns bits into one bit

A logic gate has one or two inputs and one output. Every input and the output is a bit.

An AND gate gives 1 only when both inputs are 1. An OR gate gives 1 when at least one input is 1. An XOR gate, short for exclusive or, gives 1 when the two inputs differ.

Worked example: AND, OR and XOR

aba AND ba OR ba XOR b
00000
01011
10011
11110

The cells students fill in are left blank in this preview.

A truth table lists every pattern

One input has two patterns, 0 and 1. Two inputs have four. Three inputs have eight, because each new input doubles the rows.

The alarm has three inputs: door, window and off. Its table has eight rows. The next four Runs write the alarm as a module in Verilog, write a testbench for it, and print the table by simulation. The files go in /tmp, so your own lab files stay as they were.

Write the alarm circuit

Command
echo 'module alarm(input door, input window, input off, output siren); assign siren = (door | window) & ~off; endmodule' > /tmp/alarm.v

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write its testbench

Command
echo 'module alarm_tb; reg door, window, off; wire siren; integer i; alarm dut(door, window, off, siren); initial begin $display("door window off | siren"); for (i = 0; i < 8; i = i + 1) begin {door, window, off} = i; #1 $display("  %b     %b     %b  |   %b", door, window, off, siren); end end endmodule' > /tmp/alarm_tb.v

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Compile both files

Command
iverilog -o /tmp/alarm /tmp/alarm.v /tmp/alarm_tb.v

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run the simulation

Command
vvp /tmp/alarm

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Verilog writes a gate as an operator

The line assign siren = (door | window) & ~off; is the whole circuit. | is OR, & is AND, ^ is XOR and ~ is NOT, which flips a bit.

assign joins a wire to an expression, and the wire follows its inputs at every moment. It is not a step that runs once.

The same table, worked in Python

Code: python
# Print the truth table of a rule with three inputs.
for door in [0, 1]:
    for window in [0, 1]:
        for off in [0, 1]:
            siren = (door or window) and not off
            print(door, window, off, "->", int(siren))
# Change the rule: make the siren sound only when door and window are both 1.

A common mistake

Warning

Many people think OR means one or the other, so 1 OR 1 gives 0. That is XOR. OR gives 1 when at least one input is 1, and that includes both. Check the last row of the table before you trust a gate.

Going further

Change the alarm so the siren also sounds when off is 1 but the door and window are both open.

Write the new truth table by hand first. Then change the assign line in a copy of the file and compare the simulation with your table.

About this lab and its source: Digital design lab: Verilog, VHDL and yosys on one private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.