' debug_breakpoint.pb — Breakpoint and watchpoint simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL breakpoints(50) AS LONG
    LOCAL breakEnabled(50) AS LONG
    LOCAL breakHits(50) AS LONG
    LOCAL watchVars(30) AS LONG
    LOCAL watchValues(30) AS LONG
    LOCAL watchOldValues(30) AS LONG
    LOCAL numBreakpoints AS LONG
    LOCAL numWatchVars AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL addr AS LONG
    LOCAL varVal AS LONG
    LOCAL oldVal AS LONG
    LOCAL hitCount AS LONG
    LOCAL simPC AS LONG
    LOCAL totalHits AS LONG
    LOCAL stepCount AS LONG
    LOCAL iter AS LONG
    LOCAL bpID AS LONG
    LOCAL breakStr AS STRING

    PRINT "=== Debugger: Breakpoints ==="

    numBreakpoints = 5
    breakpoints(0) = 100
    breakEnabled(0) = 1
    breakHits(0) = 0

    breakpoints(1) = 200
    breakEnabled(1) = 1
    breakHits(1) = 0

    breakpoints(2) = 300
    breakEnabled(2) = 1
    breakHits(2) = 0

    breakpoints(3) = 400
    breakEnabled(3) = 0
    breakHits(3) = 0

    breakpoints(4) = 150
    breakEnabled(4) = 1
    breakHits(4) = 0

    numWatchVars = 3
    watchVars(0) = 1
    watchValues(0) = 0
    watchOldValues(0) = 0

    watchVars(1) = 2
    watchValues(1) = 0
    watchOldValues(1) = 0

    watchVars(2) = 3
    watchValues(2) = 0
    watchOldValues(2) = 0

    PRINT "Breakpoints set: " + STR$(numBreakpoints)
    FOR i = 0 TO numBreakpoints - 1
        IF breakEnabled(i) = 1 THEN
            PRINT "  BP " + STR$(i + 1) + " at addr 0x" + STR$(breakpoints(i)) + " [enabled]"
        ELSE
            PRINT "  BP " + STR$(i + 1) + " at addr 0x" + STR$(breakpoints(i)) + " [disabled]"
        END IF
    NEXT i

    PRINT "Watch variables: " + STR$(numWatchVars)

    PRINT "Simulating execution..."
    stepCount = 0
    FOR simPC = 1 TO 50
        stepCount = stepCount + 1

        varVal = simPC * 2
        watchValues(0) = varVal
        IF stepCount / 3 * 3 = stepCount THEN
            varVal = simPC * 5
            watchValues(1) = varVal
        END IF
        IF stepCount / 5 * 5 = stepCount THEN
            varVal = simPC * 7
            watchValues(2) = varVal
        END IF

        FOR i = 0 TO numWatchVars - 1
            oldVal = watchOldValues(i)
            varVal = watchValues(i)
            IF oldVal <> varVal THEN
                PRINT "  Watch var" + STR$(watchVars(i)) + " changed: " + STR$(oldVal) + " -> " + STR$(varVal)
                watchOldValues(i) = varVal
            END IF
        NEXT i

        FOR i = 0 TO numBreakpoints - 1
            IF breakEnabled(i) = 1 AND breakpoints(i) = simPC THEN
                breakHits(i) = breakHits(i) + 1
                totalHits = totalHits + 1
                PRINT "  >> BP hit at 0x" + STR$(simPC) + " (hit #" + STR$(breakHits(i)) + ")"
            END IF
        NEXT i
    NEXT simPC

    PRINT "--- Breakpoint Summary ---"
    FOR i = 0 TO numBreakpoints - 1
        IF breakEnabled(i) = 1 THEN
            PRINT "  BP at 0x" + STR$(breakpoints(i)) + ": " + STR$(breakHits(i)) + " hits"
        END IF
    NEXT i
    PRINT "Steps: " + STR$(stepCount) + " Total hits: " + STR$(totalHits)
    PRINT "=== End Breakpoint ==="
    FUNCTION = 0
END FUNCTION
