146 lines
2.9 KiB
Text
146 lines
2.9 KiB
Text
//-----------------------------------------------------------
|
|
// Memory Library Demo
|
|
// Tests malloc and free functionality
|
|
//-----------------------------------------------------------
|
|
|
|
#INCLUDE <c64start.c65>
|
|
#INCLUDE <c64defs.c65>
|
|
#INCLUDE <memlib.c65>
|
|
#INCLUDE <cbmiolib.c65>
|
|
|
|
GOTO start
|
|
|
|
WORD ptr1
|
|
WORD ptr2
|
|
WORD ptr3
|
|
WORD ptr4
|
|
|
|
//-----------------------------------------------------------
|
|
// Wait for key
|
|
//-----------------------------------------------------------
|
|
FUNC wait_key
|
|
BYTE key
|
|
|
|
// Wait for no key
|
|
WHILE 1
|
|
key = PEEK $c5
|
|
IF key = 64
|
|
BREAK
|
|
ENDIF
|
|
WEND
|
|
|
|
// Wait for key press
|
|
WHILE 1
|
|
key = PEEK $c5
|
|
IF key != 64
|
|
BREAK
|
|
ENDIF
|
|
WEND
|
|
|
|
// Reset key buffer
|
|
POKE $c5 WITH 64
|
|
|
|
FEND
|
|
|
|
//-----------------------------------------------------------
|
|
// Main demo
|
|
//-----------------------------------------------------------
|
|
FUNC main
|
|
|
|
#PRAGMA _P_USE_CBM_STRINGS 1
|
|
|
|
lib_cbmio_cls()
|
|
|
|
lib_cbmio_printlf("memlib demo")
|
|
lib_cbmio_printlf("===========")
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_print("initializing...")
|
|
lib_mem_init()
|
|
lib_cbmio_printlf("ready")
|
|
lib_cbmio_lf()
|
|
|
|
// Test 1: Allocate 4 blocks
|
|
lib_cbmio_printlf("test 1: alloc 4 blocks")
|
|
lib_cbmio_printlf("----------------------")
|
|
|
|
lib_mem_malloc(100, ptr1)
|
|
lib_cbmio_print(" ptr1 (100 bytes): $")
|
|
lib_cbmio_hexoutw(ptr1)
|
|
lib_cbmio_lf()
|
|
|
|
lib_mem_malloc(200, ptr2)
|
|
lib_cbmio_print(" ptr2 (200 bytes): $")
|
|
lib_cbmio_hexoutw(ptr2)
|
|
lib_cbmio_lf()
|
|
|
|
lib_mem_malloc(150, ptr3)
|
|
lib_cbmio_print(" ptr3 (150 bytes): $")
|
|
lib_cbmio_hexoutw(ptr3)
|
|
lib_cbmio_lf()
|
|
|
|
lib_mem_malloc(50, ptr4)
|
|
lib_cbmio_print(" ptr4 ( 50 bytes): $")
|
|
lib_cbmio_hexoutw(ptr4)
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Test 2: Free middle blocks
|
|
lib_cbmio_printlf("test 2: free ptr2 & ptr3")
|
|
lib_cbmio_printlf("------------------------")
|
|
|
|
lib_mem_free(ptr2)
|
|
lib_cbmio_printlf(" freed ptr2")
|
|
|
|
lib_mem_free(ptr3)
|
|
lib_cbmio_printlf(" freed ptr3")
|
|
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Test 3: Reallocate in freed space
|
|
lib_cbmio_printlf("test 3: realloc in gap")
|
|
lib_cbmio_printlf("----------------------")
|
|
|
|
lib_mem_malloc(75, ptr2)
|
|
lib_cbmio_print(" new ptr2 ( 75 bytes): $")
|
|
lib_cbmio_hexoutw(ptr2)
|
|
lib_cbmio_lf()
|
|
|
|
lib_mem_malloc(100, ptr3)
|
|
lib_cbmio_print(" new ptr3 (100 bytes): $")
|
|
lib_cbmio_hexoutw(ptr3)
|
|
lib_cbmio_lf()
|
|
|
|
lib_cbmio_lf()
|
|
lib_cbmio_print("press any key...")
|
|
wait_key()
|
|
lib_cbmio_lf()
|
|
lib_cbmio_lf()
|
|
|
|
// Test 4: Cleanup
|
|
lib_cbmio_printlf("test 4: free all")
|
|
lib_cbmio_printlf("----------------")
|
|
|
|
lib_mem_free(ptr1)
|
|
lib_mem_free(ptr2)
|
|
lib_mem_free(ptr3)
|
|
lib_mem_free(ptr4)
|
|
|
|
lib_cbmio_printlf(" all freed")
|
|
lib_cbmio_lf()
|
|
lib_cbmio_printlf("demo complete!")
|
|
|
|
FEND
|
|
|
|
LABEL start
|
|
main()
|
|
SUBEND
|