
		DEVICE	SX28L,oschs2,TURBO,Protect
		DEVICE  stackx_optionx,watchdog
		RESET	start_point
		FREQ 	50_000_000

		org 	$08
signs		ds	1
Turn_Coil_On	ds	1
Turn_Coil_Off	ds	2	
clock		ds	3
Bounceless	ds	1
		org	$10	
XRPM		ds	3
RPM		ds	3
Antic		ds	3
No_Bounce	ds	1
fix_dwell	ds	1
Fine_Tune	ds	1
Cylinder	ds	1
		org	$30
XREV		ds	2
XADV		ds	3
mult		ds	3
answer		ds	3
counter		ds	1
Soft_Rev	ds	2
		org	$50
Base_Timing	ds	1
Initial_Adv	ds	1
Mid_Advance	ds	1
Max_Advance     ds	1
Retard_1	ds	1
Retard_2	ds	1
Retard_3	ds	1
Retard_4	ds	1
Init_Adv_RPM   	ds	2
Mid_Adv_RPM	ds	2
Max_Adv_RPM	ds	2
		org	$5e
Variable	ds	1
Variable2	ds	1
		org	$70
Rev_Limit_1	ds	2
Rev_Limit_2	ds	2
Rev_Limit_3	ds	2
Rev_Limit_4	ds	2
Rev_Limit_Max	ds	2
RPM_Switch_1	ds	2
RPM_Switch_2	ds	2
		org	$90
RPM_Switch_3	ds	2
RPM_Switch_4	ds	4
HS_Retard_RPM	ds	2
HS_Retard	ds	1

Page1_Bank	EQU	$00
Page2_Bank	EQU	$20
Page3_Bank	EQU	$40
Page4_Bank	EQU	$60
Page5_Bank	EQU	$80
Page6_Bank	EQU	$A0
Page7_Bank	EQU	$C0
Page8_Bank	EQU	$E0
Bit_Ret_1	EQU	ra.0
Bit_Ret_2	EQU	ra.1
Bit_Ret_3	EQU	ra.2
Bit_Ret_4	EQU	ra.3
Spark_Out	EQU	rb.0
Bypass		EQU	rb.1
Spark_In	EQU	rb.2
Bit_RPM_1	EQU	rb.3
Bit_RPM_2	EQU	rb.4
Bit_RPM_3	EQU	rb.5
Bit_RPM_4	EQU	rb.6
Bit_Rev_1	EQU	rb.7
Bit_Rev_2	EQU	rc.0
Bit_Rev_3	EQU	rc.1
Bit_Rev_4	EQU	rc.2
HHP		EQU	rc.3
ECU		EQU	rc.4
Data		EQU	rc.5
DL		EQU	rc.6
Bit_Tach	EQU	rc.7
watch		RPM,24,udec
watch		XRPM,24,udec
;watch		antic,24,udec
watch		XREV,16,udec
watch		mult,24,udec
watch		answer,24,udec
watch		xadv,24,udec
watch		turn_coil_off,16,udec
		
		org	0

isr		jb	signs.0,no_trigger
		jb	spark_in,no_trigger
		djnz	bounceless,no_trigger
		setb	signs.3
No_trigger	cjne	clock,turn_coil_off,no_fire		
		csne	clock+1,turn_coil_on
		setb	spark_out
fire		csne	clock+1,turn_coil_off+1
		clrb	spark_out
no_fire		inc	clock
		snz
		inc	clock+1
		snz
		inc	clock+2
		mov	w,#-50
		retiw
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start_point 	bank	page1_bank
		clr	xrpm
		clr	xrpm+1
		clr	xrpm+2;,#19
fix_die		djnz	xrpm,fix_die
		djnz	xrpm+1,fix_die
		djnz	xrpm+2,fix_die

		mode	Level
		mov	!ra,#%0000
		mov	!rb,#%00000000
		mov	!rc,#%00000000
		mode	Schmitt
		mov	!rc,#%11010111
		mode	Direction
		mov	!ra,#%1111
		mov	!rb,#%10000100
		mov	!rc,#%00101111
		clr	ra
		clr	rb
		clr	rc
		clr	turn_coil_on
		clr	turn_coil_off
		clr	turn_coil_off+1
		and	signs,#00000100
		mov	!option,#$cf
		bank	page2_bank
		clr	soft_rev
		clr	soft_rev+1
				
		bank	page3_bank
		call	check_twice
		snb	signs.1
		call	check_twice
		jb	signs.1,damn_it
		bank	page4_bank
		call	check_twice2
		snb	signs.1
		call	check_twice2
		jb	signs.1,damn_it
		bank	page5_bank
		call	check_twice3
		snb	signs.1
		call	check_twice3
		jb	signs.1,damn_it

Check_strip	bank	page6_bank
		call	check_twice
		snb	signs.1
		call	check_twice
		jb	signs.1,damn_it2
		bank	page7_bank
		call	check_twice2
		snb	signs.1
		call	check_twice2
		jb	signs.1,damn_it2
		bank	page8_bank
		call	check_twice3
		snb	signs.1
		call	check_twice3
		jb	signs.1,damn_it2
		jmp	@all_default


Check_twice	csbe	Base_Timing,#200
		setb	signs.1
		csae	Initial_Adv,Base_Timing
		setb	signs.1
		csbe	initial_adv,#200
		setb	signs.1
		cja	init_adv_rpm+1,#$01,low_init
		cjb	init_adv_rpm+1,#$01,high_init
		csae	init_adv_rpm,#$90
high_init	setb	signs.1
low_init	csbe	init_adv_rpm+1,#$24
		setb	signs.1
		csae	mid_advance,Base_Timing
		setb	signs.1
		csbe	mid_advance,#200
		setb	signs.1
		csae	mid_adv_rpm+1,init_adv_rpm+1
		setb	signs.1
		csbe	mid_adv_rpm+1,#$24
		setb	signs.1
		csae	max_advance,Base_Timing
		setb	signs.1
		csbe	max_advance,#200
		setb	signs.1
		csae	max_adv_rpm+1,mid_adv_rpm+1
		setb	signs.1
		csbe	max_adv_rpm+1,#$24
		setb	signs.1
		csbe	retard_1,#200
		setb	signs.1
		csbe	retard_2,#200
		setb	signs.1
		csbe	retard_3,#200
		setb	signs.1
		csbe	retard_4,#200
		setb	signs.1
		ret

Check_twice2	csbe	rev_limit_1+1,#$24
		setb	signs.1
		csbe	rev_limit_2+1,#$24
		setb	signs.1
		csbe	rev_limit_3+1,#$24
		setb	signs.1
		csbe	rev_limit_4+1,#$24
		setb	signs.1
		csbe	rev_limit_max+1,#$24
		setb	signs.1
		csbe	rpm_switch_1+1,#$24
		setb	signs.1
		csbe	rpm_switch_2+1,#$24
		setb	signs.1
		ret

Check_twice3	csbe	rpm_switch_3+1,#$24
		setb	signs.1
		csbe	rpm_switch_4+1,#$24
		setb	signs.1
		csbe	hs_retard,#200
		setb	signs.1
		cja	hs_retard_rpm+1,#$01,low_hs
		cjb	hs_retard_rpm+1,#$01,high_hs
		csae	hs_retard_rpm,#$90
high_hs		setb	signs.1
low_hs		csbe	hs_retard_rpm+1,#$24
		setb	signs.1
		ret
		
Call_it_up	setb	signs.7	
		skip
Damn_it		clrb	signs.2
		bank	page1_bank
		clr	fine_tune
		mov	cylinder,#8
		bank	page3_bank
		nop
		mov	Base_Timing,#40
		mov	Initial_Adv,#48
		mov	Init_Adv_RPM,#$84
		mov	Init_Adv_RPM+1,#$03
		mov	Mid_Advance,#112
		mov	Mid_Adv_RPM,#$c4
		mov	Mid_Adv_RPM+1,#$09
		mov	Max_Advance,#128
		mov	Max_Adv_RPM,#$ac
		mov	Max_Adv_RPM+1,#$0d
		clr	Retard_1
		clr	Retard_2
		clr	Retard_3
		clr	Retard_4
		bank	page4_bank
		nop
		mov	Rev_Limit_Max,#$70
		mov	Rev_Limit_Max+1,#$17
		mov	w,#$28
		mov	Rev_Limit_1,w
		mov	Rev_Limit_2,w
		mov	Rev_Limit_3,w
		mov	Rev_Limit_4,w
		mov	RPM_Switch_1,w
		mov	RPM_Switch_2,w
		mov	w,#$23
		mov	Rev_Limit_1+1,w
		mov	Rev_Limit_2+1,w
		mov	Rev_Limit_3+1,w
		mov	Rev_Limit_4+1,w
		mov	RPM_Switch_1+1,w
		mov	RPM_Switch_2+1,w
		bank	page5_bank
		nop
		mov	RPM_Switch_3+1,w
		mov	RPM_Switch_4+1,w
		mov	RPM_Switch_4+3,w
		mov	w,#$28
		mov	RPM_Switch_3,w
		mov	RPM_Switch_4,w
		mov	RPM_Switch_4+2,w
		clr	HS_Retard
		mov	HS_Retard_RPM,#$88
		mov	HS_Retard_RPM+1,#$13
		jnb	signs.7,damn_pages
		jmp	damn_it_X
damn_pages	jmp	Check_strip

damn_it2	clr	signs.2
damn_it_X	bank	page1_bank
		clr	fine_tune
		mov	cylinder,#8
		bank	page6_bank
		nop
		mov	Base_Timing,#40
		mov	Initial_Adv,#64
		mov	Init_Adv_RPM,#$e8
		mov	Init_Adv_RPM+1,#$03
		mov	Mid_Advance,#120
		mov	Mid_Adv_RPM,#$d0
		mov	Mid_Adv_RPM+1,#$07
		mov	Max_Advance,#144
		mov	Max_Adv_RPM,#$b8
		mov	Max_Adv_RPM+1,#$0b
		clr	Retard_1
		clr	Retard_2
		clr	Retard_3
		clr	Retard_4
		bank	page7_bank
		nop
		mov	Rev_Limit_Max,#$70
		mov	Rev_Limit_Max+1,#$17
		mov	w,#$28
		mov	Rev_Limit_1,w
		mov	Rev_Limit_2,w
		mov	Rev_Limit_3,w
		mov	Rev_Limit_4,w
		mov	RPM_Switch_1,w
		mov	RPM_Switch_2,w
		mov	w,#$23
		mov	Rev_Limit_1+1,w
		mov	Rev_Limit_2+1,w
		mov	Rev_Limit_3+1,w
		mov	Rev_Limit_4+1,w
		mov	RPM_Switch_1+1,w
		mov	RPM_Switch_2+1,w
		bank	page8_bank
		nop
		mov	RPM_Switch_3+1,w
		mov	RPM_Switch_4+1,w
		mov	RPM_Switch_4+3,w
		mov	w,#$28
		mov	RPM_Switch_3,w
		mov	RPM_Switch_4,w
		mov	RPM_Switch_4+2,w
		clr	HS_Retard
		mov	HS_Retard_RPM,#$88
		mov	HS_Retard_RPM+1,#$13
		jnb	signs.7,All_default
		jmp	@its_done
		
All_default	clrb	signs.1
		mov	!option,#$8f

Trigger		bank	page1_bank
		clr	no_bounce
		clr	bounceless
trigger2	cjb	clock+2,#3,trigger3	;shut off coil if motor dies
		clr	!wdt
		clr	turn_coil_on
		setb	signs.1
		and	rb,#%00000001
		setb	signs.5
		setb	bypass
		
trigger3	jnb	signs.5,trigger4
		jmp	@Start_it_up
trigger4	jnb	spark_in,not_low
		djnz	no_bounce,trigger2
		clr	bounceless
		clrb	signs.0
not_low		clr	no_bounce

the_fix		jnb	signs.3,trigger2
		
		setb	signs.0
		clrb	signs.3
		clrb	signs.5
		
		mov	!option,#$cf
		mov	rpm,clock
		mov	rpm+1,clock+1
		mov	rpm+2,clock+2
	
	;	mov	rpm,#$7a
	;	mov	rpm+1,#$1a
	;	clr	rpm+2

		clr	clock
		;mov	clock,#200
		mov	clock+1,#1
		clr	clock+2
		mov	!option,#$8f
		clrb	spark_out
		snb	signs.1
		setb	spark_out
		
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Move_It	;	sub	rpm,#200
	;	jc	super_sub
	;	sub	rpm+1,#1
	;	sc
	;	dec	rpm+2

super_sub	sub	rpm+1,#1
		sc
		dec	rpm+2
		test	rpm+2
		jnz	Still_check
		cjae	cylinder,#8,start_8
		cjbe	cylinder,#4,not_start
		cjb	rpm+1,#$c3,not_Start
		jmp	still_check
start_8		cjb	rpm+1,#$93,Not_start
Still_check	mov	turn_coil_on,turn_coil_off+1
		clr	!wdt
		clrb	bypass
		jmp	trigger

Not_Start	clr	!wdt
		mov	!option,#$8d
		setb	bypass

		mov	w,rpm+2
		mov	w,antic+2-w
		mov	xrpm+2,w
		mov	w,rpm+1
		mov	w,antic+1-w
		mov	xrpm+1,w
		mov	fix_dwell,w
		sc
		dec	xrpm+2
		mov	w,rpm
		mov	w,antic-w
		mov	xrpm,w
		jc	Set_Antic
		test	xrpm+1
		snz
		dec	xrpm+2
		dec	xrpm+1
Set_Antic	mov	antic+2,rpm+2
		mov	antic+1,rpm+1
		mov	antic,rpm
		sub	rpm+2,xrpm+2
		sub	rpm+1,xrpm+1
		sc
		dec	rpm+2
		sub	rpm,xrpm
		jc	Done_Sub
		test	rpm+1
		snz
		dec	rpm+2
		dec	rpm+1
Done_Sub

;REV-------------------------------------------------		
		jmp	@thats_it

		org	$200
thats_it	mov	w,antic
		bank	page2_bank
		mov	mult,w
		bank	page1_bank
		mov	w,antic+1
		bank	page2_bank
		mov	mult+1,w
		bank	page1_bank
		mov	w,antic+2
		bank	page2_bank
		mov	mult+2,w
		bank	page1_bank
		cjae	cylinder,#8,not_six
		bank	page2_bank
		clc
		rr	mult+2
		rr	mult+1
		rr	mult
		bank	page1_bank
		cjbe	cylinder,#4,not_six
		bank	page2_bank
		mov	answer,#$80
		mov	answer+1,#$96
		mov	answer+2,#$98
		jmp	math_part
not_six		bank	page2_bank
		mov	answer,#$c0
		mov	answer+1,#$e1
		mov	answer+2,#$e4
math_part	call	division
		mov	xrev,xadv
		mov	xrev+1,xadv+1
		jmp	Advance

		

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		
advance1	mov	w,initial_adv
		bank	page2_bank 
		mov	xadv,w
		jmp	@extras
advance2	mov	w,max_advance
		bank	page2_bank
		mov	xadv,w
		jmp	@extras
Advance		mov	w,xrev
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable,w
		bank	page2_bank
		mov	w,xrev+1
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable2,w
		cjb	variable2,init_adv_rpm+1,advance1
		cjne	variable2,init_adv_rpm+1,not_init
		cjbe	variable,init_adv_rpm,advance1
not_init	cja	variable2,max_adv_rpm+1,advance2
		cjne	variable2,max_adv_rpm+1,not_max
		cjae	variable,max_adv_rpm,advance2
not_max		cja	variable2,mid_adv_rpm+1,big_spring
		cjb	variable2,mid_adv_rpm+1,not_big
		cja	variable,mid_adv_rpm,big_spring

not_big		jmp	@Move_Advance
move_mult	call	multiply
		jmp	@done_move_mult
move_div	call	division
		jmp	@done_move_div

Big_Spring	mov	variable,max_advance
		sub	variable,mid_advance
		clrb	signs.6
		jc	all_positive
		setb	signs.6
		mov	variable,mid_advance
		sub	variable,max_advance
all_positive	mov	w,variable
		bank	page2_bank
		mov	xadv,w
		mov	w,xrev
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable,w
		bank	page2_bank
		mov	w,xrev+1
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable2,w
		sub	variable2,mid_adv_rpm+1
		sub	variable,mid_adv_rpm
		sc
		dec	variable2
		mov	w,variable
		bank	page2_bank
		mov	mult,w
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,variable2
		bank	page2_bank
		mov	mult+1,w
		clr	mult+2
		call	multiply
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable,max_adv_rpm
		mov	variable2,max_adv_rpm+1
		sub	variable2,mid_adv_rpm+1
		sub	variable,mid_adv_rpm
		sc
		dec	variable2
		mov	w,variable
		bank	page2_bank
		mov	mult,w
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,variable2
		bank	page2_bank
		mov	mult+1,w
		clr	mult+2
		call	division
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,mid_advance
		bank	page2_bank
		jb	signs.6,less_some
		add	xadv,w
		jmp	@extras
less_some	mov	w,xadv
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,mid_advance-w
		bank	page2_bank
		mov	xadv,w
		jmp	@extras

multiply ;-----------------------------------------	
		clr	answer
		clr	answer+1
		clr	answer+2
		mov	counter,#8
mloop2		rr	xadv
		jnc	noadd2
		add	answer,mult
		jnc	no_carry1
		inc	answer+1
		snz
		inc	answer+2
no_carry1	add	answer+1,mult+1
		snc
		inc	answer+2
		add	answer+2,mult+2
noadd2		clc
		rl	mult
		rl	mult+1
		rl	mult+2
		djnz	counter,mloop2
		ret
 		
division ;-------------------------------------------	
		clr	counter
		clc
dloop		rl	mult
		rl	mult+1
		rl	mult+2
		inc	counter
		jnc	dloop
		rr	mult+2
		rr	mult+1
		rr	mult
		clr	xadv
		clr	xadv+1
		clr	xadv+2

ddloop		test	counter
		jz	ddone
		clc
		rl	xadv
		rl	xadv+1
		rl	xadv+2
		cjb	answer+2,mult+2,ddloop1
		cje	answer+2,mult+2,dequal
		jmp	dfinish
dequal		cjb	answer+1,mult+1,ddloop1
		cje	answer+1,mult+1,equalx
		jmp	dfinish
equalx		cjb	answer,mult,ddloop1
dfinish		sub	answer+2,mult+2
		sub	answer+1,mult+1
		sc
		dec	answer+2
		sub	answer,mult
		sc
		dec	answer+1
		inc	xadv
		snz
		inc	xadv+1
		snz
		inc	xadv+2

ddloop1		dec	counter
		clc
		rr	mult+2
		rr	mult+1
		rr	mult
		jmp	ddloop

ddone		
		ret
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Find_adv	bank	page1_bank
		mov	w,rpm
		bank	page2_bank
		mov	mult,w
		bank	page1_bank
		mov	w,rpm+1
		bank	page2_bank
		mov	mult+1,w
		clr	mult+2
		call	multiply
		mov	mult,#$e0
		mov	mult+1,#$01
		bank	page1_bank
		cjae	cylinder,#8,dif_adv8
		cja	cylinder,#4,dif_adv
		bank	page2_bank
		mov	mult,#$d0
		mov	mult+1,#$02
		jmp	dif_adv
dif_adv8	bank	page2_bank
		mov	mult,#$68
		mov	mult+1,#$1
dif_adv		bank	page2_bank
		clr	mult+2
		call	division
		bank	page1_bank
		mov	w,rpm	
		bank	page2_bank
		mov	mult,w
		bank	page1_bank
		mov	w,rpm+1
		bank	page2_bank
		mov	mult+1,w
		sub	mult+1,xadv+1
		sub	mult,xadv
		sc
		dec	mult+1
		bank	page1_bank
		mov	w,fine_tune
		bank	page2_bank
		sub	mult,w
		sc
		dec	mult+1
		mov	turn_coil_off,mult
		mov	turn_coil_off+1,mult+1

;-------------------------------------------------
Find_Dwell	bank	page1_bank
		mov	w,rpm+1
		bank	page2_bank
		mov	xadv,w
		mov	xadv+1,w
		mov	xadv+2,#$0f  ;on time
		mov	counter,turn_coil_off+1 ;xx

		bank	page1_bank
		cjb	fix_dwell,#$10,no_fix
		cja	fix_dwell,#$80,no_fix
		mov	w,>>rpm+1
		bank	page2_bank
		mov	mult+2,w
		rr	mult+2
		add	xadv+2,mult+2

no_fix		bank	page2_bank
		cjae	xrev+1,#$0d,high_side

		sub	xadv,xadv+2
		jnc	skip_next
		cjae	xadv,#$07,no_roll_over  ;off time
skip_next	mov	w,#$07                  ;make same as above
		mov	w,xadv+1-w
		mov	xadv+2,w
		jmp	no_roll_over

high_side	sub	xadv,xadv+2
		jnc	skip_next2
		cjae	xadv,#$05,no_roll_over  ;off time
skip_next2	mov	w,#$05                  ;make same as above
		mov	w,xadv+1-w
		mov	xadv+2,w

no_roll_over	sub	counter,xadv+2 ;xx
		jnc	next_dwell
		clrb	signs.1
		cjb 	counter,#$01,pre_trig_dwell  ;xx
		mov	turn_coil_on,counter
		cja	turn_coil_on,#$02,tach
		setb	spark_out
		jmp	tach
pre_trig_dwell	mov	turn_coil_on,xadv+1
		setb	spark_out
		setb	signs.1
		jmp	tach
next_dwell;	setb	signs.1
		mov	w,turn_coil_off+1
		mov	xadv,w
		sub	xadv+2,xadv
		mov	w,xadv+2
		mov	w,xadv+1-w
		mov	turn_coil_on,w
		setb	spark_out
		
		
;--------------------------------------------------
Tach		jmp	@tachometer

		org	$400

Tachometer;	bank	page2_bank
	;	mov	xadv+1,clock+1
	;	mov	xadv,clock
	;	add	xadv,#42
	;	snc
	;	inc	xadv+1
	;	cjbe	xrev,#53,no_tach_fix
	;	add	xadv,#3
	;	snc
	;	inc	xadv+1
;No_Tach_Fix	setb	Bit_Tach
;Tach_On		cjb	clock+1,xadv+1,Tach_On
;Tach_Off	cjb	clock,xadv,Tach_Off
;		clrb	Bit_Tach

	;	jmp	@trigger
;-------------------------------------------------
	;	jmp	its_done
Start_it_up	jnb	data,its_done
		jnb	hhp,its_done
		clr	!wdt
		setb	dl
Now_dl2		jb	hhp,Now_up2
		clrb	dl
		setb	signs.4
		jmp	download
Now_up2		jb	data,now_dl2		
		clrb	dl

No_init		jb	signs.4,its_done
		clrb	ECU
		bank	page3_bank
		mov	variable2,#6
		clr	variable
		call	get_stuff
		clc
		rr	variable
		rr	variable
		cjb	variable,#60,reg_op
		cje	variable,#60,now_call_it
		clrb	signs.2
		csne	variable,#62
		setb	signs.2
		jmp	its_done
now_call_it	jmp	@call_it_up
Reg_op		call	what_value
		mov	fsr,w
		cjbe	fsr,#base_timing,no_strip
		mov	w,#$60
		snb	signs.2
		add	fsr,w
no_strip	mov	variable2,#8
		call	get_stuff
		mov	ind,variable
		cjne	fsr,#base_timing,nibble
		mov	w,variable
		bank	page6_bank
		mov	base_timing,w
		jmp	its_done

nibble		cje	fsr,#hs_retard,its_done
		cje	fsr,#hs_retard+$60,its_done
		jb	signs.2,next_nibble
		cjb	fsr,#init_adv_rpm,its_done
		jmp	ziggy_wig
next_nibble	cjb	fsr,#init_adv_rpm+$60,its_done
ziggy_wig	inc	fsr
		mov	variable2,#8
		call	get_stuff
		mov	ind,variable
		jmp	its_done
		
Get_Stuff	jnb	hhp,get_stuff
		setb	ecu
gwait_1		jb	hhp,gwait_1
		clc
		snb	data
		stc
		rr	variable
		clrb	ecu
		djnz	variable2,get_stuff
		ret

;--------------------------------------------------
what_value	mov	w,variable
		jmp	pc+w
		retw	$50 ;0 Base_timing
		retw	$51 ;1 Initial_adv
		retw	$58 ;2 Init_adv_rpm	x
		retw	$52 ;3 Mid_advance
		retw	$5a ;4 Mid_adv_rpm	x
		retw	$53 ;5 Max_advance
		retw	$5c ;6 Max_adv_rpm	x
		retw	$70 ;7 Rev_limit_1	x
		retw	$72 ;8 Rev_limit_2	x
		retw	$74 ;9 Rev_limit_3	x
		retw	$76 ;10 Rev_limit_4	x
		retw	$78 ;11 Rev_limit_max	x
		retw	$7a ;12 Rpm_switch_1	x
		retw	$7c ;13 Rpm_switch_2	x
		retw	$90 ;14 Rpm_switch_3	x
		retw	$92 ;15 Rpm_switch_4	x
		retw	$94 ;16 Rpm_switch_4+2	x
		retw	$54 ;17 Retard_1
		retw	$55 ;18 Retard_2
		retw	$56 ;19 Retard_3
		retw	$57 ;20 Retard_4
		retw	$98 ;21 Hs_retard
		retw	$96 ;22 Hs_retard_rpm	x
		retw	$1b ;23 Fine_tune
		retw	$1c ;24	Cylinder
		
;--------------------------------------------------
Download	clrb	ECU
		mov	fsr,#$50
		mov	w,#$b0
		snb	signs.2
		mov	fsr,w
		mov	variable,#14
		call	dlwait_1
		mov	fsr,#$70
		mov	w,#$d0
		snb	signs.2
		mov	fsr,w
		mov	variable,#14
		call	dlwait_1
		mov	fsr,#$90
		mov	w,#$f0
		snb	signs.2
		mov	fsr,w
		mov	variable,#09
		call	dlwait_1
		mov	fsr,#fine_tune
		mov	variable,#02
		call	dlwait_1
		clrb	signs.4

Its_Done	jnb	signs.5,enough
		clr	variable
back_it		djnz	variable,back_it
alright		clrb	ECU
		snb	spark_in
		clrb	signs.0
		bank	page1_bank
		jmp	@the_fix
Done_it		jb	signs.5,alright
enough		jmp	@trigger
		
	;	jnb	HHP,dlwait_1
dlwait_1	clrb	dl
		snb	ind.0
		setb	dl
		nop
		nop
		nop
		setb	ecu
dlwait_2	jnb	hhp,dlwait_2
		clrb	dl
		snb	ind.1
		setb	dl
		nop
		nop
		nop
		clrb	ecu
dlwait_3	jb	hhp,dlwait_3
		clrb	dl
		snb	ind.2
		setb	dl
		nop
		nop
		nop
		setb	ecu
dlwait_4	jnb	hhp,dlwait_4
		clrb	dl
		snb	ind.3
		setb	dl
		nop
		nop
		nop
		clrb	ecu
dlwait_5	jb	hhp,dlwait_5
		clrb	dl
		snb	ind.4
		setb	dl
		nop
		nop
		nop
		setb	ecu
dlwait_6	jnb	hhp,dlwait_6
		clrb	dl
		snb	ind.5
		setb	dl
		nop
		nop
		nop
		clrb	ecu
dlwait_7	jb	hhp,dlwait_7
		clrb	dl
		snb	ind.6
		setb	dl
		nop
		nop
		nop
		setb	ecu
dlwait_8	jnb	hhp,dlwait_8
		clrb	dl
		snb	ind.7
		setb	dl
		nop
		nop
		nop
		clrb	ecu
dlwait_9	jb	hhp,dlwait_9		
		clrb	dl
		inc	fsr
		djnz	variable,dlwait_1
		ret

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	;	org	$600


;---------------------------------------------
Move_Advance	mov	variable,mid_advance
		sub	variable,initial_adv
		clrb	signs.6
		jc	all_positives
		setb	signs.6
		mov	variable,initial_adv
		sub	variable,mid_advance
all_positives	mov	w,variable
		bank	page2_bank
		mov	xadv,w
		mov	w,xrev
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable,w
		bank	page2_bank
		mov	w,xrev+1
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable2,w
		sub	variable2,init_adv_rpm+1
		sub	variable,init_adv_rpm
		sc
		dec	variable2
		mov	w,variable
		bank	page2_bank
		mov	mult,w
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,variable2
		bank	page2_bank
		mov	mult+1,w
		clr	mult+2
		jmp	@move_mult
done_move_mult	bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	variable,mid_adv_rpm
		mov	variable2,mid_adv_rpm+1
		sub	variable2,init_adv_rpm+1
		sub	variable,init_adv_rpm
		sc
		dec	variable2
		mov	w,variable
		bank	page2_bank
		mov	mult,w
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,variable2
		bank	page2_bank
		mov	mult+1,w
		clr	mult+2
		jmp	@move_div
done_move_div	bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,initial_adv
		bank	page2_bank
		jb	signs.6,less_somex
		add	xadv,w
		jmp	@extras
less_somex	mov	w,xadv
		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,initial_adv-w
		bank	page2_bank
		mov	xadv,w
	;	jmp	@extras

	;	org	$600
Extras ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		bank	page3_bank	;adjust for base timing
		nop
		mov	w,base_timing
		bank	page2_bank
		sub	xadv,w

		mov	w,xrev
		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		mov	variable,w
		bank	page2_bank
		mov	w,xrev+1
		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		mov	variable2,w
		cjb	variable2,HS_Retard_RPM+1,No_Retard
		cja	variable2,HS_Retard_RPM+1,hs_r
		cjb	variable,HS_Retard_RPM,No_Retard
hs_r		mov	w,hs_retard
		bank	page2_bank
		sub	xadv,w
		sc	
		clr	xadv
		
No_Retard	mov	w,xrev
		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		mov	variable,w
		bank	page2_bank
		mov	w,xrev+1
		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		mov	variable2,w
		cjb	variable2,rpm_switch_1+1,not_s_1
		cja	variable2,rpm_switch_1+1,yes_s_1
		csb	variable,rpm_switch_1
yes_s_1		setb	Bit_RPM_1
not_s_1		mov	w,rpm_switch_1
		bank	page2_bank
		mov	mult,w
		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		mov	w,rpm_switch_1+1
		bank	page2_bank
		mov	mult+1,w
		sub	mult,#200
		sc
		dec	mult+1
		cja	xrev+1,mult+1,off_s_1
		cjb	xrev+1,mult+1,s_1
		csae	xrev,mult
s_1		clrb	Bit_RPM_1

off_s_1		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		cjb	variable2,rpm_switch_2+1,not_s_2
		cja	variable2,rpm_switch_2+1,yes_s_2
		csb	variable,rpm_switch_2
yes_s_2		setb	Bit_RPM_2
not_s_2		mov	w,rpm_switch_2
		bank	page2_bank
		mov	mult,w
		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		mov	w,rpm_switch_2+1
		bank	page2_bank
		mov	mult+1,w
		sub	mult,#200
		sc
		dec	mult+1
		cja	xrev+1,mult+1,off_s_2
		cjb	xrev+1,mult+1,s_2
		csae	xrev,mult
s_2		clrb	Bit_RPM_2

off_s_2		jmp	@POS
;---------------------------------------------------------
		org	$600
;---------------------------------------------------------
pos		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		cjb	variable2,rpm_switch_3+1,not_s_3
		cja	variable2,rpm_switch_3+1,yes_s_3
		csb	variable,rpm_switch_3
yes_s_3		setb	Bit_RPM_3
not_s_3		mov	w,rpm_switch_3
		bank	page2_bank
		mov	mult,w
		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		mov	w,rpm_switch_3+1
		bank	page2_bank
		mov	mult+1,w
		sub	mult,#200
		sc
		dec	mult+1
		cja	xrev+1,mult+1,off_s_3
		cjb	xrev+1,mult+1,s_3
		csae	xrev,mult
s_3		clrb	Bit_RPM_3

off_s_3		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		cjb	variable2,rpm_switch_4+1,not_s_4
		cja	variable2,rpm_switch_4+1,yes_s_4
		cjb	variable,rpm_switch_4,not_s_4
yes_s_4		cja	rpm_switch_4+1,rpm_switch_4+3,st_on_4
		cjb	rpm_switch_4+1,rpm_switch_4+3,win_on_hi
		cja	rpm_switch_4,rpm_switch_4+2,st_on_4
win_on_hi	cja	variable2,rpm_switch_4+3,s_4
		cjb	variable2,rpm_switch_4+3,hyster_off
		cjae	variable,rpm_switch_4+2,s_4
hyster_off	add	variable,#200
		snc
		inc	variable2
		cja	variable2,rpm_switch_4+3,off_s_4
		cjb	variable2,rpm_switch_4+3,st_on_4
		cjae	variable,rpm_switch_4+2,off_s_4
st_on_4		setb	Bit_RPM_4
		jmp	off_s_4
not_s_4		mov	w,rpm_switch_4
		bank	page2_bank
		mov	mult,w
		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		mov	w,rpm_switch_4+1
		bank	page2_bank
		mov	mult+1,w
		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		mov	w,rpm_switch_4+2
		bank	page2_bank
		mov	answer,w
		bank	page5_bank
		snb	signs.2
		bank	page8_bank
		nop
		mov	w,rpm_switch_4+3
		bank	page2_bank
		mov	answer+1,w
		
		sub	mult,#200
		sc
		dec	mult+1

		cja	answer+1,mult+1,window_up
		cjb	answer+1,mult+1,window_down
		cja	answer,mult,window_up

window_down	cja	xrev+1,answer+1,off_s_4
		cjb	xrev+1,answer+1,s_4
		cjbe	xrev,mult,s_4
		jmp	off_s_4

window_up	cja	xrev+1,mult+1,off_s_4
		cjb	xrev+1,mult+1,s_4
		csae	xrev,mult
s_4		clrb	Bit_RPM_4
		
		
off_s_4		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,retard_1
		bank	page2_bank
		stc
		snb	Bit_Ret_1
		sub	xadv,w
		sc	
		clr	xadv

		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,retard_2
		bank	page2_bank
		stc
		snb	Bit_Ret_2
		sub	xadv,w
		sc	
		clr	xadv

		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,retard_3
		bank	page2_bank
		stc
		sb	Bit_Ret_3
		sub	xadv,w
		sc	
		clr	xadv

		bank	page3_bank
		snb	signs.2
		bank	page6_bank
		nop
		mov	w,retard_4
		bank	page2_bank
		stc
		sb	Bit_Ret_4
		sub	xadv,w
		sc	
		clr	xadv

		csbe	xadv,#200
		clr	xadv

		;jmp	@Soft_Rev_Limit
Soft_Rev_Limit	mov	w,xrev
		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		mov	variable,w
		bank	page2_bank
		mov	w,xrev+1
		bank	page4_bank
		snb	signs.2
		bank	page7_bank
		nop
		mov	variable2,w
		cja	variable2,rev_limit_max+1,sftrvmx
		cjb	variable2,rev_limit_max+1,soft_rev_0
		cjb	variable,rev_limit_max,soft_rev_0
sftrvmx		sub	variable2,rev_limit_max+1
		sub	variable,rev_limit_max
		sc
		dec	variable2
		jmp	go_rev

soft_rev_0	jb	Bit_Rev_4,soft_rev_1
		cja	variable2,rev_limit_4+1,sftrv0
		cjb	variable2,rev_limit_4+1,soft_rev_1
		cjb	variable,rev_limit_4,soft_rev_1
sftrv0		sub	variable2,rev_limit_max+1
		sub	variable,rev_limit_max
		sc
		dec	variable2
		jmp	go_rev

soft_rev_1	jb	Bit_Rev_3,soft_rev_2
		cja	variable2,rev_limit_3+1,sftrv1
		cjb	variable2,rev_limit_3+1,soft_rev_2
		cjb	variable,rev_limit_3,soft_rev_2
sftrv1		sub	variable2,rev_limit_max+1
		sub	variable,rev_limit_max
		sc
		dec	variable2
		jmp	go_rev

soft_rev_2	jnb	Bit_Rev_2,soft_rev_3
		cja	variable2,rev_limit_2+1,sftrv2
		cjb	variable2,rev_limit_2+1,soft_rev_3
		cjb	variable,rev_limit_2,soft_rev_3
sftrv2		sub	variable2,rev_limit_max+1
		sub	variable,rev_limit_max
		sc
		dec	variable2
		jmp	go_rev

soft_rev_3	jnb	Bit_Rev_1,almost_skip
		cja	variable2,rev_limit_1+1,sftrv3
		cjb	variable2,rev_limit_1+1,almost_skip
		cjb	variable,rev_limit_1,almost_skip
sftrv3		sub	variable2,rev_limit_max+1
		sub	variable,rev_limit_max
		sc
		dec	variable2
		jmp	go_rev

Almost_skip	jmp	@find_adv

Go_Rev		test	variable2
		jnz	not_zero
		cja	variable,#$96,not_zero
		and	variable,#$f0
		mov	w,<>variable
		bank	page2_bank
		mov	counter,w
		test	soft_rev+1
		jnz	soft_stuff
		bank	page1_bank
		mov	w,++cylinder
		bank	page2_bank
		mov	soft_rev+1,w
		jmp	@find_adv
soft_stuff	bank	page1_bank
		cjb	cylinder,#8,lessthan8
		bank	page2_bank
		cjae	counter,#8,not_zero
		cjne	counter,#1,zxy_01
		mov	mult,#6
		jmp	ran_drop
zxy_01		cjne	counter,#2,zxy_02
		mov	mult,#3
		jmp	ran_drop
zxy_02		cjne	counter,#6,zxy_03
		mov	mult,#3
		jmp	ran_fire
zxy_03		cjne	counter,#7,zxy_04
		mov	mult,#6
		jmp	ran_fire
zxy_04		mov	mult,#1
		jmp	ran_drop

lessthan8	cjbe	cylinder,#4,lessthan6
		bank	page2_bank
		cjae	counter,#6,not_zero
		cjne	counter,#1,zxy_11
		mov	mult,#4
		jmp	ran_drop
zxy_11		cjne	counter,#2,zxy_12
		mov	mult,#2
		jmp	ran_drop
zxy_12		cjne	counter,#4,zxy_13
		mov	mult,#2
		jmp	ran_fire
zxy_13		cjne	counter,#5,zxy_14
		mov	mult,#4
		jmp	ran_fire
zxy_14		mov	mult,#1
		jmp	ran_drop

lessthan6	bank	page2_bank
		cjae	counter,#4,not_zero
		cjne	counter,#1,zxy_21
		mov	mult,#2
		jmp	ran_drop
zxy_21		cjne	counter,#3,zxy_22
		mov	mult,#2
		jmp	ran_fire
zxy_22		mov	mult,#1
		
ran_drop	djnz	soft_rev,do_ran_drop
		mov	soft_rev,mult
		jmp	not_zero

ran_fire	djnz	soft_rev,not_zero
		mov	soft_rev,mult
do_ran_drop	jmp	@find_adv		

not_zero	mov	turn_coil_on,turn_coil_off+1
		clrb	signs.1
		jmp	@tach
;-------------------------------------------------



