' Memory Fill — simulates memset with various fill patterns
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL arr(1000) AS LONG
    LOCAL pattern(100) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL n AS LONG
    LOCAL fillVal AS LONG
    LOCAL fillSize AS LONG
    LOCAL startIdx AS LONG
    LOCAL matchCount AS LONG
    LOCAL mismatchCount AS LONG
    LOCAL lineStr AS STRING
    LOCAL elemStr AS STRING
    PRINT "============================================"
    PRINT "          MEMORY FILL SIMULATION"
    PRINT "============================================"
    PRINT ""
    n = 80
    PRINT "Filling entire array with value 42..."
    fillVal = 42
    FOR i = 0 TO n - 1
        arr(i) = fillVal
    NEXT i
    PRINT "Array after fill:"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    matchCount = 0
    mismatchCount = 0
    FOR i = 0 TO n - 1
        IF arr(i) = fillVal THEN
            matchCount = matchCount + 1
        ELSE
            mismatchCount = mismatchCount + 1
        END IF
    NEXT i
    PRINT "Match: " + STR$(matchCount) + " Mismatch: " + STR$(mismatchCount)
    PRINT ""
    PRINT "============================================"
    PRINT "      PARTIAL FILL (range 20-40)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO n - 1
        arr(i) = 0
    NEXT i
    startIdx = 20
    fillSize = 20
    fillVal = 99
    FOR i = startIdx TO startIdx + fillSize - 1
        arr(i) = fillVal
    NEXT i
    PRINT "Array after partial fill ([" + STR$(startIdx) + "-" + STR$(startIdx + fillSize - 1) + "]):"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      PATTERN FILL (repeating 1-2-3)"
    PRINT "============================================"
    PRINT ""
    pattern(0) = 1
    pattern(1) = 2
    pattern(2) = 3
    LOCAL patternLen AS LONG
    patternLen = 3
    FOR i = 0 TO n - 1
        arr(i) = pattern(i MOD patternLen)
    NEXT i
    PRINT "Array with repeating pattern 1-2-3:"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      STRIDE FILL (every Nth element)"
    PRINT "============================================"
    PRINT ""
    LOCAL stride AS LONG
    FOR i = 0 TO n - 1
        arr(i) = 0
    NEXT i
    stride = 5
    fillVal = 77
    FOR i = 0 TO (n - 1) / stride
        j = i * stride
        IF j < n THEN
            arr(j) = fillVal
        END IF
    NEXT i
    PRINT "Stride fill (every " + STR$(stride) + "th = " + STR$(fillVal) + "):"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      GRADIENT FILL (increasing values)"
    PRINT "============================================"
    PRINT ""
    LOCAL baseVal AS LONG
    baseVal = 10
    FOR i = 0 TO n - 1
        arr(i) = baseVal + i
    NEXT i
    PRINT "Gradient fill (starting at " + STR$(baseVal) + "):"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      ZERO FILL COMPARISON"
    PRINT "============================================"
    PRINT ""
    LOCAL arrA(1000) AS LONG
    LOCAL arrB(1000) AS LONG
    FOR i = 0 TO 255
        arrA(i) = 0
    NEXT i
    FOR i = 0 TO 255
        arrB(i) = 0
    NEXT i
    matchCount = 0
    FOR i = 0 TO 255
        IF arrA(i) = arrB(i) THEN
            matchCount = matchCount + 1
        END IF
    NEXT i
    IF matchCount = 256 THEN
        PRINT "Zero fill: both arrays match (" + STR$(matchCount) + " elements)"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      CHECKERBOARD FILL"
    PRINT "============================================"
    PRINT ""
    LOCAL fillLow AS LONG
    LOCAL fillHigh AS LONG
    fillLow = 10
    fillHigh = 20
    FOR i = 0 TO n - 1
        IF i MOD 2 = 0 THEN
            arr(i) = fillLow
        ELSE
            arr(i) = fillHigh
        END IF
    NEXT i
    PRINT "Checkerboard fill (" + STR$(fillLow) + ", " + STR$(fillHigh) + "):"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "Memory fill operations complete"
    FUNCTION = 0
END FUNCTION
