#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

    cblock 0x20
index1
index2               
index2_save
light
y
temp
curmask
relaymask
increment
    endc
      
    org 0
Start:
     bsf       STATUS,RP0          
     movlw     0xFF
     movwf     TRISA               
     clrf      TRISC               

     bcf       STATUS,RP0          
     bsf       STATUS,RP1     
     movlw     0xf7                
     movwf     ANSEL
     bcf       STATUS,RP0          
     bcf       STATUS,RP1

	movlw	0x1
	movwf	curmask
	movlw	0x10
	movwf	relaymask

	; Delay a bit
	clrf	increment
	clrf	PORTC
	clrf	index1
	clrf	index2
lp1:
	decfsz	index2
	goto $-1
	decfsz	index1
	goto lp1

BigLoop:
	movlw	.1
	movwf	light
LightUp:
	call	SenseKey
	movlw	.6
	movwf	index1
	call	CalcIndex2
	movwf	index2_save
d1:
	movf	index2_save, w
	movwf	index2
	movf	curmask, w
	
	movwf	PORTC
cyc1:
	decfsz index2
	goto cyc1

	movf	index2_save, w
	movwf	index2
	movlw	0x0
	
	movwf	PORTC
cyc2:
	incfsz	index2
	goto	cyc2
	decfsz	index1
	goto	d1
	incfsz	light
	goto	LightUp
;;;;;;;;;;;;;;;;;;;
	call	CheckIncrement
	clrf	light
	decfsz	light
LightDown:
	call 	SenseKey
	movlw	.6
	movwf	index1
	call	CalcIndex2
	movwf	index2_save
d2:
	movf	index2_save, w
	movwf	index2
	movf	curmask, w
	movwf	PORTC
cyc3:
	decfsz index2
	goto cyc3

	movf	index2_save, w
	movwf	index2
	movlw	0x0
	movwf	PORTC
cyc4:
	incfsz	index2
	goto	cyc4
	decfsz	index1
	goto d2
	decfsz	light
	goto	LightDown
	call	CheckIncrement

	goto	BigLoop


SenseKey:
	clrw
	btfss	PORTA,	3
	bsf		increment,	0
	return

CheckIncrement:
	btfss	increment,	0
	return
	clrf	increment
	bcf		STATUS,	C
	rlf		relaymask
	rlf		curmask
	btfss	curmask,	4
	return
	clrf	curmask
	bsf		curmask,	0
	clrf	relaymask
	bsf		relaymask,	4
	return

	; Having X in light, 1 <= X <= 255,
	; Calculate Y = 2**(X/32)
	org     0xf4                  ; force table to cross a page boundary
CalcIndex2:
	movf	light,	w
	movwf	temp
	bcf		STATUS, C
	rrf		temp,	f
    movlw     high TableStart     
    movwf     PCLATH
    movlw     low TableStart      
    addwf     temp,w              
    btfsc     STATUS,C            
    incf      PCLATH,f            
    movwf     PCL                 
TableStart:
        retlw   .1              ; 0
        retlw   .1              ; 1
        retlw   .1              ; 2
        retlw   .1              ; 3
        retlw   .1              ; 4
        retlw   .1              ; 5
        retlw   .1              ; 6
        retlw   .1              ; 7
        retlw   .1              ; 8
        retlw   .1              ; 9
        retlw   .1              ; 10
        retlw   .1              ; 11
        retlw   .1              ; 12
        retlw   .1              ; 13
        retlw   .1              ; 14
        retlw   .1              ; 15
        retlw   .2              ; 16
        retlw   .2              ; 17
        retlw   .2              ; 18
        retlw   .2              ; 19
        retlw   .2              ; 20
        retlw   .2              ; 21
        retlw   .2              ; 22
        retlw   .2              ; 23
        retlw   .2              ; 24
        retlw   .2              ; 25
        retlw   .3              ; 26
        retlw   .3              ; 27
        retlw   .3              ; 28
        retlw   .3              ; 29
        retlw   .3              ; 30
        retlw   .3              ; 31
        retlw   .4              ; 32
        retlw   .4              ; 33
        retlw   .4              ; 34
        retlw   .4              ; 35
        retlw   .4              ; 36
        retlw   .4              ; 37
        retlw   .5              ; 38
        retlw   .5              ; 39
        retlw   .5              ; 40
        retlw   .5              ; 41
        retlw   .6              ; 42
        retlw   .6              ; 43
        retlw   .6              ; 44
        retlw   .7              ; 45
        retlw   .7              ; 46
        retlw   .7              ; 47
        retlw   .8              ; 48
        retlw   .8              ; 49
        retlw   .8              ; 50
        retlw   .9              ; 51
        retlw   .9              ; 52
        retlw   .9              ; 53
        retlw   .10             ; 54
        retlw   .10             ; 55
        retlw   .11             ; 56
        retlw   .11             ; 57
        retlw   .12             ; 58
        retlw   .12             ; 59
        retlw   .13             ; 60
        retlw   .14             ; 61
        retlw   .14             ; 62
        retlw   .15             ; 63
        retlw   .16             ; 64
        retlw   .16             ; 65
        retlw   .17             ; 66
        retlw   .18             ; 67
        retlw   .19             ; 68
        retlw   .19             ; 69
        retlw   .20             ; 70
        retlw   .21             ; 71
        retlw   .22             ; 72
        retlw   .23             ; 73
        retlw   .24             ; 74
        retlw   .25             ; 75
        retlw   .26             ; 76
        retlw   .28             ; 77
        retlw   .29             ; 78
        retlw   .30             ; 79
        retlw   .32             ; 80
        retlw   .33             ; 81
        retlw   .34             ; 82
        retlw   .36             ; 83
        retlw   .38             ; 84
        retlw   .39             ; 85
        retlw   .41             ; 86
        retlw   .43             ; 87
        retlw   .45             ; 88
        retlw   .47             ; 89
        retlw   .49             ; 90
        retlw   .51             ; 91
        retlw   .53             ; 92
        retlw   .56             ; 93
        retlw   .58             ; 94
        retlw   .61             ; 95
        retlw   .64             ; 96
        retlw   .66             ; 97
        retlw   .69             ; 98
        retlw   .72             ; 99
        retlw   .76             ; 100
        retlw   .79             ; 101
        retlw   .82             ; 102
        retlw   .86             ; 103
        retlw   .90             ; 104
        retlw   .94             ; 105
        retlw   .98             ; 106
        retlw   .103            ; 107
        retlw   .107            ; 108
        retlw   .112            ; 109
        retlw   .117            ; 110
        retlw   .122            ; 111
        retlw   .128            ; 112
        retlw   .133            ; 113
        retlw   .139            ; 114
        retlw   .145            ; 115
        retlw   .152            ; 116
        retlw   .158            ; 117
        retlw   .165            ; 118
        retlw   .173            ; 119
        retlw   .181            ; 120
        retlw   .189            ; 121
        retlw   .197            ; 122
        retlw   .206            ; 123
        retlw   .215            ; 124
        retlw   .224            ; 125
        retlw   .234            ; 126
        retlw   .245            ; 127
	end
