//-----------------------------------------------------------
// FOR Loop BYTE Max Value Test
// Verifies that FOR b = 0 TO 255 correctly iterates 256 times
// without causing an infinite loop.
//-----------------------------------------------------------

#INCLUDE <c64start.c65>
#INCLUDE <c64defs.c65>
#INCLUDE <cbmiolib.c65>

#PRAGMA _P_USE_CBM_STRINGS 1

GOTO start

WORD count
BYTE iter

//-----------------------------------------------------------
// Test 1: FOR BYTE = 0 TO 255
// Should iterate exactly 256 times (0, 1, 2, ..., 255)
//-----------------------------------------------------------
FUNC test_byte_to_255
    LET count = 0

    FOR iter = 0 TO 255
        count++
    NEXT

    lib_cbmio_print("byte 0-255: ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0100)")
FEND

//-----------------------------------------------------------
// Test 2: FOR BYTE = 250 TO 255
// Should iterate exactly 6 times (250, 251, 252, 253, 254, 255)
//-----------------------------------------------------------
FUNC test_byte_250_to_255
    LET count = 0

    FOR iter = 250 TO 255
        count++
    NEXT

    lib_cbmio_print("byte 250-255: ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0006)")
FEND

//-----------------------------------------------------------
// Test 3: Verify last iteration value
// iter should be 255 after the loop completes
//-----------------------------------------------------------
FUNC test_final_value
    LET iter = 0

    FOR iter = 0 TO 255
        // just iterate
    NEXT

    lib_cbmio_print("final iter: ")
    lib_cbmio_hexoutb(iter)
    lib_cbmio_printlf(" (exp ff)")
FEND

//-----------------------------------------------------------
// Test 4: FOR WORD = 65530 TO 65535
// Should iterate exactly 6 times (65530, 65531, ..., 65535)
// Tests WORD overflow protection without taking forever
//-----------------------------------------------------------
WORD witer

FUNC test_word_to_65535
    LET count = 0

    FOR witer = 65530 TO 65535
        count++
    NEXT

    lib_cbmio_print("word 65530-65535: ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0006)")
FEND

//-----------------------------------------------------------
// Test 5: Verify WORD final value
// witer should be 65535 ($FFFF) after the loop completes
//-----------------------------------------------------------
FUNC test_word_final_value
    LET witer = 0

    FOR witer = 65530 TO 65535
        // just iterate
    NEXT

    lib_cbmio_print("final witer: ")
    lib_cbmio_hexoutw(witer)
    lib_cbmio_printlf(" (exp ffff)")
FEND

//-----------------------------------------------------------
// Test 6: FOR BYTE with variable end value
// Tests that variable end values work correctly
//-----------------------------------------------------------
BYTE endval

FUNC test_byte_var_end
    LET count = 0
    LET endval = 10

    FOR iter = 0 TO endval
        count++
    NEXT

    lib_cbmio_print("byte 0-var(10): ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 000b)")
FEND

//-----------------------------------------------------------
// Test 7: FOR BYTE with variable end at max (255)
//-----------------------------------------------------------
FUNC test_byte_var_max
    LET count = 0
    LET endval = 255

    FOR iter = 250 TO endval
        count++
    NEXT

    lib_cbmio_print("byte 250-var(255): ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0006)")
FEND

//-----------------------------------------------------------
// Test 8: FOR WORD with variable end value
//-----------------------------------------------------------
WORD wendval

FUNC test_word_var_end
    LET count = 0
    LET wendval = 100

    FOR witer = 0 TO wendval
        count++
    NEXT

    lib_cbmio_print("word 0-var(100): ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0065)")
FEND

//-----------------------------------------------------------
// Test 9: FOR WORD with variable end at max (65535)
//-----------------------------------------------------------
FUNC test_word_var_max
    LET count = 0
    LET wendval = 65535

    FOR witer = 65530 TO wendval
        count++
    NEXT

    lib_cbmio_print("word 65530-var(max): ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0006)")
FEND

//-----------------------------------------------------------
// Test 10: FOR with start == end (single iteration)
//-----------------------------------------------------------
FUNC test_same_start_end
    LET count = 0

    FOR iter = 5 TO 5
        count++
    NEXT

    lib_cbmio_print("byte 5-5: ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0001)")
FEND

//-----------------------------------------------------------
// Test 11: FOR with start var == loop var (skip assignment)
// When doing FOR i = i TO 10, assignment should be skipped
//-----------------------------------------------------------
FUNC test_self_start
    LET count = 0
    LET iter = 3

    FOR iter = iter TO 5
        count++
    NEXT

    lib_cbmio_print("byte i=i to 5: ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0003)")
FEND

//-----------------------------------------------------------
// Test 12: Long jump FOR loop
// Uses _P_USE_LONG_JUMP pragma with a 200-byte buffer in
// the loop body. Without long jump, branches would fail
// as they're limited to ±127 bytes.
//-----------------------------------------------------------

FUNC test_long_jump
    LET count = 0

#PRAGMA _P_USE_LONG_JUMP 1
    FOR iter = 0 TO 5
#PRAGMA _P_USE_LONG_JUMP 0
        count++
        GOTO _test_lj_after_buffer
ASM
        !fill 200, 0
ENDASM
        LABEL _test_lj_after_buffer
    NEXT

    lib_cbmio_print("long jump 0-5: ")
    lib_cbmio_hexoutw(count)
    lib_cbmio_printlf(" (exp 0006)")
FEND


//-----------------------------------------------------------
// Main program
//-----------------------------------------------------------
FUNC main
    lib_cbmio_cls()

    lib_cbmio_printlf("for max value test")
    lib_cbmio_lf()

    test_byte_to_255()
    test_byte_250_to_255()
    test_final_value()
    test_word_to_65535()
    test_word_final_value()

    lib_cbmio_lf()
    lib_cbmio_printlf("variable end tests:")
    test_byte_var_end()
    test_byte_var_max()
    test_word_var_end()
    test_word_var_max()

    lib_cbmio_lf()
    lib_cbmio_printlf("edge case tests:")
    test_same_start_end()
    test_self_start()

    lib_cbmio_lf()
    lib_cbmio_printlf("long jump test:")
    test_long_jump()

    lib_cbmio_lf()
    lib_cbmio_printlf("tests complete!")
FEND

LABEL start
    main()
    SUBEND
