TSOS/include/kernel/util.h

59 lines
1.6 KiB
C

/*
Copyright 2022 Mattia Giambirtone & Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef TSOS_UTIL_H
#define TSOS_UTIL_H
#include "kernel/types.h"
// Some handy macros
/*
These bitwise tricks *seem* like black
magic, but they're quite simple: shifting
the offset by 8 bits moves the high bits 8
positions down, so we lose the low bits and
the high bits now fit into a single byte. To
get the high bits, we do a bitwise and with 0xff
(11111111 in decimal) leaving us with only the low
bits: the reason this works is because 0xff (which
is smaller than our offset), is zero-extended from
the beginning with zeroes, so when we perform the
operation the high bits are cancelled out. Neat huh?
*/
#define LOW8(x) (u8)(x & 0xff)
#define HIGH8(x) (u8)(x >> 8)
/*
Here we do the same thing, except it's to get the
low/high 16 bits of a 32-bit value instead
*/
#define LOW16(x) (u16)(x & 0xffff)
#define HIGH16(x) (u16)(LOW16(x >> 16))
void copystr(const char* source, char* dest, i32 n);
void memset(byte* dest, byte val, u32 len);
void itoa(const i32 i, char* a);
void utoa(const u32 i, char* a);
#endif