From bc933dcafd2d7a8afa59023ecff3b3459b0b8558 Mon Sep 17 00:00:00 2001 From: able Date: Tue, 26 Dec 2023 03:54:21 -0600 Subject: [PATCH] Misc: Moved parts of log into their own header to start tracing stuff all over the place Added random number generators --- TODO | 7 +++ src/ableos_std/ableos_std.c | 10 ++- src/ableos_std/arguments.c | 1 + src/ableos_std/log.h | 5 ++ src/ableos_std/random/mersenne_twister/lib.c | 63 +++++++++++++++++++ src/ableos_std/random/mersenne_twister/lib.h | 12 ++++ src/ableos_std/random/random.c | 1 + src/ableos_std/services/csprng_service.c | 6 ++ .../services/{log.c => log_service.c} | 6 -- src/ableos_std/types/float.h | 2 + src/main.c | 5 +- 11 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 TODO create mode 100644 src/ableos_std/log.h create mode 100644 src/ableos_std/random/mersenne_twister/lib.c create mode 100644 src/ableos_std/random/mersenne_twister/lib.h create mode 100644 src/ableos_std/random/random.c create mode 100644 src/ableos_std/services/csprng_service.c rename src/ableos_std/services/{log.c => log_service.c} (74%) create mode 100644 src/ableos_std/types/float.h diff --git a/TODO b/TODO new file mode 100644 index 0000000..d01ac23 --- /dev/null +++ b/TODO @@ -0,0 +1,7 @@ +- [ ] Fix string_eq +- [ ] Move function signatures to the *.h file +- [ ] Sorting stuff +- [x] Random Libraries + - [x] Mersenne Twister + - [ ] add in u64 and f64 generation proper +- [ ] \ No newline at end of file diff --git a/src/ableos_std/ableos_std.c b/src/ableos_std/ableos_std.c index 28e9015..a6ef25d 100644 --- a/src/ableos_std/ableos_std.c +++ b/src/ableos_std/ableos_std.c @@ -1,15 +1,19 @@ +#include "log.h" + #include "types/bool.h" #include "types/int.h" - +#include "types/float.h" #include "types/string/string.h" #include "types/string/string.c" - #include "types/sys_id.h" #include "types/services.h" #include "ecalls.c" -#include "services/log.c" +#include "services/log_service.c" +#include "services/csprng_service.c" + +#include "random/random.c" #include "arguments.c" #include "process.c" \ No newline at end of file diff --git a/src/ableos_std/arguments.c b/src/ableos_std/arguments.c index 01031bf..e8ef40a 100644 --- a/src/ableos_std/arguments.c +++ b/src/ableos_std/arguments.c @@ -4,6 +4,7 @@ typedef struct { } Argument; Argument get_arguments() { + // TODO read arguments from registers Argument args[0]; return (Argument) { diff --git a/src/ableos_std/log.h b/src/ableos_std/log.h new file mode 100644 index 0000000..0fbdc12 --- /dev/null +++ b/src/ableos_std/log.h @@ -0,0 +1,5 @@ +#define error(MSG) inner_log(0, (MSG), __FILE__, __LINE__) +#define warn(MSG) inner_log(1, (MSG), __FILE__, __LINE__) +#define info(MSG) inner_log(2, (MSG), __FILE__, __LINE__) +#define debug(MSG) inner_log(3, (MSG), __FILE__, __LINE__) +#define trace(MSG) inner_log(4, (MSG), __FILE__, __LINE__) \ No newline at end of file diff --git a/src/ableos_std/random/mersenne_twister/lib.c b/src/ableos_std/random/mersenne_twister/lib.c new file mode 100644 index 0000000..6314ad7 --- /dev/null +++ b/src/ableos_std/random/mersenne_twister/lib.c @@ -0,0 +1,63 @@ +#include "lib.h" + +#define UPPER_MASK 0x80000000 +#define LOWER_MASK 0x7fffffff +#define TEMPERING_MASK_B 0x9d2c5680 +#define TEMPERING_MASK_C 0xefc60000 + + +inline static void mt_seedRand(MTRand* rand, unsigned long seed) { + rand->mt[0] = seed & 0xffffffff; + for(rand->index=1; rand->indexindex++) { + rand->mt[rand->index] = (6069 * rand->mt[rand->index-1]) & 0xffffffff; + } +} + +/* +* Creates a new random number generator from a given seed. +*/ +MTRand mersenne_twister_seed_rand(u32 seed) { + MTRand rand; + trace("Seeding a mersenne_twister"); + mt_seedRand(&rand, seed); + return rand; +} + +/** + * Generates a pseudo-randomly generated u32. + */ +u32 mt_generate_u32(MTRand* rand) { + u32 y; + static u32 mag[2] = {0x0, 0x9908b0df}; /* mag[x] = x * 0x9908b0df for x = 0,1 */ + if(rand->index >= STATE_VECTOR_LENGTH || rand->index < 0) { + /* generate STATE_VECTOR_LENGTH words at a time */ + int kk; + if(rand->index >= STATE_VECTOR_LENGTH+1 || rand->index < 0) { + mt_seedRand(rand, 4357); + } + for(kk=0; kkmt[kk] & UPPER_MASK) | (rand->mt[kk+1] & LOWER_MASK); + rand->mt[kk] = rand->mt[kk+STATE_VECTOR_M] ^ (y >> 1) ^ mag[y & 0x1]; + } + for(; kkmt[kk] & UPPER_MASK) | (rand->mt[kk+1] & LOWER_MASK); + rand->mt[kk] = rand->mt[kk+(STATE_VECTOR_M-STATE_VECTOR_LENGTH)] ^ (y >> 1) ^ mag[y & 0x1]; + } + y = (rand->mt[STATE_VECTOR_LENGTH-1] & UPPER_MASK) | (rand->mt[0] & LOWER_MASK); + rand->mt[STATE_VECTOR_LENGTH-1] = rand->mt[STATE_VECTOR_M-1] ^ (y >> 1) ^ mag[y & 0x1]; + rand->index = 0; + } + y = rand->mt[rand->index++]; + y ^= (y >> 11); + y ^= (y << 7) & TEMPERING_MASK_B; + y ^= (y << 15) & TEMPERING_MASK_C; + y ^= (y >> 18); + return y; +} + +/** + * Generates a pseudo-randomly generated float 64 in the range [0..1]. + */ +f64 mt_generate_f64_0_to_1(MTRand* rand) { + return((f64)mt_generate_u32(rand) / (u32)0xffffffff); +} \ No newline at end of file diff --git a/src/ableos_std/random/mersenne_twister/lib.h b/src/ableos_std/random/mersenne_twister/lib.h new file mode 100644 index 0000000..5274ae9 --- /dev/null +++ b/src/ableos_std/random/mersenne_twister/lib.h @@ -0,0 +1,12 @@ +#define STATE_VECTOR_LENGTH 624 +/* changes to STATE_VECTOR_LENGTH also require changes to this */ +#define STATE_VECTOR_M 397 + +typedef struct tagMTRand { + unsigned long mt[STATE_VECTOR_LENGTH]; + int index; +} MTRand; + +MTRand mersenne_twister_seed_rand(unsigned long seed); +u32 mt_generate_u32(MTRand* rand); +f64 mt_generate_f64_0_to_1(MTRand* rand); \ No newline at end of file diff --git a/src/ableos_std/random/random.c b/src/ableos_std/random/random.c new file mode 100644 index 0000000..6a77ae3 --- /dev/null +++ b/src/ableos_std/random/random.c @@ -0,0 +1 @@ +#include "mersenne_twister/lib.c" \ No newline at end of file diff --git a/src/ableos_std/services/csprng_service.c b/src/ableos_std/services/csprng_service.c new file mode 100644 index 0000000..99121fb --- /dev/null +++ b/src/ableos_std/services/csprng_service.c @@ -0,0 +1,6 @@ +// Use this u64 as a seed to your random number generator +u64 get_u64(){ + + + return 0; +} \ No newline at end of file diff --git a/src/ableos_std/services/log.c b/src/ableos_std/services/log_service.c similarity index 74% rename from src/ableos_std/services/log.c rename to src/ableos_std/services/log_service.c index a0e9b6b..80342a6 100644 --- a/src/ableos_std/services/log.c +++ b/src/ableos_std/services/log_service.c @@ -33,9 +33,3 @@ void inner_log(Level level, const char* message, const char* file, int line) { printf("%s [%s:%d]: %s\r\n", lvl_str, file, line, message); } - -#define error(MSG) inner_log(0, (MSG), __FILE__, __LINE__) -#define warn(MSG) inner_log(1, (MSG), __FILE__, __LINE__) -#define info(MSG) inner_log(2, (MSG), __FILE__, __LINE__) -#define debug(MSG) inner_log(3, (MSG), __FILE__, __LINE__) -#define trace(MSG) inner_log(4, (MSG), __FILE__, __LINE__) \ No newline at end of file diff --git a/src/ableos_std/types/float.h b/src/ableos_std/types/float.h new file mode 100644 index 0000000..ee948df --- /dev/null +++ b/src/ableos_std/types/float.h @@ -0,0 +1,2 @@ +typedef float f32; +typedef double f64; diff --git a/src/main.c b/src/main.c index 2a95091..e05ce41 100644 --- a/src/main.c +++ b/src/main.c @@ -59,11 +59,14 @@ PS2Response scancode_command(PS2ScancodeSet scancode) { return Resend; } - // Replacement for the PS/2 Keyboard Driver on ableOS int main() { Argument args = get_arguments(); + MTRand a = mersenne_twister_seed_rand(10); + f64 ac = mt_generate_f64_0_to_1(&a); + printf("%f\n\r", ac); + int b = string_eq(args.value, from_c_str("x86-64")); if (b == 1) { info("running on X86");