256 lines
5.3 KiB
Text
256 lines
5.3 KiB
Text
//-----------------------------------------------------------
|
|
// SWITCH/CASE Statement Demo
|
|
// Demonstrates the SWITCH/CASE control flow statement
|
|
// with implicit breaks and long jump pragma support
|
|
//-----------------------------------------------------------
|
|
|
|
#INCLUDE <c64start.c65>
|
|
#INCLUDE <c64defs.c65>
|
|
#INCLUDE <cbmiolib.c65>
|
|
|
|
#PRAGMA _P_USE_CBM_STRINGS 1
|
|
|
|
GOTO start
|
|
|
|
WORD result
|
|
BYTE test_var
|
|
|
|
BYTE CONST TEST_VAL1 = 10
|
|
BYTE CONST TEST_VAL2 = 20
|
|
BYTE CONST TEST_VAL3 = 30
|
|
BYTE CONST OFFSET = 5
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 1: Basic SWITCH with DEFAULT
|
|
//-----------------------------------------------------------
|
|
FUNC test_basic_switch
|
|
LET test_var = 2
|
|
|
|
SWITCH test_var
|
|
CASE 1
|
|
LET result = $0A
|
|
CASE 2
|
|
LET result = $14
|
|
CASE 3
|
|
LET result = $1E
|
|
DEFAULT
|
|
LET result = $63
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("1.basic: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 0014)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 2: SWITCH without DEFAULT
|
|
//-----------------------------------------------------------
|
|
FUNC test_no_default
|
|
LET test_var = 5
|
|
LET result = 0
|
|
|
|
SWITCH test_var
|
|
CASE 1
|
|
LET result = $64
|
|
CASE 5
|
|
LET result = $01F4
|
|
CASE 10
|
|
LET result = $03E8
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("2.no default: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 01f4)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 3: Nested SWITCH statements
|
|
//-----------------------------------------------------------
|
|
FUNC test_nested_switch
|
|
BYTE outer
|
|
BYTE inner
|
|
|
|
LET outer = 2
|
|
LET inner = 3
|
|
LET result = 0
|
|
|
|
SWITCH outer
|
|
CASE 1
|
|
LET result = $01
|
|
CASE 2
|
|
SWITCH inner
|
|
CASE 2
|
|
LET result = $16
|
|
CASE 3
|
|
LET result = $17
|
|
DEFAULT
|
|
LET result = $14
|
|
ENDSWITCH
|
|
CASE 3
|
|
LET result = $03
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("3.nested: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 0017)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 4: SWITCH with long jump pragma
|
|
// (for cases where branches are far apart)
|
|
//-----------------------------------------------------------
|
|
FUNC test_long_jump
|
|
#PRAGMA _P_USE_LONG_JUMP 1
|
|
|
|
LET test_var = 3
|
|
|
|
SWITCH test_var
|
|
CASE 1
|
|
LET result = $0B
|
|
CASE 2
|
|
LET result = $16
|
|
CASE 3
|
|
LET result = $21
|
|
CASE 4
|
|
LET result = $2C
|
|
DEFAULT
|
|
LET result = $00
|
|
ENDSWITCH
|
|
|
|
#PRAGMA _P_USE_LONG_JUMP 0
|
|
|
|
lib_cbmio_print("4.long jump: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 0021)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 5: SWITCH with WORD values
|
|
//-----------------------------------------------------------
|
|
FUNC test_word_switch
|
|
WORD big_value
|
|
|
|
LET big_value = 1000
|
|
LET result = 0
|
|
|
|
SWITCH big_value
|
|
CASE 100
|
|
LET result = $0001
|
|
CASE 1000
|
|
LET result = $0002
|
|
CASE 10000
|
|
LET result = $0003
|
|
DEFAULT
|
|
LET result = $0063
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("5.word: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 0002)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 6: SWITCH with constants and compile-time evaluation
|
|
//-----------------------------------------------------------
|
|
FUNC test_constants
|
|
LET test_var = 25
|
|
LET result = 0
|
|
|
|
SWITCH test_var
|
|
CASE TEST_VAL1
|
|
LET result = $01
|
|
CASE TEST_VAL2
|
|
LET result = $02
|
|
CASE TEST_VAL2+OFFSET
|
|
LET result = $03
|
|
CASE TEST_VAL3
|
|
LET result = $04
|
|
DEFAULT
|
|
LET result = $63
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("6.constants: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 0003)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 7: SWITCH with variable cases (not just literals)
|
|
//-----------------------------------------------------------
|
|
FUNC test_variables
|
|
BYTE match_val1
|
|
BYTE match_val2
|
|
WORD match_val3
|
|
|
|
LET match_val1 = 15
|
|
LET match_val2 = 42
|
|
LET match_val3 = 1000
|
|
|
|
LET test_var = 42
|
|
LET result = 0
|
|
|
|
SWITCH test_var
|
|
CASE match_val1
|
|
LET result = $01
|
|
CASE match_val2
|
|
LET result = $02
|
|
CASE match_val3
|
|
LET result = $03
|
|
DEFAULT
|
|
LET result = $63
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("7.variables: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 0002)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Test 8: SWITCH that actually executes DEFAULT
|
|
//-----------------------------------------------------------
|
|
FUNC test_default_execution
|
|
LET test_var = 99
|
|
LET result = 0
|
|
|
|
SWITCH test_var
|
|
CASE 1
|
|
LET result = $0A
|
|
CASE 2
|
|
LET result = $14
|
|
CASE 3
|
|
LET result = $1E
|
|
DEFAULT
|
|
LET result = $03E7
|
|
ENDSWITCH
|
|
|
|
lib_cbmio_print("8.default exec: ")
|
|
lib_cbmio_hexoutw(result)
|
|
lib_cbmio_printlf(" (exp 03e7)")
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Main program
|
|
//-----------------------------------------------------------
|
|
FUNC main
|
|
lib_cbmio_cls()
|
|
|
|
lib_cbmio_printlf("switch/case demo")
|
|
lib_cbmio_lf()
|
|
|
|
test_basic_switch()
|
|
test_no_default()
|
|
test_nested_switch()
|
|
test_long_jump()
|
|
test_word_switch()
|
|
test_constants()
|
|
test_variables()
|
|
test_default_execution()
|
|
|
|
lib_cbmio_lf()
|
|
lib_cbmio_printlf("all tests complete!")
|
|
|
|
FEND
|
|
|
|
LABEL start
|
|
main()
|
|
SUBEND
|