;------------------------------------------------------------------------
;
;  TL_NL.ASM
;
;  Author: San Bergmans
;          www.sbprojects.com
;
;  Created for the 6802 Nano Computer at 15-11-1993
;
;  This program demonstrates the traffic lights on a normal intersection
;  The lights on the main road show green simultaneously, while the
;  lights from the side road show green in turn.
;
;------------------------------------------------------------------------

             .CR    6800                Select cross overlay
             .OR    $F000
             .TF    TL_NL.HEX,INT       Specify target file

;------------------------------------------------------------------------
;  Declaration of constants
;------------------------------------------------------------------------

TABLE_PNTR   .EQ    $00,01              Pointer in state table
LIGHTS_A1    .EQ    $02                 Status for lights on road A
LIGHTS_A2    .EQ    $03
LIGHTS_B1    .EQ    $04                 Status for lights on road B
LIGHTS_B2    .EQ    $05

PIA_A        .EQ    $0080               Pia data register A
PIA_B        .EQ    $0081               Pia data register B
CON_A        .EQ    $0082               Pia control register A
CON_B        .EQ    $0083               Pia control register B

DELAY        .EQ    12500               Delay counter for approx. 100ms.

;------------------------------------------------------------------------
;  Reset and initialisation
;------------------------------------------------------------------------

RESET        LDS    #$007F              Reset stack pointer
             LDAA   #%0000.0100         Initialise PIA ports
             STAA   CON_A
             STAA   CON_B
             LDAA   #%0011.1111         b0..b5 are outputs b6+b7 are
             STAA   PIA_A                inputs for both PIA ports
             STAA   PIA_B
             CLRB
             STAB   CON_A
             STAB   CON_B
             STAA   PIA_A
             STAA   PIA_B
             LDAA   #%0000.0100         Select data registers again
             STAA   CON_A
             STAA   CON_B

             LDX    #STATE_TABLE        Load start address of
             STX    TABLE_PNTR           state table

;------------------------------------------------------------------------
;  Main program loop
;------------------------------------------------------------------------

MAIN         LDX    TABLE_PNTR          Load state table pointer
             LDAB   0,X                 Get delay time from table
             BNE    .NOT_END            Not the end of the table yet!
             LDAA   1,X                 Restart the table (take reload
             STAA   TABLE_PNTR           value from the end of the table)
             LDAA   2,X
             STAA   TABLE_PNTR+1
             LDX    TABLE_PNTR
             LDAB   0,X                 Get new delay time again

.NOT_END     INX                        Read light patterns for Road A
             LDAA   0,X
             STAA   LIGHTS_A1           Keep light pattern in RAM
             INX
             LDAA   0,X
             STAA   LIGHTS_A2
             INX                        Read light patterns for Road B
             LDAA   0,X
             STAA   LIGHTS_B1           Keep light pattern in RAM
             INX
             LDAA   0,X
             STAA   LIGHTS_B2
             INX                        Save the incremented pointer
             STX    TABLE_PNTR
             BSR    WAIT                Show this pattern for a while
             BRA    MAIN                Repeat the main loop for ever

;------------------------------------------------------------------------
;  Wait until the delay is over (includes flashing of lights).
;------------------------------------------------------------------------

WAIT         BITB   #%0000.0100         What is the current flash state?
             BEQ    .LIGHTS_ON          State 1! Lights are always on!
             LDAA   LIGHTS_A2           Copy 2nd status to the outputs
             STAA   PIA_A
             LDAA   LIGHTS_B2
             STAA   PIA_B
             BRA    .DEC_WAIT

.LIGHTS_ON   LDAA   LIGHTS_A1           Copy 1st status to the outputs
             STAA   PIA_A
             LDAA   LIGHTS_B1
             STAA   PIA_B

.DEC_WAIT    BSR    DELAY_100MS         Delay about 100 milli seconds
             DECB                       Decrement total delay counter
             BNE    WAIT                Not 0 yet! Repeat delay loop
             RTS

;------------------------------------------------------------------------
;  Wait 0.1s routine
;------------------------------------------------------------------------

DELAY_100MS  LDX    #DELAY              Set delay counter and count down
.LOOP        DEX                         to 0
             BNE    .LOOP               Not 0 yet!
             RTS

;------------------------------------------------------------------------
;  State table
;
;  5 Bytes per record
;  Byte 1: Time of this state (x 0.1s)
;  Byte 2: b7 = not used
;          b6 = not used
;          b5 = red light A1 (0=light on)
;          b4 = yellow light A1
;          b3 = green licht A1
;          b2 = red light A0
;          b1 = yellow light A0
;          b0 = green light A0
;  Byte 3: same as byte 1, now for the off state during flashing
;  Byte 4: Same as Byte 2, but this time for road B
;  Byte 5: Same is Byte 3, but this time for road B
;
;  End of table if first record byte = 0. Then the next 2 bytes indicate
;  the loop address in the state table.
;------------------------------------------------------------------------

STATE_TABLE  ;      show during 10 seconds all yellow flashing lights
             .DA    #100                100x 0.1 seconds
             .DA    #%00.101.101        Road A Yellow flashing
             .DA    #%00.111.111
             .DA    #%00.101.101        Road B Yellow flashing
             .DA    #%00.111.111

             ;      show 5 seconds all red lights
             .DA    #50                 50x 0.1 seconds
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.011.011        Road B all Red
             .DA    #%00.011.011

.LOOP        ;      road A green, road B red, 8 seconds
             .DA    #80
             .DA    #%00.110.110        Road A all Green
             .DA    #%00.110.110
             .DA    #%00.011.011        Road B all Red
             .DA    #%00.011.011

             ;      road A yellow, road B red, 2.5 seconds
             .DA    #25
             .DA    #%00.101.101        Road A all Yellow
             .DA    #%00.101.101
             .DA    #%00.011.011        Road B all Red
             .DA    #%00.011.011

             ;      all roads red for 1 second
             .DA    #10
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.011.011        Road B all Red
             .DA    #%00.011.011

             ;      road A red, road B0 green, road B1 red, 8 seconds
             .DA    #80
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.011.110        Road B0 Green, B1 Red
             .DA    #%00.011.110

             ;      road A red, road B0 yellow, road B1 red, 2.5 seconds
             .DA    #25
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.011.101        Road B0 Yellow, B1 Red
             .DA    #%00.011.101

             ;      all roads red for 1 second
             .DA    #10
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.011.011        Road B all Red
             .DA    #%00.011.011

             ;      road A red, road B1 green, road B0 red, 8 seconds
             .DA    #80
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.110.011        Road B0 Red, B1 Green
             .DA    #%00.110.011

             ;      road A red, road B1 yellow, weg B0 red, 2.5 seconds
             .DA    #25
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.101.011        Road B0 Red, B1 Yellow
             .DA    #%00.101.011

             ;      all roads red for 1 second
             .DA    #10
             .DA    #%00.011.011        Road A all Red
             .DA    #%00.011.011
             .DA    #%00.011.011        Road B all Red
             .DA    #%00.011.011

             ;      end of table and next address in table to contionue
             .DA    #%00.000.000,.LOOP

;------------------------------------------------------------------------
;  Interrupt and reset vectors
;------------------------------------------------------------------------

             .NO    $FFF8,$FF
             .DA    RESET               IRQ (not used)
             .DA    RESET               SWI (not used)
             .DA    RESET               NMI (not used)
             .DA    RESET               Reset

             .LI    OFF
