#pragma once #include "../syscall.h" #include #include void _Noreturn panic(const char* message) { syscall(SYS_switch_to_text, 0); // ??? syscall(SYS_puts, (uintptr_t)message); syscall(SYS_exit, 1); __builtin_unreachable(); } #define check(expr) if (!(expr)){ panic( "Assertion failed at " __FILE__ ": " #expr); } typedef struct { int x, y; } ivec2; void* memcpy(void* dst, const void* src, uint32_t size) { uint8_t* d = (uint8_t*)dst; const uint8_t* s = (const uint8_t*)src; for (uint32_t i = 0; i < size; i++) { d[i] = s[i]; } return dst; } void* memset(void* dst, int value, uint32_t size) { uint8_t* d = (uint8_t*)dst; uint8_t byte = (uint8_t)value; for (uint32_t i = 0; i < size; i++) { d[i] = byte; } return dst; } #define STRING_BUILDER_CAPACITY 150 typedef struct { char buf[STRING_BUILDER_CAPACITY]; int len; } StringBuilder; void StringBuilder_putc(StringBuilder* self, char ch) { if (self->len + 1 > STRING_BUILDER_CAPACITY) { panic("string builder\n"); } self->buf[self->len++] = ch; } void StringBuilder_append_cstr(StringBuilder* self, const char* str) { for (const char* p = str; *p; p++) { StringBuilder_putc(self, *p); } } void StringBuilder_append_u32(StringBuilder* self, uint32_t x) { char b[11]; int len = 0; if (x == 0) { b[len++] = '0'; } else { while (x > 0) { b[len++] = (char)('0' + (x % 10)); x /= 10; } int i = 0, j = len - 1; while (i < j) { char tmp = b[i]; b[i] = b[j]; b[j] = tmp; i++; j--; } } b[len] = 0; StringBuilder_append_cstr(self, b); } const char* StringBuilder_get_cstr(StringBuilder* self) { StringBuilder_putc(self, 0); return self->buf; } /* We still have beef with keyboard, even in userspace */ #define KEYCODE_SHIFT 42 #define KEYCODE_ENTER 28 #define KEYCODE_BACKSPACE 14 #define KEYCODE_SPACE 57 #define KEYCODE_ESCAPE 1 #define KEYCODE_A 30 #define KEYCODE_S 31 #define KEYCODE_D 32 #define KEYCODE_W 17 #define KEYCODE_Q 16 #define KEYCODE_ENTER 28 #define KEYCODE_LEFT 75 #define KEYCODE_RIGHT 77 #define KEYCODE_UP 72 #define KEYCODE_DOWN 80 #define KEYCODE_1 2 #define KEYCODE_2 3 bool is_keycode_for_press_left(uint8_t keycode) { return keycode == KEYCODE_LEFT || keycode == KEYCODE_A; } bool is_keycode_for_press_right(uint8_t keycode) { return keycode == KEYCODE_RIGHT || keycode == KEYCODE_D; } bool is_keycode_for_press_up(uint8_t keycode) { return keycode == KEYCODE_UP || keycode == KEYCODE_W; } bool is_keycode_for_press_down(uint8_t keycode) { return keycode == KEYCODE_DOWN || keycode == KEYCODE_S; }