
		device 	SX28L,turbo,stackx_optionx
		device	osc1mhz,watchdog,protect
		freq 	1_000_000 		
		reset 	start 			

	org $08
dlyctr 	ds  1 			
dlymult	ds  1 			
work 	ds  1 
i 	ds  1
value	ds  1
hold	ds  1
signs	ds  1
	org $10
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 $30
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 $50
RPM_Switch_3  ds 2
RPM_Switch_4  ds 4
HS_Retard_RPM ds 2
HS_Retard     ds 1
Fine_Tune     ds 1
Cylinders     ds 1
	org $70
NumH          ds 1
NumL	      ds 1
TenK	      ds 1
Thou	      ds 1
Hund	      ds 1
Tens	      ds 1
Ones	      ds 1

Bank1		EQU	$00
Bank2		EQU	$20
Bank3		EQU	$40
Bank4		EQU	$60
rsbit 	equ rb.5 		
ebit 	equ rb.7
DL	equ rb.1 		
hhp	equ rb.2
ecu	equ rb.3
data	equ rb.4
b1	equ ra.2
b2	equ ra.3
b3	equ ra.0
b4	equ ra.1
bit16 	equ signs.0
scroll_fast 	equ signs.1
send_bit 	equ signs.2
start_screen	equ signs.3
	org 0

ldelay 		mov	dlymult,#5 		
delaym 		clr 	dlyctr 			

:delay 		nop
		djnz 	dlyctr,:delay
		djnz 	dlymult,delaym
		ret

init 		mode	pull_up
		mov	!ra,#0000
		mode	Schmitt
		mov	!rb,#%11110110
		mode	direction
		mov 	!rb,#%00001011 		
		clr	rb
		mov 	!rc,#%00000000
		clr	rc
		clr	signs
		mov	!option,#%11001110
		call 	ldelay 			
		mov 	rc,#$38			
		call 	pulsee
		call 	pulsee
		call 	pulsee
		call 	pulsee
		mov 	rc,#$06		
		call 	pulsee
		mov 	rc,#$0c			
		call 	pulsee
		ret

Home		clrb	rsbit
		mov	rc,#$01
		call	pulsee
		mov	rc,#$80
		call	pulsee
		setb	rsbit
		ret
						
pulsee 		setb 	ebit 			
		call 	ldelay
		clrb 	ebit
		ret

NL 		mov 	rc,#$c0 	; Move to line 2.
NL2		clrb	rsbit
		call	pulsee
		setb	rsbit
		ret

Go_to_2		call	nl		
		jmp	display_

start 		call 	init 
		jmp	download		
		
Display		call	home
		clr	i
Display_	cja	value,#26,goto3
		cja	value,#12,goto2
		jmp	@screen
goto2		jmp	@screen2
goto3		jmp	@screen3
_Display	test 	w
		jz 	yes_no
		inc 	i
		mov 	work,w 	
		cje 	work,#13,go_to_2
		mov 	rc,work
		call 	pulsee 			
		jmp 	display_ 			

Numbers		mov	rc,#$c4
		call	nl2
		mov	rc,thou
		call	pulsee
		mov	rc,hund
		call	pulsee
		mov	rc,tens
		call	pulsee
		mov	rc,ones
		call	pulsee
		mov	fsr,hold
;---------------------------------------------
		jnb	send_bit,no_send
Send_reset	setb	hhp
		setb	data
ld_up		jnb	dl,ld_up
		clrb	data
		mov	dlyctr,value
		mov	dlymult,#6
		call	send_stuff
		cjae	value,#25,downloadx
		mov	dlyctr,ind
		mov	dlymult,#8
		call	send_stuff

		cjb	fsr,#$18,no_send
		cjae	fsr,#$58,no_send
		inc	fsr
		mov	dlyctr,ind
		mov	dlymult,#8
		call	send_stuff	
		dec	fsr
		jmp	no_send

send_stuff	setb	hhp
		rr	dlyctr
wait_1		jnb	ecu,wait_1
		clrb	data
		snc
		setb	data
		clrb	hhp
wait_2		jb	ecu,wait_2
		clrb	data
		djnz	dlymult,send_stuff
		clrb	send_bit
		ret
;---------------------------------------------
downloadx	clr	!wdt
		call	ldelay
download	clrb	signs.4
		setb	hhp
		setb	data
up_ld		jnb	dl,up_ld
		clrb	hhp
		mov	fsr,#$10
		mov	dlymult,#39
reset_bits	mov	dlyctr,#4
dlwait_2	jnb	ecu,dlwait_2
		clrb	data
		clc
		snb	dl
		stc	
		rr	value
		setb	hhp
dlwait_3	jb	ecu,dlwait_3		
		clc
		snb	dl
		stc
		rr	value
		clrb	hhp
		djnz	dlyctr,dlwait_2
		cse	value,ind	;If values aren't the same then
		setb	signs.4		;it will download again to 
		mov	ind,value	;correct error.
		inc	fsr
		cjne	fsr,#$1e,still_first
		mov	fsr,#$30
still_first	cjne	fsr,#$3e,still_sec
		mov	fsr,#$50
still_sec	djnz	dlymult,reset_bits
		jb	signs.4,downloadx
		clr	rb
		clr	fsr
		mov	value,#1
		jmp	Display
;-----------------------------------------
No_send		jb	scroll_fast,fast_man
		mov	dlymult,#200
		call	delaym
fast_man	mov	dlymult,#50
		call	delaym
		cje	value,#0,this_again
		cje	value,#23,this_again
		cje	value,#24,this_again
still_hold	jmp	@button1

reset_it	mov	i,#8
long_wait	clr	dlymult
		call	delaym
		jb	b1,still_hold
		jb	b2,still_hold
		clr	!wdt
		djnz	i,long_wait
		mov	value,#28
		jmp	display

Yes_No		cjae	value,#25,next_menu
		jmp	@con_2
next_menu	mov	dlymult,#200
		call	delaym
this_again	clr	!wdt
		jb	b2,prev_menu
		clrb	scroll_fast
		clrb	send_bit
		sub	value,#1
		jnc	nozer
		csne	value,#22
		clr	value
		jmp	display
nozer		mov	value,#28
		jmp	display
Prev_menu	jb	b1,not_no
		clrb	scroll_fast
		clrb	send_bit
		inc	value
		cjne	value,#1,notv1
		mov	value,#23
		jmp	display
notv1		cjb	value,#29,display
		clr	value
		jmp	display
Not_no		jnb	b4,nrsb
		clr	i
		jmp	thereal4
nrsb		test	i
		snz
		clrb	scroll_fast
		inc	i
		csb	i,#4
		setb	scroll_fast

		cje	value,#23,wuzywuz
		cje	value,#24,buzybuz
		cjne	value,#0,thereal4
		cje	base_timing,#0,thereal4
		sub	ind,#4
		setb	send_bit
		jmp	@con_2
wuzywuz		dec	ind
		setb	send_bit
		jmp	@con_2
buzybuz		sub	ind,#2
		cja	ind,#4,big4
		mov	ind,#4
		setb	send_bit
		jmp	@con_2
big4		cjb	ind,#8,big5
		mov	ind,#8
		setb	send_bit
		jmp	@con_2
big5		mov	ind,#6
		setb	send_bit
		jmp	@con_2

thereal4	jnb	b3,nrsbx
		clr	work
		jmp	this_again
nrsbx		test	work
		snz
		clrb	scroll_fast
		inc	work
		csb	work,#4
		setb	scroll_fast

		cje	value,#23,not_0x12

		cjne	value,#0,thrett
		cjae	base_timing,initial_adv,this_again
		cjae	base_timing,mid_advance,this_again
		cjae	base_timing,max_advance,this_again
		cjae	base_timing,#200,this_again
		add	ind,#4
		setb	send_bit
		jmp	@con_2
Not_0x12	inc	ind
		setb	send_bit
		jmp	@con_2


thrett		cjne	value,#24,biznit
		add	ind,#2
		cja	ind,#4,big4x
		mov	ind,#4
		setb	send_bit
		jmp	@con_2
big4x		cjb	ind,#8,big5x
		mov	ind,#8
		setb	send_bit
		jmp	@con_2
big5x		mov	ind,#6
		setb	send_bit
		jmp	@con_2
biznit		cjne	value,#25,rockset
		mov	value,#60
		jmp	send_reset
rockset		cjne	value,#26,porkeat
		mov	value,#61
		jmp	send_reset
porkeat		cjne	value,#27,stripsit
		mov	value,#62
		jmp	send_reset
stripsit	mov	value,#1
		jmp	display

;---------------------------------------------------------
		org	$4f0
button1		clr	!wdt
		jb	b1,button2
		jb	b2,not_rstit
		jmp	@reset_it
not_rstit	clrb	scroll_fast
		clrb	send_bit
		inc	value
		cjb	value,#23,tagz
		mov	value,#1
tagz		jmp	@display
button2		jb	b2,button3
		clrb	scroll_fast
		clrb	send_bit
		dec	value
		cjne	value,#0,tagx
		mov	value,#22
tagx		jmp	@display
button3		jnb	b3,nb3
		clr	i
		jmp	button4
nb3		test	i
		snz
		clrb	scroll_fast
		inc	i
		csb	i,#4
		setb	scroll_fast
		
just_inc	jb	bit16,rev_number
		cjae	ind,#200,button4
		add	ind,#4
		setb	send_bit
		jmp	convert
rev_number	add	ind,#50
		inc	fsr
		snc
		inc	ind
		dec	fsr
		jnb	scroll_fast,latty
		add	ind,w
		inc	fsr
		snc
		inc	ind
		dec	fsr
		nop
latty		cjne	value,#2,Not_2
		cja	init_adv_rpm+1,mid_adv_rpm+1,toomc
		cjb	init_adv_rpm+1,mid_adv_rpm+1,lattyx
		cjbe	init_adv_rpm,mid_adv_rpm,lattyx
toomc		mov	init_adv_rpm+1,mid_adv_rpm+1
		mov	init_adv_rpm,mid_adv_rpm
		jmp	lattyx
not_2		cjne	value,#4,Not_butt
		cja	mid_adv_rpm+1,max_adv_rpm+1,toomc2
		cjb	mid_adv_rpm+1,max_adv_rpm+1,lattyx
		cjbe	mid_adv_rpm,max_adv_rpm,lattyx
toomc2		mov	mid_adv_rpm+1,max_adv_rpm+1
		mov	mid_adv_rpm,max_adv_rpm
		jmp	lattyx
not_butt	inc	fsr
		cje	ind,#$23,no_latt
		dec	fsr
		jmp	lattyx
no_latt		dec	fsr
		cjbe	ind,#$28,lattyx
		mov	ind,#$28		
lattyx		setb	send_bit
		jmp	convert
button4		jnb	b4,nb4
		clr	work
		jmp	button1
nb4		test	work
		snz
		clrb	scroll_fast
		inc	work
		csb	work,#4
		setb	scroll_fast
			
		cjne	value,#1,Not_01
		cjbe	initial_adv,base_timing,button1
		jmp	only8
Not_01		cjne	value,#3,Not_03
		cjbe	mid_advance,base_timing,button1
		jmp	only8
Not_03		cjne	value,#5,just_dec
		cjbe	max_advance,base_timing,button1
		jmp	only8
just_dec	jb	bit16,rev_number2
		cje	ind,#0,button1
only8		sub	ind,#4
		setb	send_bit
		jmp	convert
rev_number2	sub	ind,#50
		inc	fsr
		sc
		dec	ind
		dec	fsr
		jnb	scroll_fast,miss_me
		sub	ind,w
		inc	fsr
		sc
		dec	ind
		dec	fsr
		nop
miss_me		cjne	value,#4,Not_2me
		cjb	mid_adv_rpm+1,init_adv_rpm+1,toomc1
		cja	mid_adv_rpm+1,init_adv_rpm+1,watkins
		cjae	mid_adv_rpm,init_adv_rpm,watkins
toomc1		mov	mid_adv_rpm+1,init_adv_rpm+1
		mov	mid_adv_rpm,init_adv_rpm
		jmp	watkins
not_2me		cjne	value,#6,Not_2sx
		cjb	max_adv_rpm+1,mid_adv_rpm+1,toomc2x
		cja	max_adv_rpm+1,mid_adv_rpm+1,watkins
		cjae	max_adv_rpm,mid_adv_rpm,watkins
toomc2x		mov	max_adv_rpm+1,mid_adv_rpm+1
		mov	max_adv_rpm,mid_adv_rpm
		jmp	watkins
not_2sx		inc	fsr
		cje	ind,#$01,no_miss
		dec	fsr
		jmp	watkins
no_miss		dec	fsr
		cjae	ind,#$90,watkins
		mov	ind,#$90
watkins		setb	send_bit
	;	jmp	convert

convert		jmp	@con_2
;------------------------------------------------------
		org	$300
con_2		mov	hold,fsr
		mov	w,ind
		bank	bank4
		mov	numl,w
		jb	bit16,both_bytes
		clr	numh
		cjae	value,#23,con_now
		clc
		rr	numl
		rr	numl
		jmp	con_now
both_bytes	mov	w,++hold
		mov	fsr,w
		mov	w,ind
		bank	bank4
		mov	numh,w
con_now		mov     W, <>NumH
        	or      W, #$F0  
        	mov     Thou, W 
        	add     Thou, W 
        	mov     Hund, W
        	mov     W, #$E2
        	add     Hund, W 
        	mov     W, #$32
        	add     W, Hund
        	mov     Ones, W  

        	mov     W, NumH 
        	and     W, #$0F 
        	add     Hund, W 
        	add     Hund, W 
        	add     Ones, W 

        	mov     Tens, W
        	mov     W, #$E9
        	add     Tens, W  
        	mov     W, Tens
        	add     Tens, W  
        	add     Tens, W 

        	mov     W, <>NumL  
        	and     W, #$0F  
        	add     Tens, W 
        	add     Ones, W  

        	rl      Tens 
        	rl      Ones 
        	not     Ones  

	        rl      Ones 

        	mov     W, NumL  
        	and     W, #$0F  
        	add     Ones, W 
        	rl      Thou  

        	mov     W, #$07 
        	mov     TenK, W 

	        mov     W, #$0A 
Lb1 
        	add     Ones, W 
        	dec     Tens  
        	sb      3.0
        	jmp     Lb1  
        
Lb2 
        	add     Tens, W 
        	dec     Hund 
        	sb      3.0
        	jmp     Lb2 
Lb3
        	add     Hund, W 
        	dec     Thou 
        	sb      3.0
        	jmp     Lb3 
Lb4 
        	add     Thou, W 
        	dec     TenK 
        	sb      3.0
        	jmp     Lb4 

		mov	w,#$30
		add	thou,w
		add	hund,w
		add	tens,w
		add	ones,w
		cjne	thou,#$30,num_2
		sub	thou,#$10
		cjne	hund,#$30,num_2
		sub	hund,#$10
		cjne	tens,#$30,num_2
		sub	tens,#$10
num_2		jmp	@numbers
;-------------------------------------------------------
	org	$200
Screen		call 	lookup
		jmp	@_display
lookup		mov	w,value
		jmp	pc+w
		jmp	lookup00
		jmp	lookup01
		jmp	lookup02
		jmp	lookup03
		jmp	lookup04
		jmp	lookup05
		jmp	lookup06
		jmp	lookup07
		jmp	lookup08
		jmp	lookup09
		jmp	lookup10
		jmp	lookup11
		jmp	lookup12
		
lookup00	mov	fsr,#Base_Timing
		clrb	bit16
		mov	w,i
		jmp	pc+w
		retw	'Base Tim',13
		retw	'ing',0

lookup01	mov	fsr,#initial_adv
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Initial ',13
		retw	'Adv',0

lookup02	mov	fsr,#init_adv_rpm
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Init Adv',13
		retw 	'RPM',0

lookup03	mov	fsr,#mid_advance
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Mid Adva',13
		retw 	'nce',0

lookup04	mov	fsr,#mid_adv_rpm
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Mid Adv ',13
		retw 	'RPM',0

lookup05	mov	fsr,#max_advance
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Max Adva',13
		retw 	'nce',0

lookup06	mov	fsr,#max_adv_rpm
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Max Adv ',13
		retw 	'RPM',0

lookup07	mov	fsr,#Rev_limit_1
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Rev Limi',13
		retw 	't 1',0

lookup08	mov	fsr,#Rev_limit_2
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Rev Limi',13
		retw 	't 2',0

lookup09	mov	fsr,#Rev_limit_3
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Rev Limi',13
		retw 	't 3',0

lookup10	mov	fsr,#Rev_limit_4
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Rev Limi',13
		retw 	't 4',0

lookup11	mov	fsr,#Rev_limit_max
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Max Rev',0

lookup12	mov	fsr,#Rpm_switch_1
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'RPM Swit',13
		retw 	'ch1',0


	org	$400
Screen2		call 	lookupx
		jmp	@_display
lookupx		mov	w,#13
		mov	w,value-w
		jmp	pc+w
		jmp	lookup13
		jmp	lookup14
		jmp	lookup15
		jmp	lookup16
		jmp	lookup17
		jmp	lookup18
		jmp	lookup19
		jmp	lookup20
		jmp	lookup21
		jmp	lookup22
		jmp	lookup23
		jmp	lookup24
		jmp	lookup25
		jmp	lookup26
		
lookup13	mov	fsr,#Rpm_switch_2
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'RPM Swit',13
		retw 	'ch2',0

lookup14	mov	fsr,#Rpm_switch_3
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'RPM Swit',13
		retw 	'ch3',0

lookup15	mov	fsr,#Rpm_switch_4
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Switch 4',13
		retw 	' On',0

lookup16	mov	fsr,#Rpm_switch_4+2
		setb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Switch4',13
		retw 	'Off',0

lookup17	mov	fsr,#retard_1
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Retard 1',0

lookup18	mov	fsr,#retard_2
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Retard 2',0

lookup19	mov	fsr,#retard_3
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Retard 3',0

lookup20	mov	fsr,#retard_4
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Retard 4',0

lookup21	mov	fsr,#HS_retard
		clrb	bit16
		mov 	w,i 			
		jmp 	pc+w
 		retw 	'Hi Speed',13
		retw 	' Ret',0

lookup22	mov	fsr,#HS_Retard_RPM
		setb	bit16
		mov	w,i
		jmp	pc+w
		retw 	'HS Ret R',13
		retw 	'PM',0

lookup23	mov	fsr,#Fine_Tune
		clrb	bit16
		mov	w,i
		jmp	pc+w
		retw 	'Fine Tun',13
		retw 	'e',0

lookup24	mov	fsr,#Cylinders
		clrb	bit16
		mov	w,i
		jmp	pc+w
		retw 	'Cylinder',13
		retw 	's',0

lookup25	mov	w,i
		jmp	pc+w
		retw	'Reset',0
		
lookup26	mov	w,i
		jmp	pc+w
		retw	'Street',0

	org	$600
Screen3		call 	lookupx3
		jmp	@_display
lookupx3	mov	w,#27
		mov	w,value-w
		jmp	pc+w
		jmp	lookup27
		jmp	lookup28


lookup27	mov	w,i
		jmp	pc+w
		retw	'Strip',0
		
lookup28	mov	w,i
		jmp	pc+w
		retw	'Main Men',13
		retw	'u',0


