/*
*
*	University of Oregon PHYS 432
*	This example shows how CUPL can be used to create
*       a state machine.
*
*	In this program, a 2-bit up/down counter grays code
*       counting sequence.  This would work to drive a stepper motor.
*
*/

Name            smotor;
Partno          PHYS432;
Revision        01;
Date            21/05/2006;
Designer        E. Torrence;
Company         University of Oregon;
Location        None;
Assembly        None;
Device          g16v8;

/** Inputs **/

pin  1 = clk;			/* Counter clock */
pin  2 = up;

/** Outputs **/

/* Output the register bits as the count value */
pin 13 = q0;
pin 15 = q1;

/* Also output the inverted values */
/* This just assigns a name, the logic comes below */
pin 14 = q0bar;
pin 16 = q1bar;

/* Declarations and Intermediate Variable Definitions */

/* Define the state machine values */
field counter = [q1,q0];		/* Define "count" as the two counter bits */
$define s0 'b'01		/* Define the values of the counter bits in */
$define s1 'b'11		/* each of the eight counter states */
$define s2 'b'10
$define s3 'b'00

/** Logic Equations **/

/* 2-bit up/down counter */

/* We don't need to define a clock since all the registers in the g16v8 are
   permanently connected to pin one where we will put in our clock */

sequence counter {		/* Describe the action of the state machine */
  present s0 
    if up   next s1;
    default next s3;
  present s1
    if up   next s2;
    default next s0;
  present s2 
    if up   next s3;
    default next s1;
  present s3 
    if up   next s0;
    default next s2;
}

/* Define the logic for any additional outputs */

q0bar = !q0;
q1bar = !q1;