* Nick Shin                                             7/31/95
* Joe Daniels                                           Final Project

*                       Elevator Controller



* Define Parallel Port Names as Actual Port Addresses

PORTA   EQU     $1000
PORTB   EQU     $1004
PORTC   EQU     $1003
PORTCD  EQU     $1007
PORTD   EQU     $1008
PORTDD  EQU     $1009
PORTE   EQU     $100A


* Define Registers Needed for Serial Communications

SCDR    EQU     $102F
SCSR    EQU     $102E
SCCR1   EQU     $102C
SCCR2   EQU     $102D
BAUD    EQU     $102B


* Initalize Microcontroller

     ORG     $D000           ; Start program at address D000
     LDS     #$F000          ; Set top of stack

     LDAA    #%00110000      ; Set BAUD rate at 9600
     STAA    BAUD

     LDAA    #$08            ; Enable Serial Transmit
     STAA    SCCR2

     LDAA    #%00000000      ; Setting up port C as input ports
     STAA    PORTCD
     LDAA    #%11111111      ; Setting up port D as output ports
     STAA    PORTDD


* Initalize the chips on the Proto-Board

     LDAA    #$2A            ; This is just for debugging purposes
     JSR     TRANSMIT        ;
     JSR     TRANSMIT        ;
     JSR     PRINT           ;

     LDAA    #%00000000      ; Clearing everything
     STAA    PORTA
     STAA    PORTB

     LDAB    #%00000011      ; Giving the chips time to be "cleared"
     JSR     BIGDELAY

     LDAA    #%01111000      ; Initalizing "Floor" chips
     STAA    PORTA
     LDAA    #%00011011      ; Initalizing "Counter" chips
     STAA    PORTB

     LDAB    #%00000011      ; Giving the chips time to be "set"
     JSR     BIGDELAY


*           Start of elevator controller program
*      ----------------------------------------------
*      ----------       MAIN PROGRAM       ----------
*      ----------------------------------------------

NORMAL

     LDAB    #%00010000      ; This is just for debugging purposes
NORM1                        ;
     LDAA    #$2A            ;
     JSR     TRANSMIT        ;
     DECB                    ;
     BEQ     NORM2           ;
     JMP     NORM1           ;
NORM2                        ;
     JSR     CRLF            ;

     LDAA    PORTC           ; Getting direction before ORing
     ANDA    #%00010011      ;    and setting PandT = 1
     STAA    PORTB

     LDAA    PORTA           ; Check what floor the elev is on
     ANDA    #%00000011      ;    by getting Qb and Qa
     CMPA    #%00000000      ; Checking if floor1
     BEQ     FLR1
     CMPA    #%00000001      ; Checking if floor2
     BEQ     FLR2
     CMPA    #%00000010      ; Checking if floor3
     BEQ     FLR3_0
     CMPA    #%00000011      ; Checking if floor4
     BEQ     FLR4_0
     JMP     NORMAL


*      ----------------------------------------------
*      ----------     FLOOR 1 ROUTINES     ----------
*      ----------------------------------------------

FLR1

     LDAA    #$41            ; Debugging spot
     JSR     PRINT           ;

     LDAB    #%11111111      ; Giving time (so it can be seen on the HEX disp)
     JSR     BIGDELAY2       ;    for the elevator to move

     LDAA    PORTC
     ANDA    #%00000001      ; Checking if floor1 is choosen --
     CMPA    #%00000001      ;    if so, then open the doors
     BNE     FLR1RET

FLR1OPEN
     LDAB    #%00000011      ; A delay feature to adjust to the quick M64HC11
     JSR     BIGDELAY

     LDAA    #%01110000      ; Turning off ELEV floor1 and floor1 UP LED
     STAA    PORTA

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%01111000      ; Reactivating all ELEV LED lites
     STAA    PORTA
     JMP     OPEN

FLR1RET
     LDAA    #$59            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC
     ANDA    #%00001110      ; Checking for upper floor requests
     CMPA    #%00000000
     BEQ     FLR1RET2
     JMP     FLOORUP         ; Jump to Move Elev Up Subroutine

FLR1RET2
     LDAA    PORTE
     ANDA    #%00111110      ; Checking for upper floor ARROW requests
     CMPA    #%00000000
     BEQ     NORMAL_0
     JMP     FLOORUP         ; Jump to Move Elev Up Subroutine


NORMAL_0     JMP   NORMAL    ; Jump extension
FLR3_0       JMP   FLR3      ; Jump extension
FLR4_0       JMP   FLR4      ; Jump extension


*      ----------------------------------------------
*      ----------     FLOOR 2 ROUTINES     ----------
*      ----------------------------------------------

FLR2

     LDAA    #$42            ; Debugging spot
     JSR     PRINT           ;

     LDAB    #%11111111      ; Giving time (so it can be seen on the HEX disp)
     JSR     BIGDELAY2       ;    for the elevator to move

     LDAA    PORTC
     ANDA    #%00010000      ; Checking the last known elev direction
     CMPA    #%00010000
     BNE     FLR2DWN

FLR2UP
     LDAA    PORTE
     ANDA    #%00000010      ; Checking if floor2 UP is choosen
     CMPA    #%00000010
     BNE     FLR2NXT

     LDAA    #%00001110      ; Clearing floor2 UP signal
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%00001111      ; Reactivating all ARROW LED lites
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     JMP     FLR2OPEN

FLR2DWN
     LDAA    PORTE
     ANDA    #%00000010      ; Checking if floor2 DOWN is choosen
     CMPA    #%00000010
     BNE     FLR2NXT

     LDAA    #%00001101      ; Clearing floor2 DOWN signal
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%00001111      ; Reactivating all ARROW LED lites
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     JMP     FLR2OPEN

FLR2NXT
     LDAA    PORTC
     ANDA    #%00000010      ; Checking if floor2 is choosen
     CMPA    #%00000010
     BNE     FLR2RET

FLR2OPEN
     LDAB    #%00000011      ; A delay feature to adjust to the quick M64HC11
     JSR     BIGDELAY

     LDAA    #%01101000      ; Turning off elevator floor2 LED lite
     STAA    PORTA

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%01111000      ; Reactivating the LED lites
     STAA    PORTA
     JMP     OPEN            ; Jump to Open Door Subroutine

FLR2RET
     LDAA    #$5A            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC
     ANDA    #%00001101      ; Checking for other floor requests
     CMPA    #%00000000
     BEQ     FLR2RET2
     JMP     SUBFLR2

FLR2RET2
     LDAA    PORTE
     ANDA    #%00110101      ; Checking for other floor ARROW requests
     CMPA    #%00000000
     BEQ     NORMAL_1

SUBFLR2
     LDAA    PORTC
     ANDA    #%00010000      ; Checking the last known elev direction
     CMPA    #%00010000
     BNE     SUBFLR2U0

SUBFLR2U1
     LDAA    PORTC
     ANDA    #%00001100      ; Checking for 3rd & 4th floor requests
     CMPA    #%00000000
     BEQ     FLOORDOWN_0     ; Jump to Move Elev Down Subroutine
     JMP     FLOORUP         ; Jump to Move Elev Up Subroutine

SUBFLR2U0
     LDAA    PORTC
     ANDA    #%00000001      ; Checking for 1st floor requests
     CMPA    #%00000000
     BEQ     FLOORUP_0       ; Jump to Move Elev Up Subroutine
     JMP     FLOORDOWN       ; Jump to Move Elev Down Subroutine


FLOORUP_0    JMP  FLOORUP    ; Jump extension
FLOORDOWN_0  JMP  FLOORDOWN  ; Jump extension
NORMAL_1     JMP  NORMAL     ; Jump extension


*      ----------------------------------------------
*      ----------     FLOOR 3 ROUTINES     ----------
*      ----------------------------------------------

FLR3

     LDAA    #$43            ; Bebugging spot
     JSR     PRINT           ;

     LDAB    #%11111111      ; Giving time (so it can be seen on the HEX disp)
     JSR     BIGDELAY2       ;    for the elevator to move

     LDAA    PORTC
     ANDA    #%00010000      ; Checking the last known elev direction
     CMPA    #%00010000
     BNE     FLR3DWN

FLR3UP
     LDAA    PORTE
     ANDA    #%00000010      ; Checking if floor3 UP is choosen
     CMPA    #%00000010
     BNE     FLR3NXT

     LDAA    #%00001110      ; Clearing floor3 UP signal
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%00001111      ; Reactivating all ARROW LED lites
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     JMP     FLR3OPEN

FLR3DWN
     LDAA    PORTE
     ANDA    #%00000010      ; Checking if floor3 DOWN is choosen
     CMPA    #%00000010
     BNE     FLR3NXT

     LDAA    #%00001101      ; Clearing floor3 DOWN signal
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%00001111      ; Reactivating all ARROW LED lites
     STAA    PORTD

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     JMP     FLR3OPEN

FLR3NXT
     LDAA    PORTC
     ANDA    #%00000100      ; Checking if floor3 is choosen
     CMPA    #%00000100
     BNE     FLR3RET

FLR3OPEN
     LDAB    #%00000011      ; A delay feature to adjust to the quick M64HC11
     JSR     BIGDELAY

     LDAA    #%01011000      ; Turning off elevator floor3 LED lite
     STAA    PORTA

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%01111000      ; Reactivating the LED lites
     STAA    PORTA
     JMP     OPEN            ; Jump to Open Door Subroutine

FLR3RET
     LDAA    #$44            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC
     ANDA    #%00001011      ; Checking for other floor requests
     CMPA    #%00000000
     BEQ     FLR3RET2
     JMP     SUBFLR3

FLR3RET2
     LDAA    PORTE
     ANDA    #%00101011      ; Checking for other floor ARROW requests
     CMPA    #%00000000
     BEQ     NORMAL_2

SUBFLR3
     LDAA    PORTC
     ANDA    #%00010000      ; Checking the last known elev direction
     CMPA    #%00010000
     BNE     SUBFLR3U0

SUBFLR3U1
     LDAA    PORTC
     ANDA    #%00001000      ; Checking for 4th floor requests
     CMPA    #%00000000
     BEQ     FLOORDOWN_1     ; Jump to Move Elev Down Subroutine
     JMP     FLOORUP         ; Jump to Move Elev Up Subroutine

SUBFLR3U0
     LDAA    PORTC
     ANDA    #%00000011      ; Checking for 1st & 2nd floor requests
     CMPA    #%00000000
     BEQ     FLOORUP_1       ; Jump to Move Elev Up Subroutine
     JMP     FLOORDOWN       ; Jump to Move Elev Down Subroutine


FLOORUP_1    JMP  FLOORUP    ; Jump extension
FLOORDOWN_1  JMP  FLOORDOWN  ; Jump extension
NORMAL_2     JMP  NORMAL     ; Jump extension


*      ----------------------------------------------
*      ----------     FLOOR 4 ROUTINES     ----------
*      ----------------------------------------------

FLR4

     LDAA    #$45            ; Debugging spot
     JSR     PRINT           ;

     LDAB    #%11111111      ; Giving time (so it can be seen on the HEX disp)
     JSR     BIGDELAY2       ;    for the elevator to move

     LDAA    PORTC
     ANDA    #%00001000      ; Checking if floor4 is choosen
     CMPA    #%00001000
     BNE     FLR4RET

FLR4OPEN
     LDAB    #%00000011      ; A delay feature to adjust to the quick M64HC11
     JSR     BIGDELAY

     LDAA    #%00111000      ; Turning off elevator floor4 LED lite
     STAA    PORTA

     LDAB    #%00000011      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%01111000      ; Reactivating the LED lites
     STAA    PORTA
     JMP     OPEN            ; Jump to Open Door Subroutine

FLR4RET
     LDAA    #$46            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC
     ANDA    #%00000111      ; Checking for lower floor requests
     CMPA    #%00000000
     BEQ     FLR4RET2
     JMP     FLOORDOWN       ; Jump to Move Elev DOWN Subroutine

FLR4RET2
     LDAA    PORTE
     ANDA    #%00011111      ; Checking for lower floor ARROW requests
     CMPA    #%00000000
     BEQ     NORMAL_2
     JMP     FLOORDOWN       ; Jump to Move Elev DOWN Subroutine



* -----------------------------------------------------
* ----------    OPEN DOOR ROUTINE     -----------------
*
*  This routine will set the OPEN signal to low
*  and then high (to toggle the counter) indicating
*  the beginning sequence to open the Elev doors.
*
*  After the Closing signal is detected (from another
*  counter) then the Elev doors will be closed.
*
* -----------------------------------------------------

OPEN

     LDAA    #$47            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC           ; Getting direction before ORing
     ANDA    #%11011111      ;    Setting OPENsignal = 0
     STAA    PORTB

     LDAB    #%00001111      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     ORAA    #%00100000      ;    Setting OPENsignal = 1
     STAA    PORTB

     LDAB    #%00001111      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

OPEN1
     LDAA    #$48            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC
     ANDA    #%01000000      ; Checking CLOSINGsignal
     CMPA    #%01000000
     BNE     OPEN1

OPEN2
     LDAA    #$49            ; Debugging spot
     JSR     PRINT           ;

     LDAA    PORTC
     ANDA    #%00100000      ; Checking CLOSEDsignal
     CMPA    #%00100000
     BNE     OPEN2
     JMP     NORMAL_0


* -----------------------------------------------------
* ----------  ELEVATOR MOVEMENT ROUTINES     ----------
*
*  These routines will move the elevator.  First by
*  checking the direction of the elevator the elevator
*  was just in, and then moving the elevator in that
*  direction (by forcing a clock pulse to the Elevator
*  Floor Register/Counter).
*
* -----------------------------------------------------

FLOORUP

     LDAA    #$50            ; Debugging spot
     JSR     PRINT           ;

     LDAA    #%00011001      ; Setting PandT = 0, elev dir = 1, & CLR/D = 0
     STAA    PORTB

     LDAB    #%00000100      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%00011011      ; Now setting CLR/D = 1
     STAA    PORTB

     LDAB    #%00000100      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     JMP     NORMAL_0

FLOORDOWN

     LDAA    #$51            ; Debuging spot
     JSR     PRINT           ;

     LDAA    #%00001001      ; Setting PandT = 0, elev dir = 0, & CLR/D = 0
     STAA    PORTB

     LDAB    #%00000100      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     LDAA    #%00001011      ; Now setting CLR/D = 1
     STAA    PORTB

     LDAB    #%00000100      ; A delay feature to adjust to the slow response
     JSR     BIGDELAY        ;    of the chips

     JMP     NORMAL_0


* -------------------------------------------------------
* ----------  HELPER - DEBUGGING SECTION     ------------
*
*  Debugging Subroutines using the HP2392 Display by
*  printing out ASCII characters.
*
*  Also, the chips on the Proto-Board are very slow
*  compared to the speed of the M68HC11 microcontroller.
*  So included in this helper section is the delay
*  subroutines used in this program.
*
* -------------------------------------------------------

*      ----------------------------------------------
*      ----------   DEBUGGING - PRINTOUT   ----------
*      ----------------------------------------------

PRINT
     JSR     TRANSMIT        ; The compressed calling function
     JSR     CRLF
     RTS

CRLF LDAA    #$0A            ; Send carriage return
     JSR     TRANSMIT
     LDAA    #$0D            ; Send line feed
     JSR     TRANSMIT
     RTS

TRANSMIT
     STAA    SCDR            ; Put character into transmit register
COMPLETE
     LDAA    SCSR
     ANDA    #%40
     BEQ     COMPLETE        ; Loop until transmition is complete
     RTS                     ; Return from transmit Queue

*      ----------------------------------------------
*      ----------    DELAY SUBROUTINES     ----------
*      ----------------------------------------------

DELAY                        ; A little loop to kill time
     LDAB    #%01111111
DEL1 DECB
     BEQ     DEL2
     JMP     DEL1
DEL2 RTS

BIGDELAY                     ; A loop to run the little loop
     PSHB                    ;    to kill even more time
     JSR     DELAY           ; This subroutine uses the value of accum B
     JSR     DELAY           ;    which is passed in by the user before
     PULB                    ;    calling this subroutine
     DECB
     BEQ     BIGDEL1
     JMP     BIGDELAY
BIGDEL1
     RTS

BIGDELAY2                    ; An even bigger loop
     PSHB
     LDAB    #%00000100
     JSR     BIGDELAY
     PULB
     DECB
     BEQ     BIGDEL2
     JMP     BIGDELAY2
BIGDEL2
     RTS

