	;RPS-2.asm
        ;Project: "Rock, Paper, Scissors" Game
	;List P = 16F84
	;#include <p16F84.inc>
	;__CONFIG 1Bh    ;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC


	ORG 0		;Start of memory for the program.
SetUp	BSF 03,5	;Go to Bank 1	
	MOVLW 01	;Load W with 0000 0001
	MOVWF 05	;Make RA0 input
	CLRF 06		;Make all port B output
	BCF 03,5	;Go to Bank 0 - the program memory area.	
	CLRF 16h	;Clear the number-of-games counter
	CLRF 17h	;Clear the Player counter
	CLRF 18h	;Clear the Computer counter
	CLRF 1Ch	;Clear the jump counter	
	CLRF 1Fh	;Clear the button-press file	
	GOTO Main

Table	ADDWF 02h,1	;Add W to Program Counter to create a jump.
	RETLW 04h	;4   
	RETLW 01h	;1    
	RETLW 02h	;2    
	RETLW 02h	;2
	RETLW 01h	;1  - third selection
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 04h	;4
	RETLW 01h	;1
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 04h	;4
	RETLW 01h	;1
	RETLW 01h	;1
	RETLW 04h	;4
	RETLW 02h	;2
	RETLW 01h	;1
	RETLW 0FFh	;End of table
	RETLW 0FFh	;End of table
	RETLW 0FFh	;End of table
	RETLW 0FFh	;End of table


Table2	ADDWF 02h,1	;Add W to Program Counter to create a jump.
	RETLW 6Eh	;y    format= gfedcba
	RETLW 3Fh	;O
	RETLW 3Eh	;U
	RETLW 00h	;
	RETLW 38h	;L
	RETLW 3Fh	;O
	RETLW 6Dh	;S
	RETLW 79h	;E
	RETLW 00h	;
	RETLW 0FFh	;End of message
	RETLW 78h	;t
	RETLW 30h	;I	
	RETLW 79h	;E
	RETLW 00h	;
	RETLW 0FFh	;End of message




;"Attract"  Attract removes the LEDs from the middle of the display and 
;continues until all LEDs are removed, then reverses

Attract	CLRF 06
	DECF 06,1		;Illuminate all LEDs
	CALL Del2
	MOVLW 0E7h
	MOVWF 06
	CALL Del2
	MOVLW 0C3h
	MOVWF 06
	CALL Del2
	MOVLW 81h
	MOVWF 06
	CALL Del2
	CLRF 06
	CALL Del2
	MOVLW 81
	MOVWF 06
	CALL Del2
	MOVLW 0C3h
	MOVWF 06
	CALL Del2
	MOVLW 0E7h
	MOVWF 06
	CALL Del2	
	CLRF 06
	DECF 06,1		;Illuminate all LEDs
	CALL Del2
	CLRF 06
	INCF 1D
	RETURN


Del100	MOVLW 64h	; 
	MOVWF 1B
Del101	NOP
	DECFSZ 1A,1
	GOTO Del101
	BTFSC 05,0
	GOTO Del103	; 
	BCF 1F,0
Del102	DECFSZ 1B,1
	GOTO Del101		
	RETURN
Del103	BTFSC 1F,0	;
	GOTO Del102	;
	BTFSC 1F,5
	GOTO Del104
	BSF 1F,5
	GOTO Del106
Del104	BTFSC 1F,6
	GOTO Del105
	BSF 1F,6
	GOTO Del106
Del105	BSF 1F,7
Del106	BSF 1F,0
	GOTO Del102


Delay1	MOVLW 0A	;Create 1 second delay
	MOVWF 10h
DelA	CALL Del100
	DECFSZ 10h,1
	GOTO DelA
	RETURN

Del2	MOVLW 34h	;Create 100mS delay
	MOVWF 1B
Del2A	NOP
	BTFSC 05,0
	GOTO Del2B
	DECFSZ 1A,1
	GOTO Del2A
	DECFSZ 1B,1
	GOTO Del2A	
	RETURN	
Del2B	BSF 1F,0	
	RETURN


Del300	MOVLW 03
	MOVWF 10h
DelB	CALL Del100
	DECFSZ 10h,1
	GOTO DelB
	RETURN

Del4	MOVLW 04	;1 sec delay not using Del100
	MOVWF 1C
Del4A	NOP
	DECFSZ 1A,1
	GOTO Del4A
	DECFSZ 1B,1
	GOTO Del4A
	DECFSZ 1C,1
	GOTO Del4A
	RETURN

Display	MOVF 0Eh,0
	MOVWF 06
	CALL Del100
	CLRF 06
	CALL Del100	
	MOVF 0Eh,0
	MOVWF 06	
	CALL Del300
	CLRF 06
	CALL Del300
	MOVF 0Eh,0
	MOVWF 06
	CALL Delay1
	CALL Delay1
	CALL Delay1
	CALL Delay1
	CLRF 06
	CALL Delay1
	INCF 1C,1
	INCF 1D,1
	INCF 1D,1
	RETURN


;"Flash"   Flashes all 8 LEDs on the display 8 times. 
;then RETURNs to Main

Flash	MOVLW 08
	MOVWF 1E
Flash1	CLRF 06h	;Zero file 6
	CALL Del100
	CALL Del100
	DECF 06,1	;Make file 06 = FF  Turn on all LEDs
	CALL Del100
	CALL Del100
	DECFSZ 1E,1
	GOTO Flash1
	CLRF 06
	RETURN	


Mess1	MOVLW 30h	; 
	MOVWF 06h	;Show the letter "I"
	CALL Show
	CALL Show
	MOVLW 03h	;Start at "Lose" on table
	MOVWF 12h	; 
Mess1A	CLRF 06		;Jumper for table
	CALL Show
	MOVF 12h,0
	CALL Table2
	MOVWF 06h	; 
	MOVLW 0FFh	;Output to display
	XORWF 06,0	;Detect end of Table
	BTFSC 03,2	; 
	RETURN		;End of Table detected
	INCF 12h,1	;Jump to next letter
	CALL Show	
	CALL Show
	GOTO Mess1A

			;"Mess2" shows on 7-segment display: "You Lose"
Mess2	CLRF 12h	; 
	GOTO Mess1A	;Jumper for table


			;"Mess3" shows on 7-segment display: "tie"
Mess3	MOVLW 0Ah	;Start at "tIE" on table
	MOVWF 12h	; 
	GOTO Mess1A	;Jumper for table


;"Run" produces a Running LEDs effect across the display. 
;The Running bit is stored in file 0D. It moves across and back 4 times 
;then RETURNs to Main

Run	MOVLW 04	;4 runs
	MOVWF 1E
RunA	CLRF 06
	INCF 06,1	;Create "bit"
Run1	RLF 06,1
	BTFSC 03,0	;Test the carry bit
	GOTO Run2	
	CALL Del100	;100mS delay
	GOTO Run1
Run2	RRF 06,1	;Move the bit back onto the display
Run3	CALL Del100	;100mS delay
	RRF 06,1	
	BTFSS 03,0	;Test the carry bit
	GOTO Run3	
	DECFSZ 1E,1
	GOTO RunA
	CLRF 06
	RETURN 



			;Number of games in file 16h
			;Player Score in file 17h  
			;Computer Score in file 18h.
			

Score	BTFSS 1Fh,7	;Player Scissors?
	GOTO Score2	;Not player scissors
	BTFSS 12h,0	;Computer Rock?		
	GOTO Score1
	INCF 18h,1	;Computer wins	
	GOTO Score7
Score1	BTFSS 12h,1	;Computer Paper?	
	GOTO Score6	
	INCF 17h,1	;Player Scissors, Computer Paper
	GOTO Score7
Score2	BTFSS 1Fh,6	;Player Paper?
	GOTO Score4	;Not player paper
	BTFSS 12h,0	;Computer Rock?
	GOTO Score3
	INCF 17h,1	;Player wins
	GOTO Score7
Score3	BTFSC 12h,1	;Computer Paper?		
	GOTO Score6
	INCF 18h,1	;Player Paper, Computer Scissors. Computer wins
	GOTO Score7
Score4	BTFSS 1Fh,5	;Player Rock?
	GOTO Score7	;Player not playing
	BTFSC 12h,0	;Player Rock, Computer Rock?
	GOTO Score6		
	BTFSS 12h,1	;Player Rock, Computer Paper?
	GOTO Score5
	INCF 18h,1	;Computer wins
	GOTO Score7
Score5	INCF 17h,1	;Player wins
	GOTO Score7		
Score6	INCF 17h,1	;Draw
	INCF 18h,1	;Draw
Score7	INCF 16h,1	;INCrement the number of games.
	CLRF 1F		
	RETURN

SHOW	NOP		;Create 250mS delay 
	DECFSZ 1A,1
	GOTO Show
	DECFSZ 1B,1
	GOTO Show
	RETURN

	
;"Toggle" Flashes the top 4 LEDs then the bottom 4 LEDs.
;This is repeated 6 times

Toggle	MOVLW 06
	MOVWF 1E
Toggle1	MOVLW 0F0h
	MOVWF 06	;Turn on 4 top LEDs
	CALL Del300	
	MOVLW 0Fh
	MOVWF 06	;Turn on 4 bottom LEDs
	CALL Del300	
	DECFSZ 1E,1
	GOTO Toggle1
	CLRF 06
	RETURN


;File 1C = jump value for attract sub-routine before displaying RPS
;File 1D = jump value for RPS value

Main	CALL Attract
	BTFSS 1F,0
	GOTO Main	
	CLRF 06
	CALL Del4	;Blank screen for 1 sec	
	MOVLW 0Fh
	ANDWF 1Dh	;Produce random number up to F
MainA	MOVF 1C,0	;Copy 1C to W
	CALL Table
	MOVWF 11h	;File 11h  holds 1, 2, 4 or FF
	BTFSS 11h,7	;Test for End of Table
	GOTO MainB
	CLRF 1Ch	;Start at top of table
	GOTO MainA
MainB	BTFSS 11h,0
	GOTO Main2
	CALL Flash	
	GOTO Main4
Main2	BTFSS 11h,1
	GOTO Main3
	CALL Run	
	GOTO Main4
Main3	CALL Toggle
Main4	CALL Delay1	;Call 1 second delay		
	MOVF 1D,0	;Copy 1D to W
	CALL Table
MainC	MOVWF 12h	;File 12h  holds 1, 2 or 4
	BTFSS 12h,7	;Test for End of Table
	GOTO MainD
	CLRF 1D
	GOTO Main4
MainD	BTFSS 12h,0
	GOTO Main5
	MOVLW 33h	;Produce "R"
	MOVWF 0E	;Put display value into file 0E
	CALL Display
	GOTO Main7
Main5	BTFSS 12h,1
	GOTO Main6
	MOVLW 73h	;Produce "P"
	MOVWF 0E	;Put display value into file 0E
	CALL Display
	GOTO Main7
Main6	MOVLW 6Dh	;Produce "S"
	MOVWF 0E	;Put display value into file 0E
	CALL Display
	MOVF 1F,0
	MOVWF 06	
Main7	CALL Score	;Increment three counters	
	MOVLW 03h	;Sample at 3 games. 14h for twenty games
	XORWF 16h,0
	BTFSS 03,2	;Match?
	GOTO MainA	;Not 3 games
	CLRF 16h	;Reset the games counter	
	MOVF 17,1
	BTFSS 03,2	;Test for zero in file
	GOTO MainE
	MOVF 18,1
	BTFSC 03,2	;Test for zero in file - no games played!
	GOTO SetUp
MainE	MOVF 17h,0	;Copy Player Score into W	
	SUBWF 18h,0	;Subtract Player from Computer Score
	BTFSS 03,2	;Check for "tie"
	GOTO Main8	;Not a tie
	GOTO Mess3	;Display "tie"
	GOTO SetUp
Main8	BTFSS 03,0	;Test carry flag
	GOTO Main9
	GOTO Mess2	;Display "You Lose"
	GOTO SetUp
Main9	GOTO Mess1	;Display "I Lose"
	GOTO SetUp	

	END		;Tells assembler end of program












