' Hex editor display: offset, hex bytes, ASCII representation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL dataLen  AS LONG
    LOCAL offset  AS LONG
    LOCAL cursorPos  AS LONG
    LOCAL fileData(256) AS LONG
    LOCAL hexStr  AS STRING
    LOCAL asciiStr  AS STRING
    LOCAL offsetStr  AS STRING
    LOCAL byteVal  AS LONG
    LOCAL rowStart  AS LONG
    LOCAL rowEnd  AS LONG
    LOCAL numRows  AS LONG
    LOCAL bytesPerRow  AS LONG
    LOCAL selectedByte  AS LONG
    LOCAL editMode  AS LONG
    LOCAL ch AS STRING

    PRINT "=== Hex Editor ==="
    PRINT

    bytesPerRow = 16
    dataLen = 64

    FOR i = 0 TO dataLen - 1
        fileData(i) = (i * 37 + 13) AND 255
    NEXT i

    fileData(0) = 77
    fileData(1) = 90
    fileData(2) = 144
    fileData(3) = 0
    fileData(4) = 3
    fileData(5) = 0
    fileData(6) = 0
    fileData(7) = 0
    fileData(8) = 4
    fileData(9) = 0
    fileData(10) = 0
    fileData(11) = 0
    fileData(12) = 255
    fileData(13) = 255
    fileData(14) = 0
    fileData(15) = 0

    cursorPos = 0
    selectedByte = -1
    editMode = 0

    PRINT "File size: " & FORMAT$(dataLen) & " bytes"
    PRINT "Bytes per row: " & FORMAT$(bytesPerRow)
    PRINT

    numRows = dataLen / bytesPerRow
    IF dataLen MOD bytesPerRow > 0 THEN numRows = numRows + 1

    PRINT "--- Hex Dump ---"
    PRINT "  Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F  ASCII"
    PRINT "  ------  -------------------------------------------------  ----------------"

    FOR i = 0 TO numRows - 1
        rowStart = i * bytesPerRow
        rowEnd = rowStart + bytesPerRow - 1
        IF rowEnd >= dataLen THEN rowEnd = dataLen - 1

        offsetStr = RIGHT$("0000000" & HEX$(rowStart), 8)
        hexStr = " "
        asciiStr = " "

        FOR j = 0 TO bytesPerRow - 1
            IF rowStart + j <= rowEnd THEN
                byteVal = fileData(rowStart + j)
                hexStr = hexStr + RIGHT$("0" & HEX$(byteVal), 2) + " "
                IF byteVal >= 32 AND byteVal <= 126 THEN
                    asciiStr = asciiStr + CHR$(byteVal)
                ELSE
                    asciiStr = asciiStr + "."
                END IF
            ELSE
                hexStr = hexStr + "   "
                asciiStr = asciiStr + " "
            END IF
        NEXT j

        PRINT "  " & offsetStr & " " & hexStr & " " & asciiStr
    NEXT i
    PRINT

    PRINT "--- Byte at cursor ---"
    byteVal = fileData(cursorPos)
    PRINT "  Offset " & FORMAT$(cursorPos) & " (0x" & RIGHT$("0000" & HEX$(cursorPos), 4) & "): " & FORMAT$(byteVal) & " (0x" & RIGHT$("0" & HEX$(byteVal), 2) & ")"
    IF byteVal >= 32 AND byteVal <= 126 THEN
        PRINT "  ASCII: '" & CHR$(byteVal) & "'"
    ELSE
        PRINT "  ASCII: (non-printable)"
    END IF
    PRINT

    PRINT "--- Edit Mode ---"
    PRINT "  Current: " & STR$(editMode)
    PRINT "  0 = Navigation, 1 = Hex edit, 2 = ASCII edit"
    PRINT "  Press Tab to switch mode"
    PRINT

    PRINT "--- Navigation ---"
    PRINT "  Arrow keys: move cursor"
    PRINT "  Page Up/Down: scroll by page"
    PRINT "  Home: start of file"
    PRINT "  End: end of file"
    PRINT "  F3: search for hex pattern"
    PRINT "  F5: go to offset"
    PRINT "  Ctrl+S: save changes"

    PRINT
    PRINT "Hex editor simulation complete."

    FUNCTION = 0
END FUNCTION
