133 lines
2.1 KiB
C
133 lines
2.1 KiB
C
#include <utils.h>
|
|
#include <errcode.h>
|
|
#include <uart.h>
|
|
|
|
uint32_t msb32(uint32_t x)
|
|
{
|
|
static const int msb[32] = {
|
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
};
|
|
|
|
x |= x >> 1;
|
|
x |= x >> 2;
|
|
x |= x >> 4;
|
|
x |= x >> 8;
|
|
x |= x >> 16;
|
|
|
|
return msb[(uint32_t)(x * 0x07c4acddu) >> 27];
|
|
}
|
|
|
|
uint32_t hton32(const uint32_t h)
|
|
{
|
|
const uint8_t *p = (const void *)&h;
|
|
|
|
return (uint32_t)p[0] << 24 |
|
|
(uint32_t)p[1] << 16 |
|
|
(uint32_t)p[2] << 8 |
|
|
(uint32_t)p[3];
|
|
}
|
|
|
|
uint32_t ntoh32(const uint32_t h)
|
|
{
|
|
return hton32(h);
|
|
}
|
|
|
|
uint64_t hton64(const uint64_t h)
|
|
{
|
|
return (uint64_t)hton32(h & 0xffffffff) << 32 |
|
|
(uint64_t)hton32(h >> 32);
|
|
}
|
|
|
|
uint64_t ntoh64(const uint64_t h)
|
|
{
|
|
return hton64(h);
|
|
}
|
|
|
|
int isspace(int c)
|
|
{
|
|
switch (c) {
|
|
case ' ':
|
|
case '\t':
|
|
case '\n':
|
|
case '\v':
|
|
case '\f':
|
|
case '\r':
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int isdigit(int c)
|
|
{
|
|
return ('0' <= c && c <= '9');
|
|
}
|
|
|
|
int isxdigit(int c)
|
|
{
|
|
return ('0' <= c && c <= '9') ||
|
|
('a' <= c && c <= 'f') ||
|
|
('A' <= c && c <= 'F');
|
|
}
|
|
|
|
int isupper(int c)
|
|
{
|
|
return ('A' <= c && c <= 'F');
|
|
}
|
|
|
|
int32_t atoi32(const char *s)
|
|
{
|
|
if (!s)
|
|
exit(ERR_CONVERSION);
|
|
|
|
int is_neg = 0;
|
|
if (*s != '\0' && *s == '-')
|
|
is_neg = 1, s++;
|
|
|
|
int32_t ret = 0;
|
|
for (; *s != '\0'; ++s) {
|
|
if (!isdigit(*s))
|
|
exit(ERR_CONVERSION);
|
|
ret *= 10, ret += *s - '0';
|
|
}
|
|
|
|
return is_neg ? -ret : ret;
|
|
}
|
|
|
|
uint32_t atoh32(const char *s)
|
|
{
|
|
if (!s)
|
|
exit(ERR_CONVERSION);
|
|
|
|
uint32_t ret = 0;
|
|
for (int i = 8; i--; ++s) {
|
|
if (!isxdigit(*s))
|
|
exit(ERR_CONVERSION);
|
|
ret <<= 4, ret |= (isdigit(*s) ? *s - '0' :
|
|
(isupper(*s) ? *s - 'A' + 10 :
|
|
*s - 'a' + 10));
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void exit(int exit_code)
|
|
{
|
|
static int print = 0;
|
|
if (!print) {
|
|
uart_puts(
|
|
"OOPS! YOUR KERNEL DEAD" ENDL
|
|
"ERROR CODE: "
|
|
);
|
|
uart_hex(exit_code);
|
|
uart_puts(ENDL);
|
|
print = 1;
|
|
}
|
|
|
|
asm volatile(
|
|
"wfe\n"
|
|
"b exit"
|
|
);
|
|
__builtin_unreachable();
|
|
}
|