This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn://svn.cc65.org/cc65/trunk@3 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
63
libsrc/common/rand.s
Normal file
63
libsrc/common/rand.s
Normal file
@@ -0,0 +1,63 @@
|
||||
;
|
||||
; Randum number generator
|
||||
;
|
||||
; Written and donated by Sidney Cadot - sidney@ch.twi.tudelft.nl
|
||||
;
|
||||
; May be distributed with the cc65 runtime using the same license.
|
||||
;
|
||||
;
|
||||
; int rand (void);
|
||||
; void srand (unsigned seed);
|
||||
;
|
||||
; Uses 4-byte state.
|
||||
; Multiplier must be 1 (mod 4)
|
||||
; Added value must be 1 (mod 2)
|
||||
; This guarantees max. period (2**32)
|
||||
; Bits 8-22 are returned (positive 2-byte int)
|
||||
; where 0 is LSB, 31 is MSB.
|
||||
; This is better as lower bits exhibit easily
|
||||
; detectable patterns.
|
||||
;
|
||||
|
||||
.export _rand, _srand
|
||||
|
||||
.bss
|
||||
|
||||
rand: .res 4 ; Seed
|
||||
|
||||
.code
|
||||
|
||||
_rand: clc
|
||||
lda rand+0 ; SEED *= $01010101
|
||||
adc rand+1
|
||||
sta rand+1
|
||||
adc rand+2
|
||||
sta rand+2
|
||||
adc rand+3
|
||||
sta rand+3
|
||||
clc
|
||||
lda rand+0 ; SEED += $31415927
|
||||
adc #$27
|
||||
sta rand+0
|
||||
lda rand+1
|
||||
adc #$59
|
||||
sta rand+1
|
||||
pha
|
||||
lda rand+2
|
||||
adc #$41
|
||||
sta rand+2
|
||||
and #$7f ; Suppress sign bit (make it positive)
|
||||
tax
|
||||
lda rand+3
|
||||
adc #$31
|
||||
sta rand+3
|
||||
pla ; return bit 8-22 in (X,A)
|
||||
rts
|
||||
|
||||
_srand: sta rand+0 ; Store the seed
|
||||
stx rand+1
|
||||
lda #0
|
||||
sta rand+2 ; Set MSW to zero
|
||||
sta rand+3
|
||||
rts
|
||||
|
||||
Reference in New Issue
Block a user