base:two_very_fast_16bit_pseudo_random_generators_as_lfsr
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:two_very_fast_16bit_pseudo_random_generators_as_lfsr [2015-04-17 04:34] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | < | ||
+ | ;LFSR random generators | ||
+ | ; | ||
+ | ;with cc65 compiler/ | ||
+ | ;compile: ca65 -t c64 random.s && ld65 -t c64 -o random random.o c64.lib | ||
+ | ; | ||
+ | ;© 2007 Hanno Behrens (pebbles@schattenlauf.de) | ||
+ | ;LGPL Licence | ||
+ | ; | ||
+ | ;Call random and you get a lowbyte in A and an highbyte in Y | ||
+ | ;you should only use the 8 bit lowbyte cause thats best random. | ||
+ | ;The full 16 A/Y may be used but suffer of some bad disadvantages | ||
+ | ;that should be best avoided by not using Y. | ||
+ | ;The lowbyte of the one algo shifts right, the other left, | ||
+ | ;so the result is mixed up a bit better than just with plain shifts | ||
+ | ;The periode of this two combined generators is nearly 2^31 (exact 2^16-1*2^15-1) | ||
+ | ;Make shure you don't feed either seeds just with plain zero cause | ||
+ | ;the zero remains zero. All other seeds will do. | ||
+ | ; | ||
+ | ;DO NOT USE THIS ALGORITHMS FOR CRYPTOGRAPHY, | ||
+ | ;USE FORTUNA, YARROW OR OTHER STRONG (AND MUCH SLOWER THAN THIS) INSTEAD | ||
+ | ; | ||
+ | ;LFSR do produce on bit feedback periods as followed: | ||
+ | ;0+1 = 3 | ||
+ | ;1+2 = 7 | ||
+ | ;2+3 = 15 | ||
+ | ;2+4 = 31 | ||
+ | ;4+5 = 63 | ||
+ | ;5+6 = 127 | ||
+ | ;1+2+3+7 = 255 | ||
+ | ;4+8 = 511 | ||
+ | ;6+9 = 1023 | ||
+ | ;8+10 = 2047 | ||
+ | ;1+9+10+11 = 4095 | ||
+ | ;0+10+11+12 = 8191 | ||
+ | ;1+11+12+13 = 16383 | ||
+ | ;13+14 = 32767 | ||
+ | ; | ||
+ | .setcpu " | ||
+ | .macpack generic | ||
+ | .macpack cbm | ||
+ | |||
+ | .segment " | ||
+ | |||
+ | .org $9000 | ||
+ | .word * | ||
+ | .org *-2 | ||
+ | |||
+ | ; | ||
+ | ;result in a (lo) and y (hi) | ||
+ | |||
+ | .proc rand | ||
+ | jsr rand64k | ||
+ | jsr rand32k | ||
+ | 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 | ||
+ | .endproc | ||
+ | |||
+ | ;periode with 65535 | ||
+ | ; | ||
+ | .proc rand64k | ||
+ | lda sr1+1 | ||
+ | asl | ||
+ | asl | ||
+ | eor sr1+1 | ||
+ | asl | ||
+ | eor sr1+1 | ||
+ | asl | ||
+ | asl | ||
+ | eor sr1+1 | ||
+ | asl | ||
+ | rol sr1 ; | ||
+ | rol sr1+1 | ||
+ | rts | ||
+ | .endproc | ||
+ | |||
+ | ;periode with 32767 | ||
+ | ;13+14 | ||
+ | .proc rand32k | ||
+ | lda sr2+1 | ||
+ | asl | ||
+ | eor sr2+1 | ||
+ | asl | ||
+ | asl | ||
+ | ror sr2 ; | ||
+ | rol sr2+1 | ||
+ | rts | ||
+ | .endproc | ||
+ | |||
+ | ;feel free to set seeds as wished, if put in zeropage some speed-boost is | ||
+ | ;the result. For example sr1=$5c sr2=5e would fit | ||
+ | sr1: .word $a55a | ||
+ | sr2: .word $7653 | ||
+ | </ |
base/two_very_fast_16bit_pseudo_random_generators_as_lfsr.txt · Last modified: 2015-04-17 04:34 by 127.0.0.1