-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
46 lines (39 loc) · 1.04 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
#include "util.h"
#include "htif.h"
void *memset(void *s, int c, size_t n) {
for(size_t i = 0; i < n; ++i) {
((char*)s)[i] = 0;
}
return s;
}
void *memcpy(void *dest, const void *src, size_t n) {
for(size_t i = 0; i < n; ++i) {
((char*)dest)[i] = ((char*)src)[i];
}
return dest;
}
int strcmp(const char *s1, const char *s2) {
for (; *s1 && *s1 == *s2; ++s1, ++s2) {
}
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
static unsigned strlen(const char* str) {
long i = 0;
while(str[i] != 0)
++i;
return i;
}
void print(const char *s) {
syscall(SYS_write, 0, (uintptr_t)s, (uintptr_t)strlen(s), 0, 0, 0, 0);
}
void print_hex(size_t x) {
for(int i = 7; i >= 0; i--) {
size_t digit = x >> (4 * i);
char buf[2] = {0, 0};
buf[0] = "0123456789ABCDEF"[digit & 0xF];
print(buf);
}
}