diff --git a/libsrc/common/rand.s b/libsrc/common/rand.s index fc23b6ee4..a272af80a 100644 --- a/libsrc/common/rand.s +++ b/libsrc/common/rand.s @@ -3,6 +3,7 @@ ; ; Written and donated by Sidney Cadot - sidney@ch.twi.tudelft.nl ; 2016-11-07, modified by Brad Smith +; 2019-10-07, modified by Lewis "LRFLEW" Fox ; ; May be distributed with the cc65 runtime using the same license. ; @@ -22,6 +23,17 @@ ; The best 8 bits, 24-31 are returned in the ; low byte A to provide the best entropy in the ; most commonly used part of the return value. +; +; Uses the following LCG values for ax + c (mod m) +; a = $01010101 +; c = $B3B3B3B3 +; m = $100000000 (32-bit truncation) +; +; The multiplier was carefully chosen such that it can +; be computed with 3 adc instructions, and the increment +; was chosen to have the same value in each byte to allow +; the addition to be performed in conjunction with the +; multiplication, adding only 1 additional adc instruction. ; .export _rand, _srand @@ -35,10 +47,10 @@ rand: .dword 1 .code _rand: clc - lda rand+0 ; SEED += $B3 + lda rand+0 adc #$B3 sta rand+0 - adc rand+1 ; SEED *= $01010101 + adc rand+1 sta rand+1 adc rand+2 sta rand+2