;***************************************	
;* psuran23--pseudorandom # generator *
;* of the shift/xor type, 23 bit shift*
;* very good as coin flipper, test +/-*
;* OK (not super) random integers     *
;*    by Tracy Allen  76670,326       *
;* Enter with BASIC  CALL 64704,A,HL  *
;*   A=0 for next # in sequence       *
;*   HL=varptr(X%), returns # in X%   *
;* or                                 *
;*   1<A<256 to re-seed sequence      *
;*   HL=two lo bytes, A=hi byte       *
;**************************************
;destroys A,BC,DE,HL

;adapted to asm80
;6/20/2012
;Willard Goosey

;$Id: psuran.Asm,v 1.16 2017/03/18 06:27:38 goosey Exp goosey $

;also returns rnd value in HL

;needs 8085 undocumented opcode macros goosey 2012
ifdef(`shlx',`',`include(`8085undoc.def')')

;m4 -DRANSTANDALONE to make the standalone
;version that fits in the altbuf.

;else this builds a function that goes where included

ifdef(`RANSTANDALONE',`	
;standalone version code
altbuf	equ 0fcc0h	;for M100
			;use F854h in T200
	org altbuf	
;standalone version
',`')


psuran:	ora a		;test entry
	jz rule		;its get next #
reset:	shld seed	;it's a reseed
	sta seed+2
	ret
rule:	xchg		;de holds varptr
	lxi h,seed+2	;hi byte
	mov a,m
	ani 42h		;mask for xor
	stc		;perform xor
	jpo rule1
	cmc
rule1	dcx h		;result in carry
	dcx h		;point to lo byte
	mvi c,3		;3 bytes to rotate
rule2	mov a,m		;rotate carry
	ral		;into low bit
	mov m,a		;& rotate all
	inx h		;3 bytes left
	dcr c
	jnz rule2
	lhld seed	;done, get low word
	shlx		;store it using varptr
			;I use tundras names
			;goosey 6/20/2012
	ret
seed:	db 85,85,85

ifdef(`RANSTANDALONE',`
;standalone version
	end psuran
;standalone version
',`')

;end of psuran.asm
