Skip to content

Commit

Permalink
Files partially implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
djordje200179 committed Aug 25, 2024
1 parent 5eda9b8 commit 72719c0
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 30 deletions.
5 changes: 4 additions & 1 deletion Niski.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
"thread_local.h": "c",
"stdint.h": "c",
"signal.h": "c",
"threads.h": "c"
"threads.h": "c",
"stdio.h": "c",
"syscall_codes.h": "c",
"io.h": "c"
}
}
}
12 changes: 12 additions & 0 deletions Software/h/common/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <stddef.h>

enum __file {
__FILE_LCD,
__FILE_KEYBOARD,
__FILE_UART
};

size_t __io_write(enum __file file, const char* buffer, size_t size);
size_t __io_read(enum __file file, char* buffer, size_t size);
5 changes: 4 additions & 1 deletion Software/h/common/syscall_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@
#define __SYSCALL_THREAD_STORAGE_SET 0x44

#define __SYSCALL_SIGNAL_SET_HANDLER 0x51
#define __SYSCALL_SIGNAL_RAISE 0x52
#define __SYSCALL_SIGNAL_RAISE 0x52

#define __SYSCALL_FILE_WRITE 0x61
#define __SYSCALL_FILE_READ 0x62
6 changes: 5 additions & 1 deletion Software/h/common/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdbool.h>
#include "common/threads.h"
#include "common/signals.h"
#include "common/io.h"

void* __memory_allocate(size_t size);
void __memory_free(void* ptr);
Expand Down Expand Up @@ -33,4 +34,7 @@ void* __thread_storage_get(struct __thread_storage* ts);
enum __thread_status __thread_storage_set(struct __thread_storage* ts, void* data);

void (*__signal_set_handler(enum __signal sig, void (*handler)(enum __signal)))(enum __signal);
void __signal_raise(enum __signal sig);
void __signal_raise(enum __signal sig);

size_t __file_write(enum __file file, const char* buffer, size_t size);
size_t __file_read(enum __file file, char* buffer, size_t size);
12 changes: 12 additions & 0 deletions Software/h/kernel/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <stddef.h>
#include "common/io.h"

struct kfile {
size_t (*write)(struct kfile* file, const char* buffer, size_t size);
size_t (*read)(struct kfile* file, char* buffer, size_t size);
};

size_t kfile_write(enum __file id, const char* buffer, size_t size);
size_t kfile_read(enum __file id, char* buffer, size_t size);
47 changes: 45 additions & 2 deletions Software/h/stdio.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
#pragma once

#include "common/io.h"
#include "common/syscalls.h"

#define EOF (-1)

int putchar(int ch);
int puts(const char* str);
typedef struct FILE FILE;

#define stdin ((FILE*)__FILE_KEYBOARD)
#define stdout ((FILE*)__FILE_LCD)
#define stderr ((FILE*)__FILE_LCD)

static inline int fputs(const char* str, FILE* restrict stream) {
size_t len = 0;
while (str[len] != '\0') len++;

size_t chars = __file_write((enum __file)stream, str, len);
if (chars != len) {
// TODO: set ferror
return EOF;
}

return chars;
}

static inline int fputc(int ch, FILE* stream) {
char c = ch;
size_t res = __file_write((enum __file)stream, &c, 1);
if (res != 1)
return EOF;

return ch;
}

#define putc(ch, stream) fputc(ch, stream)

static inline int puts(const char* str) {
int res = fputs(str, stdout);
if (res == EOF)
return EOF;

if (fputc('\n', stdout) == EOF)
return EOF;

return res + 1;
}

static inline int putchar(int ch) { return fputc(ch, stdout); }
11 changes: 11 additions & 0 deletions Software/src/common/syscalls.S
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.global __condition_create, __condition_wait, __condition_signal, __con_signal_all, __condition_destroy
.global __thread_storage_create, __thread_storage_destroy, __thread_storage_get, __thread_storage_set
.global __signal_set_handler, __signal_raise
.global __file_write, __file_read

#include "common/syscall_codes.h"

Expand Down Expand Up @@ -126,4 +127,14 @@ __signal_set_handler:
__signal_raise:
li a5, __SYSCALL_SIGNAL_RAISE
ecall
ret

__file_write:
li a5, __SYSCALL_FILE_WRITE
ecall
ret

__file_read:
li a5, __SYSCALL_FILE_READ
ecall
ret
22 changes: 22 additions & 0 deletions Software/src/kernel/exceptions/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "kernel/sync/thread_local.h"
#include "kernel/sync/scheduler.h"
#include "kernel/signals.h"
#include "kernel/io.h"
#include "common/syscall_codes.h"

#define GET_PARAM(index, type) (type)(kthread_current->context.regs[REG_A ## index])
Expand Down Expand Up @@ -310,6 +311,24 @@ static void syscall_sig_raise() {
SET_RET_VALUE(0);
}

static void syscall_file_write() {
enum __file file = GET_PARAM(0, enum __file);
const char* buffer = GET_PARAM(1, const char*);
size_t size = GET_PARAM(2, size_t);

size_t written = kfile_write(file, buffer, size);
SET_RET_VALUE(written);
}

static void syscall_file_read() {
enum __file file = GET_PARAM(0, enum __file);
char* buffer = GET_PARAM(1, char*);
size_t size = GET_PARAM(2, size_t);

size_t read = kfile_read(file, buffer, size);
SET_RET_VALUE(read);
}

static void (*syscalls[])() = {
[__SYSCALL_MEMORY_ALLOCATE] = syscall_mem_alloc,
[__SYSCALL_MEMORY_FREE] = syscall_mem_free,
Expand Down Expand Up @@ -341,6 +360,9 @@ static void (*syscalls[])() = {

[__SYSCALL_SIGNAL_SET_HANDLER] = syscall_sig_set_handler,
[__SYSCALL_SIGNAL_RAISE] = syscall_sig_raise,

[__SYSCALL_FILE_WRITE] = syscall_file_write,
[__SYSCALL_FILE_READ] = syscall_file_read,
};

void handle_syscall() {
Expand Down
17 changes: 17 additions & 0 deletions Software/src/kernel/io/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "kernel/io.h"

extern struct kfile klcd;

struct kfile* files[] = {
[__FILE_LCD] = &klcd
};

size_t kfile_write(enum __file id, const char* buffer, size_t size) {
struct kfile* file = files[id];
return file->write(file, buffer, size);
}

size_t kfile_read(enum __file id, char* buffer, size_t size) {
struct kfile* file = files[id];
return file->read(file, buffer, size);
}
35 changes: 35 additions & 0 deletions Software/src/kernel/io/lcd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "kernel/io.h"
#include "devices/lcd.h"

struct klcd {
char buffer[16][2];
int cursor_y, cursor_x;
};

size_t klcd_write(struct kfile* file, const char* buffer, size_t size) {
struct klcd* lcd = (struct klcd*)file;

for (size_t i = 0; i < size; i++) {
char ch = buffer[i];

// TODO: Fix this
switch (ch) {
case '\n':
lcd_move_cursor(1, 0);
break;
case '\r':
lcd_move_cursor(0, 0);
break;
default:
lcd_write_ch(ch);
break;
}
}

return size;
}

struct kfile klcd = {
.write = klcd_write,
.read = NULL
};
25 changes: 0 additions & 25 deletions Software/src/std/stdio/stdio_functions.c

This file was deleted.

0 comments on commit 72719c0

Please sign in to comment.