Page 1 of 1

Fast RNG algorithm

Posted: October 11th, 2022, 00:54
by zoranc
Just found a nice pseudo random number generator algorithm suitable for S1C88. It uses only 16 bit numbers and is very fast when implemented in assembly:

Code: Select all

uint16_t xs = 1; // initialised with a non-zero seed
xs ^= xs << 7
xs ^= xs >> 9
xs ^= xs << 8
Here is implementation for several processors including z80 which is very similar to S1C88: https://github.com/impomatic/xorshift798

Re: Fast RNG algorithm

Posted: October 25th, 2022, 04:20
by YasaSheep
Nice! Here's a 36 cycle version in Epson syntax, best I could get:

Code: Select all

xorshift:
	LD HL,#XSHFT
	LD BA,[HL]
	RR B
	RR A
	XOR [HL],A
	LD BA,[HL]
	RR B
	XOR A,B
	XOR [HL],A
	INC HL
	LD [HL],A
	RET
xor is fairly limited (the only two register version is a,b) so I switched to xoring in memory where possible.