From e8c6577617bffa4402c07c7aa20e3c24f03c1c20 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 6 Sep 2020 00:48:07 -0400 Subject: program loading, others big kernel additions: paging, elf loading, separate kernel and user page allocation it now properly loads and runs sd0:bin/init.elf still need to determine which disk was booted from, and start the init on that disk --- src/user/elf.ld | 8 ++ src/user/hello/hello.asm | 15 ---- src/user/include/canyo/file.h | 9 --- src/user/include/knob/block.h | 8 ++ src/user/include/knob/env.h | 9 +++ src/user/include/knob/file.h | 17 +++++ src/user/include/knob/format.h | 9 +++ src/user/include/knob/heap.h | 9 +++ src/user/include/knob/quit.h | 7 ++ src/user/include/knob/user.h | 12 +++ src/user/include/pland.h | 157 --------------------------------------- src/user/include/pland/syscall.h | 139 ++++++++++++++++++++++++++++++++++ src/user/init/main.c | 29 ++++++-- src/user/knob/entry.asm | 23 ++++++ src/user/knob/env.c | 3 + src/user/knob/file.c | 82 ++++++++++++++++++++ src/user/knob/format.c | 13 ++++ src/user/knob/heap.c | 112 ++++++++++++++++++++++++++++ src/user/knob/quit.c | 29 ++++++++ src/user/knob/user.c | 134 +++++++++++++++++++++++++++++++++ src/user/libcanyo/file.c | 15 ---- 21 files changed, 635 insertions(+), 204 deletions(-) create mode 100644 src/user/elf.ld delete mode 100644 src/user/hello/hello.asm delete mode 100644 src/user/include/canyo/file.h create mode 100644 src/user/include/knob/block.h create mode 100644 src/user/include/knob/env.h create mode 100644 src/user/include/knob/file.h create mode 100644 src/user/include/knob/format.h create mode 100644 src/user/include/knob/heap.h create mode 100644 src/user/include/knob/quit.h create mode 100644 src/user/include/knob/user.h delete mode 100644 src/user/include/pland.h create mode 100644 src/user/include/pland/syscall.h create mode 100644 src/user/knob/entry.asm create mode 100644 src/user/knob/env.c create mode 100644 src/user/knob/file.c create mode 100644 src/user/knob/format.c create mode 100644 src/user/knob/heap.c create mode 100644 src/user/knob/quit.c create mode 100644 src/user/knob/user.c delete mode 100644 src/user/libcanyo/file.c (limited to 'src/user') diff --git a/src/user/elf.ld b/src/user/elf.ld new file mode 100644 index 0000000..aaef517 --- /dev/null +++ b/src/user/elf.ld @@ -0,0 +1,8 @@ +OUTPUT_FORMAT(elf32-i386) +OUTPUT_ARCH(i386) +ENTRY(_entry) + +MEMORY { + kernel (!a) : ORIGIN = 0x00000000, LENGTH = 0x08000000 + user (awx) : ORIGIN = 0x08000000, LENGTH = 0xf8000000 +} \ No newline at end of file diff --git a/src/user/hello/hello.asm b/src/user/hello/hello.asm deleted file mode 100644 index 345abb4..0000000 --- a/src/user/hello/hello.asm +++ /dev/null @@ -1,15 +0,0 @@ -global _entry - -section .text -_entry: - mov eax, 3 - mov ebx, msg - int 0x32 - - int 0x30 - -section .rodata -msg db "Hello, world!", 0x0a, 0x00 - -section .bss -stack resb 8 \ No newline at end of file diff --git a/src/user/include/canyo/file.h b/src/user/include/canyo/file.h deleted file mode 100644 index 5f09387..0000000 --- a/src/user/include/canyo/file.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef CANYO_FILE_H -#define CANYO_FILE_H - -#include -#include - -uint32_t read_line(fs_handle handle, uint32_t max_length, void *buffer); - -#endif \ No newline at end of file diff --git a/src/user/include/knob/block.h b/src/user/include/knob/block.h new file mode 100644 index 0000000..53b3deb --- /dev/null +++ b/src/user/include/knob/block.h @@ -0,0 +1,8 @@ +#ifndef KNOB_BLOCK_H +#define KNOB_BLOCK_H + +#include + +void blockcpy(void *to, const void *from, uint32_t count); + +#endif \ No newline at end of file diff --git a/src/user/include/knob/env.h b/src/user/include/knob/env.h new file mode 100644 index 0000000..6d750ae --- /dev/null +++ b/src/user/include/knob/env.h @@ -0,0 +1,9 @@ +#ifndef KNOB_ENV_H +#define KNOB_ENV_H + +#include + +//not implemented yet +extern uint32_t current_drive; + +#endif \ No newline at end of file diff --git a/src/user/include/knob/file.h b/src/user/include/knob/file.h new file mode 100644 index 0000000..6068077 --- /dev/null +++ b/src/user/include/knob/file.h @@ -0,0 +1,17 @@ +#ifndef KNOB_FILE_H +#define KNOB_FILE_H + +#include + +struct file; + +struct file *open_file(const char *path); +void close_file(struct file *f); + +uint32_t read_from_file(struct file *f, uint32_t max, void *buf); +uint32_t seek_file_to(struct file *f, uint32_t to); +int32_t seek_file_by(struct file *f, int32_t by); + +uint32_t file_size(struct file *f) __attribute__ ((pure)); + +#endif \ No newline at end of file diff --git a/src/user/include/knob/format.h b/src/user/include/knob/format.h new file mode 100644 index 0000000..16d3d83 --- /dev/null +++ b/src/user/include/knob/format.h @@ -0,0 +1,9 @@ +#ifndef KNOB_FORMAT_H +#define KNOB_FORMAT_H + +#include +#include + +bool try_sntoi(const char *s, uint32_t n, uint32_t *out); + +#endif \ No newline at end of file diff --git a/src/user/include/knob/heap.h b/src/user/include/knob/heap.h new file mode 100644 index 0000000..32dc44c --- /dev/null +++ b/src/user/include/knob/heap.h @@ -0,0 +1,9 @@ +#ifndef KNOB_HEAP_H +#define KNOB_HEAP_H + +#include + +void *get_block(uint32_t bytes) __attribute__ ((malloc)); +void free_block(void *block); + +#endif \ No newline at end of file diff --git a/src/user/include/knob/quit.h b/src/user/include/knob/quit.h new file mode 100644 index 0000000..7b10d09 --- /dev/null +++ b/src/user/include/knob/quit.h @@ -0,0 +1,7 @@ +#ifndef KNOB_QUIT_H +#define KNOB_QUIT_H + +void on_quit(void (*run_f)()); +void quit() __attribute__ ((noreturn)); + +#endif \ No newline at end of file diff --git a/src/user/include/knob/user.h b/src/user/include/knob/user.h new file mode 100644 index 0000000..c4ec7db --- /dev/null +++ b/src/user/include/knob/user.h @@ -0,0 +1,12 @@ +#ifndef KNOB_USER_H +#define KNOB_USER_H + +#include + +void tell_user_sz(const char *sz); +void tell_user_n(uint32_t n); + +//return value and max_length both include null terminator +uint32_t ask_user_line_sz(char *sz, uint32_t max_length); + +#endif \ No newline at end of file diff --git a/src/user/include/pland.h b/src/user/include/pland.h deleted file mode 100644 index f174a51..0000000 --- a/src/user/include/pland.h +++ /dev/null @@ -1,157 +0,0 @@ -#ifndef PLAND_H -#define PLAND_H - -#include -#include - -typedef uint8_t fs_handle; -typedef uint8_t task_handle; - -static inline void exit() __attribute__ ((noreturn)) { - asm volatile ("int $0x30"); - __builtin_unreachable(); -} - -static inline void yield() { - asm volatile ("int $0x31"); -} - -static inline uint32_t data_extend(uint32_t amount) { - uint32_t actual_amount; - asm volatile ( - "int $0x33" - : "eax" (actual_amount) : "eax" (amount)); - return actual_amount; -} - -static inline void vga_blank() { - asm volatile ( - "xor %%eax, %%eax\n" - "int $0x32" - : : : "eax"); -} - -static inline void vga_set_color(uint8_t color) { - asm volatile ( - "mov $0x1, %%eax\n" - "int $0x32" - : : "ebx" (color) : "eax"); -} - -static inline void vga_printch(uint8_t ch) { - asm volatile ( - "mov $0x2, %%eax\n" - "int $0x32" - : : "ebx" (ch) : "eax"); -} - -static inline void vga_printsz(uint8_t *sz) { - asm volatile ( - "mov $0x3, %%eax\n" - "int $0x32" - : : "ebx" (sz) : "eax"); -} - -static inline void vga_printsn(uint8_t *sn, uint8_t length) { - asm volatile ( - "mov $0x4, %%eax\n" - "int $0x32" - : : "ebx" (sn), "ecx" (length) : "eax"); -} - -static inline fs_handle fs_open(uint8_t *path) { - fs_handle handle; - asm volatile ( - "mov $0x5, %%eax\n" - "int $0x32" - : "eax" (handle) : "ebx" (path)); - return handle; -} - -static inline fs_handle fs_open_root() { - fs_handle handle; - asm volatile ( - "mov $0x6, %%eax\n" - "int $0x32" - : "eax" (handle)); - return handle; -} - -static inline fs_handle fs_new(uint8_t *path) { - fs_handle handle; - asm volatile ( - "mov $0x7, %%eax\n" - "int $0x32" - : "eax" (handle) : "ebx" (path)); - return handle; -} - -static inline void fs_close(fs_handle handle) { - asm volatile ( - "mov $0x8, %%eax\n" - "int $0x32" - : : "ebx" (handle) : "eax"); -} - -static inline void fs_delete(uint8_t *path) { - asm volatile ( - "mov $0x9, %%eax\n" - "int $0x32" - : : "ebx" (path) : "eax"); -} - -static inline bool fs_exists(uint8_t *path) { - bool does; - asm volatile ( - "mov $0xa, %%eax\n" - "int $0x32" - : "eax" (does) : "ebx" (path)); - return does; -} - -static inline int32_t fs_seek(fs_handle handle, int32_t by) { - int32_t seeked_by; - asm volatile ( - "mov $0xb, %%eax\n" - "int $0x32" - : "eax" (seeked_by) : "ebx" (handle), "ecx" (by)); - return seeked_by; -} - -static inline uint32_t fs_tell(fs_handle handle) { - uint32_t position; - asm volatile ( - "mov $0xc, %%eax\n" - "int $0x32" - : "eax" (position) : "ebx" (handle)); - return position; -} - -static inline uint32_t fs_read(fs_handle handle, uint32_t max, void *buffer) { - uint32_t read; - asm volatile ( - "mov %0xd, %%eax\n" - "int $0x32" - : "eax" (read) : "ebx" (handle), "ecx" (max), "edx" (buffer) : "memory"); - return read; -} - -static inline uint32_t fs_write(fs_handle handle, uint32_t max, void *buffer) { - uint32_t written; - asm volatile ( - "mov %0xe, %%eax\n" - "int $0x32" - : "eax" (written) : "ebx" (handle), "ecx" (max), "edx" (buffer)); - return written; -} - -static inline task_handle plef_run(uint8_t *image_path) { - task_handle handle; - asm volatile ( - "mov %0xf, %%eax\n" - "int $0x32" - : "eax" (handle) : "ebx" (image_path)); - return handle; -} - -#endif diff --git a/src/user/include/pland/syscall.h b/src/user/include/pland/syscall.h new file mode 100644 index 0000000..01f7151 --- /dev/null +++ b/src/user/include/pland/syscall.h @@ -0,0 +1,139 @@ +#ifndef PLAND_SYSCALL_H +#define PLAND_SYSCALL_H + +#include + +typedef uint32_t _file_handle_t; +typedef uint32_t _task_handle_t; +typedef uint32_t _drive_number_t; +typedef enum { + _KEY_BACKSPACE = '\b', + _KEY_RETURN = '\n', + //etc. + + _KEY_LSHIFT = 0x00000100, + _KEY_RSHIFT = 0x00000200, + _KEY_CAPS = 0x00000400, + _KEY_INSERT = 0x00000800, + _KEY_NUM = 0x00001000, + _KEY_SCROLL = 0x00002000, + _KEY_LALT = 0x00004000, + _KEY_RALT = 0x00008000, + _KEY_LCTRL = 0x00010000, + _KEY_RCTRL = 0x00020000, + _KEY_LMETA = 0x00040000, + _KEY_RMETA = 0x00080000, + + _KEY_SHIFT = 0x00000300, + _KEY_SCAPS = 0x00000700, + _KEY_ALT = 0x0000c000, + _KEY_CTRL = 0x00030000, + _KEY_META = 0x000c0000, +} _key_code_t; + +enum _scn { + _SCN_OPEN_FILE, + _SCN_CLOSE_FILE, + _SCN_FILE_READ, + _SCN_FILE_SIZE, + _SCN_START_TASK, + _SCN_LOG_STRING, + _SCN_GET_KEY, + _SCN_ALLOCATE_RAM +}; + +static inline uint32_t _sc0(enum _scn eax) { + volatile uint32_t out; + asm ( + "int $0x30" + : "=a" (out) : "a" (eax) : "ecx", "edx"); + return out; +} + +static inline uint32_t _sc1(enum _scn eax, uint32_t ebx) { + volatile uint32_t out; + asm ( + "int $0x30" + : "=a" (out) : "a" (eax), "b" (ebx) : "ecx", "edx"); + return out; +} + +static inline uint32_t _sc2(enum _scn eax, uint32_t ebx, uint32_t ecx) { + volatile uint32_t out; + asm ( + "int $0x30" + : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx) : "edx"); + return out; +} + +static inline uint32_t _sc3(enum _scn eax, uint32_t ebx, uint32_t ecx, uint32_t edx) { + volatile uint32_t out; + asm ( + "int $0x30" + : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx)); + return out; +} + +static inline uint32_t _sc4(enum _scn eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi) { + volatile uint32_t out; + asm ( + "int $0x30" + : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi)); + return out; +} + +static inline uint32_t _sc5(enum _scn eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi) { + volatile uint32_t out; + asm ( + "int $0x30" + : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi)); + return out; +} + +static inline void _yield_task() { + asm ( + "int $0x39" + ); +} + +__attribute__ ((noreturn)) +static inline void _exit_task() { + asm ( + "int $0x38" + ); + __builtin_unreachable(); +} + +static inline _file_handle_t _open_file(_drive_number_t drive_number, const char *path) { + return _sc2(_SCN_OPEN_FILE, drive_number, (uint32_t)path); +} + +static inline void _close_file(_file_handle_t handle) { + _sc1(_SCN_CLOSE_FILE, handle); +} + +static inline uint32_t _file_read(_file_handle_t handle, uint32_t file_offset, uint32_t count, void *buffer) { + return _sc4(_SCN_FILE_READ, handle, file_offset, count, (uint32_t)buffer); +} + +static inline uint32_t _file_size(_file_handle_t handle) { + return _sc1(_SCN_FILE_SIZE, handle); +} + +static inline void _start_task(_drive_number_t drive_number, char *path) { + _sc2(_SCN_START_TASK, drive_number, (uint32_t)path); +} + +static inline void _log_string(const char *sz) { + _sc1(_SCN_LOG_STRING, (uint32_t)sz); +} + +static inline _key_code_t _get_key() { + return _sc0(_SCN_GET_KEY); +} + +static inline void *_allocate_ram(uint32_t pages) { + return (void *)_sc1(_SCN_ALLOCATE_RAM, pages); +} + +#endif \ No newline at end of file diff --git a/src/user/init/main.c b/src/user/init/main.c index 002fd92..9a411e7 100644 --- a/src/user/init/main.c +++ b/src/user/init/main.c @@ -1,16 +1,29 @@ -#include -#include +#include +#include +#include void main() { - fs_handle f = fs_open("sys/startup.rc"); + tell_user_sz("\n\nThis is a userland program.\n"); + + tell_user_sz("Opening sd0:TEST.TXT.\n"); + struct file *f = open_file("sd0:TEST.TXT"); if (!f) { - vga_printsz("Couldn't open sys/startup.rc\n"); + tell_user_sz("Failed to open.\n"); return; } - uint8_t line_buffer[128]; - while (read_line(f, 128, line_buffer)) - plef_run(line_buffer); + tell_user_sz("Length: "); + uint32_t size = file_size(f); + tell_user_n(size); + tell_user_sz(" bytes\n\nContents:\n"); + + char *buf = get_block(size + 1); + read_from_file(f, size, buf); + buf[size] = '\0'; + + close_file(f); + + tell_user_sz(buf); - fs_close(f); + tell_user_sz("\n\nGoodbye!\n"); } diff --git a/src/user/knob/entry.asm b/src/user/knob/entry.asm new file mode 100644 index 0000000..ae024c4 --- /dev/null +++ b/src/user/knob/entry.asm @@ -0,0 +1,23 @@ +bits 32 + +global _entry + +extern main +extern quit + +extern current_disk + +section .text +_entry: + mov esp, stack + + ;TODO: heap stuff? + ;TODO: determine current_disk + ;any further needed initialization + + push quit + jmp main + +section .stack nobits alloc noexec write align=16 +resb 1024 +stack: \ No newline at end of file diff --git a/src/user/knob/env.c b/src/user/knob/env.c new file mode 100644 index 0000000..952ab86 --- /dev/null +++ b/src/user/knob/env.c @@ -0,0 +1,3 @@ +#include + +uint32_t current_drive; \ No newline at end of file diff --git a/src/user/knob/file.c b/src/user/knob/file.c new file mode 100644 index 0000000..8ab7acd --- /dev/null +++ b/src/user/knob/file.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +struct file { + _file_handle_t handle; + uint32_t position; + uint32_t length; +}; + +static const char *try_remove_prefix(const char *path, uint8_t *dn_out) { + if ((path[0] != 's') || (path[1] != 'd')) + return 0; + + const char *num_part = path + 2; + for (uint32_t i = 0; num_part[i]; ++i) + if (num_part[i] == ':') { + + uint32_t dn_large; + if (!try_sntoi(num_part, i, &dn_large) || dn_large > 255) + return 0; + + *dn_out = (uint8_t)dn_large; + return num_part + i + 1; + } + + return 0; +} + +struct file *open_file(const char *path) { + uint8_t dn; + const char *path_part = try_remove_prefix(path, &dn); + if (path_part) + path = path_part; + else + dn = current_drive; + + _file_handle_t h = _open_file(dn, path); + if (!h) + return 0; + + struct file *f = get_block(sizeof(struct file)); + f->handle = h; + f->position = 0; + f->length = _file_size(h); + + return f; +} + +void close_file(struct file *f) { + _close_file(f->handle); + free_block(f); +} + +uint32_t read_from_file(struct file *f, uint32_t max, void *buf) { + if (f->position + max > f->length) + max = f->length - f->position; + + uint32_t read = _file_read(f->handle, f->position, max, buf); + + f->position += read; + return read; +} + +uint32_t seek_file_to(struct file *f, uint32_t to) { + if (to > f->length) + to = f->length; + return f->position = to; +} + +int32_t seek_file_by(struct file *f, int32_t by) { + uint32_t old = f->position; + uint32_t to = old + by > f->length ? f->length : old + by; + f->position = to; + return to - old; +} + +__attribute__ ((pure)) +uint32_t file_size(struct file *f) { + return f->length; +} \ No newline at end of file diff --git a/src/user/knob/format.c b/src/user/knob/format.c new file mode 100644 index 0000000..f55e857 --- /dev/null +++ b/src/user/knob/format.c @@ -0,0 +1,13 @@ +#include +#include + +bool try_sntoi(const char *s, uint32_t n, uint32_t *out) { + uint32_t calc = 0; + for (uint32_t i = 0; i < n; ++i) { + if ((s[i] < '0') || (s[i] > '9')) + return false; + calc = calc * 10 + s[i] - '0'; + } + *out = calc; + return true; +} \ No newline at end of file diff --git a/src/user/knob/heap.c b/src/user/knob/heap.c new file mode 100644 index 0000000..ec21129 --- /dev/null +++ b/src/user/knob/heap.c @@ -0,0 +1,112 @@ +#include +#include +#include + +//structure appears at the start of each block +struct block_header { + struct block_header *next; + struct block_header *prev; + + //does not include header + uint32_t length; + bool allocated; +}; + +static struct block_header *first_block = 0; + +static void add_header(struct block_header *bh) { + bh->next = first_block; + bh->prev = 0; + if (first_block) + first_block->prev = bh; + first_block = bh; +} + +__attribute__ ((malloc)) +void *get_block(uint32_t bytes) { + struct block_header *header = 0; + + for (struct block_header *ptr = first_block; ptr; ptr = ptr->next) { + if (ptr->allocated) + continue; + if (ptr->length == bytes) { + header = ptr; + break; + } + if (ptr->length > bytes + sizeof(struct block_header)) { + struct block_header *bh = (void *)ptr + sizeof(struct block_header) + bytes; + + bh->length = ptr->length - sizeof(struct block_header) - bytes; + bh->allocated = false; + add_header(bh); + + ptr->length = bytes; + header = ptr; + break; + } + } + + if (!header) { + uint32_t size_with_header = bytes + sizeof(struct block_header); + if (!(size_with_header % 4096)) { + header = _allocate_ram(size_with_header / 4096); + if (!header) + return 0; + header->length = bytes; + add_header(header); + } + else { + uint32_t pages = (bytes + sizeof(struct block_header) * 2) / 4096 + 1; + header = _allocate_ram(pages); + if (!header) + return 0; + header->length = bytes; + add_header(header); + + struct block_header *bh = (void *)header + sizeof(struct block_header) + bytes; + bh->length = pages * 4096 - bytes - 2 * sizeof(struct block_header); + bh->allocated = false; + add_header(bh); + } + } + + header->allocated = true; + return (void *)header + sizeof(struct block_header); +} + +static void remove_header(struct block_header *bh) { + if (bh == first_block) + first_block = bh->next; + if (bh->prev) + bh->prev->next = bh->next; + if (bh->next) + bh->next->prev = bh->prev; +} + +void free_block(void *block) { + struct block_header *header = block - sizeof(struct block_header); + + header->allocated = false; + + void *after = block + header->length; + for (struct block_header *ptr = first_block; ptr; ptr = ptr->next) { + if (ptr == after) { + if (!ptr->allocated) { + header->length += ptr->length + sizeof(struct block_header); + remove_header(ptr); + } + break; + } + } + + //we traverse the linked list twice. it would probably be more efficient + //to do both finding the after and finding the before in the same loop. + for (struct block_header *ptr = first_block; ptr; ptr = ptr->next) + if ((void *)ptr + sizeof(struct block_header) + ptr->length == header) { + if (!ptr->allocated) { + ptr->length += sizeof(struct block_header) + header->length; + remove_header(header); + } + break; + } +} \ No newline at end of file diff --git a/src/user/knob/quit.c b/src/user/knob/quit.c new file mode 100644 index 0000000..a2ef5aa --- /dev/null +++ b/src/user/knob/quit.c @@ -0,0 +1,29 @@ +#include +#include + +struct quit_list_node { + struct quit_list_node *prev; + void (*f)(); +}; + +static struct quit_list_node head = { + .f = &_exit_task +}; + +static struct quit_list_node *last = &head; + +void on_quit(void (*run_f)()) { + struct quit_list_node *new = get_block(sizeof(struct quit_list_node)); + new->prev = last; + new->f = run_f; + last = new; +} + +__attribute__ ((noreturn)) +void quit() { + struct quit_list_node *node = last; + while (1) { + node->f(); + node = node->prev; + } +} \ No newline at end of file diff --git a/src/user/knob/user.c b/src/user/knob/user.c new file mode 100644 index 0000000..fb39851 --- /dev/null +++ b/src/user/knob/user.c @@ -0,0 +1,134 @@ +#include +#include +#include + +static const uint8_t caps_and_shift[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x22, + 0x28, 0x29, 0x2a, 0x2b, 0x3c, 0x5f, 0x3e, 0x3f, + 0x29, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5e, 0x26, + 0x2a, 0x28, 0x3a, 0x3a, 0x3c, 0x2d, 0x3e, 0x3f, + + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x57, 0x7c, 0x7d, 0x5e, 0x5f, + 0x7e, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00 + + //TODO: higher +}; + +static const uint8_t caps_no_shift[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00 + + //TODO: higher +}; + +static const uint8_t shifted[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x22, + 0x28, 0x29, 0x2a, 0x2b, 0x3c, 0x5f, 0x3e, 0x3f, + 0x29, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5e, 0x26, + 0x2a, 0x28, 0x3a, 0x3a, 0x3c, 0x2d, 0x3e, 0x3f, + + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x57, 0x7c, 0x7d, 0x5e, 0x5f, + 0x7e, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00 + + //TODO: higher +}; + +static char get_key_char() { + _key_code_t key; + while (!(key = _get_key())) + _yield_task(); + return (char)( + (key & (_KEY_ALT | _KEY_CTRL | _KEY_META)) + ? 0 + : (key & _KEY_CAPS) + ? (key & _KEY_SHIFT) + ? caps_and_shift[key & 0xff] + : caps_no_shift[key & 0xff] + : (key & _KEY_SHIFT) + ? shifted[key & 0xff] + : (key & 0x80) + ? 0 + : (key & 0x7f) + ); +} + +void tell_user_sz(const char *sz) { + _log_string(sz); +} + +void tell_user_n(uint32_t n) { + char buf[11]; + char *buf_ptr = buf; + bool zero_yet = false; + for (uint32_t d = 1000000000U; d; d /= 10) { + uint8_t v = (n / d ) % 10; + if (v || zero_yet) { + zero_yet = true; + *buf_ptr++ = v | '0'; + } + } + if (buf_ptr == buf) + *buf_ptr++ = '0'; + *buf_ptr = '\0'; + tell_user_sz(buf); +} + +//return value and max_length don't include null terminator +uint32_t ask_user_line_sz(char *sz, uint32_t max_length) { + char log_buf[2]; + log_buf[1] = '\0'; + + uint32_t i; + for (i = 0; i != max_length; ++i) { + char key = get_key_char(); + if (key) { + log_buf[0] = key; + _log_string(log_buf); + + if (key == '\b') + i -= i ? 2 : 1; + else if (key == '\n') + break; + else + sz[i] = key; + } + } + + sz[i] = '\0'; + return i; +} \ No newline at end of file diff --git a/src/user/libcanyo/file.c b/src/user/libcanyo/file.c deleted file mode 100644 index d314887..0000000 --- a/src/user/libcanyo/file.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -//max_length and return value include null-terminator -uint32_t read_line(fs_handle handle, uint32_t max_length, void *buffer) { - int index = 0; - - while (++index < max_length) { - if (!fs_read(handle, 1, buffer + index - 1) || (*(uint8_t *)(buffer + index - 1) == '\n')) - break; - } - - *(uint8_t *)(buffer + index - 1) = '\0'; - return index; -} \ No newline at end of file -- cgit v1.2.3