;16F648A  Univer closedbolt




	list p=PIC16f688
	#include "p16f688.inc"

	__config _CP_OFF & _CPD_OFF & _WDT_OFF & _BOD_OFF & _PWRTE_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF 

	ERRORLEVEL	-224
	ERRORLEVEL	-302

#define	switch2		PORTA,0
#define	trigger		PORTA,1
#define	switch1		PORTA,2
#define	eyeout		PORTA,3
#define	DIP4		PORTA,4
#define	valve		PORTA,5

#define	GREEN_LED	PORTC,0	;GREEN
#define RED_LED		PORTC,1	;RED
#define eyepower	PORTC,2
#define	solenoid	PORTC,3
#define BLUE_LED	PORTC,4 ;Actualy Blue
#define	shutdown	PORTC,5


	bcf			STATUS,RP0
	clrf		PORTA
	movlw		b'00010111' ;05h
	movwf		CMCON0
	bsf			STATUS,RP0
	clrf		ANSEL
	movlw		b'00011111' ;0,1,2,3,4 input   5 output
	movwf		TRISA

	bcf			OPTION_REG, NOT_RAPU	;enable pullups
	bcf			WPUA,0	;switch22pullup;
	bsf			WPUA,1	;trigger pullup
	bcf			WPUA,2	;switch22pullup
	bsf			WPUA,4	;jumper pullup
	bcf			WPUA,5	;valve pullup

	bcf			STATUS,RP0
	clrf		PORTC
	bsf			STATUS,RP0
	clrf		ANSEL
	movlw		b'00000000' ;  input  0,1,2,3,4,5 output
	movwf		TRISC
	bcf			STATUS,RP0

count1			equ		0x20
count2			equ		0x21
count3			equ		0x22
dwell			equ		0x23
eyedelay		equ	0x24
debounce		equ	0x25
tmp				equ	0x26
CDEL			equ	0x27	;
semi_force_time	equ	0x28	; time to disable eye
eye_mode		equ	0x29	;0 break ff bounce
abs_count1		equ	0x2a

ramp_active		equ	0x2b	;not used
auto_off_count3	equ	0x2c

ramp_adj_cntdwn	equ	0x2d

eye_block_cntdwn equ	0x2e

ramp_cntdwn		equ	0x2f
ball_drop		equ	0x30	
valve_dwell		equ	0x31
valve_on_delay	equ	0x32
valve_disable	equ 0x33
valve_eye_delay	equ 0x34

malfun_eye_cntdwn	equ	0x35

tmp_addr		equ	0x36
tmp_data		equ 0x37
ramp_cont1		equ	0x38
ramp_cont2		equ	0x39
ROF_count		equ	0x3a	;keeps track of how long it takes for the ball to load
ramp_pulls	equ	0x3b
ramp_speed		equ	0x3c
ramp_speed_tmp	equ	0x3d
trig_speed		equ	0x3e
trig_speed_tmp	equ	0x3f
flags			equ	0x40	;0-trigger pulled, 1-ramping active, 2 - trigger released
trig_active_pull	equ	0x41
after_nitro		equ	0x42	;mode after nitro
ramp_pulls_tmp	equ	0x43

temp_dwell		equ 0x44	
temp_eyedelay	equ	0x45
temp_tournement	equ 0x46
mode			equ	0x51	;0 semi, 1 rt, 2 burst, 3 full, 4 RT flag, 5 burst flag, 6 shotqing on
rof				equ 0x48
true_rof		equ 0x49	;actual ROF of marker after subtracting because of mode
burst_count		equ	0x4a
shotq_count1 	equ	0x4b
shotq_count2 	equ	0x4c
flag			equ	0x4d
auto_off_count1	equ	0x4e
auto_off_count2	equ	0x4f
eye_disabled_ROF	equ	0x50

eyedelay_temp	equ	0x54
nxl_release_ctndwn	equ	0x55
ROF_raw			equ	0x56	;21-0 21 = 9   0=30
Eye_delay_real	equ	0x57
PSP_runaway		equ 0x58	;only fires to shots if trig released
CON				equ 0x59
CTO				equ 0x5A
COFF			equ	0x5B
Shot_qcount		equ	0x5C




#define	Zero		STATUS,2

#define	semi	mode,0		;semi flag
#define	rt		mode,1		;rt flag
#define	burst	mode,2		;burst flag
#define	full	mode,3		;full flag
#define	nitro	mode,4		;ramp flag
#define	nxl		mode,5		;nxl flag
#define	psp		mode,6		;psp flag
#define	milen	mode,7		;crazy flag
  
#define	training			flags,0		;semi flag
#define	eye_type			flags,1		;0-bouncebeam
#define	shotq_flag			flags,2		;burst flag
#define	after_nitro_LED_flag	flags,3		;rt flag
#define	Instant_ON_enabled	flags,4		;0 goes to nitro, 1 goes to after_nitro
#define	valve_mode			flags,5		;classic or automatic
;#define	abs_flag			flags,6		;abs_flagflag
#define	shotq_enabled		flags,7		; flag

#define	trig_releas_flag		flag,0		;
#define	rt_flag					flag,1		;ramping flag
;#define	trig_released			flag,2		; trigger released flag
#define	ramp_trig_flag			flag,3		;rt flag
#define	eye_blocked_flag		flag,4		;0 goes to semi, 1 goes to ramp
;#define	eye_installed			flag,5		;nxl flag
#define	rof_zero_flag			flag,6		;psp flag
;#define	LBI_flickering			flag,7		;stop all when LBI flickering

#define	after_nitro_semi		after_nitro,0		;
#define	after_nitro_rt			after_nitro,1		;
#define	after_nitro_burst		after_nitro,2		;
#define	after_nitro_full		after_nitro,3		;
#define	after_nitro_nitro		after_nitro,4		;
#define	after_nitro_psp			after_nitro,5		;
#define	after_nitro_nxl			after_nitro,6		;
#define	after_nitro_milen			after_nitro,7		; 

#define	eye_disabled				tmp,0		;
#define	trigger_flag				tmp,1		;
#define	ramping_flag				tmp,2		;
;#define	after_nitro_full		tmp,3		;
#define		malfun_eyes_flag		tmp,4		;
#define		dry_fire_flag			tmp,5		;
;#define		bolt_clear			tmp,6		;set when bolt clears eye
#define		nxl_release_flag		tmp,7


	bcf			solenoid
	bcf			valve
	bcf			RED_LED
	bcf 		GREEN_LED
	bcf	 		BLUE_LED

	movlw	0x30
	call	readee
	movwf	flags

	btfsc	Instant_ON_enabled
	goto	turnon2
	btfsc	switch2	
	goto	turnon	;if switch2, turn on,  else turn off.
	call	switch2_release1

	
wait_release
	bcf		RED_LED
	bcf		GREEN_LED
	bcf		BLUE_LED
	call	switch2_release1
	bcf		eyepower
	bcf		eye_disabled
	btfsc	Instant_ON_enabled
	goto	turnon2
	bcf		shutdown	;turns marker off  FUCKING FINALLY!!

wait
	bcf		shutdown	;turns marker off  FUCKING FINALLY!!
	btfss	switch2	
	goto	wait

turnon
	bsf		shutdown	;turns on
	call	debounce_all
	bsf		RED_LED
	movlw	0x50
	movwf	count2
on_loop
	decfsz count1,1	;trigger count for eye disable
	goto	on_loop
	btfss	switch2		;next command on up
	goto	wait_release		;trigger released,
	decfsz count2,1
	goto	on_loop
	bcf		RED_LED
	bsf		BLUE_LED

turnon2
	bsf		shutdown

init
	clrf		count1
	clrf		count2
	clrf		mode
	clrf		malfun_eye_cntdwn		;5 trig pulls without eye unclearing, dieables them
	clrf		after_nitro		;
	bcf			solenoid
	bcf			RED_LED
	bcf			GREEN_LED
	bcf			BLUE_LED
	bcf			rof_zero_flag
	bsf			eyepower ;Turns on eye sensor
	clrf		flag
	bcf			dry_fire_flag
	movlw	0x4		
	movwf	ramp_cntdwn		;on forth trig pull, start ramping	
	movlw	0x0A				;10=10 minutes to auto off	
	movwf	auto_off_count3
	movlw	0xFF
	movwf	eye_block_cntdwn	;speed of no ball flash
	movlw	0x05
	movwf	malfun_eye_cntdwn	;pull trigger 5 times without firing and it autodisable the eyes
	movlw	0x0C				;12=3 sec	
	movlw	0x05				;	
	movwf	nxl_release_ctndwn
; Test to reset default setting	

	

mem

	movlw	0x00
	call	readee
	movwf	ROF_raw

	movlw	0x05
	call	readee
	movwf	dwell

	movlw	0x07
	call	readee
	movwf	eyedelay

	movlw	0x0A
	call	readee
	movwf	debounce

	movlw	0x10
	call	readee
	movwf	flags

	movlw	0x15
	call	readee
	movwf	semi_force_time

	movlw	0x1A
	call	readee
	movwf	after_nitro

	movlw	0x20
	call	readee
	movwf	CDEL

	movlw	0x25
	call	readee
	movwf	CON

	movlw	0x2A
	call	readee
	movwf	CTO

	movlw	0x30
	call	readee
	movwf	mode

	movlw	0x35
	call	readee
	movwf	ball_drop

	movlw	0x3A
	call	readee
	movwf	COFF

	movlw	0x40
	call	readee
	movwf	ramp_pulls

	movlw	0x45
	call	readee
	movwf	trig_speed


tourney_check
	btfss	DIP4
	goto	tourney
	btfsc	switch1 
	goto	setup	;enter setup mode

tourney		;tourney lock out, no setup.
	call	switch2_release1
	btfsc	semi
	goto	test_dwell	;if in NPPL, no ROF adjust

;----------------------------------tournement lock-----------------------------
;see if DIP4 is ON.
	btfsc	DIP4
	goto	test_dwell

	movf	dwell,0
	movwf	temp_dwell
	movf	eyedelay,0	;w=eye delay

	addwf	temp_dwell,0	;rof+ramp_speed -> w
	movwf	count1	;ramp+eye delay
	movlw	0x54
	btfsc	milen
	movlw	0x4c
	movwf	count2	;0x55 which is the milen value for 15bps ramp

test_ramp_speed
	bsf		tmp,1	;tmp1 = current setting  
	decfsz 	count1,1
	bcf		tmp,1
	btfsc 	tmp,1
	goto	illegal 	;marker illegal

	bsf		tmp,2	;tmp2 = minumum setting=55h
	decfsz 	count2,1
	bcf		tmp,2
	btfsc	tmp,2
	goto	test_dwell		;marker legal

	goto	test_ramp_speed

illegal_milen	
	movf	dwell,0
	sublw	0x4c
	movwf	eyedelay	;set to 15 bps ROF
	movlw	0x04
	movwf	ramp_pulls	;set ramp pulls to 4
	goto	test_dwell

illegal
	btfsc	milen
	goto	illegal_milen
	movf	dwell,0
	sublw	0x54
	movwf	eyedelay	;set to 15 bps ROF
	movlw	0x04
	movwf	ramp_pulls	;set ramp pulls to 4


;--------------------------------------------------------------------------------

test_dwell


test_dwell2
	btfss	trigger	;if trigger held, change modes
	goto	change_mode
; Test to reset default setting	
	btfss	dwell,0
	goto	main_release	
	btfss	dwell,1
	goto	main_release	
	btfss	dwell,2
	goto	main_release	
	btfss	dwell,3
	goto	main_release	
	btfss	dwell,4
	goto	main_release	
	btfss	dwell,5
	goto	main_release	
	btfss	dwell,6
	goto	main_release	
	btfss	dwell,7
	goto	main_release	
	goto	setup4



main_release
	call	ROF_set
	bcf		ramping_flag
;	bcf		eyepower
	bsf		eyepower
	btfss	eye_disabled
;	bcf		eyepower
	bsf		eyepower	
	call	switch2_release1
	bsf		eye_blocked_flag
;--------------eye disable ROF adjust---------------
;	movf	eyedelay,0	;DWELL
;	addwf	CON,0	;CON
;	subwf	eyedelay,0			;80-50 ->30->w
	movlw	0x1c
	movwf	eye_disabled_ROF	;
;--------------------------------------------------








main1
;----------------------------------------------------------------------------
	call	set_LED		;setup LEDs according to mode
	movlw	0x03
	movwf	burst_count	;number of burst fires
	bcf		rt_flag		;set rt_flag for rt trigger
	call	one_ms_delay
	btfsc	switch2		;push switch2
	goto	off
	btfsc	switch1
	call	eye_disabled1

;--------------------auto_off-----------------------
	btfsc	trigger_flag
	goto	auto_off_reset
	decfsz	auto_off_count1,1	; coutdown to activate off
	goto 	Auto_Off_done			
	decfsz	auto_off_count2,1	; coutdown to activate off
	goto 	Auto_Off_done	
	decfsz	auto_off_count3,1	; coutdown to activate off
	goto 	Auto_Off_done	
	goto	wait_release

auto_off_reset
	movlw	0x0A				;0A=10 minutes to auto off	
	movwf	auto_off_count3
;-----------------------------------------------------
Auto_Off_done



;-------------skip, no ramping modes-------------
	btfsc	psp		
	goto	ramp_modes	;PSP mode active
	btfss	nxl		
	goto	main_no_ramp	;all others go here	

;-------------------------------ramping-----------------------------
ramp_modes
	btfss	trigger_flag		;if trigger hasn't been pulled skip ramping shit
	goto	no_trig
no_ramp						;not ramping yet
	decfsz	trig_speed_tmp,1	;minumim trig pull speed to activate ramp
	goto	no_reset
	goto	reset_all		;minimum pull speed not atained, reset

no_reset
	btfsc	trigger
	bsf		trig_releas_flag
	btfsc	nxl		;if nxl mode, set trig release flag
	bsf		trig_releas_flag
	btfss	ramping_flag
	goto	no_trig
	goto	pull

no_trig	
	btfsc	trigger
	goto	trigger_released	;trigger released, set release flag
	btfsc	ramping_flag
	call	reset_ramping_trig
	goto 	pull

reset_ramping_trig			;resets the trig timer to keep ramp going
	btfss	trig_releas_flag		;see if the trigger has been released
	return
	movf	trig_speed,0		;resets min trig speed
	movwf	trig_speed_tmp		;minimum trigger pull speed.
	bcf		trig_releas_flag
	movlw	0x04
	movwf	PSP_runaway		;two shots with trig released
	return

trigger_released
	bsf		trig_releas_flag	;set release flag
	goto	main1

reset_all
	bcf		trigger_flag
	bcf		ramping_flag	
	bcf		trig_releas_flag
	movf	trig_speed,0		;resets min trig speed
	movwf	trig_speed_tmp		;minimum trigger pull speed.
	movf	ramp_pulls,0		;moves number of pulls to cntdwn
	movwf	ramp_cntdwn			;4 pulls to ramp
	goto 	release


;---------------------------------------------------------------------
main_no_ramp
	bcf		trigger_flag
	bcf		ramping_flag
	btfss	trigger
	goto 	pull
	btfsc	shotq_flag
	goto	shotq_check
	goto	main1

shotq_check
	btfss	shotq_enabled
	goto	main1
	movlw	0x20
	movwf	Shot_qcount
	btfsc	semi
	goto	pull
	btfsc	rt
	goto	pull
	goto	main1


;---------------------firing portion----------------------
pull	
	call	clear_LEDs
	bcf		shotq_flag
	bsf		trigger_flag
	movlw	0x00
	movwf	ROF_count 	;reset to zero

;-----------------countdown to ramp-----------------------------
	btfsc	psp		
	goto	ramp_decr	;PSP mode active
	btfss	nxl		
	goto	pull_no_ramp	;NXL mode active
ramp_decr	
	decfsz	ramp_cntdwn,1
	goto	pull_no_ramp
	bsf		ramping_flag
;-------------------------------------------------------------

pull_no_ramp


ROF_delay				; eye is disabled
	btfsc	ramping_flag
	goto	fire_ramp_check		;skip eye delay if ramping
	btfsc	semi
	goto	fire
	btfsc	rt
	goto	fire



nxl_no_shot
	btfsc	trigger
	goto	release
	goto	fire

;----------------------------rof delay for ramping-------------------
fire_ramp_check
	movlw	0x01
	movwf	count2
fire_ramp_loop
	call	one_ms_delay
	btfsc	trigger		;check to see if trigger pulled
	goto	no_release_trig
	btfsc	trig_releas_flag	;if trig released, reset trigger cntdwn
	call	reset_ramping_trig	;resets countdown for trigger pull
no_release_trig
	decfsz	trig_speed_tmp,1	;minumim trig pull speed to activate ramp
	goto	fire_ramp_skip
	goto	reset_all		;minimum pull speed not atained, reset
fire_ramp_skip
	decfsz 	count2,1
	goto	fire_ramp_loop
	bcf		shotq_flag
	goto	fire
;---------------------------------------------------------------
;----------------------------------------------
nxl_glitch
	btfsc	trigger	
	goto	fire_ramp
	goto	fire_noglitch
;-----------------------------------------------------
fire
;-----------------------------------------------------
	movlw	0x01
	movwf	count2
loop_fire
	call	one_ms_delay
	decfsz 	count2,1
	goto	loop_fire

fire_no_abs
;-----------------nxl glitch--------------
	btfsc	nxl	
	goto	nxl_glitch
;-------------------------------------------
fire_noglitch
	btfss	psp
	goto	skip_psp_test
	btfss	ramping_flag
	goto	skip_psp_test
	decfsz	PSP_runaway,1		;two shots, then stop.
	goto	skip_psp_test
	goto	psp_plus1



;--------------required for firing-------------------
;----------------------------------------------------
skip_psp_test
	btfsc	training
	goto	training_skip
	bsf		solenoid
	call	delay_on1
	bcf		solenoid
	goto	fire_continue

training_skip
	call	delay_on1	;keeps timing

;-------CDEL---Delay for valve to activate----------------	
fire_continue
;	bsf		eyepower	;turn on eye
	movf	CDEL,0 ;CDEL
	movwf	count2
valve_on_loop		
	call	one_ms_delay
	decfsz 	count2,1
	goto	valve_on_loop	

;----------Classic or automatic?----------------------------
	btfsc	valve_mode	;classic or adjust
	goto	classic

;---------activate valve----------------------
	bsf		valve	
valve_ON
	movf	CON,0
	movwf	count2
CON_loop_release1
	call	one_ms_delay
	decfsz 	count2,1
	goto	CON_loop_release1


;-------------------------------------------------------------------
valve_release
	movf	CTO,0		;Valve timeout
	movwf	count2
CTO_delay_loop
	call	one_ms_delay

	btfsc	eye_disabled	;eye enabled, goto valve_eye_delay
	goto	eye_disabled_CTO	;eye disabled, just wait CON
;-------------------------------------------
	btfsc	trigger
	bsf		trig_releas_flag	;trigger released

	btfsc	trigger
	goto	skip_CTO
	btfss	trig_releas_flag
	goto	skip_CTO	
	decfsz	Shot_qcount,1		;must release for 30ms before shotQ accepted
	goto	skip_CTO
	btfsc	semi
	bsf		shotq_flag
	btfsc	rt
	bsf		shotq_flag
	bcf		trig_releas_flag
skip_CTO
	btfss	eye_type
	goto	break_valve_eye	;goto breakbeam eye

	btfss	eyeout			;bouncebeam
	goto	ball_drop_delay		;bolt cleared, ball detected
	goto 	CTO_wait		;eye is blocked

break_valve_eye
	btfsc	eyeout
	goto 	ball_drop_delay		;eye is blocked, exit wait

CTO_wait
;---------------------ROF adjustment-----------------------
	movf	ROF_count,0	;move to W
	sublw	0xFF		;minus 255
	btfsc	Zero		;if zero, skip incrementing more.
	goto	CTO_cont
	incf	ROF_count,1	;increaments for ROF-adjustment
;-----------------------------------------------------------
CTO_cont
	decfsz 	count2,1
	goto	CTO_delay_loop


;------------------Eyes disabled set CTO to ROF----------------------------
eye_disabled_CTO
	movf	eye_disabled_ROF,0		
	movwf	count2
no_eye_CTO_loop
	call	one_ms_delay	
	decfsz 	count2,1
	goto	no_eye_CTO_loop
;-----------------------------------------------

ball_drop_delay
	movf	ball_drop,0
	movwf	count2
ball_drop_loop
	call	one_ms_delay
	decfsz 	count2,1
	goto 	ball_drop_loop



close_valve
	clrf	count2		;sets to zero if skips out of loop early
;---------------------close valve----------------
	bcf		valve
;	bcf		eyepower	;turn off eye

	movf	COFF,0		;min valve off time
	movwf	count2
COFF_delay_loop
	call	one_ms_delay
	btfsc	trigger
	bsf		trig_releas_flag	;trigger released

	decfsz 	count2,1
	goto	COFF_delay_loop
;----------------------------------------------------
;----------------------------------------------------

	btfsc	semi
	goto	after_shot
	btfsc	rt
	goto	after_shot
	btfsc	burst
	goto	nonramping_ROF
	btfsc	full
	goto	nonramping_ROF
	btfsc	milen
	goto	nonramping_ROF
	btfsc	psp
	goto	ramping_ROF
	btfsc	nxl
	goto	ramping_ROF

nonramping_ROF
	btfsc	eye_disabled	;eye enabled, goto valve_eye_delay
	goto	nonramping_ROF_noeye		
	movf	eyedelay,0
	movwf	count2			;count2=eyedelay
	movf	ROF_count,0		;w=ROF_cout
	subwf	count2,1		;count2-w->count2
	btfss	Zero		;if zero
	goto	loop_eye1
nonramping_ROF_noeye
	movlw	0x01
	movwf	count2		;if less than zero, set count to 1
loop_eye1
	call	one_ms_delay
	decfsz 	count2,1
	goto	loop_eye1
	goto	after_shot

ramping_ROF
	btfsc	eye_disabled	;eye enabled, goto valve_eye_delay
	goto	ramping_ROF_noeye	
	movf	eyedelay,0
	movwf	count2			;count2=eyedelay
	movf	ROF_count,0		;w=ROF_cout
	subwf	count2,1		;count2-w->count2
	btfss	Zero		;if zero
	goto	fire_ramp_loop2
ramping_ROF_noeye
	movlw	0x01
	movwf	count2		;if less than zero, set count to 1
fire_ramp_loop2
	call	one_ms_delay
	btfsc	trigger		;check to see if trigger pulled
	goto	no_release_trig2
	btfsc	trig_releas_flag	;if trig released, reset trigger cntdwn
	call	reset_ramping_trig	;resets countdown for trigger pull
no_release_trig2
	decfsz	trig_speed_tmp,1	;minumim trig pull speed to activate ramp
	goto	fire_ramp_skip2
	goto	reset_all		;minimum pull speed not atained, reset
fire_ramp_skip2
	decfsz 	count2,1
	goto	fire_ramp_loop2


after_shot
;	btfsc	rt
;	goto	rt_firing
	goto	fire_ramp

psp_plus1
	incf	PSP_runaway,1

fire_ramp
;---------full auto---------------------
	btfsc	full
	goto	full_auto
	btfsc	nitro		;goto full auto on first pull
	goto	full_auto
;---------------------------------

;------------burst-------------------
	btfss	burst	;if not burst goto release else burst
	goto	release
	btfsc	trigger		;if trigger still pulled, burst
	goto	release
	decfsz	burst_count,1
	goto	pull	
	goto	release
;---------------------------------------

full_auto
	btfss	trigger
	goto	pull

;------------------nitro mode-----------------------
after_nitro_mode
	btfss	nitro
	goto	release

	bcf		nitro
	btfsc	after_nitro_semi
	bsf		semi
	btfsc	after_nitro_rt
	bsf		rt
	btfsc	after_nitro_burst
	bsf		burst
	btfsc	after_nitro_nxl
	bsf		nxl
	btfsc	after_nitro_psp
	bsf		psp
	btfsc	after_nitro_milen
	bsf		milen
	btfsc	after_nitro_full
	bsf		full
;--------------------------------------------------


release
	btfsc	full
	goto	main1
;------------------ramping----------------------------
	btfss	nxl			;chekc for nxl mode
	goto	nxl_no_release
	btfss	ramping_flag	;check that it is ramping
	goto	nxl_no_release	
	btfss	nxl_release_flag	;check if release flag set
	goto	no_nxl_release_flag
	btfss	trigger		;if trigger released, clear ramp flag
	goto	nxl_no_release
	bcf		ramping_flag
	bcf		nxl_release_flag
	movlw	0x05				;	
	movwf	nxl_release_ctndwn
	goto	nxl_no_release
no_nxl_release_flag
	decfsz	nxl_release_ctndwn,1
	goto	nxl_no_release
	bsf		nxl_release_flag

;-------------------------------------------------------
nxl_no_release
	btfsc	ramping_flag		;skip debounce if ramping 
	goto	main1

;------------------ramping----------------------------


release_valve
	movf	semi_force_time,0
	movwf	count3
loop_release1
	call	one_ms_delay
	btfsc	trigger		;next command on 1
	goto	debounce1		;trigger released,
	decfsz 	count2,1
	goto	loop_release1
	decfsz 	count3,1
	goto	loop_release1
	bsf		valve_mode  	;classic mode
	clrf 	mode
	bsf		semi
	bsf		RED_LED

debounce1
	movf	debounce,0
	movwf	count2	
loop_debounce
	call	one_ms_delay
	btfss	trigger		;next command on 0
	goto 	debounce1		;pull trigger
	decfsz 	count2,1
	goto	loop_debounce
	call	set_LED		;setup LED to current mode
	btfss	rt
	goto	main1
;---------------------rt---------------------
	btfsc	rt_flag		;if rt_flag is set it already fired
	goto	main1
	bsf		rt_flag
	goto	pull
;---------------------------------------------

;---------------classic mode-----------------------------------------
classic
	bsf		valve
	movlw	0x15	;debounce_hold
	movwf	count2
eye_disable_hold
	call	one_ms_delay
	decfsz 	count2,1
classic_loop
	btfss	trigger		;next command on 0	
	goto	classic_loop
	bcf		valve
	goto	debounce1


delay_on1
	movf	dwell,0
	movwf	count2
loop_on1
	decfsz 	count1,1
	goto	loop_on1
	movlw	0x55		;only count 55->0  1/3 of 255
	movwf	count1
	decfsz 	count2,1
	goto	loop_on1
	return


debounce_all
	movlw	0x50
	movwf	count2	
loop_debounce_all
	call	one_ms_delay
	decfsz 	count2,1
	goto	loop_debounce_all
	return

trigger_release1
	call	debounce_all
	btfss	trigger		;next command on 0	
	goto	trigger_release1
	call	debounce_all
	return


switch1_release1
	call	debounce_all
	btfsc	switch1		;next command on 0	
	goto	switch1_release1
	call	debounce_all
	return

	
switch2_release1
	call	debounce_all
	btfsc	switch2		;next command on 0	
	goto	switch2_release1
	call	debounce_all
	return

	
semi_force
	clrf	mode
	bsf		semi
	bsf		RED_LED
	call	trigger_release1
	return

;---------------------------eye disable---------------------------------
eye_disabled1
	call	clear_LEDs
	movlw	0x02
	movwf	count3
eye_disabled_loop
	call	one_ms_delay
	decfsz 	count2,1
	goto	eye_disabled_loop
	decfsz 	count3,1
	goto	eye_disabled_loop

	btfsc	eye_disabled		;if enabled then disable
	goto	dry_fire
	bsf		eye_disabled
	bcf		dry_fire_flag
	bsf		RED_LED
	bcf		eyepower
;	movf	eyedelay,0
;	movwf	eyedelay_temp	;saves rof setting	
;	movlw	0x54	;12 bps
;	movwf	eyedelay	
	call	switch1_release1
	call	set_LED		;setup LEDs according to mode
	return

dry_fire
	btfsc	dry_fire_flag
	goto	eye_enable
	bsf		dry_fire_flag
	bsf		BLUE_LED
	bsf		valve_mode		;classis mode
	call	switch1_release1
	call	set_LED		;setup LEDs according to mode
	return

eye_enable
	bcf		dry_fire_flag
	bcf		eye_disabled
	bcf		valve_mode		;auto mode
	bsf		GREEN_LED
	bsf		eyepower
	call	switch1_release1
	call	set_LED		;setup LEDs according to mode
	return
;----------------------------------------------------------------------

;**************************setup*************************************
setup
	bcf		RED_LED
	movlw	0x09
	movwf	count3
loop_setup9
	decfsz count1,1	;trigger count for eye disable
	goto	loop_setup9
	decfsz count2,1
	goto	loop_setup9
	decfsz count3,1
	goto	loop_setup9
	
	bsf	RED_LED
loop_flash_off2_1
	decfsz 	count1,1
	goto	loop_flash_off2_1
	decfsz 	count2,1
	goto	loop_flash_off2_1
	bcf		RED_LED

	movlw	0x09
	movwf	count3
loop_setup1
	decfsz count1,1	;trigger count for eye disable
	goto	loop_setup1
	decfsz count2,1
	goto	loop_setup1
	btfss	switch1		;next command on 1
	goto	setup1		;trigger released, 
	decfsz count3,1
	goto	loop_setup1
	goto	setup_flash2	;trigger held long enough for eye disable

setup1
	call	debounce_all
	call	trigger_release1
	call	switch1_release1
	call	switch2_release1
	bsf		RED_LED

;*******************************ROF***************************

setup_eyedelay_delay
	btfss	trigger
	goto 	set_eyedelay
	btfsc	switch1
	goto	setup_dwell_enter
	btfsc	switch2
	goto	exit
	goto	setup_eyedelay_delay
	
set_eyedelay
	bcf		RED_LED
	call	debounce_all
	movlw	0x29
	movwf	ROF_raw
	call	trigger_release1
	bsf		RED_LED

inc_eyedelay
	btfss	trigger
	goto	inc_eyedelay_1
	btfsc	switch1
	goto	setup_dwell_enter
	btfsc	switch2
	goto	exit
	goto	inc_eyedelay

inc_eyedelay_1
	bcf		RED_LED
	call	debounce_all
	decf	ROF_raw,1
	call	trigger_release1
;	call	ROF_set
	bsf		RED_LED
	goto	inc_eyedelay



ROF_same
;--------------takes 1/3 of dwell---------------------
	movlw	0x00
	movwf	count3		;reset count3	
	movf	dwell,0
	movwf	tmp			;move Dwell into tmp
dwell_adj
	incf	count3,1
	decfsz	tmp,1
	goto	dwell_adj1
	goto	dwell_adj_done
dwell_adj1
	decfsz	tmp,1
	goto	dwell_adj2
	goto	dwell_adj_done
dwell_adj2
	decfsz	tmp,1
	goto	dwell_adj
	goto	dwell_adj_done
;-------------------------------------------------------
dwell_adj_done
	movf	count3,0	;CDEL ->w
	addwf	CDEL,0
	addwf	CON,0
	addwf	COFF,0
	return

ROF_set
ROF_9
	movf	ROF_raw,0
	sublw	0x29
	btfss	Zero
	goto	ROF_10
	call	ROF_same
	sublw	0x8d				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_10
	movf	ROF_raw,0
	sublw	0x28
	btfss	Zero
	goto	ROF_11
	call	ROF_same
	sublw	0x7f				;0x6b-w ->w
	movwf	eyedelay
	return
	
ROF_11
	movf	ROF_raw,0
	sublw	0x27
	btfss	Zero
	goto	ROF_12
	call	ROF_same
	sublw	0x75				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_12
	movf	ROF_raw,0
	sublw	0x26
	btfss	Zero
	goto	ROF_13
	call	ROF_same
	sublw	0x6b				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_13
	movf	ROF_raw,0
	sublw	0x25
	btfss	Zero
	goto	ROF_14
ROF_13_force
	call	ROF_same
	sublw	0x62				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_14
	movf	ROF_raw,0
	sublw	0x24
	btfss	Zero
	goto	ROF_15
	call	ROF_same
	sublw	0x5c				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_15
	movf	ROF_raw,0
	sublw	0x23
	btfss	Zero
	goto	ROF_16
ROF_15_force
	call	ROF_same
	sublw	0x55				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_16
	movf	ROF_raw,0
	sublw	0x22
	btfss	Zero
	goto	ROF_17
	call	ROF_same
	sublw	0x50				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_17
	movf	ROF_raw,0
	sublw	0x21
	btfss	Zero
	goto	ROF_18
	call	ROF_same
	sublw	0x4b				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_18
	movf	ROF_raw,0
	sublw	0x20
	btfss	Zero
	goto	ROF_19
	call	ROF_same
	sublw	0x47				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_19
	movf	ROF_raw,0
	sublw	0x1f
	btfss	Zero
	goto	ROF_20
	call	ROF_same
	sublw	0x43				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_20
	movf	ROF_raw,0
	sublw	0x1e
	btfss	Zero
	goto	ROF_21
	call	ROF_same
	sublw	0x40				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_21
	movf	ROF_raw,0
	sublw	0x1d
	btfss	Zero
	goto	ROF_22
	call	ROF_same
	sublw	0x3d				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_22
	movf	ROF_raw,0
	sublw	0x1c
	btfss	Zero
	goto	ROF_23
	call	ROF_same
	sublw	0x3a				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_23
	movf	ROF_raw,0
	sublw	0x1b
	btfss	Zero
	goto	ROF_24
	call	ROF_same
	sublw	0x38				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_24
	movf	ROF_raw,0
	sublw	0x1a
	btfss	Zero
	goto	ROF_25
	call	ROF_same
	sublw	0x36				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_25
	movf	ROF_raw,0
	sublw	0x19
	btfss	Zero
	goto	ROF_26
	call	ROF_same
	sublw	0x33				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_26
	movf	ROF_raw,0
	sublw	0x18
	btfss	Zero
	goto	ROF_27
	call	ROF_same
	sublw	0x31				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_27
	movf	ROF_raw,0
	sublw	0x17
	btfss	Zero
	goto	ROF_28
	call	ROF_same
	sublw	0x30				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_28
	movf	ROF_raw,0
	sublw	0x16
	btfss	Zero
	goto	ROF_29
	call	ROF_same
	sublw	0x2f				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_29
	movf	ROF_raw,0
	sublw	0x15
	btfss	Zero
	goto	ROF_30
	call	ROF_same
	sublw	0x2c				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_30
	movf	ROF_raw,0
	sublw	0x14
	btfss	Zero
	goto	ROF_31
	call	ROF_same
	sublw	0x2a				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_31
	movf	ROF_raw,0
	sublw	0x13
	btfss	Zero
	goto	ROF_32
	call	ROF_same
	sublw	0x29				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_32
	movf	ROF_raw,0
	sublw	0x12
	btfss	Zero
	goto	ROF_33
	call	ROF_same
	sublw	0x28				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_33
	movf	ROF_raw,0
	sublw	0x11
	btfss	Zero
	goto	ROF_34
	call	ROF_same
	sublw	0x27				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_34
	movf	ROF_raw,0
	sublw	0x10
	btfss	Zero
	goto	ROF_35
	call	ROF_same
	sublw	0x26				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_35
	movf	ROF_raw,0
	sublw	0x0f
	btfss	Zero
	goto	ROF_36
	call	ROF_same
	sublw	0x25				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_36
	movf	ROF_raw,0
	sublw	0x0e
	btfss	Zero
	goto	ROF_37
	call	ROF_same
	sublw	0x24				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_37
	movf	ROF_raw,0
	sublw	0x0d
	btfss	Zero
	goto	ROF_38
	call	ROF_same
	sublw	0x23				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_38
	movf	ROF_raw,0
	sublw	0x0c
	btfss	Zero
	goto	ROF_39
	call	ROF_same
	sublw	0x22				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_39
	movf	ROF_raw,0
	sublw	0x0b
	btfss	Zero
	goto	ROF_40
	call	ROF_same
	sublw	0x21				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_40
	movf	ROF_raw,0
	sublw	0x0a
	btfss	Zero
	goto	ROF_41
	call	ROF_same
	sublw	0x20				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_41
	movf	ROF_raw,0
	sublw	0x09
	btfss	Zero
	goto	ROF_42
	call	ROF_same
	sublw	0x1f				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_42
	movf	ROF_raw,0
	sublw	0x08
	btfss	Zero
	goto	ROF_43
	call	ROF_same
	sublw	0x1e				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_43
	movf	ROF_raw,0
	sublw	0x07
	btfss	Zero
	goto	ROF_44
	call	ROF_same
	sublw	0x1d				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_44
	movf	ROF_raw,0
	sublw	0x06
	btfss	Zero
	goto	ROF_45
	call	ROF_same
	sublw	0x1c				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_45
	movf	ROF_raw,0
	sublw	0x05
	btfss	Zero
	goto	ROF_46
	call	ROF_same
	sublw	0x1b				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_46
	movf	ROF_raw,0
	sublw	0x04
	btfss	Zero
	goto	ROF_47
	call	ROF_same
	sublw	0x1a				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_47
	movf	ROF_raw,0
	sublw	0x03
	btfss	Zero
	goto	ROF_48
	call	ROF_same
	sublw	0x19				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_48
	movf	ROF_raw,0
	sublw	0x02
	btfss	Zero
	goto	ROF_49
	call	ROF_same
	sublw	0x18				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_49
	movf	ROF_raw,0
	sublw	0x01
	btfss	Zero
	goto	ROF_50
	call	ROF_same
	sublw	0x17				;0x6b-w ->w
	movwf	eyedelay
	return

ROF_50
	call	ROF_same
	sublw	0x16				;0x6b-w ->w
	movwf	eyedelay
	return



;***************************dwell**************************************
setup_dwell_enter
	bcf		RED_LED
	call	switch1_release1
	call	switch2_release1

	bsf		GREEN_LED

setup_dwell_delay
	bsf		GREEN_LED
	btfss	trigger
	goto 	set_dwell
	btfsc	switch1
	goto	setup_debounce_enter
	btfsc	switch2
	goto	exit
	goto	setup_dwell_delay
	
set_dwell
	bcf		GREEN_LED
	movlw	0x09
	movwf	dwell
	call	trigger_release1
	bsf		GREEN_LED

inc_dwell
	btfss	trigger
	goto	inc_dwell_1
	btfsc	switch1
	goto	setup_debounce_enter
	btfsc	switch2
	goto	exit
	goto	inc_dwell

inc_dwell_1
	bcf		GREEN_LED
	incf	dwell,1
	call	trigger_release1
	bsf		GREEN_LED
	call	debounce_all
	goto	inc_dwell


;*****************************debounce*******************************

setup_debounce_enter
	bcf		GREEN_LED
	call	switch1_release1
	call	switch2_release1
	call	debounce_all
	bsf		BLUE_LED

setup_debounce_delay
	btfss	trigger
	goto 	set_debounce_delay
	btfsc	switch1
	goto	setup_classic_enter
	btfsc	switch2
	goto	exit
	goto	setup_debounce_delay
	
set_debounce_delay
	bcf		BLUE_LED
	movlw	0x01
	movwf	debounce
	call	trigger_release1
	bsf		BLUE_LED

inc_debounce
	btfss	trigger
	goto	inc_debounce_1
	btfsc	switch1
	goto	setup_classic_enter
	btfsc	switch2
	goto	exit
	goto	inc_debounce

inc_debounce_1
	bcf		BLUE_LED
	incf	debounce,1
	call	trigger_release1
	bsf		BLUE_LED
	goto	inc_debounce


;********************************Classic*******************************8
setup_classic_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
;	call	debounce_all
	call	white_LED

inc_classic
	btfss	trigger
	goto	inc_classic1
	btfsc	switch1						
	goto	setup_semi_force_time_enter
	btfsc	switch2
	goto	exit
	goto	inc_classic

inc_classic1
	call	clear_LEDs
	call	debounce_all
	call	trigger_release1
	btfsc	valve_mode
	goto	set_classic_off

set_classic_on
	bsf		valve_mode
	call	clear_LEDs
	goto	inc_classic

set_classic_off
	bcf		valve_mode ;auto
	call	white_LED
	goto	inc_classic


;******************************semi_force_time**************************

setup_semi_force_time_enter
	call	clear_LEDs
	call	switch1_release1
	call	switch2_release1
	call	debounce_all
	call	teal_LED

setup_semi_force_time_delay
	btfss	trigger
	goto 	set_semi_force_time_delay
	btfsc	switch1
	goto	setup_Qing_enter
	btfsc	switch2
	goto	exit
	goto	setup_semi_force_time_delay
	
set_semi_force_time_delay
	call	clear_LEDs
	movlw	0x01
	movwf	semi_force_time
	call	trigger_release1
	call	teal_LED

inc_semi_force_time
	btfss	trigger
	goto	inc_semi_force_time_1
	btfsc	switch1
	goto	setup_Qing_enter
	btfsc	switch2
	goto	exit
	goto	inc_semi_force_time

inc_semi_force_time_1
	call	clear_LEDs
	call	debounce_all
	incf	semi_force_time,1
	incf	semi_force_time,1
	call	trigger_release1
	call	teal_LED
	goto	inc_semi_force_time


;**************************setup2*************************************
setup_flash2

	bsf	RED_LED
	call LED_flash
	bcf		RED_LED

	call LED_flash
	bsf		RED_LED
	call LED_flash
	bcf		RED_LED


	movlw	0x09
	movwf	count3
loop_setup2
	call	one_ms_delay
	decfsz count2,1
	goto	loop_setup2
	btfss	switch1		;next command on 1
	goto	setup2		;trigger released, 
	decfsz count3,1
	goto	loop_setup2
	goto	setup_flash3	;trigger held long enough for eye disable		
;	goto	setup_flash4	;trigger held long enough for eye disable		

setup2
;	bsf		RED_LED
;	call	trigger_release1
;	call	switch1_release1
;	call	switch2_release1
;	bcf		RED_LED


;********************************ShotQing*******************************8
setup_Qing_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
;	call	debounce_all
	call	yellow_LED

inc_Qing
	btfss	trigger
	goto	inc_Qing1
	btfsc	switch1						
	goto	setup_after_nitro_enter
	btfsc	switch2
	goto	exit
	goto	inc_Qing

inc_Qing1
	call	clear_LEDs
	call	debounce_all
	call	trigger_release1
;	call	debounce_all
	btfsc	shotq_enabled
	goto	set_Qing_off

set_Qing_on
	bsf		shotq_enabled
	call	yellow_LED
;	call	debounce_all
	goto	inc_Qing

set_Qing_off
	bcf		shotq_enabled
	call	clear_LEDs
;	call	debounce_all
	goto	inc_Qing


;*****************************after_nitro**********************

setup_after_nitro_enter
	call	clear_LEDs
	call	switch1_release1
	call	switch2_release1
;	call	debounce_all
	call	purple_LED

setup_after_nitro
	call	purple_LED
	btfsc	after_nitro_LED_flag	;0=purple, 1=after color	
	call	after_nitro_color

after_nitro_loop1
	call	one_ms_delay
	btfss	trigger
	goto 	after_nitro_change
	btfsc	switch1
	goto	setup_trig_speed_enter
	btfsc	switch2
	goto	exit
	decfsz 	count2,1
	goto	after_nitro_loop1


	call	clear_LEDs
after_nitro_loop2
	call	one_ms_delay
	btfss	trigger
	goto 	after_nitro_change
	btfsc	switch1
	goto	setup_trig_speed_enter
	btfsc	switch2
	goto	exit
	decfsz 	count2,1
	goto	after_nitro_loop2

	btfsc	after_nitro_LED_flag	;if flag 1 then set to 0
	goto	clear_nitro_flag
	bsf		after_nitro_LED_flag
	goto	setup_after_nitro	
clear_nitro_flag
	bcf		after_nitro_LED_flag
	goto	setup_after_nitro


after_nitro_change
	call	trigger_release1

	btfsc	after_nitro_psp
	bsf		after_nitro_nxl
;	bcf		after_nitro_psp	; not used because of wrap around

	btfsc	after_nitro_milen
	bsf		after_nitro_psp
	bcf		after_nitro_milen

	btfsc	after_nitro_full
	bsf		after_nitro_milen
	bcf		after_nitro_full

	btfsc	after_nitro_burst
	bsf		after_nitro_full
	bcf		after_nitro_burst

	btfsc	after_nitro_rt
	bsf		after_nitro_burst
	bcf		after_nitro_rt

	btfsc	after_nitro_semi
	bsf		after_nitro_rt
	bcf		after_nitro_semi

	btfsc	after_nitro_nxl
	goto	nxl_set
	goto	setup_after_nitro

nxl_set
	btfsc	after_nitro_psp
	goto	clear_psp
set_semi
	bsf		after_nitro_semi
	bcf		after_nitro_nxl	
	goto	setup_after_nitro
clear_psp
	bcf		after_nitro_psp	
	goto	setup_after_nitro


after_nitro_color
	call	clear_LEDs
	btfsc	after_nitro_semi
	bsf		RED_LED
	btfsc	after_nitro_rt
	bsf		GREEN_LED
	btfsc	after_nitro_burst
	bsf		BLUE_LED
	btfsc	after_nitro_nxl
	goto	after_nitro_teal_LED
	btfsc	after_nitro_psp
	goto	after_nitro_yellow_LED
	btfsc	after_nitro_nitro
	goto	after_nitro_purple_LED
	btfsc	after_nitro_full
	goto	after_nitro_white_LED
	btfsc	after_nitro_milen
	bsf		RED_LED
	return

after_nitro_teal_LED		;teal
	bsf 	GREEN_LED
	bsf		BLUE_LED
	return

after_nitro_yellow_LED		;yellow
	bsf		GREEN_LED
	bsf		RED_LED
	return

after_nitro_purple_LED	;purple
	bsf 	RED_LED
	bsf		BLUE_LED
	return

after_nitro_white_LED	;white
	bsf 	RED_LED
	bsf		GREEN_LED
	bsf		BLUE_LED
	return


;----------------------------trig Speed-------------------------------------
setup_trig_speed_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
	bsf		GREEN_LED

setup_trig_speed
	btfss	trigger
	goto 	set_trig_speed
	btfsc	switch1 
	goto	setup_ramp_pulls_enter
	btfsc	switch2
	goto	exit
	goto	setup_trig_speed
	
set_trig_speed
	call	clear_LEDs
	call	debounce_all
	movlw	0xFF		;5bps
	movwf	trig_speed
	call	trigger_release1
	bsf		GREEN_LED

inc_trig_speed
	btfss	trigger
	goto	inc_trig_speed_1
	btfsc	switch1 
	goto	setup_ramp_pulls_enter
	btfsc	switch2
	goto	exit
	goto	inc_trig_speed

inc_trig_speed_1
	call	clear_LEDs
	decf	trig_speed,1
	decf	trig_speed,1
	decf	trig_speed,1
	decf	trig_speed,1
	decf	trig_speed,1
	decf	trig_speed,1
	decf	trig_speed,1
	decf	trig_speed,1
	call	trigger_release1
	bsf		GREEN_LED
;	call	debounce_all
	goto	inc_trig_speed

;----------------------------time to ramp-------------------------------------
setup_ramp_pulls_enter
	call	clear_LEDs
	call	switch1_release1
	call	switch2_release1
	bsf		BLUE_LED

setup_ramp_pulls
	btfss	trigger
	goto 	set_ramp_pulls
	btfsc	switch1
	goto	setup_CDEL_enter
	btfsc	switch2
	goto	exit
	goto	setup_ramp_pulls
	
set_ramp_pulls
	bcf		BLUE_LED
	call	debounce_all
	movlw	0x02
	movwf	ramp_pulls
	call	trigger_release1
	bsf		BLUE_LED

inc_ramp_pulls
	btfss	trigger
	goto	inc_ramp_pulls_1
	btfsc	switch1
	goto	setup_CDEL_enter
	btfsc	switch2
	goto	exit
	goto	inc_ramp_pulls

inc_ramp_pulls_1
	bcf		BLUE_LED
	incf	ramp_pulls,1
	call	trigger_release1
	bsf		BLUE_LED
;	call	debounce_all
	goto	inc_ramp_pulls


;***********************setup3********************************************

setup_flash3
	bsf	RED_LED
	call LED_flash
	bcf		RED_LED

	call LED_flash
	bsf		RED_LED
	call LED_flash
	bcf		RED_LED

	call LED_flash
	bsf		RED_LED
	call LED_flash
	bcf		RED_LED


	movlw	0x09
	movwf	count3
loop_setup3
	call	one_ms_delay
	decfsz count2,1
	goto	loop_setup3
	btfss	switch1		;next command on 1
	goto	setup_CDEL_enter		;trigger released, 
	decfsz count3,1
	goto	loop_setup3
	goto	setup_flash4	;trigger held long enough for eye disable

;----------------------------solenoid delay time------------------------
setup_CDEL_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
	call	white_LED

setup_CDEL
	btfss	trigger
	goto 	set_CDEL
	btfsc	switch1 
	goto	setup_CON_enter
	btfsc	switch2
	goto	exit
	goto	setup_CDEL
	
set_CDEL
	call	clear_LEDs
	call	debounce_all
	movlw	0x01		
	movwf	CDEL
	call	trigger_release1
	call	white_LED

inc_CDEL
	btfss	trigger
	goto	inc_CDEL_1
	btfsc	switch1 
	goto	setup_CON_enter
	btfsc	switch2
	goto	exit
	goto	inc_CDEL

inc_CDEL_1
	call	clear_LEDs
	incf	CDEL,1
	call	trigger_release1
	call	white_LED
;	call	debounce_all
	goto	inc_CDEL

;----------------------------valve on time------------------------
setup_CON_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
	call	teal_LED

setup_CON
	btfss	trigger
	goto 	set_CON
	btfsc	switch1 
	goto	setup_CTO_enter
	btfsc	switch2
	goto	exit
	goto	setup_CON
	
set_CON
	call	clear_LEDs
	call	debounce_all
	movlw	0x14		;50ms
	movwf	CON
	call	trigger_release1
	call	teal_LED

inc_CON
	btfss	trigger
	goto	inc_CON_1
	btfsc	switch1 
	goto	setup_CTO_enter
	btfsc	switch2
	goto	exit
	goto	inc_CON

inc_CON_1
	call	clear_LEDs
	incf	CON,1
	call	trigger_release1
	call	teal_LED
;	call	debounce_all
	goto	inc_CON


;----------------------------valve time out------------------------
setup_CTO_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
	call	yellow_LED

setup_CTO
	btfss	trigger
	goto 	set_CTO
	btfsc	switch1 
	goto	setup_ball_drop_enter
	btfsc	switch2
	goto	exit
	goto	setup_CTO
	
set_CTO
	call	clear_LEDs
	call	debounce_all
	movlw	0x01		;4x50ms		
	movwf	CTO
	call	trigger_release1
	call	yellow_LED

inc_CTO
	btfss	trigger
	goto	inc_CTO_1
	btfsc	switch1 
	goto	setup_ball_drop_enter
	btfsc	switch2
	goto	exit
	goto	inc_CTO

inc_CTO_1
	call	clear_LEDs
	incf	CTO,1
	incf	CTO,1
	incf	CTO,1
	incf	CTO,1
	incf	CTO,1
	incf	CTO,1
	incf	CTO,1
	incf	CTO,1
	call	trigger_release1
	call	yellow_LED
;	call	debounce_all
	goto	inc_CTO


;----------------------------valve time out------------------------
setup_ball_drop_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
	bsf		BLUE_LED

setup_ball_drop
	btfss	trigger
	goto 	set_ball_drop
	btfsc	switch1 
	goto	setup_COFF_enter
	btfsc	switch2
	goto	exit
	goto	setup_ball_drop
	
set_ball_drop
	call	clear_LEDs
	call	debounce_all
	movlw	0x01		;4x50ms		
	movwf	ball_drop
	call	trigger_release1
	bsf		BLUE_LED

inc_ball_drop
	btfss	trigger
	goto	inc_ball_drop_1
	btfsc	switch1 
	goto	setup_COFF_enter
	btfsc	switch2
	goto	exit
	goto	inc_ball_drop

inc_ball_drop_1
	call	clear_LEDs
	incf	ball_drop,1
	call	trigger_release1
	bsf		BLUE_LED
;	call	debounce_all
	goto	inc_ball_drop


;----------------------------valve off time------------------------
setup_COFF_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
	call	purple_LED

setup_COFF
	btfss	trigger
	goto 	set_COFF
	btfsc	switch1 
	goto	setup_eyes_enter
	btfsc	switch2
	goto	exit
	goto	setup_COFF
	
set_COFF
	call	clear_LEDs
	call	debounce_all
	movlw	0x01		;5ms
	movwf	COFF
	call	trigger_release1
	call	purple_LED

inc_COFF
	btfss	trigger
	goto	inc_COFF_1
	btfsc	switch1 
	goto	setup_eyes_enter
	btfsc	switch2
	goto	exit
	goto	inc_COFF

inc_COFF_1
	call	clear_LEDs
	incf	COFF,1
	call	trigger_release1
	call	purple_LED
;	call	debounce_all
	goto	inc_COFF

;------------------------------eye type-----------------------

setup_eyes_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
;	call	debounce_all
	bsf		RED_LED

inc_eyes
	btfss	trigger
	goto	inc_eyes1
	btfsc	switch1						
	goto	setup_training_enter
	btfsc	switch2
	goto	exit
	goto	inc_eyes

inc_eyes1
	call	clear_LEDs
	call	debounce_all
	call	trigger_release1
;	call	debounce_all
	btfss	eye_type
	goto	set_eyes_bounce

set_eyes_break
	bcf		eye_type
	call	clear_LEDs
;	call	debounce_all
	goto	inc_eyes

set_eyes_bounce
	bsf		eye_type
	bsf		RED_LED
;	call	debounce_all
	goto	inc_eyes

;----------------------taining-------------------------
setup_training_enter
	call	clear_LEDs
	call	switch1_release1
	call	trigger_release1
	call	switch2_release1
;	call	debounce_all
	bsf		GREEN_LED

inc_training
	btfss	trigger
	goto	inc_training1
	btfsc	switch1						
	goto	exit
	btfsc	switch2
	goto	exit
	goto	inc_training

inc_training1
	call	clear_LEDs
	call	debounce_all
	call	trigger_release1
	call	debounce_all
	btfsc	training
	goto	set_training_off

set_training_on
	bsf		training
	bsf		GREEN_LED
	goto	inc_training

set_training_off
	bcf		training
	call	clear_LEDs
	goto	inc_training

;***************************setup4-restore defaults***************************

setup_flash4
	bsf	RED_LED
	call LED_flash
	bcf		RED_LED

	call LED_flash
	bsf		RED_LED
	call LED_flash
	bcf		RED_LED

	call LED_flash
	bsf		RED_LED
	call LED_flash
	bcf		RED_LED

	call LED_flash
	bsf		RED_LED
	call LED_flash
	bcf		RED_LED

	movlw	0x09
	movwf	count3
loop_setup4
	call	one_ms_delay
	decfsz count2,1
	goto	loop_setup4
	btfss	switch1		;next command on 1
	goto	setup4		;trigger released, 
	decfsz count3,1
	goto	loop_setup4
	goto	eye_align	;trigger held long enough for eye disable

setup4
	call	debounce_all
	bcf		RED_LED

restore_default
;------------------------------------------------------
	movlw 	0x0d;  
	movwf	dwell
	movlw	0x00	
	movwf	Eye_delay_real	;ROF
	movlw	0x23
	movwf	ROF_raw
	movlw	0x04
	movwf	debounce
	movlw	0x06
	movwf	semi_force_time
	bcf		valve_mode	;auto
;---------------------ramping------------------------------
	movlw	0xFF	
	movwf	trig_speed
	movwf	trig_speed_tmp		;minimum trigger pull speed.
	movlw	0x04			;four pull to start ramp
	movwf	ramp_pulls
;---------------------shotq'ing
	bcf		shotq_enabled		;shotq'ing ON
;------------------Nitro--------------------------
	movlw	0x01
	movwf	after_nitro		;semi
;-----------------------ABS-------------------------
	clrf	mode
	bsf		semi
;----------------------------------------------------
	movlw	0x34	;2A works but 30 is safe  	
	movwf	CON
	movlw	0x05
	movwf	CDEL
	movlw	0x10
	movwf	ball_drop
	movlw	0xFF	
	movwf	CTO
	movlw	0x08	
	movwf	COFF
	bcf		eye_type ;bouncebeam
	bcf		training
	bcf		Instant_ON_enabled  ;Auto off diabled
;***********************write to mem and exit***************************
exit

	call	ROF_set
	bsf		GREEN_LED
	call	trigger_release1
	call	switch1_release1
	call	debounce_all
	bcf		GREEN_LED
	call	disco


	movlw	0x00	
	movwf	tmp_addr
	movf	ROF_raw,W
	call	writee
;	call	debounce_all

	movlw	0x05	
	movwf	tmp_addr
	movf	dwell,W
	call	writee
;	call	debounce_all

	movlw	0x07	
	movwf	tmp_addr
	movf	eyedelay,W
	call	writee
;	call	debounce_all


	movlw	0x0A	
	movwf	tmp_addr
	movf	debounce,W
	call	writee
;	call	debounce_all

	movlw	0x10	
	movwf	tmp_addr
	movf	flags,W
	call	writee
;	call	debounce_all

	movlw	0x15	
	movwf	tmp_addr
	movf	semi_force_time,W
	call	writee
;	call	debounce_all

	movlw	0x1A	
	movwf	tmp_addr
	movf	after_nitro,W
	call	writee
;	call	debounce_all

	movlw	0x20	
	movwf	tmp_addr
	movf	CDEL,W
	call	writee
;	call	debounce_all

	movlw	0x25	
	movwf	tmp_addr
	movf	CON,W
	call	writee
;	call	debounce_all

	movlw	0x2A	
	movwf	tmp_addr
	movf	CTO,W
	call	writee

	movlw	0x30
	movwf	tmp_addr
	movf	mode,W
	call	writee

	movlw	0x35
	movwf	tmp_addr
	movf	ball_drop,W
	call	writee

	movlw	0x3A
	movwf	tmp_addr
	movf	COFF,W
	call	writee

	movlw	0x40
	movwf	tmp_addr
	movf	ramp_pulls,W
	call	writee

	movlw	0x45
	movwf	tmp_addr
	movf	trig_speed,W
	call	writee


;	goto	mem
	goto	init


writee
	movwf	tmp	
	bsf		STATUS,RP0
	movwf	EEDATA
	bcf		STATUS,RP0
	movf	tmp_addr,W
	bsf		STATUS,RP0
	movwf	EEADR
	bcf		STATUS, RP0
ee_write
	bsf		STATUS, RP0
	bsf		EECON1,WREN
	bcf		INTCON,GIE

	btfsc	INTCON, GIE
	goto	$-2

	movlw	0x55
	movwf	EECON2
	movlw	0xAA
	movwf	EECON2
	bsf		EECON1,WR
	bsf		INTCON,GIE
ee_writewr
	bsf		STATUS, RP0	;added
	clrwdt
	btfsc	EECON1,WR
	goto	ee_writewr

	bcf		EECON1,WREN
	bcf		STATUS, RP0	;added	
	movf	tmp_addr,W
	call	readee
	subwf	tmp,W
	skpnz
	return
	bsf		STATUS,RP0
	goto	ee_write



readee
	bsf	STATUS,RP0
	movwf	EEADR
	bsf		EECON1,RD
	movf	EEDATA,W
	bcf		STATUS,RP0
	return

;eye alignment tool___________________________________________________

eye_align
	
	bsf		RED_LED
	call	switch1_release1
	call	switch2_release1
	bsf		eyepower
	bcf		RED_LED

loop_eye
	btfsc	switch2		;push switch2
	goto	off_on
	btfss	eyeout		;next command on 0
	goto 	eye_on		;pull trigger
	goto	eye_off

eye_on
	bsf		GREEN_LED
	goto 	loop_eye		;eye is NOT active

eye_off	
	bcf		GREEN_LED		;eye is active
	goto	loop_eye		;eye blocked, fire

off_on3
	bsf		RED_LED
	movlw	0x06
	movwf	count3
loop_off_on3
	call	one_ms_delay
	btfss	switch2		;safe mode if trigger released
	goto	eye_align		
	decfsz count2,1
	goto	loop_off_on3
	decfsz count3,1
	goto	loop_off_on3
	bcf		RED_LED
	goto	wait_release

LED_flash
loop_LED_flash
	call	one_ms_delay
	decfsz 	count2,1
	goto	loop_LED_flash
	return

one_ms_delay
	movlw	0xDD
	movwf	count1
one_ms_delay_loop	
	decfsz 	count1,1
	goto	one_ms_delay_loop
	return
	
;----------------------------off/safety-------------------------
off
	call	clear_LEDs
	bsf		GREEN_LED
	call	debounce_all
	movlw	0x04
	movwf	count3
loop_off
	call	one_ms_delay
	btfss	switch2		;safe mode if trigger released
	goto	safe
	decfsz count2,1
	goto	loop_off
	decfsz count3,1
	goto	loop_off
	bcf		GREEN_LED
	goto	wait_release


safe
	call	clear_LEDs
	bcf		eyepower
	call 	debounce_all 
safe_loop
	bsf		GREEN_LED
	call	safe_blink
	bcf		GREEN_LED
	call	safe_blink
	goto 	safe_loop

safe_blink	
	movlw	0x40
	movwf	count2
safe_blink_loop
	call	one_ms_delay
	btfsc	switch2		;leave safe mode
	goto 	off_on	
	decfsz count2,1
	goto	safe_blink_loop
	return
	
off_on
	movlw	0x06
	movwf	count3
loop_off_on1
	call	one_ms_delay
	btfss	switch2		;
	goto	main_release		
	decfsz count2,1
	goto	loop_off_on1
	decfsz count3,1
	goto	loop_off_on1
	bcf		RED_LED
	bcf		GREEN_LED
	call	switch2_release1
	goto	wait_release
;-------------------------------------------------------------

clear_LEDs		;turns all LEDs off
	bcf 	RED_LED
	bcf		GREEN_LED
	bcf		BLUE_LED
	return
	
;-------------------set the LEDs---------------------------
set_LED
	
	call	clear_LEDs
	btfsc	semi
	bsf		RED_LED
	btfsc	rt
	bsf		GREEN_LED
	btfsc	burst
	bsf		BLUE_LED
	btfsc	nxl
	goto	teal_LED
	btfsc	psp
	goto	yellow_LED
	btfsc	nitro
	goto	purple_LED
	btfsc	full
	goto	white_LED
	btfsc	milen
	bsf		RED_LED
	return

teal_LED		;teal
	bsf 	GREEN_LED
	bsf		BLUE_LED
	return

yellow_LED		;yellow
	bsf		GREEN_LED
	bsf		RED_LED
	return

purple_LED	;purple
	bsf 	RED_LED
	bsf		BLUE_LED
	return

white_LED	;white
	bsf 	RED_LED
	bsf		GREEN_LED
	bsf		BLUE_LED
	return
;-----------------------disco--------------------------------
disco
	call	clear_LEDs
	call	safe_blink
	bsf		RED_LED
	call	safe_blink
	call	clear_LEDs
	bsf		GREEN_LED
	call	safe_blink
	call	clear_LEDs
	bsf		BLUE_LED
	call	safe_blink
	call	clear_LEDs
	call	teal_LED
	call	safe_blink
	call	clear_LEDs
	call	yellow_LED
	call	safe_blink
	call	clear_LEDs
	call	purple_LED
	call	safe_blink
	call	clear_LEDs
	call	white_LED
	call	safe_blink
	call	clear_LEDs
	return
;-------------------set the modes---------------------------

change_mode
	call	disco
change_mode1
	call	clear_LEDs
	call 	trigger_release1
	call	set_LED

mode_change_loop
	btfsc	switch2
	goto	mode_save
	btfss	trigger
	goto	next_mode
	goto	mode_change_loop

next_mode
	btfsc	semi
	goto	mode_rt
	btfsc	rt
	goto	mode_burst
	btfsc	burst
	goto	mode_full
	btfsc	full
	goto	mode_milen
	btfsc	milen
	goto	mode_nitro
	btfsc	nitro
	goto	mode_psp
	btfsc	psp
	goto	mode_nxl
	btfsc	nxl



mode_semi
	clrf	mode
	bsf		semi
	goto 	change_mode1

mode_rt
	clrf	mode
	bsf		rt
	goto 	change_mode1

mode_burst
	clrf	mode
	bsf		burst
	goto 	change_mode1

mode_full
	clrf	mode
	bsf		full
	goto 	change_mode1

mode_milen
	clrf	mode
	bsf		milen
	goto 	change_mode1

mode_nitro
	clrf	mode
	bsf		nitro
	goto 	change_mode1

mode_nxl
	clrf	mode
	bsf		nxl
	goto 	change_mode1

mode_psp
	clrf	mode
	bsf		psp
	goto 	change_mode1

mode_save
	movlw	0x30
	movwf	tmp_addr
	movf	mode,W
	call	writee
	call	debounce_all
	call	trigger_release1
	call	disco
	goto	init

	end


