Logic Gates
Everything an FPGA does reduces to lookup tables implementing boolean
functions, so we start where the hardware starts. In Verilog, stateless
("combinational") logic is written with assign: the left-hand side
continuously follows the right-hand side, like wiring gates together,
there is no notion of "executing" an assign, it just is.
The design below implements the six classic gates on two inputs. Points to notice:
&,|,^,~are bitwise operators, applied to vectors they operate lane by lane. On 1-bit signals they behave like the gate.- There is no "order of execution". All five assigns exist simultaneously, exactly like five pieces of copper.
- In VHDL the same thing is a concurrent signal assignment; the keywords
are friendlier (
and,or,xor,not).
In the waveform, the testbench sweeps all four input combinations of
a/b. Check each output against the truth table you know, this habit
(predict first, then look) is the core skill of reading waveforms.
Experiment in the playground: make a 3-input majority gate
(assign y = (a & b) | (a & c) | (b & c);) and verify it with an
exhaustive 8-case testbench.