#!/usr/bin/perl # see # http://www.newwaveinstruments.com/resources/articles/m_sequence_linear_feedback_shift_register_lfsr.htm # Google search terms LSFR (linear feedback shift register) # # in all cases, missing seed is $FF if $seed=0, and $newbit = 1. #@plist = (1,2,3,4); #@plist = (1,3,4,7); ### 8 bit values #@plist = (1,2,3,7); # works with newbit=1, seed =0, also with seed=1, newbit =0 #@plist = (3,4,5,7); # with seed=1, newbit =0, or with seed=0, newbit=1 #@plist = (2,4,6,7); # with seed=1, newbit =0, or with seed=0, newbit=1 #@plist = (1,2,5,7); # with seed=1, newbit =0, or with seed=0, newbit=1 # other maximum length sequences, subtract one from these bit numbers # [8, 7, 6, 1] # [8, 7, 5, 3] # [8, 7, 3, 2] # [8, 6, 5, 4] # [8, 6, 5, 3] # [8, 6, 5, 2] @plist = (1,4,5,7); $num_bits = 8; # 16 bit values #@plist = (1,2,4,15); # works with newbit=1, seed =0, also with seed=1, newbit =0 #@plist = (5,8,9,15); # works with newbit=1, seed =0, also with seed=1, newbit =0 # other maxium length sequences, subtract one from these bit numbers # [16, 15, 13, 4] # [16, 15, 12, 10] # [16, 15, 12, 1] # [16, 15, 10, 4] # [16, 15, 9, 6] # [16, 15, 9, 4] # [16, 15, 7, 2] # [16, 15, 4, 2] # [16, 14, 13, 11] # [16, 14, 13, 5] # [16, 14, 12, 7] # [16, 14, 11, 7] # [16, 14, 9, 7] # [16, 14, 9, 4] # [16, 14, 8, 3] # [16, 13, 12, 11] # [16, 13, 12, 7] # [16, 13, 11, 6] # [16, 13, 9, 6] # [16, 13, 6, 4] # [16, 12, 9, 7] # [16, 12, 9, 6] # [16, 11, 10, 5] # [16, 11, 9, 8] # [16, 11, 9, 7] # [16, 10, 9, 6] #$num_bits = 16; $max_value = 1 << $num_bits; $seed = 0; for ($i=0;$i<$max_value;$i++){ $bitarray[$i] = 0; } for ($i=0;$i<$max_value;$i++) { $newbit = 1; # $newbit = 0; foreach $bit (@plist) { $newbit = $newbit ^ &get_bit($seed,$bit) } $newseed = ($seed << 1) | $newbit; $newseed = $newseed & ($max_value-1); if ($bitarray[$newseed] == 1) { printf("Duplicate seed: %x\n",$newseed); } $bitarray[$newseed] = 1; if ($i > ($max_value-5)) { printf ("$i: %x\n",$newseed); } $seed = $newseed ; } for ($i=0;$i<$max_value;$i++){ if ($bitarray[$i] == 0) { printf("Missing seed: %x\n",$i); } } print "Finished\n"; exit; sub get_bit { my($val,$bitnum) = @_; my($mask) = 1 << $bitnum; # printf ("val: $val, maske:$mask\n"); $val = $val & $mask; if ($val > 0) { return(1); } else { return(0); } }