HomeArticlesProjectsBlogContact
Articles
Tic Tac Toe Page 3
Colin Mitchell
Colin Mitchell
Make sure to subscribe to our newsletter and be the first to know the news.

THE PROGRAM

Page 3

Go to: Page 1 Page 2 Page 4


Here is the program for TIC TAC TOE. Take it slowly and concentrate on one sub-routine at a time.
The layout of the program is as follows:
SetUp - sets up the ports, disables the comparators and clears the files.
Tables - tables of values for the attract mode
Att - Attract Mode. Produces the patterns on the display when the project is first turned on.
Raster - Puts anything on the 3x3 display - depending on the values you provide in a Table. 3-values are needed to produce a “cell.” In our case, Raster produces a “dot” moving around the display and uses Table 4.

The .asm and .hex files for Tic Tac Toe

    ;TicTacToe.asm
    ;Project: TIC TAC TOE
    ; use internal 4MHz oscillator
LIST P=16F628 ;f=inhx8m
#include "P16F628.inc"
__CONFIG _CP_OFF &_BODEN_OFF &_PWRTE_OFF &_WDT_OFF &_LVP_OFF &_MCLRE_OFF &_INTRC_OSC_NOCLKOUT
         ORG 0             ;This is the start of memory for the program.
SetUp    MOVLW 07          ;Disable the comparators
         MOVWF 1Fh         ;File 1F is the CMCON file
         BSF 03,5          ;Go to Bank 1
         CLRF 06           ;Make all port B output
         MOVLW 18h         ;Load W with 0001 1000
         MOVWF 05          ;Make RA0,1,2,6,7 output and RA3, RA4 input
         BCF 03,5          ;Go to Bank 0 - the program memory area.
         CLRF 2F           ;Clear the button-press file
         CLRF 05           ;Clear the output lines of PortA
         CLRF 06           ;Blank the output
         GOTO Att

Table1   ADDWF 02,1
         RETLW 31h
         RETLW 32h
         RETLW 33h
         RETLW 34h
         RETLW 35h
         RETLW 36h
         RETLW 37h
         RETLW 38h
         RETLW 39h
         RETLW 31h
         RETLW 34h
         RETLW 37h
         RETLW 32h
         RETLW 35h
         RETLW 38h
         RETLW 33h
         RETLW 36h
         RETLW 39h
         RETLW 31h
         RETLW 35h
         RETLW 39h
         RETLW 37h
         RETLW 35h
         RETLW 33h
         RETLW 31h
         RETLW 32h
         RETLW 33h
         RETLW 0FFh

Table2   ADDWF 02,1
         RETLW 31h
         RETLW 31h
         RETLW 33h
         RETLW 37h
         RETLW 39h
         RETLW 31h
         RETLW 33h
         RETLW 37h
         RETLW 39h

Table3   ADDWF 02,1
         RETLW 32h
         RETLW 32h
         RETLW 34h
         RETLW 36h
         RETLW 38h
         RETLW 32h
         RETLW 34h
         RETLW 36h
         RETLW 38h
         RETLW 32h
         RETLW 32h
         RETLW 34h
         RETLW 36h
         RETLW 38h
         RETLW 32h
         RETLW 34h
         RETLW 36h
         RETLW 38h


Table4   ADDWF 02,1
         RETLW 20h
         RETLW 00h
         RETLW 00h
         RETLW 08h
         RETLW 00h
         RETLW 00h
         RETLW 02h
         RETLW 00h
         RETLW 00h
         RETLW 00h
         RETLW 02h
         RETLW 00h
         RETLW 00h
         RETLW 00h
         RETLW 02h
         RETLW 00h
         RETLW 00h
         RETLW 08h
         RETLW 00h
         RETLW 00h
         RETLW 20h
         RETLW 00h
         RETLW 20h
         RETLW 00h
         RETLW 25h
         RETLW 15h
         RETLW 15h
         RETLW 19h
         RETLW 15h
         RETLW 15h
         RETLW 16h
         RETLW 15h
         RETLW 15h
         RETLW 15h
         RETLW 16h
         RETLW 15h
         RETLW 15h
         RETLW 15h
         RETLW 16h
         RETLW 15h
         RETLW 15h
         RETLW 19h
         RETLW 15h
         RETLW 15h
         RETLW 25h
         RETLW 15h
         RETLW 25h
         RETLW 15h
         RETLW 0FFh
         RETLW 0FFh


Table5   ADDWF 02,1
         RETLW 31h
         RETLW 33h
         RETLW 37h
         RETLW 39h
         RETLW 31h
         RETLW 33h
         RETLW 37h
         RETLW 39h
         RETLW 31h
         RETLW 32h
         RETLW 33h
         RETLW 34h
         RETLW 36h
         RETLW 37h
         RETLW 38h
         RETLW 39h
         RETLW 0FFh


                           ;ATTRACT mode comes ON when the project is
                           ;    switched on.
                           ;Push either button to get to TIC TAC TOE mode.

Att      MOVLW 10h
         MOVWF 22h         ;File to be decremented
Att1     MOVLW 15
         MOVWF 06
         MOVLW 20h
         MOVWF 23h
Att2     BSF 05,0
         CALL Del2
         BCF 05,0
         BSF 05,2
         CALL Del2
         BCF 05,2
         BSF 05,1
         CALL Del2
         BCF 05,1
         DECFSZ 23h,1
         GOTO Att2

         MOVLW 0FFh
         MOVWF 06
         MOVLW 20h
         MOVWF 23h
Att3     BSF 05,0
         CALL Del2
         BCF 05,0
         BSF 05,2
         CALL Del2
         BCF 05,2
         BSF 05,1
         CALL Del2
         BCF 05,1
         DECFSZ 23h,1
         GOTO Att3

         MOVLW 2A
         MOVWF 06
         MOVLW 20h
         MOVWF 23h
Att4     BSF 05,0
         CALL Del2
         BCF 05,0
         BSF 05,2
         CALL Del2
         BCF 05,2
         BSF 05,1
         CALL Del2
         BCF 05,1
         DECFSZ 23h,1
         GOTO Att4
         DECFSZ 22h,1
         GOTO Att1

         MOVLW 04          ;Create 4 cycles of green-to-red-to-green
         MOVWF 22h
         CLRF 20h
         CLRF 21h
Att5     INCF 21h,1
         MOVLW 08h
         MOVWF 27h
Att6     MOVLW 04
         MOVWF 06
         BSF 05,2
         MOVF 20h,0
         MOVWF 25h
Att7     DECFSZ 25h,1
         GOTO Att7
         MOVLW 08
         MOVWF 06
         BSF 05,2
         MOVF 21h,0
         MOVWF 26h
Att8     DECFSZ 26h,1
         GOTO Att8
         DECFSZ 27h
         GOTO Att6
         BTFSS 05,3        ;Test for ButtonA
         GOTO Preload
         BTFSS 05,4        ;Test for ButtonB
         GOTO Preload
         DECFSZ 20h,1
         GOTO Att5

         CLRF 20h
         CLRF 21h
Att9     INCF 20h,1
         MOVLW 08h
         MOVWF 27h
Att10    MOVLW 04
         MOVWF 06
         BSF 05,2
         MOVF 20h,0
         MOVWF 25h
Att11    DECFSZ 25h,1
         GOTO Att11
         MOVLW 08
         MOVWF 06
         BSF 05,2
         MOVF 21h,0
         MOVWF 26h
Att12    DECFSZ 26h,1
         GOTO Att12
         DECFSZ 27h
         GOTO Att10
         BTFSS 05,3        ;Test for ButtonA
         GOTO Preload
         BTFSS 05,4        ;Test for ButtonB
         GOTO Preload
         DECFSZ 21h,1
         GOTO Att9
         DECFSZ 22h
         GOTO Att5
         GOTO Raster       ;Not needed but tells you where micro is going




                           ;Raster is a scanning program for the display
                           ;Raster displays each row in turn and scans down the rows.  It has a display time of 250mS
                           ;Values on the display are taken from Table4 - 3 bytes are needed.




                           ;Raster puts anything you want, on the display

Raster   CLRF 22h          ;Jump value for table4
         CLRF 05
Rast1    MOVLW 0Ch         ;Number of scans for each increment of the display
         MOVWF 26h
Rast2    MOVF 22h,0        ;Copy jump value into W
         CALL Table4
         XORLW 0FFh        ;See if end of table reached
         BTFSC 03,2        ;Test zero flag
         GOTO Raster       ;Start again
         MOVF 22h,0        ;Copy jump value into W
         CALL Table4       ;Call Table4 again as value was lost via
         XORLW
         MOVWF 06          ;Move table value to display
         BSF 05,0          ;Turn on top row
         CALL Del2         ;Show top row
         BCF 05,0          ;Turn off top row
         INCF 22h,1        ;Increment the table pointer
         MOVF 22h,0        ;Copy jump value into W
         CALL Table4       ;Call Table4
         MOVWF 06          ;Move table value to display
         BSF 05,2          ;Turn on middle row
         CALL Del2         ;Show middle row
         BCF 05,2          ;Turn off top row
         INCF 22h,1        ;Increment the table pointer
         MOVF 22h,0        ;Copy jump value into W
         CALL Table4       ;Call Table4
         MOVWF 06          ;Move table value to display
         BSF 05,1          ;Turn on bottom row
         CALL Del2         ;Show bottom row
         BCF 05,1          ;Turn off bottom row
         DECF 22h,1        ;Decrement the table pointer
         DECF 22h,1
         DECFSZ 26h,1
         GOTO Rast2
         INCF 22h,1
         INCF 22h,1
         INCF 22h,1
         GOTO Rast1




ButtonA  BSF 2F,0          ;Set button-press flag
         BTFSS 2F,3        ;Is Win-LED/stalemate bit=1?
         GOTO ButAA
         BCF 2F,3
         BCF 05,6
         BCF 05,7
         MOVLW 05          ;Put cursor into "ready" file
         MOVWF 30h
         CLRF 31h
         CLRF 32h
         CLRF 33h
         CLRF 34h
         CLRF 35h
         CLRF 36h
         CLRF 37h
         CLRF 38h
         CLRF 39h
         CLRF 3Ah
         RETURN
ButAA    MOVLW 30h         ;Start of "board"
         MOVWF 04          ;Start FSR at file 30h
ButA1    BTFSC 00,3        ;Look for "8"
         GOTO ButA3        ;Found cursor
         BTFSS 00,0
         GOTO ButA2        ;Cursor not found
         BTFSS 00,2
         GOTO ButA2
         GOTO ButA3        ;Found cursor
ButA2    INCF 04
         GOTO ButA1
ButA3    MOVLW 3Ah         ;Has cursor reached end of "board?"
         XORWF 04,0
         BTFSS 03,2        ;Test the zero flag
         GOTO ButA4        ;not at end of board
         CLRF 3Ah          ;reached end of board
         MOVLW 05
         MOVWF 30h         ;Return cursor to start
         RETURN
ButA4    CLRF 00           ;Clear the cursor
ButA5    INCF 04,1
         MOVLW 3A
         XORWF 04,0        ;If cursor has reached end of board,
         BTFSS 03,2        ; return it to file 30h
         GOTO ButA6
         MOVLW 30h
         MOVWF 04          ;Return cursor to start
ButA6    MOVLW 00
         XORWF 00,0        ;Is board location empty?
         BTFSS 03,2        ;Test zero flag
         GOTO ButA5        ;Not empty
         MOVLW 05          ;Empty location
         MOVWF 00          ;Show cursor in new location
         RETURN

                           ;ButtonB converts a flashing cursor into a red LED =01

ButtonB  BSF 2F,1          ;Set button-press flag
         MOVLW 09
         MOVWF 23h         ;Decrementing file
         MOVLW 31h         ;Start of "board"
         MOVWF 04          ;FSR starts at file 31h
ButB1    MOVLW 05
         XORWF 00,0        ;Is the file looked at by FSR=5?
         BTFSC 03,2        ;Test zero flag
         GOTO ButB2        ;Found
         MOVLW 08          ;Not found try "08" = flashing cursor
         XORWF 00,0        ;Is the file looked at by FSR=8?
         BTFSS 03,2
         GOTO ButB3        ;Not found
ButB2    MOVLW 01          ;Cursor found
         MOVWF 00          ;Make file = 1 = red
         MOVLW 05
         MOVWF 30h         ;Put cursor into "hide" file
         GOTO PWin
ButB3    INCF 04,1         ;Increment the pointer
         DECFSZ 23h
         GOTO ButB1
         GOTO PWin         ;ButtonB pushed for computer goes first



Del1     NOP               ;Create delay
         DECFSZ 20h,1
         GOTO Del1
         RETURN

Del2     NOP               ;Create delay
         NOP
         NOP
         NOP
         NOP
         NOP
         NOP
         NOP
         DECFSZ 20h,1
         GOTO Del2
         BTFSS 05,3        ;Test for ButtonA
         GOTO Preload
         BTFSS 05,4        ;Test for ButtonB
         GOTO Preload
         RETURN


                           ;Player WINS
                           ;Check all possibilities for PLAYER WIN
                           ;Looking for a HIGH in bit0 = 01 in three files
                           ;This routine is in "Linear Programming Mode"
PWin     BTFSS 31h,0
         GOTO PWin1
         BTFSS 32h,0
         GOTO PWin1
         BTFSC 33h,0
         GOTO PWin21
PWin1    BTFSS 34h,0
         GOTO PWin2
         BTFSS 35h,0
         GOTO PWin2
         BTFSC 36h,0
         GOTO PWin21
PWin2    BTFSS 37h,0
         GOTO PWin3
         BTFSS 38h,0
         GOTO PWin3
         BTFSC 39h,0
         GOTO PWin21
PWin3    BTFSS 31h,0
         GOTO PWin4
         BTFSS 34h,0
         GOTO PWin4
         BTFSC 37h,0
         GOTO PWin21
PWin4    BTFSS 32h,0
         GOTO PWin5
         BTFSS 35h,0
         GOTO PWin5
         BTFSC 38h,0
         GOTO PWin21
PWin5    BTFSS 33h,0
         GOTO PWin6
         BTFSS 36h,0
         GOTO PWin6
         BTFSC 39h,0
         GOTO PWin21
PWin6    BTFSS 31h,0
         GOTO PWin7
         BTFSS 35h,0
         GOTO PWin7
         BTFSC 39h,0
         GOTO PWin21
PWin7    BTFSS 33h,0
         GOTO PWin8
         BTFSS 35h,0
         GOTO PWin8
         BTFSC 37h,0
         GOTO PWin21
PWin8    CALL Stale
         BTFSS 2F,3
         GOTO CWin
         RETURN
PWin21   BSF 05,6          ;Player WINS!
         BSF 2F,3          ;Set the Win-LED/stalemate bit
         RETURN



                           ;Computer WINS
                           ;Check all possibilities for "COMPUTER WINS"
                           ;Looking for a HIGH in bit2 = 04 in 2 files
                           ;This routine is in "Algorithm Mode"

CWin     MOVLW 0FFh        ;The value will be incremented to 00!
         MOVWF 25h         ;Table jump file
CWin1    CLRF 27h          ;Clear the Computer counter
         MOVLW 03
         MOVWF 26h         ;Fetch 3 table values in a loop.
CWin2    INCF 25h,1        ;Increment the table-jump file
         MOVF 25h,0        ;Copy jump file into W
         CALL Table1       ;Get first table value
         XORLW 0FFh        ;Look for end-of-table
         BTFSC 03,2        ;Test zero flag
         GOTO Stop         ;End of table found - no result obtained
         MOVF 25h,0        ;Copy jump file into W again
         CALL Table1       ;Call table again as previous value was lost via XORLW
         MOVWF 04          ;Move table value into FSR
         BTFSC 00,0        ;Look for unwanted player piece
         DECF 27h,1        ;Dec. computer counter so count cannot be recognised
         BTFSC 00,2        ;Look at bit2 of file 31h (first table value)
         INCF 27h,1        ;Increment the Computer counter
CWin3    DECFSZ 26h,1      ;Decrement the 3 table-values counter
         GOTO CWin2        ;Loop for next piece
         MOVLW 02          ;See if 2 computer pieces are found
         XORWF 27h,0
         BTFSS 03,2
         GOTO CWin1        ;2 computer pieces not found
CWin4    MOVF 25h,0        ;2 Computer pieces found. Put 25h into W
         CALL Table1       ;Locate table value
         MOVWF 04          ;Put W into FSR
         MOVF 00,1         ;Move value in file in and out to see if it is zero
         BTFSS 03,2        ;Test zero flag
         GOTO CWin5        ;Not zero
         MOVLW 04          ;Square is zero. Load computer piece
         MOVWF 00
         BSF 05,7          ;Computer WINS!
         BSF 2F,3          ;Set the Win-LED/stalemate bit
         RETURN
CWin5    DECF 25h,1
         GOTO CWin4

                           ;Look for a "stopper"
                           ;Any row or diagonal containing a "2" must be stopped
                           ; i.e: two files in a row or diag with 01


Stop     MOVLW 0FFh        ;The value will be incremented to 00!
         MOVWF 25h         ;Table jump file
Stop1    CLRF 27h          ;Clear the "player" counter
         MOVLW 04          ;This will be decremented to 3!
         MOVWF 26h         ;Fetch 3 table-values in a loop.
Stop2    DECFSZ 26h,1
         GOTO Stop2A
         GOTO Stop4        ;3 values have been processed
Stop2A   INCF 25h,1        ;Increment the table-jump file
         MOVF 25h,0        ;Copy jump file into W
         CALL Table1       ;Get first table value
         MOVWF 04          ;Move table value into FSR
         BTFSC 00,7        ;Look for end-of-table
         GOTO Corner       ;End of table found - no result obtained
         BTFSC 00,2        ;Look at bit2 of file 31h for computer piece
         GOTO Stop3        ;Computer piece found
         BTFSC 00,0        ;Look at bit0 of file 31h (first table value)
         INCF 27h,1        ;Increment the "player" counter
         GOTO Stop2
Stop3    BSF 27h,7         ;Set bit 7 of "player" counter
         GOTO Stop2
Stop4    BTFSC 27h,7       ;Is computer in the row?
         GOTO Stop1
         BTFSS 27h,1       ;Is counter=2?
         GOTO Stop1
         DECF 25h,1        ;Two player files found! Go back to start of the 3 files.
         DECF 25h,1        ;Take table-jump value up-table
Stop5    MOVF 25h,0        ;Start of the three files. Move Jump value into W
         CALL Table1       ;Look at value of first of three files.
         MOVWF 04          ;Put jump value into FSR
         BTFSC 00,0        ;Test for player
         GOTO Stop6        ;Not empty
         MOVLW 04          ;Empty so put 04 into file looked at by FSR
         MOVWF 00
         RETURN
Stop6    INCF 25h,1        ;Increment the pointer
         GOTO Stop5



                           ;Random Corner if first move.
                           ;Look to see if all locations are empty.
                           ;Place Computer value in a corner.

Corner   MOVLW 09
         MOVWF 25h         ;The loop file
         MOVLW 30h         ;This will be incremented before starting routine
         MOVWF 04          ;The pointer file
Corner1  INCF 04,1         ;Look at first board location
         MOVLW 00          ;Put 00 into W
         XORWF 00,0        ;Is location empty?
         BTFSS 03,2        ;Test the zero flag
         GOTO Center       ;Location is not empty
         DECFSZ 25h,1      ;File is zero. Decrement the loop file
         GOTO Corner1      ;Loop again
         MOVLW 07          ;Board is empty. Put masking value (7) into W
         ANDWF 28h,1       ;AND 07 with random number file. Result in 28h
         INCF 28h,1        ;File will be 1 to 8
         MOVF 28h,0        ;Put value in file 28 into W
         CALL Table2       ;Fetch random corner from table
         MOVWF 04          ;Put random corner into FSR
         MOVLW 04
         MOVWF 00          ;Put computer piece onto board
         RETURN


                           ;If board is not empty, place computer in center square

Center   MOVF 35h,1        ;Move 35h in and out of file to see if it is zero
         BTFSS 03,2        ;Test zero flag
         GOTO Fork
         MOVLW 04
         MOVWF 35h         ;Put computer into center square
         RETURN



                           ;This is a "trick" situation where the player is
                           ; forming a "fork." If two player and one
                           ; computer piece is found, and computer is in
                           ; middle, computer plays a side square

Fork     BTFSS 35h,2       ;Is computer in centre square?
         GOTO Look4        ;Not computer piece
         BTFSS 31h,0       ;Computer in centre. Is player in first square?
         GOTO ForkA        ;Player not in first square
         BTFSC 39h,0       ;Player in first square. Is player in last square?
         GOTO ForkB        ;Player in last square
ForkA    BTFSS 33h,0
         GOTO Divide
         BTFSS 37h,0
         GOTO Divide
ForkB    MOVLW 07          ;Get a random value. Put masking value (7) into W
         ANDWF 28h,1       ;AND 07 with random number file. Result in file
         INCF 28h,1        ;File will be 1 to 8
         MOVF 28h,0        ;Put value in file 28 into W
         CALL Table3       ;Fetch random side from table
         MOVWF 04          ;Put random side into FSR
         MOVF 00,1         ;Move side value in and out of file to see if it is zero
         BTFSS 03,2
         GOTO ForkB        ;Side not empty
         MOVLW 04
         MOVWF 00          ;Put computer piece onto board
         RETURN



                           ;Divide is another "trick" situation.
                           ;If computer in centre and player on two adjoining sides
                           ; computer must go between player pieces.

Divide   BTFSC 31h,0       ;Test for player in first square
         GOTO Look4
         BTFSC 31h,2       ;Test for computer in first square
         GOTO Look4
         BTFSC 32h,0       ;Test for player in second square
         GOTO Look4
         BTFSC 32h,2       ;Test for computer in second square
         GOTO Look4
         BTFSC 33h,0       ;Test for player in third square
         GOTO Look4
         BTFSC 33h,2       ;Test for computer in third square
         GOTO Look4
         BTFSC 34h,0       ;Test for player in fourth square
         GOTO Look4
         BTFSC 34h,2       ;Test for computer in fourth square
         GOTO Look4
         BTFSS 35h,2       ;Look for computer in centre square
         GOTO Look4
         BTFSS 36h,0       ;Test for player in sixth square
         GOTO Look4
         BTFSC 37h,0       ;Test for player in seventh square
         GOTO Look4
         BTFSC 37h,2       ;Test for computer in seventh square
         GOTO Look4
         BTFSS 38h,0       ;Test for player in eighth square
         GOTO Look4
         BTFSC 39h,0       ;Test for player in ninth square
         GOTO Look4
         BTFSC 39h,2       ;Look for computer in last location
         GOTO Look4
         MOVLW 04
         MOVWF 39h         ;Put computer piece into last location
         RETURN


                           ;Look for a row containing just a computer piece (04)
                           ;and add another computer piece. If not, add a random
                           ; computer piece.


Look4    MOVLW 0FFh        ;The value will be incremented to 00!
         MOVWF 25h         ;Table jump file
Look4A   CLRF 27h          ;Clear the Look counter
         MOVLW 03
         MOVWF 26h         ;Fetch 3 table values in a loop.
Look4B   INCF 25h,1        ;Increment the table-jump file
         MOVF 25h,0        ;Copy jump file into W
         CALL Table1       ;Get first table value
         MOVWF 04          ;Move table value into FSR
         BTFSC 00,7        ;Look for end-of-table
         GOTO Random       ;End of table found - no result obtained
         BTFSC 00,2        ;Look for computer
         GOTO Look4L
         BTFSS 00,0        ;Look for player
         GOTO Look4C
         INCF 27h,1        ;Prevent row being accessed
Look4L   INCF 27h,1        ;Increment the Look counter (If=1, Computer only once)
Look4C   DECFSZ 26h,1      ;Decrement the 3 table-values counter
         GOTO Look4B
         MOVLW 01
         XORWF 27h,1
         BTFSS 03,2
         GOTO Look4A       ;Computer nil or more than once
         DECF 25h,1        ;Computer only once. Find start of row
         DECF 25h,1
Look4G   MOVF 25h,0        ;Move 25h into W
         CALL Table1
         MOVWF 04          ;Move table value into pointer
         MOVF 00,1         ;Move file in and out to see it is zero
         BTFSS 03,2        ;Test zero flag
         GOTO Look4H
         MOVLW 04          ;Square is empty
         MOVWF 00          ;Put computer piece into empty square
         RETURN
Look4H   INCF 25h,1
         INCF 25h,1        ;Place computer piece only in corner!
         GOTO Look4G


                           ;Insert a random computer piece
                           ;corners are first selected

Random   MOVLW 03          ;Put masking value (3) into W
         ANDWF 28h,1       ;AND 03 with random number file. Result in file
Random1  INCF 28h,1        ;File will be 1 to 4 on first pass.
         MOVF 28h,0        ;Put value into W
         CALL Table5       ;Fetch random location from table
         MOVWF 04          ;Put random location into FSR
         BTFSC 00,7        ;Test for end-of-table
         RETURN
         MOVF 00,1         ;Move value in and out of file to see if it is zero
         BTFSS 03,2        ;Test the zero-bit
         GOTO Random1      ;location not empty
         MOVLW 04
         MOVWF 00          ;Put computer piece onto board
         RETURN


                           ;STALEMATE
                           ;Board is checked to see if any square is empty
                           ;If all full, both LEDs are illuminated

Stale    BTFSC 05,6        ;Check Player-WINS! bit
         RETURN
         MOVLW 09
         MOVWF 25h         ;Decrementing file
         MOVLW 31h
         MOVWF 04          ;Start of files into FSR
Stale1   MOVLW 05
         XORWF 00,0
         BTFSC 03,2
         RETURN            ;Square has cursor
         BTFSC 00,3        ;Test for "hidden" cursor
         RETURN
         MOVLW 00
         XORWF 00,0        ;Test zero flag
         BTFSC 03,2        ;Square is empty
         RETURN            ;Square is full. Increment the pointer
         INCF 04,1
         DECFSZ 25h,1
         GOTO Stale1
         BSF 05,6
         BSF 05,7          ;Set the Win-LED/stalemate bit
         BSF 2F,3
         RETURN


Preload  MOVLW 05          ;Put cursor into "ready" file
         MOVWF 30h
         CLRF 31h
         CLRF 32h
         CLRF 33h
         CLRF 34h
         CLRF 35h
         CLRF 36h
         CLRF 37h
         CLRF 38h
         CLRF 39h
         CLRF 3Ah
         BTFSS 05,3        ;Make sure button "A" is released
         GOTO Preload
         BTFSS 05,4        ;Make sure button "B" is released
         GOTO Preload
         GOTO Main         ;Go to Main


                           ;Player Value = 01 = red
                           ;Computer Value = 04 = green
                           ;Cursor = 05

Main     MOVLW 20h         ;Loops of Main for flashing cursor
         MOVWF 22h         ;File to decrement
MainA    BTFSC 2F,0        ;ButtonA pressed first time?
         GOTO MainB
         BTFSS 05,3        ;Test for ButtonA
         CALL ButtonA
MainB    BTFSC 2F,1        ;ButtonB pressed first time?
         GOTO MainC
         BTFSS 05,4        ;Test for ButtonB
         CALL ButtonB
MainC    BTFSS 31h,0
         GOTO Main1
         BSF 06,5
Main1    BTFSS 31h,2
         GOTO Main2
         BSF 06,4
Main2    BTFSS 32h,0
         GOTO Main3
         BSF 06,3
Main3    BTFSS 32h,2
         GOTO Main4
         BSF 06,2
Main4    BTFSS 33h,0
         GOTO Main5
         BSF 06,1
Main5    BTFSS 33h,2
         GOTO Main6
         BSF 06,0
Main6    BSF 05,0
         CALL Del1
         BCF 05,0
         CLRF 06
         CALL Del1

         BTFSS 34h,0
         GOTO Main7
         BSF 06,5
Main7    BTFSS 34h,2
         GOTO Main8
         BSF 06,4
Main8    BTFSS 35h,0
         GOTO Main9
         BSF 06,3
Main9    BTFSS 35h,2
         GOTO Main10
         BSF 06,2
Main10   BTFSS 36h,0
         GOTO Main11
         BSF 06,1
Main11   BTFSS 36h,2
         GOTO Main12
         BSF 06,0
Main12   BSF 05,2
         CALL Del1
         BCF 05,2
         CLRF 06
         CALL Del1

         BTFSS 37h,0
         GOTO Main13
         BSF 06,5
Main13   BTFSS 37h,2
         GOTO Main14
         BSF 06,4
Main14   BTFSS 38h,0
         GOTO Main15
         BSF 06,3
Main15   BTFSS 38h,2
         GOTO Main16
         BSF 06,2
Main16   BTFSS 39h,0
         GOTO Main17
         BSF 06,1
Main17   BTFSS 39h,2
         GOTO Main18
         BSF 06,0
Main18   BSF 05,1
         CALL Del1
         BCF 05,1
         CLRF 06
         BTFSC 05,3        ;Skip if ButtonA is still pressed
         BCF 2F,0          ;Clear button-press flag
         BTFSC 05,4        ;Skip if ButtonB is still pressed
         BCF 2F,1          ;Clear button-press flag
         DECFSZ 22h,1
         GOTO MainA

                           ;The following section hides and shows cursor
                           ;If cursor is showing, it hides cursor
                           ;If cursor is hidden, it shows cursor
                           ;Cursor changes from 05 to 08 to 05 etc

         MOVLW 0A          ;Ten loops of routine
         MOVWF 23h         ;Loop file
         MOVLW 30h         ;Start of files for board
         MOVWF 04          ;Load start into FSR
Main19   BTFSS 00,0        ;Test bit0 of INDF
         GOTO Main20
         BTFSS 00,2        ;Is bit2 = HIGH?
         GOTO Main20
         MOVLW 08          ;File contains cursor. Put 08 into file to "hide"
         MOVWF 00          ;08 will be put into a file 30h to 39h
         GOTO Main         ;Cursor has been hidden

Main20   INCF 04,1         ;Increment pointer
         DECFSZ 23h,1      ;Decrement loop file
         GOTO Main19


         MOVLW 30h         ;Start of files for board
         MOVWF 04          ;Load start into FSR
Main21   BTFSC 00,3        ;Look for "8" in a file
         GOTO Main22
         INCF 04,1         ;Increment the pointer
         GOTO Main21
Main22   MOVLW 05          ;Put 5 in file looked at by FSR
         MOVWF 00h
         INCF 28h          ;Increment the RANDOM NUMBER file
         INCF 28h
         CALL Stale
         GOTO Main

         END               ;Tells assembler end of program


Colin Mitchell

Colin Mitchell

Expertise

electronics
writing
PIC-Chips

Social Media

instagramtwitterwebsite

Related Posts

TODO
Transistor Test
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media