	;21Match.asm
        ;Project: "21-MATCHES" 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 11h	;Clear the random number file
	CLRF 1F		;Clear the button-press file
	GOTO Main

Table	ADDWF 02h,1	;Add W to the Program Counter to create a jump.
	RETLW 3Fh	;0    format= gfedcba
	RETLW 30h	;1    
	RETLW 5Bh	;2    
	RETLW 4Fh	;3
	RETLW 66h	;4
	RETLW 6Dh	;5
	RETLW 7Dh	;6
	RETLW 07h	;7
	RETLW 7Fh	;8
	RETLW 6Fh	;9  
	RETLW 6Eh	;y
	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

;This sub-routine works out the "Computer No of matches" 
;The number of matches (file 0C) is loaded into file 0F.  The number 
;of matches to be removed is worked-out and put into file 10h.

Comp	MOVF 0C,0	;Move file 0C to W
	MOVWF 0F	;Move W to file 0F for this operation	
CompA	MOVLW 02
	XORWF 0F,0
	BTFSS 03,2
	GOTO Comp1
	MOVLW 01
	MOVWF 10h	;"Computer takes 1"
	RETURN
Comp1	MOVLW 03
	XORWF 0F,0
	BTFSS 03,2
	GOTO Comp2
	MOVLW 02
	MOVWF 10h	;"Computer takes 2"
	RETURN
Comp2	MOVLW 04
	XORWF 0F,0
	BTFSS 03,2
	GOTO Comp3
	MOVLW 03
	MOVWF 10h	;"Computer takes 3"
	RETURN
Comp3	MOVLW 05
	XORWF 0F,0
	BTFSS 03,2
	GOTO Comp4	
	MOVF 11h,0	;Put random number into W
	MOVWF 10h	;"Computer takes Random number"
	RETURN
Comp4	DECF 0Fh,1	;File 0F is greater than 5 so reduce it by 4
	DECF 0Fh,1
	DECF 0Fh,1
	DECF 0Fh,1
	GOTO CompA

;This sub-routine converts the hex value in file 0C to decimal values in files 0D and 0E. 
;File 0D is the "units" and file 0E is the "tens"

Convert	CLRF 0D
	CLRF 0E
	MOVF 0C,0	;Move file 0C to W
	MOVWF 0F	;Move W to file 0F for decrementing
	INCF 0F,1	;To account for the next two lines
Conv1	DECFSZ 0F,1	;Decrement file 0F
	GOTO Conv2
	RETURN
Conv2	INCF 0D,1
	MOVLW 0A
	XORWF 0D,0
	BTFSS 03,2	;Test the zero flag for "ten"
	GOTO Conv1
	INCF 0E,1	;Increment the "tens"
	CLRF 0D		;Zero the "units"
	GOTO Conv1	;Do another conversion


Delay	MOVLW 60h
	MOVWF 1B
DelA	NOP
	NOP
	DECFSZ 1A,1
	GOTO DelA		
	BTFSC 05,0	;Look at push button
	GOTO DelC
	BCF 1F,0
DelB	DECFSZ 1B,1
	GOTO DelA
	RETURN
DelC	BTFSC 1F,0	;Test the button-press flag
	GOTO DelB
	DECF 0C,1	;Decrement the count file
	MOVLW 04
	MOVWF 13h	;Number of loops of display before computer turn
	BSF 1F,1	;Set the flag for above loop
	BSF 1F,0	;Set the button-press flag
	GOTO DelB

							
Display	MOVLW 00	;Blank the leading "0"
	XORWF 0Eh,0	;   "      "    "
	BTFSC 03,2	;   "      "    "
	GOTO Disp1	;   "      "    "
	MOVF 0Eh,0	;Move file 0E to W.  Show "tens"			 	
	CALL Table	
	MOVWF 06	;Show value on 7-segment display
	CALL Delay	
	CLRF 06		;Blank the display	
	CALL Delay
	MOVF 0Dh,0	;Move file 0D to W.  Show "units"
	CALL Table	
	MOVWF 06	;Show value on 7-segment display
	CALL Delay		
	CLRF 06		;Blank the display	
Disp	CALL Delay
	CALL Delay	
	CALL Delay
	RETURN
Disp1	MOVF 0Dh,0	;Move file 0D to W.  Show "units"
	CALL Table	
	MOVWF 06	;Show value on 7-segment display
	GOTO Disp

;"Mess1" shows on 7-segment display:  "I Lose" 

Mess1	MOVLW 40h	;Show "-"
	MOVWF 06	
	CALL Show
	CALL Show	
	MOVLW 06	;Show the letter"I"
	MOVWF 06	
	CALL Show
	CALL Show
	CALL Show
	CALL Show
	MOVLW 0Dh
	MOVWF 12h	;Jumper for table
Mess1A	CLRF 06
	CALL Show
	MOVF 12h,0
	CALL Table
	MOVWF 06	;Output to display
	MOVLW 0FFh	;Detect end of Table
	XORWF 06,0
	BTFSC 03,2
	GOTO Main		;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	MOVLW 0Ah
	MOVWF 12h	;Jumper for table
	GOTO Mess1A

Show	NOP		;Create 250mS delay	
	DECFSZ 1A,1
	GOTO Show
	DECFSZ 1B,1
	GOTO Show
	RETURN

;"Value"  displays the number of matches the computer will remove. It is displayed on the 
;8 LEDs as one LED, two LEDs or three LEDs. File 10h is computer number. 

Value	MOVLW 01
	XORWF 10h,0
	BTFSS 03,2
	GOTO Val1
	MOVLW 80h
	MOVWF 06
	GOTO Val3
Val1	MOVLW 02
	XORWF 10h,0
	BTFSS 03,2
	GOTO Val2
	MOVLW 0C0h
	MOVWF 06
	GOTO Val3
Val2	MOVLW 0E0h
	MOVWF 06
Val3	MOVLW 0Ah	;Create 2 sec delay to show 
	MOVWF 1C	;   computer value on 8 LEDs
Val4	NOP
	DECFSZ 1A,1
	GOTO Val4
	DECFSZ 1B,1
	GOTO Val4
	DECFSZ 1C,1
	GOTO Val4
	RETURN


Main	MOVLW 16h	;15h = 21
	MOVWF 0Ch
Main1	INCF 11h,1	;Increment the random number
	MOVLW 04
	XORWF 11h,0
	BTFSC 03,2	;Test zero bit for match	
	GOTO Main4
Main2	CALL Convert
	CALL Display	
	BTFSS 1F,1	;Has button been pressed?
	GOTO Main1
	DECFSZ 13h,1	;Decrement the 4-loop file before computer turn	
	GOTO Main1
	CALL Comp	;Go to "Computer Turn" for No of matches to remove
	CALL Value	;Display computer value for 2 secs
Main3	CALL Convert
	CALL Display
	MOVLW 00
	XORWF 0Ch,0
	BTFSC 03,2	;Test zero flag for match
	GOTO Mess1
	DECF 0Ch,1
	DECFSZ 10h,1	;Decrement the "computer matches" value.
	GOTO Main3	
	BCF 1F,1	;Clear the loop flag
	MOVLW 00
	XORWF 0Ch,0
	BTFSC 03,2	;Test the zero flag
	GOTO Mess1	; "I lose"
	MOVLW 01
	XORWF 0Ch,0
	BTFSC 03,2	;Test the zero flag
	GOTO Mess2	; "You lose"
	MOVLW 02
	XORWF 0Ch,0
	BTFSC 03,2
	GOTO Mess1	; "I Lose"	
	GOTO Main1
Main4	CLRF 11h
	INCF 11h
	GOTO Main2




	END		;Tells assembler end of program


