From 930e81573a82f721468d36422e68666c8d17c78a Mon Sep 17 00:00:00 2001 From: Mattias Hansson Date: Sat, 16 May 2026 19:46:20 +0200 Subject: [PATCH] Updated libsc.c65 and added multdivlib demo --- examples/multdivlib_demo/cm.sh | 3 + examples/multdivlib_demo/multdivlib_demo.c65 | 168 +++++++++++++++++++ examples/multdivlib_demo/start_in_vice.sh | 2 + lib/libsc.c65 | 11 ++ 4 files changed, 184 insertions(+) create mode 100644 examples/multdivlib_demo/cm.sh create mode 100644 examples/multdivlib_demo/multdivlib_demo.c65 create mode 100644 examples/multdivlib_demo/start_in_vice.sh diff --git a/examples/multdivlib_demo/cm.sh b/examples/multdivlib_demo/cm.sh new file mode 100644 index 0000000..d6107db --- /dev/null +++ b/examples/multdivlib_demo/cm.sh @@ -0,0 +1,3 @@ +#!/bin/sh +PROGNAME="multdivlib_demo" +c65gm ${PROGNAME}.c65 diff --git a/examples/multdivlib_demo/multdivlib_demo.c65 b/examples/multdivlib_demo/multdivlib_demo.c65 new file mode 100644 index 0000000..b586324 --- /dev/null +++ b/examples/multdivlib_demo/multdivlib_demo.c65 @@ -0,0 +1,168 @@ +//----------------------------------------------------------- +// MultDiv Library Demo +// Demonstrates multiplication and division routines +//----------------------------------------------------------- + +#INCLUDE +#INCLUDE +#INCLUDE +#INCLUDE + +#PRAGMA _P_USE_CBM_STRINGS 1 + +GOTO start + +WORD high +BYTE i + +//----------------------------------------------------------- +// Wait for key +//----------------------------------------------------------- +FUNC wait_key + BYTE key + + WHILE 1 + key = PEEK $c5 + IF key = 64 + BREAK + ENDIF + WEND + + WHILE 1 + key = PEEK $c5 + IF key != 64 + BREAK + ENDIF + WEND + + POKE $c6 WITH 0 +FEND + +//----------------------------------------------------------- +// Main demo +//----------------------------------------------------------- +FUNC main + + lib_cbmio_cls() + + lib_cbmio_printlf("multdivlib demo") + lib_cbmio_printlf("===============") + lib_cbmio_lf() + + WORD a + WORD b + + //----------------------------------------- + // Test 1: mult - 123 * 456 (16-bit result) + // io:lib_multdiv_acc is input/output + // result in lib_multdiv_acc, high in lib_multdiv_ext + //----------------------------------------- + lib_cbmio_printlf("lib_multdiv_mult: 123 * 456 =") + + a = 123 + b = 456 + lib_multdiv_mult(a, b) + + lib_cbmio_print(" result: ") + lib_cbmio_hexoutw(lib_multdiv_acc) + lib_cbmio_lf() + lib_cbmio_lf() + + lib_cbmio_print("press any key...") + wait_key() + lib_cbmio_lf() + lib_cbmio_lf() + + //----------------------------------------- + // Test 2: mult32 - 65000 * 100 (32-bit result) + // out:high gets the high word + //----------------------------------------- + lib_cbmio_printlf("mult32: 65000 * 100 =") + + a = 65000 + b = 100 + lib_multdiv_mult32(a, b, high) + + lib_cbmio_print(" low: $") + lib_cbmio_hexoutw(lib_multdiv_acc) + lib_cbmio_print(" high: $") + lib_cbmio_hexoutw(high) + lib_cbmio_lf() + lib_cbmio_lf() + + lib_cbmio_print("press any key...") + wait_key() + lib_cbmio_lf() + lib_cbmio_lf() + + //----------------------------------------- + // Test 3: div - 1337 / 7 + // quotient in lib_multdiv_acc, remainder in lib_multdiv_ext + //----------------------------------------- + lib_cbmio_printlf("lib_multdiv_div: 1337 / 7 =") + + a = 1337 + b = 7 + lib_multdiv_div(a, b) + + lib_cbmio_print(" quotient: ") + lib_cbmio_hexoutw(lib_multdiv_acc) + lib_cbmio_lf() + + lib_cbmio_print(" remainder: ") + lib_cbmio_hexoutw(lib_multdiv_ext) + lib_cbmio_lf() + lib_cbmio_lf() + + //----------------------------------------- + // Test 4: divmod - 1000 / 13 + // out:high gets the remainder + //----------------------------------------- + lib_cbmio_printlf("divmod: 1000 / 13 =") + + a = 1000 + b = 13 + lib_multdiv_divmod(a, b, high) + + lib_cbmio_print(" quotient: ") + lib_cbmio_hexoutw(lib_multdiv_acc) + lib_cbmio_lf() + + lib_cbmio_print(" remainder: $") + lib_cbmio_hexoutw(high) + lib_cbmio_lf() + lib_cbmio_lf() + + lib_cbmio_print("press any key...") + wait_key() + lib_cbmio_lf() + lib_cbmio_lf() + + //----------------------------------------- + // Test 5: Loop 0-9 multiplied by 10 + //----------------------------------------- + lib_cbmio_printlf("loop: 0*10 through 9*10:") + lib_cbmio_lf() + + i = 0 + WHILE i < 10 + a = i + b = 10 + lib_multdiv_mult(a, b) + + lib_cbmio_print(" ") + lib_cbmio_hexoutb(i) + lib_cbmio_print(" * 10 = ") + lib_cbmio_hexoutw(lib_multdiv_acc) + lib_cbmio_lf() + + i++ + WEND + lib_cbmio_lf() + + lib_cbmio_printlf("done!") +FEND + +LABEL start + main() + SUBEND diff --git a/examples/multdivlib_demo/start_in_vice.sh b/examples/multdivlib_demo/start_in_vice.sh new file mode 100644 index 0000000..4e495f5 --- /dev/null +++ b/examples/multdivlib_demo/start_in_vice.sh @@ -0,0 +1,2 @@ +#!/bin/sh +x64 -autostartprgmode 1 multdivlib_demo.prg diff --git a/lib/libsc.c65 b/lib/libsc.c65 index 55a3109..f24a92d 100644 --- a/lib/libsc.c65 +++ b/lib/libsc.c65 @@ -45,6 +45,9 @@ #DEFINE memset = lib_string_memset #DEFINE strcat = lib_string_strcat #DEFINE strncat = lib_string_strncat + #DEFINE strstr = lib_string_strstr + #DEFINE memcmp = lib_string_memcmp + #DEFINE memcpy = lib_string_memcpy #IFEND #IFDEF __LIB_STRLIST @@ -62,6 +65,14 @@ #IFDEF __LIB_MEM #DEFINE malloc = lib_mem_malloc #DEFINE free = lib_mem_free + #DEFINE mem_init = lib_mem_init + #DEFINE mem_defrag = lib_mem_defrag + #DEFINE mem_stats = lib_mem_stats + #IFEND + + #IFDEF __LIB_MULTDIV + #DEFINE mult32 = lib_multdiv_mult32 + #DEFINE divmod = lib_multdiv_divmod #IFEND #IFEND