From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Mon, 27 Nov 2023 13:09:52 +0000 (+0100)
Subject: Implemented basic read/write of a virtual file corresponding to the serial port
X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=91c7a4439b22f548e63ef11fa37ff64a8479013f;p=voyage-au-centre-des-fichiers.git

Implemented basic read/write of a virtual file corresponding to the serial port
---

diff --git a/kernel/src/ring3.cpp b/kernel/src/ring3.cpp
index 0381358..cc2d24f 100644
--- a/kernel/src/ring3.cpp
+++ b/kernel/src/ring3.cpp
@@ -55,16 +55,40 @@ extern "C" std::int64_t syscall_open(std::int64_t, char const*, std::int64_t, in
 	__builtin_unreachable();
 }
 
-// extern "C" std::int64_t syscall_read(std::int64_t file, char* data, std::int64_t len) {
-extern "C" std::int64_t syscall_read(std::int64_t, char*, std::int64_t) {
-	os::assert(false, "read not implemented yet.");
-	__builtin_unreachable();
+extern "C" std::int64_t syscall_read(std::int64_t file, char* data, std::int64_t len) {
+	os::assert(file == 0, "Read isn't really implemented for now.");
+	os::assert(len >= 0, "Read expects a positive size.");
+	if (len == 0) {
+		return 0;
+	}
+	for (std::int64_t i = 0; i < len; i++) {
+		if (i == 0) {
+			data[i] = os::read_serial();
+			continue;
+		}
+		if (os::serial_received()) {
+			data[i] = os::read_serial();
+		} else {
+			return i;
+		}
+	}
+	return len;
 }
 
-// extern "C" std::int64_t syscall_write(std::int64_t file, char const* data, std::int64_t len) {
-extern "C" std::int64_t syscall_write(std::int64_t, char const*, std::int64_t) {
-	os::assert(false, "write not implemented yet.");
-	__builtin_unreachable();
+extern "C" std::int64_t syscall_write(std::int64_t file, char const* data, std::int64_t len) {
+	os::assert(file == 0, "Write isn't really implemented for now.");
+	os::assert(len >= 0, "Write expects a positive size.");
+	if (len == 0) {
+		return 0;
+	}
+	for (std::int64_t i = 0; i < len; i++) {
+		if (os::serial_transmit_empty()) {
+			os::write_serial(data[i]);
+		} else {
+			return i;
+		}
+	}
+	return len;
 }
 
 // extern "C" std::int64_t syscall_fseek(std::int64_t file, std::int64_t offset, int from) {
diff --git a/test_module/src/test.cpp b/test_module/src/test.cpp
index 1be50ee..0f6f836 100644
--- a/test_module/src/test.cpp
+++ b/test_module/src/test.cpp
@@ -14,13 +14,44 @@
 #include <stddef.h>
 #include <stdint.h>
 
-extern "C" int64_t open(int64_t wd, char const* path, int64_t path_len, int options);
+enum open_options {
+	MODE_RO = 0x1,
+	MODE_WO = 0x2,
+	MODE_RW = MODE_RO | MODE_WO,
+	MODE_DIRECTORY = 0x4,
+	OPTION_INPLACE = 0x8,
+};
+
+namespace sys {
+extern "C" int64_t open(int64_t wd, char const* path, int64_t path_len, open_options options);
 extern "C" int64_t read(int64_t file, char* data, int64_t len);
 extern "C" int64_t write(int64_t file, char const* data, int64_t len);
 extern "C" int64_t fseek(int64_t file, int64_t offset, int from);
+} // namespace sys
 
 extern "C" void _start() {
+	const int64_t fd = 0; // sys::open(0, "/serial", 7, sys::MODE_RW | sys::OPTION_INPLACE);
+
+	sys::write(fd, "Entrez votre nom: ", 18);
+	char data[32];
+	int64_t read = 0;
+	int64_t read_this_time;
+	do {
+		read_this_time = sys::read(fd, data + read, 1);
+		if (read_this_time) {
+			if (data[read] == '\r') {
+				sys::write(fd, "\n", 1);
+			} else {
+				sys::write(fd, data + read, 1);
+			}
+		}
+		read += read_this_time;
+	} while (read_this_time != 0 && read < 32 && data[read - 1] != '\r');
+	sys::write(fd, "Bonjour, ", 9);
+	sys::write(fd, data, read - (data[read - 1] == '\r' ? 1 : 0));
+	sys::write(fd, ".\n", 2);
+
 	while (true) {
-		fseek(0, 0, 1);
+		sys::fseek(0, 0, 1);
 	}
 }