/*
*
*	University of Oregon PHYS 432 - Lab 5 PAL Example
*	This example shows how CUPL can be used to create
*       non-trivial decoders and other combinatoric logic.
*
*	This program implements a state machine to provide
*       an internal 3-bit counter to drive an LED bar display.
*
*/

Name            ledcnt;
Partno          PHYS432;
Revision        01;
Date            20/03/2006;
Designer        E. Torrence;
Company         University of Oregon;
Location        None;
Assembly        None;
Device          g16v8;

/** Inputs **/

pin  1 = clk;			/* Counter clock */

/** Outputs **/

/* We're driving LEDs and need a low voltage to make them light up.
   So make all the outputs active low */
pin [16..19] = ![led0..3];	/* Output LED lines */

/* Also output the register bits (counter value) */
pin [13..15] = [q0..2];

/* Declarations and Intermediate Variable Definitions */

/* Note I have defined these MSB -> LSB */
field output = [led3,led2,led1,led0];	/* Shorthand for output lines */

/* Define the state machine values */
field counter = [q2..0];	/* Define "count" as the three counter bits */
$define s0 'b'000		/* Define the values of the counter bits in */
$define s1 'b'001		/* each of the eight counter states */
$define s2 'b'010
$define s3 'b'011
$define s4 'b'100
$define s5 'b'101
$define s6 'b'110
$define s7 'b'111

/** Logic Equations **/

/* 3-bit 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 next s1;
  present s1 next s2;
  present s2 next s3;
  present s3 next s4;
  present s4 next s5;
  present s5 next s6;
  present s6 next s7;
  present s7 next s0;
}

/* Define the decoder operations by anding specific count patterns
* with the desired output pattern.  By ORing together all possibilities,
* we get the correct output code depending upon the input received.
*/

$define on  'b'1
$define off 'b'0

output = 
	[ off, off, off, off ] & counter:0 #
	[ off, off, off, on  ] & counter:1 #
	[ off, off, on,  on  ] & counter:2 #
	[ off, on,  on,  on  ] & counter:3 #
	[ on,  on,  on,  on  ] & counter:4 #
	[ off, on,  on,  on  ] & counter:5 #
	[ off, off, on,  on  ] & counter:6 #
	[ off, off, off, on  ] & counter:7;