Skip to content

Commit

Permalink
Merge pull request #4 from RISC-OS-Community/develop
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pzaino authored Nov 21, 2023
2 parents 62fa99d + aca3f8c commit 226bed1
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 22 deletions.
10 changes: 5 additions & 5 deletions MkDDE,fd7
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------
Expand Down
18 changes: 5 additions & 13 deletions src/dtblib/MakefileDDE
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
88 changes: 88 additions & 0 deletions src/dtblib/c/krnllib
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/dtblib/h/krnllib
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/001BasicDTBParse/MakefileDDE
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions tests/001BasicDTBParse/c/main
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ 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.

krnl_free((void *)root); // Clean up
krnl_free((void *)simple_dtb); // Function to free DTB data
}

int main() {
int main(void) {
test_simple_dtb();
return 0;
}

0 comments on commit 226bed1

Please sign in to comment.