;fastscan assembly procedure
;its argument is the character to scan for
;it scans to the end of a word but can start in the middle of a word
;to do that set vlb to false before calling
;if char is found: vwdremain, vpw, and vlb will point at it
;if char is not found: vwdremain is set to 0, vpw and vlb are as passed

	.bext vpw,vlb,vchremain,fastscan,getsintfast

	.srel
fastscan: .fastscan

	.nrel

; ac0 the char, ac1 mask, ac2 is test word, ac3 buffer pointer

.fastscan:
	sta 3 returnaddr	; save BCPL state
	sta 2 frameptr
	lda 3 @.vchremain
	mov 3 3 snr
	jmp notfound;

	lda 1 bytemask
	lda 3 @.vpw
;in order to save a byteswap operation you test on
;char lshift 8 therefore...
	movs 0 0
	lda 2 @.vlb
	mov 2 2 szr
	jmp leftentry
	jmp rightentry

nextword:
	inc 3 3
leftentry:
	lda 2 0 3
	and 1 2
	sub 0 2 snr
	jmp foundleft
	dsz @.vchremain;
	jmp rightentry
	jmp notfound;
rightentry:
	lda 2 0 3
	movs 2 2
	and 1 2
	sub 0 2 snr
	jmp foundright

	dsz @.vchremain;
	jmp nextword

	jmp notfound
foundright:
	sub 0 0 skp
foundleft:
	adc 0 0
	sta 0 @.vlb

	sta 3 @.vpw
notfound:
	lda 3 returnaddr
	lda 2 frameptr
	jmp 1,3

; constants

.vpw:vpw
.vlb:vlb
.vchremain:vchremain
bytemask: 177400
returnaddr: 0
frameptr: 0
one: 1

	.srel
getsintfast:.getsintfast

	.nrel

.getsintfast:
	sta 2 frameptr
	inc 3 3
	sta 3 returnaddr
	sta 1 pchar

	isz @.vchremain	; vchremain must reflect rv pchar
	jmp .+1
	lda 2 @.vlb	; be sure vlb is 0 or -1
	mov# 2 2 szr
	   adc 2 2 	; it is now -1 for sure
	sta 2 @.vlb

	lda 1 @pchar	; first char
	lda 3 @.vpw
	jmp entry

loop:
	lda 1 0 3
	lda 2 @.vlb
	com 2 2 snr	; vlb = not vlb
	   movs 1 1 skp	; left byte
	   inc 3 3	; right byte
	sta 2 @.vlb

	lda 2 mask
	and 2 1

entry:
	lda 2 nine
	subz# 1 2 snc	; **?? skip if ac2 ge ac1
	   jmp exit
	lda 2 zero
	subz 2 1 snc	; ** ? subtract, and skip if result ge 0
	   jmp exit1

	movzl 0 2	; multiply by 10
	movzl 2 2
	addzl 2 0

	add 1 0		; sum = sum + ...

	dsz @.vchremain
	   jmp loop

	adc 1 1		; char = -1 if ran off end
	jmp returncode
exit1:	   add 2 1
exit:	sta 3 @.vpw

	dsz @.vchremain	; compensate for initial increment
	jmp .+1
returncode:
	sta 1 @pchar
	lda 2 frameptr
	jmp @returnaddr

mask: 377
nine: 71
zero: 60
pchar: 0


	.end