 *********************************************************************
 * Authors: B. Alex Bridges and Davy Little                          *
 * Class: ECE-374, Summer 1998                                       *
 * Project: Lab Assignment #7                                        *
 * Description: This program continuously displays a traffic light   *
 *              pattern with switches to allow the clearing and      *
 *              displaying of a counter value.                       *
 *********************************************************************

** CONSTANTS / VARIABLES **
        ABSOLUTE
        ORG     $00
T1:     FCB     91      * Time Delay #1 In Number of TOFs
                        * 3 seconds = 3000 ms / 33 ms = 90.9 -> 91 times
T2:     FCB     30      * Time Delay #2 In Number of TOFs
                        * 1 seconds = 1000 ms / 33 ms = 30.3 -> 30 times
COUNT:  FCB     0       * SW1's Counter Value
LEDOUT: FCB     %00111111 * DDRC Bit Pattern For Output LEDs
G2R1:   FCB     %00001100 * Bit Pattern For Lighting R1 and G2
Y2R1:   FCB     %00010100 * Bit Pattern For Lighting R1 and Y2
R2R1:   FCB     %00100100 * Bit Pattern For Lighting R1 and R2
R2G1:   FCB     %00100001 * Bit Pattern For Lighting G1 and R2
R2Y1:   FCB     %00100010 * Bit Pattern For Lighting Y1 and R2
NONE:   FCB     %00000000 * Bit Pattern For Lighting Nothing
DDRC:   EQU     $1007   * Address Corresponding to DDRC (PORTC's I/O Settings)
PORTB:  EQU     $1004   * Address Corresponding to Port B
PORTC:  EQU     $1003   * Address Corresponding to Port C
TCTL2:  EQU     $1021   * Address Corresponsing to Timer Control 2
TFLG1:  EQU     $1023   * Address Corresponsing to Timer Interrupt Flag 1
TFLG2:  EQU     $1025   * Address Corresponsing to Timer Interrupt Flag 2


** PROGRAM - MAIN **
        RELATIVE
        ORG     $C100
BEGIN:  LDS     #$C0FF  * Set custom value for stack pointer S
        LDAA    LEDOUT  * Set DDRC bit pattern for output LEDs
LOOP:   JSR     LIGHTS  * Call Light Pattern subroutine
        BRA     LOOP
END:    SWI


** PROGRAM - SUBROUTINES **
LIGHTS: JSR     RISE    * Call Rise subroutine
        LDAA    G2R1    * Light R1 and G2
        STAA    PORTC
        LDAA    T1      * T1 -> A = Current Time Delay
        JSR     DELAY   * Call Delay subroutine
        JSR     POLL    * Call IC's Polling subroutine
        *
        JSR     RISE    * Call Rise subroutine
        LDAA    Y2R1    * Light R1 and Y2
        STAA    PORTC
        LDAA    T2      * T2 -> A = Current Time Delay
        JSR     DELAY   * Call Delay subroutine
        JSR     POLL    * Call IC's Polling subroutine
        *
        JSR     RISE    * Call Rise subroutine
        LDAA    R2R1    * Light R1 and R2
        STAA    PORTC
        LDAA    T2      * T2 -> A = Current Time Delay
        JSR     DELAY   * Call Delay subroutine
        JSR     POLL    * Call IC's Polling subroutine
        *
        JSR     RISE    * Call Rise subroutine
        LDAA    R2G1    * Light G1 and R2
        STAA    PORTC
        LDAA    T1      * T1 -> A = Current Time Delay
        JSR     DELAY   * Call Delay subroutine
        JSR     POLL    * Call IC's Polling subroutine
        *
        JSR     RISE    * Call Rise subroutine
        LDAA    R2Y1    * Light Y1 and R2
        STAA    PORTC
        LDAA    T2      * T2 -> A = Current Time Delay
        JSR     DELAY   * Call Delay subroutine
        JSR     POLL    * Call IC's Polling subroutine
        *
        JSR     RISE    * Call Rise subroutine
        LDAA    R2R1    * Light R1 and R2
        STAA    PORTC
        LDAA    T2      * T2 -> A = Current Time Delay
        JSR     DELAY   * Call Delay subroutine
        JSR     POLL    * Call IC's Polling subroutine
        RTS             * Go Back To MAIN

RISE:   JSR     RISE3   * Call IC2's Rise subroutine
        JSR     RISE2   * Call IC3's Rise subroutine

RISE3:  LDAA    TCTL2   * Select capture on rising edge for IC3
        ORAA    #%00000001
        ANDA    #%11111101
        STAA    TCTL2
        RTS

RISE2:  LDAA    TCTL2   * Select capture on rising edge for IC2
        ORAA    #%00000100
        ANDA    #%11110111
        STAA    TCTL2
        RTS

FALL:   JSR     FALL2   * Call IC2's Fall subroutine
        JSR     FALL3   * Call IC3's Fall subroutine
        RTS
        RTS

FALL3:  LDAA    TCTL2   * Select capture on falling edge for IC3
        ORAA    #%00000010
        ANDA    #%11111110
        STAA    TCTL2
        RTS

FALL2:  LDAA    TCTL2   * Select capture on falling edge for IC2
        ORAA    #%00001000
        ANDA    #%11111011
        STAA    TCTL2
        RTS

DELAY:  JSR     TOF     * Call Timer Overflow subroutine
        DECA            * Decrease number of TOFs
        BNE     DELAY   * Repeat until 0 TOFs
        RTS

TOF:    LDAB    TFLG2   * Polling for Timer Overflow
        BPL     TOF
        LDAB    #%10000000 * Clear Timer Overflow
        STAB    TFLG2
        RTS

POLL:   JSR     POLL3   * Call IC3's Polling subroutine
        JSR     POLL2   * Call IC2's Polling subroutine
        RTS

POLL3:  LDAA    TFLG1   * Polling for IC3
        ANDA    #%00000001 * Check for IC3F = 1
        BNE     STOP3
        STAA    TFLG1   * Clear IC3F
        JSR     INCDIS  * Call Increment and Display Count subroutine
STOP3:  RTS

POLL2:  LDAA    TFLG1   * Polling for IC2
        ANDA    #%00000010 * Check for IC2F = 1
        BNE     STOP2
        STAA    TFLG1   * Clear IC2F
        JSR     CLR     * Call Clear Count subroutine
STOP2:  RTS

INCDIS: LDAA    COUNT   * Increment Count
        INCA
        STAA    COUNT
        STAA    PORTB   * Display Count
        RTS

CLR:    CLRA    COUNT   * Clear Count
        STAA    COUNT
        RTS

