;proofed 1
;this increments encoderval, compares also
;incremented stuff with encmaxval, and
;sets to 0 if overflow.
;(used by ENC_WORK in STEP_WORK)
inc_encval_ov
	INCFSZ	encLow,1,AC    ;inc lowbyte
	BRA     inc_encval_cmp ;skip this if res 0
	INCF    encHigh,1,AC   ;inc highbyte
	;compare encval with encmaxval
inc_encval_cmp
	MOVF	encLow,W,AC
	CPFSEQ  encMaxLow,AC
	RETURN
	MOVF	encHigh,W,AC
	CPFSEQ	encMaxHigh,AC
	RETURN
	CLRF	encLow         ;set all to 0 if max
	CLRF	encHigh
	RETURN


;proofed 1
;this decrements encoderval, compares also
;to underflow 0, and sets to maxval is this
;happens.
;(used by ENC_WORK in STEP_WORK )
dec_encval_ov
	MOVF	encLow,W,AC ;if 0 Z is set
	BNZ		dec_encval
	MOVF	encHigh,W,AC ;if 0 Z is set
	BNZ		dec_encval
	;ok all is 0, before decrement,
	;so we will store maxencval -1 to current val
	MOVFF	encMaxLow,encLow
	MOVFF	encMaxHigh,encHigh
	;and do a -1
dec_encval
	DECF	encLow,1,AC
	MOVLW	0xff
	CPFSEQ  encLow,AC
	RETURN  ;skip if encLow == 0xff
	DECF	encHigh,1,AC
	CPFSEQ  encHigh,AC
	RETURN
	;do overflow work, but should never reached
	RETURN
    ;max 25 cycles



;proofed 1
;this is called by STEP_WORK, when starting
;if positions are equal, only encoderwork will be
;done, no stepping
;This clears work_flags.POS_EQUAL, compares positions,
;and sets POS_EQUAL, if positions equal.
;the flags POS_SMALLER, and POS_EQUAL, are only cleared
;if positions are really equal.
;Use this, if only testing for equal, because this
;uses less cycles then test_pos
test_ifposequaltrg
	BCF		work_flags,POS_EQUAL,AC
	MOVF	target3,W,AC
	CPFSEQ	curpos3,AC
	RETURN
	MOVF	target2,W,AC
	CPFSEQ	curpos2,AC
	RETURN
	MOVF	target1,W,AC
	CPFSEQ	curpos1,AC
	RETURN
	MOVF	target0,W,AC
	CPFSEQ	curpos0,AC
	RETURN

	BCF		work_flags,POS_SMALLER,AC
	BCF		work_flags,POS_GREATER,AC
	BSF		work_flags,POS_EQUAL,AC
	RETURN
	;20 cycles max with call, and return



;proofed 1
;this is called by scan_direction, if current, and target
;are in the same section,  or if NO_CIRCLE used directly by STEP_WORK
;it sets work_flags POS_SMALLER,POS_GREATER,POS_EQUAL,
;for the status of currentpos ( compared with target )
test_pos
	BCF		work_flags,POS_SMALLER,AC
	BCF		work_flags,POS_GREATER,AC
	BCF		work_flags,POS_EQUAL,AC

	MOVF	target3,W,AC
	CPFSGT	curpos3,AC
	BRA		tstcursm1         ;smaller, or equal
	BRA		testcurisgreater  ;if sure greater
tstcursm1
	CPFSLT	curpos3,AC
	BRA		tstcursm2         ;greater, or equal
	BRA		testcurissmaller  ;if sure smaller
tstcursm2

	MOVF	target2,W,AC
	CPFSGT	curpos2,AC
	BRA		tstcursm3         ;smaller, or equal
	BRA		testcurisgreater  ;if sure greater
tstcursm3
	CPFSLT	curpos2,AC
	BRA		tstcursm4         ;greater, or equal
	BRA		testcurissmaller  ;if sure smaller
tstcursm4

	MOVF	target1,W,AC
	CPFSGT	curpos1,AC
	BRA		tstcursm5         ;smaller, or equal
	BRA		testcurisgreater  ;if sure greater
tstcursm5
	CPFSLT	curpos1,AC
	BRA		tstcursm6         ;greater, or equal
	BRA		testcurissmaller  ;if sure smaller
tstcursm6
	MOVF	target0,W,AC
	CPFSGT	curpos0,AC
	BRA		tstcursm7         ;smaller, or equal
	BRA		testcurisgreater  ;if sure greater
tstcursm7
	CPFSLT	curpos0,AC
	BRA		testcurisequal    ;this can only be equal
	BRA		testcurissmaller  ;if sure smaller

testcurisequal
	BSF		work_flags,POS_EQUAL,AC
	RETURN
testcurisgreater
	BSF		work_flags,POS_GREATER,AC
	RETURN
testcurissmaller
	BSF		work_flags,POS_SMALLER,AC
	RETURN
    ;38 cycles with call, and return