
		CASE	OBJECT 

;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	Corinfo

;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	ntab
	ENTRY	tally

	LOAD 'macros.dump'

*****************************************************************

qtheta	PROC		EXPORT
			;Using ThetaData

QBIAS	equ 30
QCHARS	equ 22

		input	sa:l,sb:l,lena:w,lenb:w
		output   result:w

		local	m:w,r:w,temp:l

		begin

		if_ lena,<,lenb,ok1

		movelong sa,temp
		movelong sb,sa
		movelong temp,sb
		moveword lena,temp
		moveword lenb,lena
		moveword temp,lenb

ok1		if_ lenb,²,#QCHARS,ok2
		moveword #QCHARS,lenb
		if_ lena,²,#QCHARS,ok2
		moveword #QCHARS,lena

ok2
		ldy lena
		dey
		lda #0
		shortm
		while_ y,<,#$8000
		lda [sa],y
		tax
		stz tally,x
		lda [sb],y
		tax
		stz tally,x
		dey
		endwhile_

		ldy lenb
		dey
		while_ y,³,lena
		lda [sb],y
		tax
		stz tally,x
		dey
		endwhile_

		ldy lenb
		while_ y,>,lena
		lda [sb]
		tax
		dec tally,x
		dey
		longm
		inc sb
		shortm
		endwhile_
		longm

		stz m
		stz r
		ldy #0
		while_ y,<,lena
		shortm
		lda [sa],y
		tax
		inc tally,x
		lda tally,x
		if_ a,=,#0,inr1
		if_ a,<,#128,tryb
inr1	inc r
tryb	lda [sb],y
		tax
		dec tally,x
		lda tally,x
		if_ a,>,#127,didem
		inc r
didem	iny
		longm
		eval 'm.+r'
		sta m
		lda #0
		endwhile_

		asl m

		moveword ntab:lena,a
		and #$ff
		sta r
		moveword ntab:lenb,a
		and #$ff
		eval '.+r'
		sta r
		if_ r,­,m,noteq

		moveword #$ffff,result
		bra done

noteq	pha
		pha
		pha
		pha
		eval 'm.+#QBIAS'
		pha
		pea 0
		pea 0
		eval 'r.+#QBIAS'
		pha
		_LongDivide

		pullword result
		pla
		pla
		pla
done
		return

		ENDP

*****************************************************************

theta	PROC		EXPORT
			;Using ThetaData

		input	sa:l,sb:l,lena:w,lenb:w
		output   result:w

		local	m:w,r:w,temp:l,totm:w,totr:w,ci:l

		begin

		movelong Corinfo,ci

		if_ lena,<,lenb,_ok

		movelong sa,temp
		movelong sb,sa
		movelong temp,sb
		moveword lena,temp
		moveword lenb,lena
		moveword temp,lenb

_ok		ldy lena
		dey
		lda #0
		shortm
		while_ y,<,#$8000
		lda [sa],y
		tax
		stz tally,x
		lda [sb],y
		tax
		stz tally,x
		dey
		endwhile_

		ldy lenb
		dey
		while_ y,³,lena
		lda [sb],y
		tax
		stz tally,x
		dey
		endwhile_

		longm
		stz m
		stz r
		stz totm
		stz totr
		shortm
		moveword lenb,temp
		while_ temp,>,lena
		lda [sb]
		tax
		dec tally,x
		tay
		lda [ci],y
		and #7
		longm
		eval '.+totr'
		sta totr
		eval '.+totm'
		sta totm
		dec temp
		inc sb
		lda #0
		shortm
		endwhile_
		longm

		moveword lena,temp
		while_ temp,>,#0
		lda #0
		shortm
		lda [sa]
		tax
		inc tally,x
		tay
		lda [ci],y
		and #7
		tay
		lda tally,x
		longm
		if_ a,=,#0,inr
		if_ a,<,#128,fixtr1
inr		tya
		eval '.+r'
		sta r
fixtr1	tya
		eval '.+totr'
		sta totr

		lda #0
		shortm
		lda [sb]
		tax
		dec tally,x
		tay
		lda [ci],y
		and #7
		tay
		lda tally,x
		longm
		if_ a,>,#127,fixtr2
		tya
		eval '.+r'
		sta r
fixtr2	tya
		eval '.+totr'
		sta totr
		dec temp
		inc sa
		inc sb
		eval 'm.+r'
		sta m
		eval 'totm.+totr'
		sta totm
		endwhile_

		lda m
		asl a
		asl a
		if_ a,³,totm,part2
		stz result
		brl done

part2	ldy lena
		lda #0
		while_ y,>,#0
		dec sa
		shortm
		lda [sa]
		tax
		stz tally,x
		dey
		longm
		endwhile_
		eval 'sa.+lena'
		sta sa

		stz r
		stz totr
		moveword lenb,temp
		while_ temp,>,lena
		dec sb
		lda #0
		shortm
		lda [sb]
		tax
		dec tally,x
		tay
		lda [ci],y
		lsr a
		lsr a
		lsr a
		longm
		eval '.+totr'
		sta totr
		eval '.+totm'
		sta totm
		dec temp
		endwhile_

		moveword lena,temp
		while_ temp,>,#0
		dec sa
		dec sb
		lda #0
		shortm
		lda [sa]
		tax
		inc tally,x
		tay
		lda [ci],y
		lsr a
		lsr a
		lsr a
		tay
		lda tally,x
		longm
		if_ a,=,#0,inr2
		if_ a,<,#128,fixtr3
inr2	tya
		eval '.+r'
		sta r
fixtr3	tya
		eval '.+totr'
		sta totr

		lda #0
		shortm
		lda [sb]
		tax
		dec tally,x
		tay
		lda [ci],y
		lsr a
		lsr a
		lsr a
		tay
		lda tally,x
		longm
		if_ a,>,#127,fixtr4
		tya
		eval '.+r'
		sta r
fixtr4	tya
		eval '.+totr'
		sta totr
		dec temp
		eval 'm.+r'
		sta m
		eval 'totm.+totr'
		sta totm
		endwhile_

		asl m

		if_ m,­,totm,noteq

		moveword #$ffff,result
		bra done

noteq	pha
		pha
		pha
		pha
		pushword m
		pea 0
		pea 0
		pushword totm
		_LongDivide

		pullword result
		pla
		pla
		pla
done
		return

		ENDP

*****************************************************************

ThetaData	PROC		EXPORT
		EXPORT	tally
		EXPORT	ntab

tally		DS.B 256
ntab	DC.B	0,1,3,6,10,15,21,28,36,45,55,66,78,91,105
	DC.B	120,136,153,171,190,210,231,253

		ENDP
		END

