replaced integer RNG with smaller and faster routine.

This commit is contained in:
Irmen de Jong 2022-10-22 20:53:29 +02:00
parent aa99a7df64
commit adcbe55307
2 changed files with 27 additions and 39 deletions

View File

@ -229,44 +229,32 @@ _divisor .word 0
.pend
randbyte = randword ; -- 8 bit pseudo random number generator into A (by just reusing randword)
randword .proc
; -- 16 bit pseudo random number generator into AY
; rand64k ;Factors of 65535: 3 5 17 257
lda sr1+1
asl a
asl a
eor sr1+1
asl a
eor sr1+1
asl a
asl a
eor sr1+1
asl a
rol sr1 ;shift this left, "random" bit comes from low
rol sr1+1
; rand32k ;Factors of 32767: 7 31 151 are independent and can be combined
lda sr2+1
asl a
eor sr2+1
asl a
asl a
ror sr2 ;shift this right, random bit comes from high - nicer when eor with sr1
rol sr2+1
lda sr1+1 ;can be left out
eor sr2+1 ;if you dont use
tay ;y as suggested
lda sr1 ;mix up lowbytes of SR1
eor sr2 ;and SR2 to combine both
; default seed = $00c2 $1137
; routine from https://codebase64.org/doku.php?id=base:x_abc_random_number_generator_8_16_bit
inc x1
clc
x1=*+1
lda #$00 ;x1
c1=*+1
eor #$c2 ;c1
a1=*+1
eor #$11 ;a1
sta a1
b1=*+1
adc #$37 ;b1
sta b1
lsr a
eor a1
adc c1
sta c1
ldy b1
rts
sr1 .word $a55a
sr2 .word $7653
.pend
randbyte = randword ; -- 8 bit pseudo random number generator into A (by just reusing randword)
; ----------- optimized multiplications (stack) : ---------
stack_mul_byte_3 .proc

View File

@ -84,14 +84,14 @@ _sinecosR8 .char trunc(127.0 * sin(range(180+45) * rad(360.0/180.0)))
}
asmsub rndseed(uword seed1 @AY, uword seed2 @R0) clobbers(A,Y) {
; -- reset the pseudo RNG's seed values. Defaults are: $a55a, $7653.
; -- set new pseudo RNG's seed values. Defaults are: $00c2, $1137
%asm {{
sta math.randword.sr1
sty math.randword.sr1+1
sta math.randword.x1
sty math.randword.c1
lda cx16.r0L
ldy cx16.r0H
sta math.randword.sr2
sty math.randword.sr2+1
sta math.randword.a1
lda cx16.r0H
sta math.randword.b1
rts
}}
}