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 .pend
randbyte = randword ; -- 8 bit pseudo random number generator into A (by just reusing randword)
randword .proc randword .proc
; -- 16 bit pseudo random number generator into AY ; -- 16 bit pseudo random number generator into AY
; default seed = $00c2 $1137
; rand64k ;Factors of 65535: 3 5 17 257 ; routine from https://codebase64.org/doku.php?id=base:x_abc_random_number_generator_8_16_bit
lda sr1+1 inc x1
asl a clc
asl a x1=*+1
eor sr1+1 lda #$00 ;x1
asl a c1=*+1
eor sr1+1 eor #$c2 ;c1
asl a a1=*+1
asl a eor #$11 ;a1
eor sr1+1 sta a1
asl a b1=*+1
rol sr1 ;shift this left, "random" bit comes from low adc #$37 ;b1
rol sr1+1 sta b1
; rand32k ;Factors of 32767: 7 31 151 are independent and can be combined lsr a
lda sr2+1 eor a1
asl a adc c1
eor sr2+1 sta c1
asl a ldy b1
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
rts rts
sr1 .word $a55a
sr2 .word $7653
.pend .pend
randbyte = randword ; -- 8 bit pseudo random number generator into A (by just reusing randword)
; ----------- optimized multiplications (stack) : --------- ; ----------- optimized multiplications (stack) : ---------
stack_mul_byte_3 .proc 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) { 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 {{ %asm {{
sta math.randword.sr1 sta math.randword.x1
sty math.randword.sr1+1 sty math.randword.c1
lda cx16.r0L lda cx16.r0L
ldy cx16.r0H sta math.randword.a1
sta math.randword.sr2 lda cx16.r0H
sty math.randword.sr2+1 sta math.randword.b1
rts rts
}} }}
} }