' asm_interrupt.pb — Interrupt handler simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL interruptVector(100) AS LONG
    LOCAL interruptHandlers(100) AS LONG
    LOCAL interruptCount AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL irqNumber AS LONG
    LOCAL handlerAddr AS LONG
    LOCAL isrCount AS LONG
    LOCAL totalHandled AS LONG
    LOCAL iter AS LONG
    LOCAL numIRQs AS LONG
    LOCAL vectorNum AS LONG
    LOCAL foundHandler AS LONG
    LOCAL simulatedIRQ AS LONG
    LOCAL handlerResult AS LONG

    numIRQs = 10

    PRINT "=== Interrupt Vector Table Simulation ==="

    FOR i = 0 TO numIRQs - 1
        interruptVector(i) = i
        interruptHandlers(i) = 1000 + i * 100
    NEXT i
    interruptCount = numIRQs

    PRINT "IVT entries:"
    FOR i = 0 TO interruptCount - 1
        PRINT "  INT " + STR$(interruptVector(i)) + " -> handler at 0x" + STR$(interruptHandlers(i))
    NEXT i

    PRINT "Simulating interrupts..."
    FOR simulatedIRQ = 1 TO 15
        irqNumber = simulatedIRQ MOD (numIRQs + 2)

        foundHandler = -1
        FOR i = 0 TO interruptCount - 1
            IF interruptVector(i) = irqNumber THEN
                foundHandler = i
            END IF
        NEXT i

        IF foundHandler >= 0 THEN
            handlerAddr = interruptHandlers(foundHandler)
            isrCount = isrCount + 1
            totalHandled = totalHandled + handlerAddr
            PRINT "  IRQ " + STR$(irqNumber) + ": handled at 0x" + STR$(handlerAddr)
        ELSE
            PRINT "  IRQ " + STR$(irqNumber) + ": unhandled (crash)"
        END IF
    NEXT simulatedIRQ

    PRINT "ISR calls: " + STR$(isrCount)
    PRINT "Total handled: " + STR$(totalHandled)
    PRINT "=== End Interrupt ==="
    FUNCTION = 0
END FUNCTION
