Skip to content

Commit aca3f8c

Browse files
committed
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
1 parent a3c98ba commit aca3f8c

File tree

6 files changed

+114
-22
lines changed

6 files changed

+114
-22
lines changed

MkDDE,fd7

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ IfThere @.!LibDTB.o Then Else CDir @.!LibDTB.o
2323
IfThere @.!LibDTB.a Then Else CDir @.!LibDTB.a
2424
IfThere @.!LibDTB.h Then Else CDir @.!LibDTB.h
2525

26-
IFthere @.src.dtblib.o.dtblib Then copy @.src.dtblib.o.dtblib @.!LibDTB.o.dtblib ~C N
27-
IFthere @.src.dtblib.o.dtblibzm Then copy @.src.dtblib.o.dtblibzm @.!LibDTB.o.dtblibzm ~C N
28-
IFthere @.src.dtblib.h.dtb Then copy @.src.dtblib.h.dtb @.!LibDTB.h.dtblib ~C N
29-
IFthere @.src.dtblib.h.krnllib Then copy @.src.dtblib.h.krnllib @.!LibDTB.h.krnllib ~C N
30-
IFthere @.src.dtblib.h.strlib Then copy @.src.dtblib.h.strlib @.!LibDTB.h.strlib ~C N
26+
IFthere @.src.dtblib.o.dtblib Then copy @.src.dtblib.o.dtblib @.!LibDTB.o.dtblib ~C N F
27+
IFthere @.src.dtblib.o.dtblibzm Then copy @.src.dtblib.o.dtblibzm @.!LibDTB.o.dtblibzm ~C N F
28+
IFthere @.src.dtblib.h.dtb Then copy @.src.dtblib.h.dtb @.!LibDTB.h.dtblib ~C N F
29+
IFthere @.src.dtblib.h.krnllib Then copy @.src.dtblib.h.krnllib @.!LibDTB.h.krnllib ~C N F
30+
IFthere @.src.dtblib.h.strlib Then copy @.src.dtblib.h.strlib @.!LibDTB.h.strlib ~C N F
3131

3232
echo
3333
echo ---------------------

src/dtblib/MakefileDDE

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@ COMPONENT = dtblib
44
OBJS = krnllib strlib dtb
55

66
include CLibrary
7+
INCLUDED_CLIBRARY = NO
78
LIBS =
89
#LDFLAGS = -bin
910

1011
# Dynamic dependencies:
1112

12-
oz.krnllib: c.krnllib
13-
oz.krnllib: h.krnllib
14-
o.krnllib: c.krnllib
15-
o.krnllib: h.krnllib
16-
oz.strlib: c.strlib
17-
oz.strlib: h.krnllib
18-
oz.strlib: h.strlib
19-
oz.strlib: h.krnllib
20-
oz.dtb: c.dtb
21-
oz.dtb: h.krnllib
22-
oz.dtb: h.strlib
23-
oz.dtb: h.krnllib
24-
oz.dtb: h.dtb
2513
o.strlib: c.strlib
2614
o.strlib: h.krnllib
2715
o.strlib: h.strlib
@@ -31,3 +19,7 @@ o.dtb: h.krnllib
3119
o.dtb: h.strlib
3220
o.dtb: h.krnllib
3321
o.dtb: h.dtb
22+
oz.krnllib: c.krnllib
23+
oz.krnllib: h.krnllib
24+
o.krnllib: c.krnllib
25+
o.krnllib: h.krnllib

src/dtblib/c/krnllib

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,45 @@ uint64_t krnl_bswap64(uint64_t val) {
7272
((val & 0xFF00000000000000ULL) >> 56);
7373
}
7474

75+
void krnl_int_to_str(int num, char* str) {
76+
if (str == NULL) {
77+
return;
78+
}
79+
80+
// Handle negative numbers
81+
int is_negative = 0;
82+
if (num < 0) {
83+
is_negative = 1;
84+
num = -num;
85+
}
86+
87+
// Start converting from the end
88+
int i = 0;
89+
do {
90+
str[i++] = (num % 10) + '0';
91+
num /= 10;
92+
} while (num != 0);
93+
94+
// Add negative sign if needed
95+
if (is_negative) {
96+
str[i++] = '-';
97+
}
98+
99+
str[i] = '\0'; // Null-terminate the string
100+
101+
// Reverse the string
102+
int start = 0;
103+
int end = i - 1;
104+
char temp;
105+
while (start < end) {
106+
temp = str[start];
107+
str[start] = str[end];
108+
str[end] = temp;
109+
start++;
110+
end--;
111+
}
112+
}
113+
75114
uint32_t read_uint32(const char* data, int offset) {
76115
uint32_t val;
77116
krnl_memcpy(&val, data + offset, sizeof(uint32_t));
@@ -84,6 +123,55 @@ uint64_t read_uint64(const char* data, int offset) {
84123
return krnl_bswap64(val);
85124
}
86125

126+
// text output
127+
void krnl_debug_print(const char* message) {
128+
// Write a zero terminated string to the debug console
129+
// using XOS_Write in inline assembly
130+
#if defined(__GNUC__) && defined(__arm__)
131+
__asm__ __volatile__ (
132+
"mov r0, %[message]\n"
133+
"swi #0x20002\n"
134+
:
135+
: [message] "r" (message)
136+
: "r0"
137+
);
138+
#elif defined(__GNUC__) && defined(__aarch64__)
139+
__asm__ __volatile__ (
140+
"mov x0, %[message]\n"
141+
"mov x8, #0x2000004\n"
142+
"svc #0x0\n" // There is no RISC OS 64bit yet, so this is a TBD
143+
:
144+
: [message] "r" (message)
145+
: "x0", "x8"
146+
);
147+
#elif defined(__NORCROFTC) && defined(__arm__)
148+
__asm {
149+
MOV a1, message
150+
SWI 0x20002, {a1}, {}, {PSR}
151+
};
152+
#elif defined(__NORCROFTC) && defined(__aarch64__)
153+
__asm {
154+
MOV x0, message
155+
SVC #0x0 ; There is no RISC OS 64bit yet, so this is a TBD
156+
};
157+
#else
158+
// Add your custom implementation here
159+
(void)message;
160+
#endif
161+
}
162+
163+
void krnl_assert_failed(const char* expression, const char* file, unsigned int line) {
164+
krnl_debug_print("Assertion failed: ");
165+
krnl_debug_print(expression);
166+
krnl_debug_print(", file ");
167+
krnl_debug_print(file);
168+
krnl_debug_print(", line ");
169+
char line_str[12];
170+
krnl_int_to_str(line, line_str);
171+
krnl_debug_print(line_str);
172+
krnl_debug_print("\n");
173+
}
174+
87175
// Memory allocation functions
88176

89177
void* krnl_memcpy(void* dest, const void* src, size_t n) {

src/dtblib/h/krnllib

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ uint64_t krnl_bswap64(uint64_t val);
100100
uint32_t read_uint32(const char* data, int offset);
101101
uint64_t read_uint64(const char* data, int offset);
102102

103+
// Define assert function
104+
void krnl_int_to_str(int num, char* str);
105+
void krnl_debug_print(const char* message);
106+
void krnl_assert_failed(const char* expression, const char* file, unsigned int line);
107+
#ifdef NDEBUG
108+
#define krnl_assert(expression) ((void)0)
109+
#else
110+
#define krnl_assert(expression) ((expression) ? (void)0 : krnl_assert_failed(#expression, __FILE__, __LINE__))
111+
#endif
112+
103113
// Memory allocation functions
104114

105115
void* krnl_malloc(size_t size);

tests/001BasicDTBParse/MakefileDDE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
COMPONENT = BasicDTBParse
44
OBJS = main
55

6+
#include HAL
7+
#LIBS = LibDTB:o.dtblibzm C:ModMalloc.o.ModMalloczm
68
include CUtil
7-
UTIL_LIBS = LibDTB:o.dtblib
9+
UTIL_LIBS = LibDTB:o.dtblibzm C:ModMalloc.o.ModMalloczm
810
LDFLAGS = -util
911

1012
# Dynamic dependencies:

tests/001BasicDTBParse/c/main

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ void test_simple_dtb(void) {
1919
char *string_block = (char *)krnl_malloc(1024);
2020
struct dt_node* root = parse_dtb(simple_dtb, string_block, 0);
2121

22-
assert(root != NULL); // Replace with your own assert function
23-
assert(str_cmp(root->name, "root_node_name") == 0);
22+
krnl_assert(root != NULL); // Replace with your own assert function
23+
krnl_assert(str_cmp(root->name, "root_node_name") == 0);
2424

2525
// Further checks for children, properties, etc.
2626

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

31-
int main() {
31+
int main(void) {
3232
test_simple_dtb();
3333
return 0;
3434
}

0 commit comments

Comments
 (0)