' Memory Compare — compares two arrays element by element
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL arrA(1000) AS LONG
    LOCAL arrB(1000) AS LONG
    LOCAL n AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL compareResult AS LONG
    LOCAL firstDiffIdx AS LONG
    LOCAL diffCount AS LONG
    LOCAL matchCount AS LONG
    LOCAL lineStr AS STRING
    LOCAL elemStr AS STRING
    n = 80
    PRINT "============================================"
    PRINT "       MEMORY COMPARE SIMULATION"
    PRINT "============================================"
    PRINT ""
    PRINT "Filling arrays with identical data..."
    FOR i = 0 TO n - 1
        arrA(i) = (i * 7 + 13) MOD 256
        arrB(i) = (i * 7 + 13) MOD 256
    NEXT i
    compareResult = 0
    firstDiffIdx = -1
    FOR i = 0 TO n - 1
        IF arrA(i) <> arrB(i) THEN
            compareResult = -1
            firstDiffIdx = i
            EXIT FOR
        END IF
    NEXT i
    IF compareResult = 0 THEN
        PRINT "PASS: Arrays are identical (" + STR$(n) + " elements)"
    ELSE
        PRINT "FAIL: Arrays differ at index " + STR$(firstDiffIdx)
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      INTRODUCING DIFFERENCES"
    PRINT "============================================"
    PRINT ""
    arrB(15) = 999
    arrB(37) = 888
    arrB(62) = 777
    PRINT "Modified elements at indices 15, 37, 62"
    PRINT ""
    PRINT "Comparing arrays..."
    compareResult = 0
    firstDiffIdx = -1
    diffCount = 0
    FOR i = 0 TO n - 1
        IF arrA(i) <> arrB(i) THEN
            IF firstDiffIdx = -1 THEN
                firstDiffIdx = i
            END IF
            diffCount = diffCount + 1
            compareResult = -1
        END IF
    NEXT i
    PRINT "First difference at index: " + STR$(firstDiffIdx)
    PRINT "Total differences: " + STR$(diffCount)
    PRINT "Matching elements: " + STR$(n - diffCount)
    PRINT ""
    PRINT "============================================"
    PRINT "      BYTE-LEVEL COMPARE (LONG array)"
    PRINT "============================================"
    PRINT ""
    LOCAL byteA AS LONG
    LOCAL byteB AS LONG
    LOCAL byteIdx AS LONG
    LOCAL byteDiff AS LONG
    byteDiff = 0
    FOR i = 0 TO n - 1
        arrA(i) = i * 257
        arrB(i) = i * 257
    NEXT i
    arrB(5) = arrB(5) XOR 255
    PRINT "Array B element 5 has bit-flipped LSB"
    PRINT ""
    FOR i = 0 TO n - 1
        IF arrA(i) <> arrB(i) THEN
            byteA = arrA(i) AND 255
            byteB = arrB(i) AND 255
            IF byteA <> byteB THEN
                byteIdx = 0
                PRINT "  Byte 0 diff at index " + STR$(i) + ": " + STR$(byteA) + " vs " + STR$(byteB)
                byteDiff = byteDiff + 1
            END IF
            byteA = (arrA(i) / 256) AND 255
            byteB = (arrB(i) / 256) AND 255
            IF byteA <> byteB THEN
                byteIdx = 1
                PRINT "  Byte 1 diff at index " + STR$(i) + ": " + STR$(byteA) + " vs " + STR$(byteB)
                byteDiff = byteDiff + 1
            END IF
        END IF
    NEXT i
    IF byteDiff = 0 THEN
        PRINT "No byte-level differences found"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      COMPARE WITH OFFSET (stride)"
    PRINT "============================================"
    PRINT ""
    LOCAL offsetA AS LONG
    LOCAL offsetB AS LONG
    LOCAL compareLen AS LONG
    offsetA = 10
    offsetB = 20
    compareLen = 30
    PRINT "Comparing arrA[" + STR$(offsetA) + "...] vs arrB[" + STR$(offsetB) + "...]"
    PRINT "Length: " + STR$(compareLen)
    PRINT ""
    diffCount = 0
    FOR i = 0 TO compareLen - 1
        IF arrA(offsetA + i) = arrB(offsetB + i) THEN
            diffCount = diffCount + 0
        ELSE
            diffCount = diffCount + 1
        END IF
    NEXT i
    PRINT "Differences: " + STR$(diffCount)
    PRINT ""
    PRINT "============================================"
    PRINT "      CASE-INSENSITIVE STRING COMPARE"
    PRINT "============================================"
    PRINT ""
    LOCAL strA AS STRING
    LOCAL strB AS STRING
    LOCAL chA AS LONG
    LOCAL chB AS LONG
    strA = "The Quick Brown Fox"
    strB = "the quick brown fox"
    PRINT "String A: " + strA
    PRINT "String B: " + strB
    PRINT ""
    compareResult = 0
    IF LEN(strA) = LEN(strB) THEN
        FOR i = 1 TO LEN(strA)
            chA = ASC(MID$(strA, i, 1))
            chB = ASC(MID$(strB, i, 1))
            IF chA >= 65 AND chA <= 90 THEN
                chA = chA + 32
            END IF
            IF chB >= 65 AND chB <= 90 THEN
                chB = chB + 32
            END IF
            IF chA <> chB THEN
                compareResult = -1
                EXIT FOR
            END IF
        NEXT i
    ELSE
        compareResult = -1
    END IF
    IF compareResult = 0 THEN
        PRINT "Case-insensitive: MATCH"
    ELSE
        PRINT "Case-insensitive: NO MATCH"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "      BULK COMPARE (all elements)"
    PRINT "============================================"
    PRINT ""
    LOCAL totalA AS LONG
    LOCAL totalB AS LONG
    FOR i = 0 TO n - 1
        totalA = totalA + arrA(i)
        totalB = totalB + arrB(i)
    NEXT i
    IF totalA = totalB THEN
        PRINT "Checksum match: " + STR$(totalA)
    ELSE
        PRINT "Checksum mismatch: " + STR$(totalA) + " vs " + STR$(totalB)
    END IF
    PRINT ""
    matchCount = 0
    FOR i = 0 TO n - 1
        IF arrA(i) = arrB(i) THEN
            matchCount = matchCount + 1
        END IF
    NEXT i
    PRINT "Summary: " + STR$(matchCount) + " of " + STR$(n) + " elements match"
    PRINT ""
    PRINT "Memory compare operations complete"
    FUNCTION = 0
END FUNCTION
