What changes when Quartus recodes a state machine?
Online version: https://fpga.labsland.com/altera/digital-logic/lab-07/part-02/
A line monitor raises an alarm on the fourth consecutive equal bit. The alarm stays on for the fifth bit and clears on the first opposite bit. Draw a nine-state diagram with an idle state and two paths of four states each. Mark the states that raise the alarm. Predict how many flip-flops you need for a compact binary encoding and for one-hot encoding.
Lab setup
Quartus Prime Lite 24.1 · Verilog · DE1-SoC
Physical DE1-SoCCyclone V · 5CSEMA5F31C6 · camera, switches and buttons in the browser
Open State Machine Viewer in desktop Quartus to compare both encodings, then upload each bitstream to the remote board.
Build
Open encoded_runs.qpf in Quartus Prime Lite with Cyclone V support. Repair the starter and compile it with the supplied User-Encoded setting. Open Tools → Netlist Viewers → State Machine Viewer. Find the two transitions leaving the fourth-one state. In the Compilation Report, under Analysis & Synthesis → State Machines, record the register headings and the codes for idle, the first zero and the fourth one. Save the report excerpt and the SOF in a folder named binary.
Copy the project to a second folder. Under Assignments → Settings → Compiler Settings → Advanced Settings (Synthesis), change State Machine Processing to One-Hot. Alternatively, change USER-ENCODED to ONE-HOT on the STATE_MACHINE_PROCESSING line of encoded_runs.qsf. For VHDL, also remove both attribute syn_encoding lines from the second copy. Quartus gives the explicit type encoding precedence over the project setting. Compile again. Record the same three codes in the same bit order. Save this SOF separately.
- Install Quartus Prime Lite with Cyclone V supportSkip this if it is already installed.
- Download the activityDownload worksheet and Quartus project Includes the worksheet, the Quartus project and the simulation files.
- Complete the design and compileStart with Simulate in the worksheet below, then complete the design as its build steps describe. Open
encoded_runs.qpfand choose Processing → Start Compilation. Quartus writesoutput_files/encoded_runs.sof. - Upload the bitstream to LabsLandUpload bitstream LabsLand asks you to sign in. Use the access your course or institution provides.
Already compiled? Upload your .sof directly
Starter files · Verilog · Quartus 24.1 · VHDL · Quartus 24.1 · SystemVerilog · Quartus 24.1
Worksheet and Quartus project (.zip) for offline work or printing
Simulate
The testbench counts identical samples and checks all nine decoded state lamps and the match lamp across 256 eight-bit words. Read the first mismatch. Which input makes the detector leave a state where it should stay?
Verilog
iverilog -g2005 -s test_encoded -o bench encoded_runs.v test_encoded.v
vvp bench
SystemVerilog
iverilog -g2012 -s test_encoded -o bench encoded_runs.sv test_encoded.v
vvp bench
VHDL
nvc --std=2008 -a encoded_runs.vhd test_encoded.vhd
nvc --std=2008 -e test_encoded
nvc --std=2008 -r test_encoded
Build
Open encoded_runs.qpf in Quartus Prime Lite with Cyclone V support. Repair the starter and compile it with the supplied User-Encoded setting. Open Tools → Netlist Viewers → State Machine Viewer. Find the two transitions leaving the fourth-one state. In the Compilation Report, under Analysis & Synthesis → State Machines, record the register headings and the codes for idle, the first zero and the fourth one. Save the report excerpt and the SOF in a folder named binary.
Copy the project to a second folder. Under Assignments → Settings → Compiler Settings → Advanced Settings (Synthesis), change State Machine Processing to One-Hot. Alternatively, change USER-ENCODED to ONE-HOT on the STATE_MACHINE_PROCESSING line of encoded_runs.qsf. For VHDL, also remove both attribute syn_encoding lines from the second copy. Quartus gives the explicit type encoding precedence over the project setting. Compile again. Record the same three codes in the same bit order. Save this SOF separately.
Test both bitstreams
Upload each SOF to the Standard binary-upload lab with a distinct filename. Select it in the firmware list and enter the board. Lower SW0 and press and release KEY0 once to reset. Raise SW0. For each sample, set SW1 and release KEY0 once. LEDR0–8 identify the current state and LEDR9 signals a match. The nine state lamps are decoded labels. Use the report to read the underlying register bits.
| Sample after reset | State | User-Encoded lamps | One-Hot lamps |
|---|---|---|---|
| Third zero | |||
| Fourth and fifth zeros | |||
| First one after the zeros | |||
| Fourth and fifth ones | |||
| First zero after the ones |
Hand in
Submit the diagram, the two sets of register headings and codes, and the camera observations. Account for any difference between your predicted one-hot idle code and the compiler's code. Explain how the same lamp can turn on in both builds when the corresponding physical register bits differ.
Original LabsLand activity following FPGAcademy Digital Logic Lab 7, Part II. Verilog handout · VHDL handout · MIT
