0000-                  4       ;
0000-                  5       ; BINARY TRANSLATION GAME FOR THE BE6502 USING '6502 GAMES' HARDWARE. BOARD
0000-                  6       ; SHOULD BE JUMPERED WITH VIA CHIPS AT THE ADDRESSES SPECIFIED BELOW. CODE
0000-                  7       ; IS MOSTLY UNCHANGED FROM THE BOOK, LOOKUP TABLE 'NUMTAB' WAS MOVED OUT OF
0000-                  8       ; ZERO PAGE TO MAKE LOADING A BIT EASIER.
0000-                  9       ;
0000-                 10       ; NOTE THAT ALL OF THE PARTS BELONGING TO THE GAME BOARD ASSUMES A TIMING OF
0000-                 11       ; 1 MHZ, SO ANY MANUAL CLOCKING AND SLOW CLOCKS ARE NOT APPLICABLE.
0000-                 12       ;
4000-                 13       VIA1    .EQ     $4000       ; GAME BOARD (VIA #1)
4C00-                 14       VIA3    .EQ     $4C00       ; GAME BOARD (VIA #3)
0000-                 15
8000-                 16               .OR     $8000
8000-                 17               .TA     $0000
8000-                 18               .IN     ../../common/CH03-Translate/game.asm
8000-            I     1       ;   'TRANSLATE'
8000-            I     2       ; PROGRAM TO TEST 2 PLAYERS SPEED IN TRANSLATING A BINARY NUMBER TO A SINGLE
8000-            I     3       ; HEXADECIMAL DIGIT. EACH PLAYER IS GIVEN A TURN, AS SHOWN BY A LIGHTED LEFT
8000-            I     4       ; OR RIGHT POINTER. THE NUMBER WILL SUDDENLY FLASH ON LEDS 12-15, ACCOMPANIED
8000-            I     5       ; BY THE LIGHTING OF LED #10. THE PLAYER MUST THEN PUSH THE CORRESPONDING
8000-            I     6       ; BUTTON. AFTER BOTH PLAYERS TAKE TURNS, RESULTS ARE SHOWN ON BOTTOM ROW. AFTER
8000-            I     7       ; 10 WINS, A PLAYERS RESULT WILL FLASH, SHOWING THE BETTER PLAYER. THEN THE
8000-            I     8       ; GAME RESTARTS.
8000-            I     9       ;
4000-            I    10       PORT1B  .EQ     VIA1        ; LEDS 9-15
4001-            I    11       PORT1A  .EQ     VIA1+1      ; LEDS 1-8
4002-            I    12       DDR1B   .EQ     VIA1+2
4003-            I    13       DDR1A   .EQ     VIA1+3
4C00-            I    14       PORT3B  .EQ     VIA3        ; KEY # OUTPUT
4C01-            I    15       PORT3A  .EQ     VIA3+1      ; KEY STROBE INPUT.
4C02-            I    16       DDR3B   .EQ     VIA3+2
4C03-            I    17       DDR3A   .EQ     VIA3+3
8000-            I    18       ;
8000-            I    19       ; VARIABLE STORAGE:
8000-            I    20       ;
0000-            I    21       ZP      .EQ     $00
0000-            I    22       TEMP    .EQ     ZP          ; TEMPORARY STORAGE FOR AMOUNT OF TIME PLAYER
8000-            I    23                                   ;  USES TO GUESS.
0001-            I    24       CNTHI   .EQ     ZP+1
0002-            I    25       CNTLO   .EQ     ZP+2
0003-            I    26       CNT1H   .EQ     ZP+3        ; AMOUNT OF TIME PLYR1 USES TO GUESS.
0004-            I    27       CNT1L   .EQ     ZP+4
0005-            I    28       PLYR1   .EQ     ZP+5        ; SCORE OF # WON FOR PLAYER 1.
0006-            I    29       PLYR2   .EQ     ZP+6        ; PLAYER 2 SCORE.
0007-            I    30       NUMBER  .EQ     ZP+7        ; STORES NUMBER TO BE GUESSED.
0008-            I    31       SCR     .EQ     ZP+8        ; SCRATCHPAD FOR RND # GEN.
8000-            I    32       ;
8000-            I    33       ; MAIN PROGRAM
8000-            I    34       ;
8000-A9 FF       I    35 (  2) START   LDA     #$FF        : SET UP PORTS
8002-8D 03 40    I    36 (  4)         STA     DDR1A
8005-8D 02 40    I    37 (  4)         STA     DDR1B
8008-8D 02 4C    I    38 (  4)         STA     DDR3B
800B-A9 00       I    39 (  2)         LDA     #0
800D-8D 03 4C    I    40 (  4)         STA     DDR3A
8010-85 05       I    41 (  3)         STA     PLYR1       ; CLEAR NUMBER OF WINS.
8012-85 06       I    42 (  3)         STA     PLYR2
8014-A9 79       I    43 (  2) MOVE    LDA     #%01111001
8016-8D 01 40    I    44 (  4)         STA     PORT1A      ; SHOW RIGHT ARROW
8019-A9 00       I    45 (  2)         LDA     #0
801B-8D 00 40    I    46 (  4)         STA     PORT1B
801E-85 02       I    47 (  3)         STA     CNTLO       ; CLEAR COUNTERS
8020-85 01       I    48 (  3)         STA     CNTHI
8022-20 8C 80    I    49 (  6)         JSR     PLAY        ; GET PLAYER 1S TIME.
8025-A5 02       I    50 (  3)         LDA     CNTLO       ; TRANSFER TEMP COUNT TO PERMANENT STORAGE
8027-85 04       I    51 (  3)         STA     CNT1L
8029-A5 01       I    52 (  3)         LDA     CNTHI
802B-85 03       I    53 (  3)         STA     CNT1H
802D-A9 3C       I    54 (  2)         LDA     #%00111100  ; SHOW LEFT ARROW
802F-8D 01 40    I    55 (  4)         STA     PORT1A
8032-A9 01       I    56 (  2)         LDA     #1
8034-8D 00 40    I    57 (  4)         STA     PORT1B
8037-A9 00       I    58 (  2)         LDA     #0
8039-85 02       I    59 (  3)         STA     CNTLO       ; CLEAR COUNTERS
803B-85 01       I    60 (  3)         STA     CNTHI
803D-20 8C 80    I    61 (  6)         JSR     PLAY        ; GET PLAYER 2S TIME.
8040-A5 01       I    62 (  3)         LDA     CNTHI       ; GET PLAYER 2S COUNT AND...
8042-C5 03       I    63 (  3)         CMP     CNT1H       ;  COMPARE TO PLAYER 1S.
8044-F0 04       I    64 (2**)         BEQ     EQUAL       ; CHECK LOW ORDER BYTES TO RESOLVE WINNER.
8046-90 27       I    65 (2**)         BCC     PLR2        ; PLAYER 2 HAS SMALLER COUNT, SHOW IT.
8048-B0 08       I    66 (2**)         BCS     PLR1        ; PLAYER 1 HAS SMALLER COUNT, SHOW IT.
804A-A5 02       I    67 (  3) EQUAL   LDA     CNTLO       ; HI BYTES WERE EQUAL, SO CHECK LOW BYTES.
804C-C5 04       I    68 (  3)         CMP     CNT1L       ; COMPARE SCORES.
804E-90 1F       I    69 (2**)         BCC     PLR2        ; PLAYER 2 WINS, SHOW IT.
8050-B0 00       I    70 (2**)         BCS     PLR1        ; PLAYER 1 WINS, SHOW IT.
8052-A9 F0       I    71 (  2) PLR1    LDA     #%11110000  ; LIGTH RIGHT SIDE OF BOTTOM ROW
8054-8D 00 40    I    72 (  4)         STA     PORT1B      ; TO SHOW WIN.
8057-A9 00       I    73 (  2)         LDA     #0
8059-8D 01 40    I    74 (  4)         STA     PORT1A      ; CLEAR LOW LEDS.
805C-A9 40       I    75 (  2)         LDA     #$40        ; WAIT A WHILE TO SHOW WIN.
805E-20 E4 80    I    76 (  6)         JSR     DELAY
8061-E6 05       I    77 (  5)         INC     PLYR1       ; PLAYER 1 WINS ONE MORE...
8063-A9 0A       I    78 (  2)         LDA     #10         ; ... HAS HE WON 10?
8065-C5 05       I    79 (  3)         CMP     PLYR1
8067-D0 AB       I    80 (2**)         BNE     MOVE        ; IF NOT, PLAY ANOTHER ROUND
8069-A9 F0       I    81 (  2)         LDA     #%11110000  ; YES - GET BLINK PATTERN.
806B-20 CC 80    I    82 (  6)         JSR     BLINK       ; BLINK WINNING SIDE.
806E-60          I    83 (  6)         RTS                 ; END OF GAME: RETURN TO MONITOR.
806F-A9 0E       I    84 (  2) PLR2    LDA     #%1110      ; LIGHT LEFT SIDE OF BOTTOM.
8071-8D 00 40    I    85 (  4)         STA     PORT1B
8074-A9 00       I    86 (  2)         LDA     #0
8076-8D 01 40    I    87 (  4)         STA     PORT1A      ; CLEAR LOW LEDS.
8079-A9 40       I    88 (  2)         LDA     #$40        ; WAIT A WHILE TO SHOW WIN.
807B-20 E4 80    I    89 (  6)         JSR     DELAY
807E-E6 06       I    90 (  5)         INC     PLYR2       ; PLAYER 2 HAS WON ANOTHER ROUND...
8080-A9 0A       I    91 (  2)         LDA     #10         ; ... HAS HE WON 10?
8082-C5 06       I    92 (  3)         CMP     PLYR2
8084-D0 8E       I    93 (2**)         BNE     MOVE        ; IF NOT, PLAY ANOTHER ROUND.
8086-A9 0E       I    94 (  2)         LDA     #%1110      ; YES - GET PATTERN TO BLINK LEDS.
8088-20 CC 80    I    95 (  6)         JSR     BLINK       ; BLINK THEM
808B-60          I    96 (  6)         RTS                 ; END.
808C-            I    97       ;
808C-            I    98       ; SUBROUTINE 'PLAY'
808C-            I    99       ; GETS TIME COUNT OF EACH PLAYER, AND IF BAD GUESSES ARE MADE, THE PLAYER IS
808C-            I   100       ; GIVEN ANOTHER CHANCE. THE NEW TIME ADDED TO THE OLD.
808C-            I   101       ;
808C-20 F5 80    I   102 (  6) PLAY    JSR     RANDOM      ; GET RANDOM NUMBER.
808F-20 E4 80    I   103 (  6)         JSR     DELAY       ; RANDOM - LENGTH DELAY.
8092-20 F5 80    I   104 (  6)         JSR     RANDOM      ; GET ANOTHER
8095-29 0F       I   105 (  2)         AND     #$0F        ; KEEP UNDER 16 FOR USE AS
8097-85 07       I   106 (  3)         STA     NUMBER      ;  NUMBER TO GUESS.
8099-AA          I   107 (  2)         TAX                 ; USE AS INDEX TO ...
809A-BD 08 81    I   108 ( 4*)         LDA     NUMTAB,X    ;  GET REVERSED PATTERN FROM TABLE
809D-0D 00 40    I   109 (  4)         ORA     PORT1B      ;  TO DISPLAY IN LEDS 12-15.
80A0-8D 00 40    I   110 (  4)         STA     PORT1B
80A3-20 B6 80    I   111 (  6)         JSR     CNTSUB      ; GET KEYSTROKE 1 DURATION COUNT.
80A6-C4 07       I   112 (  3)         CPY     NUMBER      ; IS KEYSTROKE CORRECT GUESS?
80A8-F0 0B       I   113 (2**)         BEQ     DONE        ; IF SO, DONE.
80AA-A9 01       I   114 (  2)         LDA     #01         ; NO: CLEAR OLD GUESS FROM LEDS.
80AC-2D 00 40    I   115 (  4)         AND     PORT1B
80AF-8D 00 40    I   116 (  4)         STA     PORT1B
80B2-4C 8C 80    I   117 (  3)         JMP     PLAY        ; TRY AGAIN W/ANOTHER NUMBER.
80B5-60          I   118 (  6) DONE    RTS                 ; RETURN WITH DURATION IN CNTLO+CNTHI
80B6-            I   119       ;
80B6-            I   120       ; SUBROUTINE 'COUNTER'
80B6-            I   121       ; GETS KEYSTROKE WHILE KEEPING TRACK OF AMOUNT OF TIME BEFORE KEYPRESS.
80B6-            I   122       ;
80B6-A0 0F       I   123 (  2) CNTSUB  LDY     #$0F        ; SET UP KEY # COUNTER.
80B8-8C 00 4C    I   124 (  4) KEYLP   STY     PORT3B      ; OUTPUT KEY # TO KEYBOARD MPXR
80BB-2C 01 4C    I   125 (  4)         BIT     PORT3A      ; KEY DOWN?
80BE-10 0B       I   126 (2**)         BPL     FINISH      ; IF YES, DONE.
80C0-88          I   127 (  2)         DEY                 ; COUNT DOWN KEY #.
80C1-10 F5       I   128 (2**)         BPL     KEYLP       ; TRY NEXT KEY.
80C3-E6 02       I   129 (  5)         INC     CNTLO       ; ALL KEYS TRIED, INCREMENT COUNT.
80C5-D0 EF       I   130 (2**)         BNE     CNTSUB      ; TRY KEYS AGAIN IF NO OVERFLOW.
80C7-E6 01       I   131 (  5)         INC     CNTHI       ; OVERFLOW, INCREMENT HIGH BYTE.
80C9-D0 EB       I   132 (2**)         BNE     CNTSUB      ; TRY KEYS AGAIN.
80CB-60          I   133 (  6) FINISH  RTS                 ; DONE: TIME RAN OUT OR KEY PRESSED.
80CC-            I   134       ;
80CC-            I   135       ; SUBROUTINE 'BLINK'
80CC-            I   136       ; BLINKS LEDS WHOSE BITS ARE SET IN ACCUMULATOR ON ENTRY.
80CC-            I   137       ;
80CC-A2 14       I   138 (  2) BLINK   LDX     #20         ; 20 BLINKS.
80CE-86 01       I   139 (  3)         STX     CNTHI       ; SET BLINK COUNTER.
80D0-85 02       I   140 (  3)         STA     CNTLO       ; BLINK REGISTER
80D2-A5 02       I   141 (  3) BLOOP   LDA     CNTLO       ; GET BLINK PATTERN
80D4-4D 00 40    I   142 (  4)         EOR     PORT1B      ; BLINK LEDS.
80D7-8D 00 40    I   143 (  4)         STA     PORT1B
80DA-A9 0A       I   144 (  2)         LDA     #10         ; SHORT DELAY
80DC-20 E4 80    I   145 (  6)         JSR     DELAY
80DF-C6 01       I   146 (  5)         DEC     CNTHI
80E1-D0 EF       I   147 (2**)         BNE     BLOOP       ; LOOP IF NOT DONE.
80E3-60          I   148 (  6)         RTS
80E4-            I   149       ;
80E4-            I   150       ; SUBROUTINE 'DELAY'
80E4-            I   151       ; CONTENTS OF ACCUMULATOR DETERMINES DELAY LENGTH.
80E4-            I   152       ;
80E4-85 00       I   153 (  3) DELAY   STA     TEMP
80E6-A0 10       I   154 (  2) DL1     LDY     #$10
80E8-A2 FF       I   155 (  2) DL2     LDX     #$FF
80EA-CA          I   156 (  2) DL3     DEX
80EB-D0 FD       I   157 (2**)         BNE     DL3
80ED-88          I   158 (  2)         DEY
80EE-D0 F8       I   159 (2**)         BNE     DL2
80F0-C6 00       I   160 (  5)         DEC     TEMP
80F2-D0 F2       I   161 (2**)         BNE     DL1
80F4-60          I   162 (  6)         RTS
80F5-            I   163       ;
80F5-            I   164       ; SUBROUTINE 'RANDOM'
80F5-            I   165       ; RANDOM NUMBER GENERATOR, RETURNS RANDOM NUMBER IN ACCUMULATOR.
80F5-            I   166       ;
80F5-38          I   167 (  2) RANDOM  SEC
80F6-A5 09       I   168 (  3)         LDA     SCR+1
80F8-65 0C       I   169 (  3)         ADC     SCR+4
80FA-65 0D       I   170 (  3)         ADC     SCR+5
80FC-85 08       I   171 (  3)         STA     SCR
80FE-A2 04       I   172 (  2)         LDX     #4
8100-B5 08       I   173 (  4) RNDLP   LDA     SCR,X
8102-95 09       I   174 (  4)         STA     SCR+1,X
8104-CA          I   175 (  2)         DEX
8105-10 F9       I   176 (2**)         BPL     RNDLP
8107-60          I   177 (  6)         RTS
8108-            I   178       ;
8108-            I   179       ; TABLE OF 'REVERSED' NUMBERS FOR DISPLAY
8108-            I   180       ; IN BITS 3-8 OF PORT1B, OR LEDS 12-15.
8108-            I   181       ;
8108-02          I   182       NUMTAB  .DA     #%00000010
8109-82          I   183               .DA     #%10000010
810A-22          I   184               .DA     #%00100010
810B-A2          I   185               .DA     #%10100010
810C-12          I   186               .DA     #%00010010
810D-92          I   187               .DA     #%10010010
810E-32          I   188               .DA     #%00110010
810F-B2          I   189               .DA     #%10110010
8110-0A          I   190               .DA     #%00001010
8111-8A          I   191               .DA     #%10001010
8112-2A          I   192               .DA     #%00101010
8113-AA          I   193               .DA     #%10101010
8114-1A          I   194               .DA     #%00011010
8115-9A          I   195               .DA     #%10011010
8116-3A          I   196               .DA     #%00111010
8117-BA          I   197               .DA     #%10111010
8118-                 19       ;
8118-                 20       ; STORE CPU INITIALIZATION VECTORS AT THE END OF THE EEPROM.
8118-                 21       ;
FFFA                 22               .NO     $FFFA,$FF
FFFA-00 80            23               .DA     START       ; NMI VECTOR
FFFC-00 80            24               .DA     START       ; RESET VECTOR
FFFE-00 80            25               .DA     START       ; IRQ VECTOR
