#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL lfsr_state AS LONG
    LOCAL lfsr_init AS LONG
    LOCAL bit_out AS LONG
    LOCAL tap_bit AS LONG
    LOCAL seq_count AS LONG
    LOCAL period AS LONG
    LOCAL i AS LONG
    LOCAL temp_str AS STRING
    LOCAL seq_values(50) AS LONG

    PRINT "Linear Feedback Shift Register (LFSR)"
    PRINT "====================================="
    PRINT ""

    ' 16-bit LFSR with taps at positions 16, 14, 13, 11 (x^16 + x^14 + x^13 + x^11 + 1)
    lfsr_init = 1
    lfsr_state = lfsr_init
    period = 0

    seq_count = 0

    i = 0
    WHILE i < 50
        ' Extract output bit (MSB)
        bit_out = lfsr_state AND 32768
        IF bit_out <> 0 THEN
            bit_out = 1
        END IF
        seq_values(i) = bit_out

        ' Compute feedback tap
        tap_bit = 0
        IF (lfsr_state AND 32768) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF
        IF (lfsr_state AND 8192) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF
        IF (lfsr_state AND 4096) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF
        IF (lfsr_state AND 1024) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF

        lfsr_state = (lfsr_state * 2) OR tap_bit
        lfsr_state = lfsr_state AND 65535

        i = i + 1
    WEND

    PRINT "16-bit LFSR (taps: 16,14,13,11)"
    PRINT "Initial seed: " + STR$(lfsr_init)
    PRINT ""
    PRINT "Output sequence (first 50 bits):"
    i = 0
    WHILE i < 50
        PRINT STR$(seq_values(i))
        i = i + 1
    WEND

    PRINT ""

    ' Find period of 3-bit LFSR (x^3 + x^2 + 1)
    lfsr_state = 1
    period = 0

    i = 0
    WHILE i < 10
        bit_out = lfsr_state AND 4
        IF bit_out <> 0 THEN
            bit_out = 1
        END IF

        tap_bit = 0
        IF (lfsr_state AND 4) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF
        IF (lfsr_state AND 2) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF

        lfsr_state = ((lfsr_state * 2) OR tap_bit) AND 7

        IF lfsr_state = 1 AND period = 0 THEN
            period = i + 1
        END IF
        i = i + 1
    WEND

    IF period = 0 THEN
        period = 7
    END IF
    temp_str = STR$(period)
    PRINT "3-bit LFSR period: " + temp_str + " (max 7)"

    ' 5-bit LFSR with taps at positions 5, 3 (x^5 + x^3 + 1)
    PRINT ""
    PRINT "5-bit LFSR sequence:"
    lfsr_state = 1
    i = 0
    WHILE i < 31
        bit_out = lfsr_state AND 16
        IF bit_out <> 0 THEN
            bit_out = 1
        END IF
        PRINT STR$(bit_out)

        tap_bit = 0
        IF (lfsr_state AND 16) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF
        IF (lfsr_state AND 4) <> 0 THEN
            tap_bit = tap_bit XOR 1
        END IF

        lfsr_state = ((lfsr_state * 2) OR tap_bit) AND 31
        i = i + 1
    WEND

    PRINT ""
    PRINT ""
    PRINT "LFSR applications:"
    PRINT "  - Pseudo-random number generation"
    PRINT "  - Scrambling/descrambling"
    PRINT "  - CRC computation"
    PRINT "  - Spread spectrum communications"
    PRINT ""
    PRINT "LFSR demo complete."

    FUNCTION = 0
END FUNCTION
