Start Here: What You Need Before State Machines
Before you can build a machine that remembers, you need three things: a way to write information (bits), a way to combine it (gates), and a way to store it (flip-flops). This volume teaches exactly those, plus how to read the timing diagrams used in every later lesson. No earlier knowledge is needed.
- What a bit is, and how many patterns a group of bits can make
- The four basic logic gates and their truth tables
- How a flip-flop stores one bit, and why the clock matters
- How to read a timing diagram, edge by edge
- How to run your first Verilog simulation for free
- Nothing - this is the very start
- A web browser, for the last sub-module
0.1 How to use this course
This course teaches one skill - designing circuits that remember what they are doing - in small steps. Each step uses only what came before it.
Welcome. This course is about state machines. A state machine is a circuit that always knows which situation it is in. When something happens, it moves to a new situation. You meet state machines every day. A traffic light is one. So are a lift, a washing machine and a ticket machine.
You do not need to know electronics or programming to start. This first volume teaches the few ideas the rest of the course needs. It takes less than an hour.
How every lesson is built
Every sub-module follows the same pattern, so you always know where you are:
- The big idea - the whole sub-module in one sentence, at the top.
- In plain words - the idea explained simply, without jargon.
- Think of it like this - an everyday picture of the same idea.
- A figure - a diagram or a timing picture to study.
- The exact version - the precise words an engineer would use.
- An example or some code - the idea put to work.
- Common mistake - the trap most beginners fall into, so you can step around it.
- Quick check - one or two questions, to make sure the idea has landed.
The explanations sit in coloured boxes. Here is what each kind looks like:
This is an "In plain words" box. It says the idea again, as simply as possible.
This is a "Think of it like this" box. It links the idea to something you already know from daily life.
This is a "Common mistake" box. It shows an error that many learners make, and how to avoid it.
This is a "Remember" box. It holds a rule worth keeping in your head.
New words
Every technical word is explained the first time it appears. It has a dotted underline, like this: bit. Point at it with your mouse, or tap it, to see a short meaning. Click it to open the full glossary, which lists every word from every course. At the end of each volume, a "Key words" list collects that volume's new words.
Optional parts
Going deeper: what these boxes are for
Boxes like this one hold extra detail for curious readers. You can skip them on your first read. Nothing later in the course depends on them.
Check yourself, then mark it done
Each sub-module ends with a quick check. Choose an answer, and the page tells you at once whether it is right, and why. If you get one wrong, read the explanation and try again. Mistakes made here are cheap, and they teach you the most.
When you finish a volume, press Mark as complete at the bottom of the page. Your progress then shows in the sidebar. It is saved only in this browser, so there is no account and nothing to sign up for.
The road ahead
| Part | Volumes | What you will be able to do |
|---|---|---|
| The ideas, on paper | 00 to 03 | Draw a state machine, and design one from gates |
| From paper to Verilog | 04 and 05 | Write a state machine as code, and test it |
| Real machines | 06 to 09 | Build detectors, timers, controllers and serial links |
| Better machines | 10 to 13 | Make them smaller, safer, faster and fully tested |
| Revision and interviews | 14 | Answer state machine questions with confidence |
You reach a "Going deeper" box on your first read. What should you do?
Show the answer
Answer: B. "Going deeper" boxes are optional extras. The main text never depends on them, so you can skip them the first time and come back later.
0.2 Bits, 0 and 1, and logic gates
Digital circuits work with just two values, 0 and 1, and combine them using a few simple building blocks called logic gates.
One bit: 0 or 1
Inside a digital circuit, every wire is in one of two conditions. It is either at a low voltage or at a high voltage. We call low 0 and high 1. One wire carrying a 0 or a 1 holds one bit of information.
A bit is like a light switch. It is either off (0) or on (1). There is nothing in between.
Why only two values? Because two values are easy to tell apart, even when the circuit is noisy. A little noise can make a "high" wire slightly lower. It is still clearly high, so the circuit still reads a 1. With ten levels to tell apart, small errors would change the value.
Many bits together
One bit gives two choices. Put bits side by side, and the number of patterns grows fast. Two bits make four patterns: 00, 01, 10 and 11. Each extra bit doubles the count.
| Bits | Patterns | How many |
|---|---|---|
| 1 | 0, 1 | 2 |
| 2 | 00, 01, 10, 11 | 4 |
| 3 | 000, 001, 010, 011, 100, 101, 110, 111 | 8 |
| n | every mix of n zeros and ones | 2n |
With n bits you can make 2n different patterns. 3 bits give 8 patterns, and 4 bits give 16.
This rule matters a lot for state machines. A state machine stores its situation as a pattern of bits. If a machine has 5 different situations, it needs at least 3 bits, because 2 bits give only 4 patterns. You will use this rule again in Volume 03.
We can also use bit patterns to count. This is called binary. Each place
is worth twice the place to its right: 4, then 2, then 1. So the pattern 101 means 4 + 0 + 1,
which is 5.
Logic gates
A logic gate is a tiny circuit. It takes one or more bits in and gives one bit out, following one fixed rule. The four gates you need are NOT, AND, OR and XOR. Figure 0.1 shows the symbol for each one.
We describe a gate with a truth table. A truth table lists every possible input, and the output for each one. Here are all four gates in one table:
| a | b | NOT a | a AND b | a OR b | a XOR b |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 0 |
In plain words:
- An AND gate gives 1 only when all of its inputs are 1.
- An OR gate gives 1 when at least one input is 1.
- A NOT gate flips its input: 0 becomes 1, and 1 becomes 0.
- An XOR gate gives 1 when its two inputs are different.
Think of a bank safe with two keys. It opens only when both keys are turned - that is AND. A house with two doorbell buttons rings when either one is pressed - that is OR.
Gates have no memory
A gate reacts to its inputs right now. Change an input, and the output changes a moment later. The gate does not know what its inputs were a second ago. Logic like this, with no memory, is called combinational logic.
That is a problem for us. A traffic light cannot be built from gates alone, because it must remember which colour it is showing. For that we need memory, which is the next sub-module.
In Verilog, the language this course uses, gates are written with symbols:
// Each line describes one gate. "assign" means: keep this wire
// always equal to the expression on the right.
assign y_not = ~a; // NOT
assign y_and = a & b; // AND
assign y_or = a | b; // OR
assign y_xor = a ^ b; // XOR
In everyday English, "tea or coffee?" usually means one or the other, not both. A logic OR is different: when both inputs are 1, an OR gate gives 1. The gate for "one or the other, but not both" is XOR.
How many different patterns can 3 bits make?
Show the answer
Answer: C. Each bit doubles the number of patterns: 2 × 2 × 2 = 8. They are 000, 001, 010, 011, 100, 101, 110 and 111.
a = 1 and b = 1. Which gate gives an output of 0?
Show the answer
Answer: B. XOR gives 1 only when its inputs are different. Here both inputs are 1, so XOR gives 0. AND gives 1, because both inputs are 1. OR gives 1, because at least one input is 1.
0.3 What a flip-flop remembers
A flip-flop is a one-bit memory: at each tick of the clock it copies its input, and it holds that value until the next tick.
The clock
Most digital circuits have a special signal called the clock. It goes 0, 1, 0, 1, over and over, at a steady rate. The clock carries no information of its own. Its only job is to say "now!" to the rest of the circuit.
The clock is like the beat in music, or a drummer keeping time for the rowers in a boat. Everyone moves together, exactly on the beat.
The moment the clock changes from 0 to 1 is called a rising edge. Most circuits act only on rising edges. The time from one rising edge to the next is one clock cycle. A clock with 100 million cycles in each second has a frequency of 100 MHz.
The flip-flop
A flip-flop is the smallest memory in a digital circuit. It stores one bit. The most common kind is the D flip-flop. It has an input called D (for data), a clock input, and an output called Q.
Its rule is short:
At every rising edge of the clock, Q becomes whatever D is at that moment. At all other times, Q stays the same - even if D changes.
A flip-flop is like a camera that takes one photo on each beat. The photo shows what the scene looked like at that exact moment. Between beats the photo does not change, however much the scene moves. D is the scene, and Q is the photo.
Figure 0.2 shows the rule at work. Read it from left to right. D changes at odd moments, but Q changes only just after a rising edge.
Follow the figure edge by edge:
- Edge 1. Just before the edge, D is 0. So Q becomes 0.
- Edge 2. D rose to 1 during the last cycle. At this edge Q copies it and becomes 1.
- Edge 4. D fell back to 0 during the last cycle. Q copies it and drops to 0.
- Between edges 4 and 5. D jumps up and falls back down. No edge happens during the pulse, so Q never sees it.
- Edge 7. D is 1 again, so Q becomes 1.
Why flip-flops matter for state machines
The flip-flop is the memory we were missing. A group of flip-flops that share one clock is called a register. A register of 3 flip-flops holds a 3-bit pattern, so it can hold 8 different values. A state machine keeps its current situation in exactly this kind of register.
Logic that contains memory is called sequential logic. What it does depends on the sequence of things that happened before, not only on its inputs right now.
Reset: a known starting point
When power first comes on, a flip-flop may hold a 0 or a 1. Nobody knows which. That is fine for some circuits, but not for a state machine. A machine that wakes up in a random situation could do something dangerous - such as showing green to every road at a crossing.
So circuits have a reset signal. While reset is active, it forces the flip-flops to a known value. Every state machine in this course starts from a reset.
In Verilog
You do not need to understand every word of this yet. Volume 04 explains Verilog properly. For now, read the comments on the right.
// A D flip-flop with a reset.
module dff (
input wire clk, // the clock
input wire rst, // reset: 1 means "go back to 0"
input wire d, // the value to store
output reg q // the stored value
);
always @(posedge clk) begin // at every rising edge of clk:
if (rst) q <= 1'b0; // reset wins - store 0
else q <= d; // otherwise, store d
end
endmodule
Two pieces of this code are worth knowing already. posedge clk means "at the rising edge of
clk". And 1'b0 means "a value one bit wide, equal to 0".
Many beginners think Q follows D, like a wire. It does not. Q changes only at a rising clock edge. If D changes and changes back between two edges, Q never knows it happened.
Going deeper: setup time and hold time
A real flip-flop needs D to be steady for a short time before the edge. This is called the setup time. D must also stay steady for a short time after the edge, called the hold time. If D changes inside this small window, the flip-flop may store the wrong value. This window is where all timing analysis begins - see the timing volume of the Verilog course. For now, just know that the window exists.
D changes from 0 to 1 in the middle of a clock cycle, and then stays at 1. When does Q become 1?
Show the answer
Answer: C. A D flip-flop copies D only at a rising edge. D is still 1 when the next rising edge comes, so Q becomes 1 just after that edge. Answer D would be right only if D had dropped back to 0 before the edge.
A register has 4 flip-flops. How many different values can it hold?
Show the answer
Answer: A. Each flip-flop holds one bit, so the register holds a 4-bit pattern. 4 bits give 2 × 2 × 2 × 2 = 16 patterns.
0.4 Reading a timing diagram
A timing diagram is a graph of signals over time. Read it from left to right, one clock edge at a time.
You have already seen one timing diagram: Figure 0.2. Every later volume uses them, so it is worth learning to read them properly.
The parts of a timing diagram
- Time runs from left to right. Each dotted vertical line marks a rising clock edge. The small numbers along the top count the edges.
- Each row is one signal. Its name is on the left. The line it draws is its waveform.
- High means 1, low means 0. A line near the top of its row is a 1. A line near the bottom is a 0.
- A band with a word inside is a bus. A bus is a group of wires that carry a value wider than one bit. The value is written inside the band. Where the band pinches, the value changes.
- Hatching means unknown. A hatched band is a value that nobody knows yet.
Figure 0.3 has all of these parts. It shows a tiny machine that waits for a start signal, works for three cycles, and then says it is done.
Follow it edge by edge:
- Edge 1. Reset is 1, so the state is forced to IDLE. Before this, the state was unknown.
- Edge 2. Start is still 0, so the machine stays in IDLE.
- Edge 3. Start is 1 just before this edge. The machine sees it and moves to RUN. Busy goes to 1, because the machine is now working.
- Edges 4 and 5. The machine stays in RUN. Start has gone back to 0, but that does not matter. The machine remembers that it was started.
- Edge 6. Three cycles of work are done. The state moves to DONE, and busy drops to 0.
- Edge 7. The machine returns to IDLE, ready for the next start.
Look again at edges 4 and 5. This is the whole point of a state machine. The start input was 1 for only one cycle, yet the machine kept working, because its state remembered.
Just before the edge, then just after it
Here is the most important reading rule. At a rising edge, a flip-flop takes the value its input had just before the edge. Its new output appears just after the edge. That is why, in these diagrams, outputs change a little to the right of the dotted line.
Do not read a value exactly on the dotted line, where it may be changing. To see what a flip-flop captured at an edge, look just to the left of the line. To see the result, look just to the right.
Unknown values
At the start of Figure 0.3, the state and busy rows are hatched. This is an
unknown value. The flip-flops have power, but nothing has told them
what to hold yet. Reset fixes that at edge 1. A simulator prints an unknown value as x. If you
see x after reset, some signal was not reset or not driven - a bug worth chasing.
In Figure 0.3, what is the state just after edge 6?
Show the answer
Answer: B. Look just to the right of edge 6: the state band says DONE. One edge later, at edge 7, it returns to IDLE.
The start input was 1 for only one cycle. Why did the machine keep working for three cycles?
Show the answer
Answer: A. After edge 3 the state is RUN, and the state is stored in flip-flops. The machine does not need the start input any more, because it remembers. This is the reason state machines exist.
0.5 Running Verilog free in your browser
You can describe a circuit in Verilog and watch it work in a simulator, for free, without installing anything.
Three new words
- Verilog is a hardware description language. You write text that describes a circuit. It looks a little like a programming language, but it describes wires and flip-flops, not a list of steps.
- A simulator is a program that reads your Verilog and behaves like the circuit would. You can watch every signal.
- A testbench is extra Verilog that exercises your circuit during simulation. It drives the inputs, and it prints or checks the outputs.
Think of a car engine on a test stand. The engine is your design. The test stand, with its fuel pipe and its gauges, is the testbench. The test stand is not part of the car, and the testbench is never part of the chip.
Where to run it
| Option | Cost | Install? | Best for |
|---|---|---|---|
| EDA Playground | Free account | No - it runs in the browser | Starting today, on any computer |
| Icarus Verilog and GTKWave | Free and open source | Yes | Working offline |
| An FPGA board | The price of the board | Yes | Seeing it on real hardware - optional |
This course uses EDA Playground in its examples, because it needs nothing but a browser. Every example also works with Icarus Verilog on your own computer.
Your first simulation
You will simulate the D flip-flop from sub-module 0.3. You need two pieces of code: the design,
which is dff.v above, and a testbench. Here is the testbench:
module dff_tb;
reg clk = 0, rst = 1, d = 0; // signals we drive
wire q; // the signal we watch
dff dut (.clk(clk), .rst(rst), .d(d), .q(q)); // the design under test
always #5 clk = ~clk; // flip the clock every 5 time units
initial begin
$dumpfile("dump.vcd"); // record every signal change...
$dumpvars(0, dff_tb); // ...so the waveforms can be drawn
#12 rst = 0; // release reset at time 12
#10 d = 1; // at time 22, set d to 1
#20 d = 0; // at time 42, set d back to 0
#20 $finish; // at time 62, stop
end
initial $monitor("t=%0t rst=%b d=%b q=%b", $time, rst, d, q);
endmodule
The clock starts at 0 and flips every 5 time units, so it rises at t = 5, 15, 25, 35 and so on. The last line prints a report whenever a signal changes. When you run it, you should see:
t=0 rst=1 d=0 q=x
t=5 rst=1 d=0 q=0
t=12 rst=0 d=0 q=0
t=22 rst=0 d=1 q=0
t=25 rst=0 d=1 q=1
t=42 rst=0 d=0 q=1
t=45 rst=0 d=0 q=0
Read it line by line. At t=0, q is x, because nothing has happened yet. At t=5, the first
rising edge comes while reset is 1, so q becomes 0. At t=22, d becomes 1, but q stays 0. At t=25,
the next rising edge arrives, and only then does q copy d. The same thing happens in reverse at
t=42 and t=45.
Step by step in EDA Playground
- Open EDA Playground and log in. The account is free.
- In the menu on the left, under Tools & Simulators, choose Icarus Verilog.
- Paste the testbench into the code box named testbench.sv, and the design into the box named design.sv.
- Tick Open EPWave after run, so the waveforms open by themselves.
- Press Run. The printed lines appear at the bottom, and the waveform window opens.
Working offline instead
If you prefer your own computer, install Icarus Verilog and GTKWave. Both are free. Then run three commands in a terminal:
iverilog -o sim dff.v dff_tb.v # build the simulation from both files
vvp sim # run it
gtkwave dump.vcd # open the waveforms
The file dump.vcd is a VCD file. It records every change of every signal.
A waveform viewer, such as GTKWave or EPWave, reads it and draws
the timing diagram for you.
If a simulation never ends, check for $finish. The clock line always #5 clk = ~clk; runs
forever. Without $finish, the simulator keeps going until you stop it by hand.
What is the job of a testbench?
Show the answer
Answer: C. A testbench exists only in simulation. Like a test stand for an engine, it feeds the design its inputs and checks what comes out. It is never built into hardware.
In the printed output, d becomes 1 at t=22, but q becomes 1 only at t=25. Why?
Show the answer
Answer: B. The clock rises at t = 5, 15, 25, 35 and so on. A D flip-flop copies d only at a rising edge, so the change at t=22 waits for the edge at t=25.
What you learned
- A bit is a 0 or a 1. With n bits you can make 2n different patterns.
- Logic gates (NOT, AND, OR and XOR) combine bits, but they have no memory.
- A D flip-flop stores one bit. It copies D to Q only at a rising clock edge.
- A register is a group of flip-flops. Reset puts it into a known starting value.
- In a timing diagram, look just before an edge to see what was captured, and just after it to see the result.
- You can simulate Verilog for free in EDA Playground, or offline with Icarus Verilog and GTKWave.
Key words from this volume
Every word below has a plain-English entry in the glossary.
- State machine (FSM)
- Bit
- Voltage
- Binary
- Logic gate
- Truth table
- AND gate
- OR gate
- NOT gate (inverter)
- XOR gate
- Combinational logic
- Verilog
- Clock
- Rising edge
- Clock cycle
- Frequency
- Flip-flop
- D flip-flop
- Register
- Sequential logic
- Reset
- Setup time
- Hold time
- Timing diagram
- Waveform
- Bus
- Unknown value (X)
- Hardware description language (HDL)
- Simulator
- Testbench
- Simulation
- VCD file
- Waveform viewer
Practice
How many bits?
A machine has 6 different situations, and each situation needs its own bit pattern. What is the smallest number of bits that will do?
Show the solution
Try each size in turn. 2 bits give 4 patterns, which is too few for 6. 3 bits give 8 patterns, which is enough, with 2 left over. So you need 3 bits. You will meet this question again in Volume 03, when you give states their bit patterns.
Read the gates
a = 0 and b = 1. Write down the output of NOT a, a AND b, a OR b and a XOR b.
Show the solution
- NOT a = 1, because NOT flips 0 to 1.
- a AND b = 0, because AND needs both inputs to be 1.
- a OR b = 1, because at least one input is 1.
- a XOR b = 1, because the two inputs are different.
Predict Q
A D flip-flop starts with Q = 0, and D starts at 0. The clock rises at t = 10, 20, 30 and 40. D is 1 from t = 12 to t = 18. Then it is 0 until t = 25, and 1 from t = 25 onwards. What is Q just after each edge?
Show the solution
Look at D just before each edge:
| Edge | D just before the edge | Q just after the edge |
|---|---|---|
| t = 10 | 0 | 0 |
| t = 20 | 0 - the pulse ended at t = 18 | 0 |
| t = 30 | 1 | 1 |
| t = 40 | 1 | 1 |
The pulse from t = 12 to t = 18 fell between two edges, so Q never saw it.
Next, Volume 01 puts these pieces together and builds your first state machine - on paper, one arrow at a time.