aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2021-12-20 19:57:18 -0800
committerChristian Cunningham <cc@localhost>2021-12-20 19:57:18 -0800
commit5768e8059dc3b7f56361fe5b5c578294df3fdf67 (patch)
tree0f9e769c78c5a3bf874c84293c3d10e6693f3fec /src/lib
parent234616cc369e056db7987c8217cbee77443be97d (diff)
Implemented memory functions
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/mem.c38
-rw-r--r--src/lib/mem.h11
-rw-r--r--src/lib/strings.c15
-rw-r--r--src/lib/strings.h7
4 files changed, 67 insertions, 4 deletions
diff --git a/src/lib/mem.c b/src/lib/mem.c
new file mode 100644
index 0000000..cf1c980
--- /dev/null
+++ b/src/lib/mem.c
@@ -0,0 +1,38 @@
+#include "../drivers/uart.a.h"
+
+void memshow32(unsigned long* addr, unsigned int n) {
+ for(unsigned int i = 0; i < n; i++) {
+ uart_hex(addr[i]);
+ if (i+1 != n)
+ uart_char(0x20);
+ }
+ uart_char(0x0a);
+}
+
+void memcpy(unsigned char* src, unsigned char* dest, unsigned int n) {
+ for(unsigned int i = 0; i < n; i++) {
+ dest[i] = src[i];
+ }
+}
+
+unsigned char memcmp(unsigned char* a, unsigned char* b, unsigned int n) {
+ for(unsigned int i = 0; i < n; i++) {
+ if (a[i] != b[i])
+ return 0;
+ }
+ return 1;
+}
+
+void memcpy32(unsigned long* src, unsigned long* dest, unsigned int n) {
+ for(unsigned int i = 0; i < n; i++) {
+ dest[i] = src[i];
+ }
+}
+
+unsigned char memcmp32(unsigned long* a, unsigned long* b, unsigned int n) {
+ for(unsigned int i = 0; i < n; i++) {
+ if (a[i] != b[i])
+ return 0;
+ }
+ return 1;
+}
diff --git a/src/lib/mem.h b/src/lib/mem.h
new file mode 100644
index 0000000..67e2717
--- /dev/null
+++ b/src/lib/mem.h
@@ -0,0 +1,11 @@
+#ifndef LIB_MEM_H
+#define LIB_MEM_H
+
+void memcpy(unsigned char* src, unsigned char* dest, unsigned int n);
+unsigned char memcmp(unsigned char* a, unsigned char* b, unsigned int n);
+
+void memshow32(unsigned long* addr, unsigned int n);
+void memcpy32(unsigned long* src, unsigned long* dest, unsigned int n);
+unsigned char memcmp32(unsigned long* a, unsigned long* b, unsigned int n);
+
+#endif
diff --git a/src/lib/strings.c b/src/lib/strings.c
index 8f62391..795caa2 100644
--- a/src/lib/strings.c
+++ b/src/lib/strings.c
@@ -1,6 +1,6 @@
#include "../lib/strings.h"
-unsigned long strlen(char* s) {
+unsigned long strlen(string_t s) {
unsigned long len = 0;
while (s[len] != 0) {
len += 1;
@@ -8,7 +8,7 @@ unsigned long strlen(char* s) {
return len;
}
-unsigned char strcmp(char* a, char* b) {
+unsigned char strcmp(string_t a, string_t b) {
unsigned long idx = 0;
while (a[idx] != 0 && b[idx] != 0) {
if (a[idx] != b[idx]) {
@@ -18,3 +18,14 @@ unsigned char strcmp(char* a, char* b) {
}
return a[idx] == b[idx];
}
+
+unsigned char strcmpn(string_t a, string_t b, unsigned int n) {
+ unsigned long idx = 0;
+ while (a[idx] != 0 && b[idx] != 0 && idx+1 < n) {
+ if (a[idx] != b[idx]) {
+ return 0;
+ }
+ idx += 1;
+ }
+ return a[idx] == b[idx];
+}
diff --git a/src/lib/strings.h b/src/lib/strings.h
index 61dd316..1bbcac8 100644
--- a/src/lib/strings.h
+++ b/src/lib/strings.h
@@ -1,7 +1,10 @@
#ifndef LIB_STRINGS_H
#define LIB_STRINGS_H
-unsigned long strlen(char* s);
-unsigned char strcmp(char* a, char* b);
+#define string_t char*
+
+unsigned long strlen(string_t s);
+unsigned char strcmp(string_t a, string_t b);
+unsigned char strcmpn(string_t a, string_t b, unsigned int n);
#endif