From aca3f8c64521be479080cf396cae244e4dbd86f1 Mon Sep 17 00:00:00 2001 From: Paolo Fabio Zaino Date: Tue, 21 Nov 2023 02:37:23 +0000 Subject: [PATCH] Fixed some bug, added more cLib like code to krnllib, but I am thinking of using my uCLib for this it's probably a better choice --- MkDDE,fd7 | 10 ++-- src/dtblib/MakefileDDE | 18 ++---- src/dtblib/c/krnllib | 88 ++++++++++++++++++++++++++++++ src/dtblib/h/krnllib | 10 ++++ tests/001BasicDTBParse/MakefileDDE | 4 +- tests/001BasicDTBParse/c/main | 6 +- 6 files changed, 114 insertions(+), 22 deletions(-) diff --git a/MkDDE,fd7 b/MkDDE,fd7 index 919be0a..4ba10a5 100644 --- a/MkDDE,fd7 +++ b/MkDDE,fd7 @@ -23,11 +23,11 @@ IfThere @.!LibDTB.o Then Else CDir @.!LibDTB.o IfThere @.!LibDTB.a Then Else CDir @.!LibDTB.a IfThere @.!LibDTB.h Then Else CDir @.!LibDTB.h -IFthere @.src.dtblib.o.dtblib Then copy @.src.dtblib.o.dtblib @.!LibDTB.o.dtblib ~C N -IFthere @.src.dtblib.o.dtblibzm Then copy @.src.dtblib.o.dtblibzm @.!LibDTB.o.dtblibzm ~C N -IFthere @.src.dtblib.h.dtb Then copy @.src.dtblib.h.dtb @.!LibDTB.h.dtblib ~C N -IFthere @.src.dtblib.h.krnllib Then copy @.src.dtblib.h.krnllib @.!LibDTB.h.krnllib ~C N -IFthere @.src.dtblib.h.strlib Then copy @.src.dtblib.h.strlib @.!LibDTB.h.strlib ~C N +IFthere @.src.dtblib.o.dtblib Then copy @.src.dtblib.o.dtblib @.!LibDTB.o.dtblib ~C N F +IFthere @.src.dtblib.o.dtblibzm Then copy @.src.dtblib.o.dtblibzm @.!LibDTB.o.dtblibzm ~C N F +IFthere @.src.dtblib.h.dtb Then copy @.src.dtblib.h.dtb @.!LibDTB.h.dtblib ~C N F +IFthere @.src.dtblib.h.krnllib Then copy @.src.dtblib.h.krnllib @.!LibDTB.h.krnllib ~C N F +IFthere @.src.dtblib.h.strlib Then copy @.src.dtblib.h.strlib @.!LibDTB.h.strlib ~C N F echo echo --------------------- diff --git a/src/dtblib/MakefileDDE b/src/dtblib/MakefileDDE index 30dc335..c5398e5 100644 --- a/src/dtblib/MakefileDDE +++ b/src/dtblib/MakefileDDE @@ -4,24 +4,12 @@ COMPONENT = dtblib OBJS = krnllib strlib dtb include CLibrary +INCLUDED_CLIBRARY = NO LIBS = #LDFLAGS = -bin # Dynamic dependencies: -oz.krnllib: c.krnllib -oz.krnllib: h.krnllib -o.krnllib: c.krnllib -o.krnllib: h.krnllib -oz.strlib: c.strlib -oz.strlib: h.krnllib -oz.strlib: h.strlib -oz.strlib: h.krnllib -oz.dtb: c.dtb -oz.dtb: h.krnllib -oz.dtb: h.strlib -oz.dtb: h.krnllib -oz.dtb: h.dtb o.strlib: c.strlib o.strlib: h.krnllib o.strlib: h.strlib @@ -31,3 +19,7 @@ o.dtb: h.krnllib o.dtb: h.strlib o.dtb: h.krnllib o.dtb: h.dtb +oz.krnllib: c.krnllib +oz.krnllib: h.krnllib +o.krnllib: c.krnllib +o.krnllib: h.krnllib diff --git a/src/dtblib/c/krnllib b/src/dtblib/c/krnllib index 6f67684..72199b8 100644 --- a/src/dtblib/c/krnllib +++ b/src/dtblib/c/krnllib @@ -72,6 +72,45 @@ uint64_t krnl_bswap64(uint64_t val) { ((val & 0xFF00000000000000ULL) >> 56); } +void krnl_int_to_str(int num, char* str) { + if (str == NULL) { + return; + } + + // Handle negative numbers + int is_negative = 0; + if (num < 0) { + is_negative = 1; + num = -num; + } + + // Start converting from the end + int i = 0; + do { + str[i++] = (num % 10) + '0'; + num /= 10; + } while (num != 0); + + // Add negative sign if needed + if (is_negative) { + str[i++] = '-'; + } + + str[i] = '\0'; // Null-terminate the string + + // Reverse the string + int start = 0; + int end = i - 1; + char temp; + while (start < end) { + temp = str[start]; + str[start] = str[end]; + str[end] = temp; + start++; + end--; + } +} + uint32_t read_uint32(const char* data, int offset) { uint32_t val; krnl_memcpy(&val, data + offset, sizeof(uint32_t)); @@ -84,6 +123,55 @@ uint64_t read_uint64(const char* data, int offset) { return krnl_bswap64(val); } +// text output +void krnl_debug_print(const char* message) { + // Write a zero terminated string to the debug console + // using XOS_Write in inline assembly +#if defined(__GNUC__) && defined(__arm__) + __asm__ __volatile__ ( + "mov r0, %[message]\n" + "swi #0x20002\n" + : + : [message] "r" (message) + : "r0" + ); +#elif defined(__GNUC__) && defined(__aarch64__) + __asm__ __volatile__ ( + "mov x0, %[message]\n" + "mov x8, #0x2000004\n" + "svc #0x0\n" // There is no RISC OS 64bit yet, so this is a TBD + : + : [message] "r" (message) + : "x0", "x8" + ); +#elif defined(__NORCROFTC) && defined(__arm__) + __asm { + MOV a1, message + SWI 0x20002, {a1}, {}, {PSR} + }; +#elif defined(__NORCROFTC) && defined(__aarch64__) + __asm { + MOV x0, message + SVC #0x0 ; There is no RISC OS 64bit yet, so this is a TBD + }; +#else + // Add your custom implementation here + (void)message; +#endif +} + +void krnl_assert_failed(const char* expression, const char* file, unsigned int line) { + krnl_debug_print("Assertion failed: "); + krnl_debug_print(expression); + krnl_debug_print(", file "); + krnl_debug_print(file); + krnl_debug_print(", line "); + char line_str[12]; + krnl_int_to_str(line, line_str); + krnl_debug_print(line_str); + krnl_debug_print("\n"); +} + // Memory allocation functions void* krnl_memcpy(void* dest, const void* src, size_t n) { diff --git a/src/dtblib/h/krnllib b/src/dtblib/h/krnllib index 99c81c6..545ef8d 100644 --- a/src/dtblib/h/krnllib +++ b/src/dtblib/h/krnllib @@ -100,6 +100,16 @@ uint64_t krnl_bswap64(uint64_t val); uint32_t read_uint32(const char* data, int offset); uint64_t read_uint64(const char* data, int offset); +// Define assert function +void krnl_int_to_str(int num, char* str); +void krnl_debug_print(const char* message); +void krnl_assert_failed(const char* expression, const char* file, unsigned int line); +#ifdef NDEBUG +#define krnl_assert(expression) ((void)0) +#else +#define krnl_assert(expression) ((expression) ? (void)0 : krnl_assert_failed(#expression, __FILE__, __LINE__)) +#endif + // Memory allocation functions void* krnl_malloc(size_t size); diff --git a/tests/001BasicDTBParse/MakefileDDE b/tests/001BasicDTBParse/MakefileDDE index 72b0c5b..81d6e72 100644 --- a/tests/001BasicDTBParse/MakefileDDE +++ b/tests/001BasicDTBParse/MakefileDDE @@ -3,8 +3,10 @@ COMPONENT = BasicDTBParse OBJS = main +#include HAL +#LIBS = LibDTB:o.dtblibzm C:ModMalloc.o.ModMalloczm include CUtil -UTIL_LIBS = LibDTB:o.dtblib +UTIL_LIBS = LibDTB:o.dtblibzm C:ModMalloc.o.ModMalloczm LDFLAGS = -util # Dynamic dependencies: diff --git a/tests/001BasicDTBParse/c/main b/tests/001BasicDTBParse/c/main index cbd627b..a8026de 100644 --- a/tests/001BasicDTBParse/c/main +++ b/tests/001BasicDTBParse/c/main @@ -19,8 +19,8 @@ void test_simple_dtb(void) { char *string_block = (char *)krnl_malloc(1024); struct dt_node* root = parse_dtb(simple_dtb, string_block, 0); - assert(root != NULL); // Replace with your own assert function - assert(str_cmp(root->name, "root_node_name") == 0); + krnl_assert(root != NULL); // Replace with your own assert function + krnl_assert(str_cmp(root->name, "root_node_name") == 0); // Further checks for children, properties, etc. @@ -28,7 +28,7 @@ void test_simple_dtb(void) { krnl_free((void *)simple_dtb); // Function to free DTB data } -int main() { +int main(void) { test_simple_dtb(); return 0; }