lfsr.asm (1267B)
1 ;;; Copyright 2021 Gerd Beuster (gerd@frombelow.net). This is free 2 ;;; software under the GNU GPL v3 license or any later version. See 3 ;;; COPYING in the root directory for details. 4 5 ;;; 16 Bit Galouis LFSR 6 ;;; If you use the whole state as RNG, the quality of the 7 ;;; random numbers is rather poor. Just extracting one 8 ;;; bit from the state yields random numbers with reasonable 9 ;;; statistical properties. 10 11 ;;; init 12 ;;; Initialize lfsr 13 ;;; Input: 14 ;;; - 15 ;;; Output: 16 ;;; - 17 ;;; Changes: 18 ;;; lfsr 19 lfsr .namespace 20 21 .section zero_page 22 ;; LFSR 23 state: .word ? 24 .send zero_page 25 26 .section rom 27 28 init: 29 .block 30 #mem.STORE_WORD $beef, lfsr.state 31 .bend 32 33 ;;; step 34 ;;; update lfsr 35 ;;; Input: 36 ;;; - 37 ;;; Output: 38 ;;; - 39 ;;; Changes: 40 ;;; lfsr 41 step: 42 .block 43 asl lfsr.state 44 lda lfsr.state+1 45 rol a 46 bcc cont 47 eor #$0b 48 cont: 49 sta lfsr.state+1 50 lda lfsr.state 51 adc #$00 52 sta lfsr.state 53 rts 54 .bend 55 56 .if false 57 ;;; Test function for LFSR 58 test_lfsr: .block 59 loop: 60 jsr lfsr.step 61 lda lfsr.state+1 62 jsr puth 63 lda lfsr.state 64 jsr io.puth 65 PRINTSNL("") 66 jsr io.getc 67 jmp loop 68 .bend 69 .endif ; false 70 71 .send rom 72 .endn