Joseph Tarango
Department of Computer Science and Engineering
Bourns College of Engineering
University of California, Riverside

Home
CS120B | Syllabus
Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Game Project

Lab 3: Asynchronous State Machine Design

In this lab we will examine one of the most important concepts in embedded system design - the state machine. We are going to look at asynchronous state machines, as opposed to synchronous state machines.  The difference is that asynchronous state machines do not have a concept of time or a clock that is used to advance from one state to the next.    A code format for an asynchronous state machine is given.  It is imperative that you stick precisely to the format that is given.  You should not begin to write code until you have completed your state machine design on pen and paper.

Today you will be building an asynchronous state machine that will perform various functions on two inputs, similar to how a calculator works.

PORTS

Input/Output of state machine.
PORTA: User-selected function
PORTB: Argument A
PORTC: Argument B
PORTD: Output

Functions

Functions to perform based on value of PORTA
PORTA FUNCTION
0x00 Output = A + B
0x01 Output = A(7:4) | B(3:0)
0x02 Output = Reverse of A
0x03 Output = A modulo B

When PORTA = 0x00, a simple addition should be performed. 
When PORTA = 0x01, the high nibble of A should be concatenated with the low nibble of B. 
When PORTA = 0x02, A should be reversed such that Output7 = A0, Output6 = A1, Output5 = A2, etc...
When PORTA = 0x03, then the output equals A modulo B.

The key concept you should get from this lab is how to design and format a state machine.  In this course we want you to think about how to design a system using a model (here, an asynchronous state machine), and to not focus on the actual coding.  Before you begin to code, you should already have a pen and paper state machine drawn out.  It is then a simple transformation from model to code.  Below is an example state machine design, and the code that would be generated from it.  For your own state machine design, the generated code should match the format of the code given below for the example state machine.  There should be ZERO deviation in the formatting of the code of your own state machine from the given example format.  Your code should contain enumerated state names, a second Tick() function, multiple switch statements for state actions and conditions, and a main loop.   This lab will be graded 50% on how closely you followed the state machine code format.

Note that in the below example, a prefix of EX (EXample) is used to specify variables that correspond to state machine specific characteristics, such as the current state.  You should pick your own prefix to identify your state machine.  Also, the code below is written for RIM, you of course will need to write code which will work on the AVR (and thus you will need to use correct IO names such as PORTA, and set corresponding DDR registers).



SAMPLE CODE:

#include <avr/io.h>

 

enum EX_States { EX_Set, EX_Clear } EX_State;

 

void EX_Tick( ) {

      switch(EX_State) { //Transistion to next state.

      case -1:

            EX_State = EX_Clear;

            break;

      case EX_Clear:

            if ( A == 0 ) {

                  EX_State = EX_Clear;

            }

            else if ( !(A == 0) ) {

                  EX_State = EX_Set;

                   }

            break;

      case EX_Set:

             if ( !(A == 0) ) {

                  EX_State = EX_Set;

                   }

             else if ( A == 0 ) {

                  EX_State = EX_Clear;

                   }

            break;

      default:

            EX_state = EX_Clear;

            break;

             }

       switch(EX_State) { //Execute actions of current state.

      case -1:

             break;

      case EX_Clear:

             B = 0;

             break;

      case EX_Set:

             B = 1;

             break;

      default:

            break;

             }

}

 

int main(void) {

       EX_State = -1; //Set initial state.

       while(1) {

            //Execute actions of current state and transit to next state.

            EX_Tick( );

             }

}

POST LAB

INDIVIDUALLY prepare and submit a single-spaced half page report with the following information. Include the text of the deciphered message in your report.

I.     Lab Objective
II.   Personal Contributions
III. Skill learned & knowledge gained.

Turnin

INDIVIDUALLY prepare and submit all lab files into a tar ball. All .c files should be included in lab parts, as well as post lab submitted in pdf and txt format. All files should include a header with name, login, email, lab section, assignment, and group associates; also include: "I acknowledge all content is original."

For Example
Name: John Doe
Login: jdoe
Email: jdoe@cs.ucr.edu
Lab Section: 021
Assignment: Lab 3
Group: John Doe, Jane Doe, and Joe Doe
I acknowledge all content is original.

Tar ball command: tar -cvzf name.tgz *.c *.pdf *.txt

The tar command will compress all files into a .tgz file with all .c .pdf, and .txt files in that directory. Do not include unnecessary files! The .c files be named as follows lab#_part#.c and the postlab#.pdf/postlab#.txt.

For Example:
lab3.c
postlab3.pdf
postlab3.txt