Create a gist now

Instantly share code, notes, and snippets.

@raxoft /cmwc.asm
Last active Oct 25, 2017

What would you like to do?
Fast quality CMWC random number generator for Z80 I have created to weed out the crap RNGs out there. 32+10 bytes, 146..149 T cycles, period 253*2^59, which is more than 2^66 or 10^20.
; 8-bit Complementary-Multiply-With-Carry (CMWC) random number generator.
; Created by Patrik Rak in 2012, and revised in 2014/2015,
; with optimization contribution from Einar Saukas and Alan Albrecht.
; See http://www.worldofspectrum.org/forums/showthread.php?t=39632
org 40000
call rnd ; BASIC driver
ld c,a
ld b,0
ret
rnd ld hl,table
ld a,(hl) ; i = ( i & 7 ) + 1
and 7
inc a
ld (hl),a
inc l ; hl = &cy
ld d,h ; de = &q[i]
add a,l
ld e,a
ld a,(de) ; y = q[i]
ld b,a
ld c,a
ld a,(hl) ; ba = 256 * y + cy
sub c ; ba = 255 * y + cy
jr nc,$+3
dec b
sub c ; ba = 254 * y + cy
jr nc,$+3
dec b
sub c ; ba = 253 * y + cy
jr nc,$+3
dec b
ld (hl),b ; cy = ba >> 8, x = ba & 255
cpl ; x = (b-1) - x = -x - 1 = ~x + 1 - 1 = ~x
ld (de),a ; q[i] = x
ret
table db 0,0,82,97,120,111,102,116,20,15
if (table/256)-((table+9)/256)
error "whole table must be within single 256 byte block"
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment