#include "includes.h" /* Define items specific to the compiler */ 

#include "hardware.h" /* Contains interface to physical hardware */ 
#include "example1.h" /* Interface declarations for this file */ 

/*
 * ZDS-II Board comes up with Light Up The World attitude.  74HC273 should have
 * been used.  Turn all of them off.
 */
void turn_off_digit_leds(void);
void turn_off_digit_leds(void)
{
    PEDD   = 0x00;  /* Data Direction,            0 = Output Direction */
    PEAF   = 0x00;  /* Alternate Function,        0 = Normal/Follow DDx bit */
    PEOC   = 0x00;  /* Output Control,            1 = Open Drain */
    PEHDE  = 0x00;  /* High Drive Enable,         1 = High Current Drive */
    PESMRE = 0x00;  /* Stop Mode Recovery Enable, 1 = Stop Mode Recovery Source */
    PEOUT  = 0x1F;  /* Output,                    1 = Logical High Output */

    PGDD   = 0x00;  /* Data Direction,            0 = Output Direction */
    PGAF   = 0x00;  /* Alternate Function,        0 = Normal/Follow DDx bit */
    PGOC   = 0x00;  /* Output Control,            1 = Open Drain */
    PGHDE  = 0x00;  /* High Drive Enable,         1 = High Current Drive */
    PGSMRE = 0x00;  /* Stop Mode Recovery Enable, 1 = Stop Mode Recovery Source */
    PGOUT  = 0x00;  /* Output,                    1 = Logical High Output */

    LED_D1_CLOCK();
    LED_D2_CLOCK();
    LED_D3_CLOCK();
    LED_D4_CLOCK();
}

int main()
{
    DI();

    /* Setup I/O ports: */
    turn_off_digit_leds();

    PFDD   = (PF7|PF6);  /* Data Direction,       0 = Output Direction, PF6&5 are inputs */
    PFAF   = 0x00;  /* Alternate Function,        0 = Normal/Follow DDx bit */
    PFOC   = 0x00;  /* Output Control,            1 = Open Drain */
    PFHDE  = 0xFF;  /* High Drive Enable,         1 = High Current Drive */
    PFSMRE = 0x00;  /* Stop Mode Recovery Enable, 1 = Stop Mode Recovery Source */
    PFOUT  = 0x00;  /* Output,   1 = Logical High Output, PF6&5 are inputs */

    Example1_reset(); /* Reset the Esterel code */

    /*
     * Programs often contain instantaneous initial statements, such
     * as signal emissions or variable initializations, to be performed
     * during the first reaction. To perform them, it is often useful
     * (but not mandatory) to generate a blank initial event by calling
     * the reaction function before calling any input C function. (This
     * boot transition is different from the automaton reset).
     */
    Example1(); /* Iniz the Esterel code */

    ENABLE_INTERRUPTS();

    for(;;)/*ever*/
    {
	if( TRUE == SW1_ASSERTED() )
	    Example1_I_SW1_ASSERTED(); /* Let the Esterel code know that SW1 has been asserted */

	if( TRUE == SW2_ASSERTED() )
	    Example1_I_SW2_ASSERTED(); /* Let the Esterel code know that SW2 has been asserted */

	Example1(); /* Call the Esterel Automation code.  Light LED1 when SW1 is pressed followed by SW2 */
    }
    /* While this code does function it has a potential critical flaw.  The flaw is that all inputs
     * to the Esterel reaction function, Example1(), should be edge based.  Only a edge transition one of the switches should cause
     * SWx_ASSERTED() to be called.  You can see an example of the flaw if you remove the edge debouncing from
     * the reflex_game sample.
     */
}