;  Triexml.asm	implements Random Number generator and block compair routine
;				when the ram is not present

;		Random Number generator
;		AC0 points to a table in memory with the following:
;		AC0!0 = initial random number
;		AC0!1 = number of numbers to generate
;		AC0!2 = memory location to place the random numbers
;		when done, it makes one more ranjdom number and returns it in AC0


; frame
	callersFrame=0
	savedPC=1
	temp=2
	extraArguments=3
	firstArg=4
	secondArg=5


	.srel
RandomASM: 	pRndm
CompairASM: 	pComp
	.bext RandomASM,CompairASM


	.nrel

POLLY:		77213
SavedAC2:	0
SavedAC3:	0
EndLoc: 		0
Errs: 			0


pRndm:	sta 2 SavedAC2
			sta 3 SavedAC3
			mov 0 2
			lda 0 0,2;				get AC0!0 = initial random number
			lda 1 1,2;				get AC0!1 = number of numbers to generate
			lda 3 2,2;				get AC0!2 = memory location to place the random numbers
			add 3 1
			sta 1 EndLoc;			save as the last location to store
			lda 1 POLLY;			init AC1 to xor bits
			movz 0 0 SNR;		check that the random number is non zero
			inc 0 0;				set to 1 if it is

RanLoop:	movzl 0 0 SZC;		shift number left, skip if new carry is zero
			jmp MyXor;			shift number left, skip if new carry is zero

XorRtn:	lda 2 EndLoc;		
			sub 3 2 SNR;			check for done
			jmp RanExit;				ifso foto exit
			sta 0 0,3
			inc 3 3
			jmp RanLoop

RanExit:	lda 2 SavedAC2
			lda 3 SavedAC3
			jmp 1 3

MyXor:	mov 0 2
			andzl 1 2
			add 1 0
			sub 2 0
			jmp XorRtn



pComp:	sta 2 SavedAC2
			sta 3 SavedAC3
			mov 0 2
			lda 1 2,2;				get AC0!2 = count
			lda 3 0,2;				get AC0!0 = first buffer
			lda 2 1,2;				get AC0!1 = second buffer
			add 2 1
			sta 1 EndLoc;		save as the last location for AC2
			sub 0 0
			sta 0 Errs

CompL:	lda 0 0,2;				get AC0!0 = initial random number
			inc 2 2
			lda 1 0,3;				get AC0!0 = initial random number
			inc 3 3
			sub 1 0 SZR;			test for values equal
			isz Errs;				This will never skip
			lda 1 EndLoc;		get AC0!0 = initial random number
			sub 2 1 SZR;			test for values equal
			jmp CompL

			lda 0 Errs;				get AC0!0 = initial random number
			jmp RanExit
	.end