	;RPS-1.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 03	;Load W with 0000 0011
	MOVWF 05	;Make RA0, RA1 input
	CLRF 06		;Make all port B output
	BCF 03,5	;Go to Bank 0 - the program memory area.	
	CLRF 1C		;Clear the jump counter
	CLRF 1D		;Clear the jump counter
	CLRF 1F		;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 04h	;4
	RETLW 04h	;1
	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 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


;"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 Del100
	MOVLW 0E7h
	MOVWF 06
	CALL Del100	
	MOVLW 0C3h
	MOVWF 06
	CALL Del100		
	MOVLW 81h
	MOVWF 06
	CALL Del100	
	CLRF 06
 	CALL Del100	
	MOVLW 81
	MOVWF 06
	CALL Del100	
	MOVLW 0C3h
	MOVWF 06
	CALL Del100
	MOVLW 0E7h
	MOVWF 06
	CALL Del100		
	CLRF 06
	DECF 06,1		;Illuminate all LEDs
	CALL Del100	
	CLRF 06
	RETURN


Delay1	MOVLW 0A
	MOVWF 10h
DelA	CALL Del100
	DECFSZ 10h,1
	GOTO DelA
	RETURN


Del300	MOVLW 03
	MOVWF 10h
DelB	CALL Del100
	DECFSZ 10h,1
	GOTO DelB
	RETURN


Del100	MOVLW 64h	;100 loops = 100mS
	MOVWF 1B
Del101	NOP
	DECFSZ 1A,1
	GOTO Del101
	BTFSC 05,0
	BSF 1F,0	
	DECFSZ 1B,1
	GOTO Del101
	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 4 times. 
;then RETURNs to Main

Flash	MOVLW 04
	MOVWF 1E
Flash1	CLRF 06		;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	


;"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
	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 

	
;"Toggle" Flashes the top 4 LEDs then the bottom 4 LEDs.
;This is repeated 4 times

Toggle	MOVLW 04
	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	
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
	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 MainA
Main5	BTFSS 12h,1
	GOTO Main6
	MOVLW 73h	;Produce "P"
	MOVWF 0E	;Put display value into file 0E
	CALL Display
	GOTO MainA
Main6	MOVLW 6Dh	;Produce "S"
	MOVWF 0E	;Put display value into file 0E
	CALL Display
	GOTO MainA

	END		;Tells assembler end of program












