;   'MAGIC SQUARE'
; KEYS 1-9 ON THE HEX KEYBOARD ARE EACH ASSOCIATED WITH ON LED IN THE 3X3
; ARRAY. WHEN A KEY IS PRESSET, IT CHANGES THE PATTERN OF THE LIT LEDS IN THE
; ARRAY. THE OBJECT OF THE GAME IS TO CONVERT THE RANDOM PATTERN THE GAME
; STARTS WITH TO A SQUARE OF LIT LEDS BY PRESSING THE KEYS. THE LEDS WILL FLASH
; WHEN THE WINNING PATTERN IS ACHIEVED. KEY #0 CAN BE USED AT ANY TIME TO 
; RESTART THE GAME WITH A NEW PATTERN.
;
;
T1CL    .EQ     VIA1+4      ; LOW LATCH OF TIMER 1
DDR1A   .EQ     VIA1+3      ; PORT A DATA DIRECTION REGISTER
DDR1B   .EQ     VIA1+2      ; PORT B DATA DIRECTION REGISTER
PORT1A  .EQ     VIA1+1      ; PORT A
PORT2   .EQ     VIA1
PORT1B  .EQ     VIA1        ; PORT B
DDR3A   .EQ     VIA3+3      ; PORT A DATA DIRECTION REGISTER
DDR3B   .EQ     VIA3+2      ; PORT B DATA DIRECTION REGISTER
PORT3B  .EQ     VIA3        ; PORT B    
PORT3A  .EQ     VIA3+1      ; PORT A

ZP      .EQ     $00
TEMP    .EQ     ZP          ; TEMPORARY STORAGE    
;
; COMMENTS: THIS PROGRAM USES A TIMER REGISTER FOR A RANDOM NUMBER SOURCE. IF
; NONE IS AVAILABLE, A RANDOM NUMBER GENERATOR COULD BE USED, BUT DUE TO ITS
; REPEATABILITY, IT WOULD NOT WORK AS WELL. THIS PROGRAM USES THE REGISTERS OF
; VIA1 PORT A FOR THE STORAGE OF THE LED PATTERN. SINCE WHAT IS READ BY THE
; PROCESSOR IS THE POLARITY OF THE OUTPUT, AN EXCESSIVE LOAD ON THE LINES WOULD
; PREVENT THE PROGAM FROM WORKING CORRECTLY.
;
        LDA     #$FF        ; SET UP DATA DIRECTION REGISTERS
        STA     DDR1A
        STA     DDR1B
START   LDA     T1CL        ; GET 1ST RANDOM NUMBER
        STA     PORT1A
        LDA     T1CL        ; ... AND SECOND.
        AND     #01         ; MASK OUT BOTTOM ROW LEDS
        STA     PORT1B
KEY     JSR     GETKEY
        CMP     #0          ; KEY MUST BE 1-9: IS IT 0?
        BEQ     START       ; YES, RESTART GAME WITH NEW BOARD.
        CMP     #10         ; IS IT LESS THAN 10?
        BPL     KEY         ; + IF KEY >=10, SO GET ANOTHER.
;
; FOLLOWING SECTION USES KEY NUMBER AS INDEX TO FIND IN TABLE A BIT PATTERN 
; USED TO COMPLEMENT LEDS.
;
        SEC                 ; DECREMENT A FOR TABLE ACCESS
        SBC     #1
        ASL     A           ; MULTIPLY A*2 (EACH ENTRY IN TABLE IS 2 BYTES)
        TAX                 ; USE A AS INDEX
        LDA     PORT1A      ; GET PORT CONTENTS FOR COMPLEMENT
        EOR     TABLE,X     ; EOR PORT CONTENTS WITH PATTERN
        STA     PORT1A      ; RESTORE PORT1A
        LDA     PORT1B      ; DO SAME FOR PORT1B,
        EOR     TABLE+1,X   ; ... USING NEXT TABLE ENTRY.
        AND     #01         ; MASK OUT BOTTOM ROW LEDS
        STA     PORT1B      ; ... AND RESTORE.
;
; THIS SECTION CHECKS FOR WINNING PATTERN IN LEDS.
;
        LSR     A           ; SHIFT BIT 0 OF PORT 1 INTO CARRY.
        BCC     KEY         ; IF NOT WIN PATTERN, GET NEXT MOVE.
        LDA     PORT1A      ; LOAD PORT1A FOR WIN TEST
        CMP     #%11101111  ; CHECK FOR WIN PATTERN
        BNE     KEY         ; NO WIN, GET NEXT MOVE
;
; WIN - BLINK LEDS EVERY 1/2 SECOND, 4 TIMES
;
        LDA     #14
        STA     TEMP        ; LOAD NUMBER OF BLINKS
BLINK   LDX     #$20        ; DELAY CONSTANT FOR .08 SECOND
DELAY   LDY     #$FF        ; OUTER LOOP OF VARIABLE DELAY ROUTINE, WHOSE DELAY
                            ; IS 2556 * (CONTENTS OF X ON ENTER
DLY     NOP                 ; 10 MICROSECOND LOOP)
        BNE     DLY0
DLY0    DEY
        BNE     DLY
        DEX
        BNE     DELAY
        LDA     PORT1A      ; GET PORTS AND COMPLEMENT THEM
        EOR     #$FF
        STA     PORT1A
        LDA     PORT1B
        EOR     #1
        STA     PORT1B
        DEC     TEMP        ; COUNT DOWN NUMBER OF BLINKS
        BNE     BLINK       ; DO AGAIN IF NOT DONE
        BEQ     KEY
;
; TABLE OF CODES USED TO COMPLEMENT LEDS
;
TABLE   .HS     1B.00.07.36.00.49.00.BA.00.24.01.D8.00.C0.01.B0.01