Posted: Wed Apr 20, 2011 11:00 am
Yup, slacker did what I was meaning. Here is a 48-bit LFSR based one, uses more instructions but has a very long repeat cycle.
I think it works properly, only just wrote it so hope it is correct. Sounds like noise, don't hear any tones.
Code: Select all
; LFSR for random numbers
; Frank Thomson - OCT Distribution
; 2011
;
; reg 0 : low lfsr register
; reg 1 : high lfsr register
; reg 2 : temp register
;
equ lfsrl reg0
equ lfsrh reg1
equ temp reg2
;
;
;
;
; seed the lfsr with a non-zero value
skp run,start
ldax adcl
or 0x001000 ; just to ensure it is really non-zero
wrax lfsrl,1.0
wrax lfsrh,0
;
;
start:
ldax lfsrh ; get the "high" 24-bits
and 0x000001 ; grab bit 48
wrax temp,0 ; save it and clear acc
rdax lfsrh,0.5 ; read the high again and shift 1 bit
and 0x7fffff ; clear the msb
wrax lfsrh,1.0 ; save the shifted result
and 0x000001 ; grab the bit 47
rdax temp,1.0 ; add it to the previous bit (doing an xor by adding, the lsb is the result)
wrax temp,0 ; save it
rdax lfsrl, 1.0 ; get the "low" 24-bits
and 0x000001 ; get the lsb
skp zro,nada ; if 0 do nothing
ldax lfsrh ; get the upper bits
or 0x800000 ; set the msb
wrax lfsrh,0
nada:
;
;at this point the upper 24 bits are done
;
rdax lfsrl, 0.5 ; get the "low" 24-bits and shift 1 bit
and 0x7fffff ; clear msb
wrax lfsrl,0.25 ; save shifted value and shift 2 more bits
and 0x000001 ; grab bit 21
rdax temp,1.0 ; add it to the previous result
wrax temp,0 ; save it
rdax lfsrl,0.125 ; read the low again and shift (already did 1 bit shift above)
and 0x000001 ; grab the bit 20
rdax temp,1.0 ; add it to the previous bit (doing an xor by adding, the lsb is the result)
and 0x000001 ; only care about lsb
skp zro,shftzro
; if here the bit was a 1
sof 0,0 ; clear acc
rdax lfsrl,1.0 ; get the low
or 0x800000 ; set the bit
wrax lfsrl,0 ; save it
;
shftzro:
; low 24-bits done
; use low 24 for output, could use upper or even middle points
ldax lfsrl
wrax dacl,0