Skip to content

Section 8: Verilog Finite State Machines

In this discussion section, you will use what you have learned in lecture to implement two simple FSMs and then compose them to create a larger more complex FSM. This is an example of FSM factoring. In this discussion section, we will only be using RTL. Remember from previous discussion sections that it is critical that students always know what is the hardware they are modeling; students should understand what their RTL would likely turn into at the gate-level.

1. Logging Into ecelinux with VS Code

We have drastically simplified the process of logging into ecelinux with VS Code using the workstations. Find a free workstation and log into the workstation using your NetID and standard NetID password. Then complete the following steps:

  • Open the ECE 2300 folder on the desktop to locate two shortcuts
    • ECE 2300 Handouts: opens the handouts page
    • ECE 2300 VS Code: starts VS Code (enter server number and password)

That is it! Our custom ECE 2300 VS Code script will take care of:

  • installing Remote SSH, Verilog HDL, Surfer extensions
  • configuring SSH for accessing ecelinux without extra prompts
  • starting VS Code connected to ecelinux without extra prompts
  • opening the file explorer in your ${HOME}/ece2300 directory
  • installing the Verilog HDL extension on the server
  • opening a terminal

There is no need to fork the repo for today's discussion section. Simply clone the repo as follows.

% source setup-ece2300.sh
% mkdir -p ${HOME}/ece2300
% cd ${HOME}/ece2300
% git clone git@github.com:cornell-ece2300/ece2300-sec08-verilog-fsm sec08
% cd sec08
% tree

The repo includes the following files:

  • Makefile.in: Makefile for the build system
  • configure: Configure script for the build system
  • configure.ac: Used to generate the configure script
  • scripts: Scripts used by the build system
  • ece2300: ECE 2300 unit testing library and miscellaneous macros
  • tlight/tlight.mk: Tells build system about files in this subproject
  • tlight/Register_2b_RTL.v: 2-bit register in RTL
  • tlight/LightUnit_RTL.v: Light unit FSM in RTL
  • tlight/TimerUnit_RTL.v: Timer unit FSM in RTL
  • tlight/TrafficLightController_RTL.v: Complete traffic light controller
  • tlight/test: Directory with unit tests for each hardware module

Go ahead and create a build directory, run configure to generate a Makefile, and run the tests.

% cd ${HOME}/ece2300/sec08
% mkdir -p build
% cd build
% ../configure
% make check

To make it easier to cut-and-paste commands from this handout onto the command line, you can tell Bash to ignore the % character using the following command:

% alias %=""

Now you can cut-and-paste a sequence of commands from this tutorial document and Bash will not get confused by the % character which begins each line.

Throughout this handout you will see discussion check-off tasks. The TAs will not be assessing your understanding. Instead, students should show their work to another student and then together they will perform a self and peer assessment of each discussion check-off task. The student peer will initial the check-off. As always, the assesssment should based on the demonstrated understanding using our five-point scale as follows:

  • 5 (Mastery): Student mastered the check-off without any help and feels very confident in their understanding; student could teach other students about the corresponding concepts

  • 4 (Accomplished): Student accomplished the check-off with maybe a little help and feels good about their understanding, but could probably use a little more time to really master their understanding; student is probably not quite ready to teach other students about the corresponding concepts

  • 3 (Progressing): Student completed the check-off but needed significant help; student is not quite confident in their understanding and needs more time to really improve their understanding of the corresponding concepts

  • 2 (Beginning): Student started but did not finish the check-off

The goal is not for one student to judge another student, but for the two students to agree on the demonstrated level of understanding. There is nothing wrong with agreeing on a 4 or 3. This can help a student crystallize where they should focus their time to improve the understanding!

Near the end of the discussion section, the student should raise their hand and show their overall progress to a TA who will initial and collect the check-off sheet.

Discussion section check-off sheets are not graded!

Unlike the lab check-off sheets, the discussion check-off sheets are not graded. They will not impact your grade in the course in any way. The discussion check-off sheets provide formative self and peer assessment. They enable students to gauge their progress in mastering the concepts presented in the discussion section, and enable the instructors to get an overall feel for how the class is doing. Please make sure your self and peer assessments are honest and accurate!

Discussion Check-Off Task 1: Verify Initial Release

We always start by running make check to get a high-level view of what is and is not working. So go ahead and do make check again and look closely at what tests are passing and what tests are failing. Show a peer student the result of make check. Work together to conduct a self and peer assessment and have the peer student initial the check-off.

2. FSM Next State and Output Tables

Recall from lecture the traffic light controller example which is shown below for your reference.

In lecture, we showed a 10-state FSM which can implement the traffic light controller, but we also showed how to factor this monolithic FSM into two smaller FSMs: a four-state light unit FSM and a four-state timer unit FSM. The FSM diagram for the light unit is shown below.

Use the provided paper worksheet to derive the FSM output table and the FSM next state table.

The FSM diagram for the timer unit is shown below.

Use the provided paper worksheet to derive the FSM output table and the FSM next state table.

Discussion Check-Off Task 2: Complete FSM Tables

Show a peer student your FSM tables and explain how the tables correspond to the FSM diagrams.

3. Timer Unit FSM

Since we have the FSM next state and output tables for the timer unit we can also use the systematic process discussed in lecture to directly map this FSM into Verilog RTL. Remember, our FSMs will always have three sections: sequential logic, next-state combinational logic, and output combinational logic. For the sequential logic, instantiate the provided Register_2b_RTL module. The next state combinational logic should be implemented by mapping your FSM next state table into a single always_comb block with a single case statement. The output combinational logic should be implemented by mapping your FSM output table into a separate always_comb block with a single case statement. The control unit is an example of a flat RTL module, since it does not instantiate any hardware modules other than the register.

Once you have finished your implementation verify it passes the provided test cases.

% cd ${HOME}/ece2300/sec08/build
% make TimerUnit_RTL-test
% ./TimerUnit_RTL-test +test-case=1
% ./TimerUnit_RTL-test +test-case=2
% ./TimerUnit_RTL-test +test-case=3
% ./TimerUnit_RTL-test

Discussion Check-Off Task 4: Verify Timer Unit FSM

Show a peer student your implementation for the timer unit FSM. Then show the peer student your implementation passing all of the tests.

4. Light Unit FSM

Now that we have the FSM next state and output tables for the light unit we can use the systematic process discussed in lecture to directly map this FSM into Verilog RTL. Remember, our FSMs will always have three sections: sequential logic, next-state combinational logic, and output combinational logic. For the sequential logic, instantiate the provided Register_2b_RTL module. The next state combinational logic should be implemented by mapping your FSM next state table into a single always_comb block with a single case statement. The output combinational logic should be implemented by mapping your FSM output table into a separate always_comb block with a single case statement. The control unit is an example of a flat RTL module, since it does not instantiate any hardware modules other than the register.

Once you have finished your implementation verify it passes the provided test cases.

% cd ${HOME}/ece2300/sec08/build
% make LightUnit_RTL-test
% ./LightUnit_RTL-test +test-case=1
% ./LightUnit_RTL-test +test-case=2
% ./LightUnit_RTL-test +test-case=3
% ./LightUnit_RTL-test

Discussion Check-Off Task 3: Verify Light Unit FSM

Show a peer student your implementation for the light unit FSM. Then show the peer student your implementation passing all of the tests.

5. Traffic Light Controller

We have now implemented our two factored FSMs so we can compose them to implement the traffic light controller. The traffic light controller should simple instantiate the light unit and timer unit and connect them up.

Once you have finished your implementation verify it passes the provided test cases.

% cd ${HOME}/ece2300/sec08/build
% make TrafficLightController_RTL-test
% ./TrafficLightController_RTL-test +test-case=1

Discussion Check-Off Task 5: Traffic Light Controller

Show a peer student your implementation for the traffic light controller. Then show the peer student your implementation passing all of the tests.

6. Traffic Light Simulator

Now try out our traffic light simulator see your traffic light controller in action.

% cd ${HOME}/ece2300/sec08/build
% make tlight-sim
% ./tlight-sim

Every time you press enter it toggles the clock. Count the number of cycles to confirm that the traffic light controller is correctly implementing the specification:

– E/W green and N/S red for 20 seconds – E/W yellow and N/S red for 5 seconds – E/W red and N/S green for 20 seconds – E/W red and N/S yellow for 5 seconds

Remember we are assuming the clock period is five seconds.

7. Clean Build

As a final step, do a clean build to verify everything is working correctly.

% cd ${HOME}/ece2300/sec08
% trash build
% mkdir -p build
% cd build
% ../configure
% make check