' Memory Copy — simulates memcpy with array-based byte operations
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL src(1000) AS LONG
    LOCAL dst(1000) AS LONG
    LOCAL aligned_src(1000) AS LONG
    LOCAL aligned_dst(1000) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL n AS LONG
    LOCAL copySize AS LONG
    LOCAL offsetVal AS LONG
    LOCAL matchCount AS LONG
    LOCAL mismatchCount AS LONG
    LOCAL lineStr AS STRING
    LOCAL elemStr AS STRING
    PRINT "============================================"
    PRINT "          MEMORY COPY SIMULATION"
    PRINT "============================================"
    PRINT ""
    n = 100
    PRINT "Filling source array with pattern..."
    FOR i = 0 TO n - 1
        src(i) = (i * 7 + 13) MOD 256
    NEXT i
    PRINT "Source data:"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(src(i))
        DO WHILE LEN(elemStr) < 4
            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 "Copying " + STR$(n) + " elements..."
    copySize = n
    FOR i = 0 TO copySize - 1
        dst(i) = src(i)
    NEXT i
    PRINT ""
    PRINT "Verifying copy..."
    matchCount = 0
    mismatchCount = 0
    FOR i = 0 TO copySize - 1
        IF src(i) = dst(i) THEN
            matchCount = matchCount + 1
        ELSE
            mismatchCount = mismatchCount + 1
        END IF
    NEXT i
    PRINT "Match: " + STR$(matchCount) + " Mismatch: " + STR$(mismatchCount)
    PRINT ""
    PRINT "============================================"
    PRINT "      PARTIAL COPY (first 50 elements)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO n - 1
        dst(i) = 0
    NEXT i
    copySize = 50
    FOR i = 0 TO copySize - 1
        dst(i) = src(i)
    NEXT i
    PRINT "Destination after partial copy (" + STR$(copySize) + " elements):"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(dst(i))
        DO WHILE LEN(elemStr) < 4
            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 "      OVERLAPPING COPY (shift by 10)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO n - 1
        aligned_src(i) = src(i)
    NEXT i
    offsetVal = 10
    copySize = 80
    IF offsetVal > 0 THEN
        FOR i = copySize - 1 TO 0 STEP -1
            aligned_dst(i + offsetVal) = aligned_src(i)
        NEXT i
    END IF
    PRINT "Shifted by " + STR$(offsetVal) + ":"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(aligned_dst(i))
        DO WHILE LEN(elemStr) < 4
            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 "      FILL AND COPY PERFORMANCE TEST"
    PRINT "============================================"
    PRINT ""
    LOCAL iterations AS LONG
    LOCAL tStart AS DOUBLE
    LOCAL tEnd AS DOUBLE
    iterations = 1000
    n = 100
    FOR i = 0 TO n - 1
        dst(i) = i
    NEXT i
    iterations = 100
    FOR j = 0 TO iterations - 1
        FOR i = 0 TO n - 1
            aligned_dst(i) = dst(i)
        NEXT i
        FOR i = 0 TO n - 1
            dst(i) = aligned_dst(i)
        NEXT i
    NEXT j
    PRINT "Completed " + STR$(iterations) + " iterations of copy"
    PRINT ""
    PRINT "============================================"
    PRINT "      STRUCTURED COPY (interleaved data)"
    PRINT "============================================"
    PRINT ""
    LOCAL dataA(100) AS LONG
    LOCAL dataB(100) AS LONG
    LOCAL combined(200) AS LONG
    LOCAL structSize AS LONG
    structSize = 50
    FOR i = 0 TO structSize - 1
        dataA(i) = i * 2
        dataB(i) = i * 2 + 1
    NEXT i
    FOR i = 0 TO structSize - 1
        combined(i * 2) = dataA(i)
        combined(i * 2 + 1) = dataB(i)
    NEXT i
    PRINT "Interleaved copy (A B A B pattern):"
    lineStr = ""
    FOR i = 0 TO 19
        elemStr = STR$(combined(i))
        DO WHILE LEN(elemStr) < 4
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
    NEXT i
    PRINT lineStr
    PRINT ""
    PRINT "============================================"
    PRINT "      REVERSE COPY"
    PRINT "============================================"
    PRINT ""
    copySize = 30
    FOR i = 0 TO copySize - 1
        dst(i) = src(copySize - 1 - i)
    NEXT i
    PRINT "Reverse copy of " + STR$(copySize) + " elements:"
    lineStr = ""
    FOR i = 0 TO copySize - 1
        elemStr = STR$(dst(i))
        DO WHILE LEN(elemStr) < 4
            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 copy operations complete"
    FUNCTION = 0
END FUNCTION
