]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Implemented basic read/write of a virtual file corresponding to the serial port
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Mon, 27 Nov 2023 13:09:52 +0000 (14:09 +0100)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Mon, 27 Nov 2023 13:09:52 +0000 (14:09 +0100)
kernel/src/ring3.cpp
test_module/src/test.cpp

index 03813588d9632a82f81ccfc5b2126dae2a2230b4..cc2d24fcc556adbca20839f0179e99696496b023 100644 (file)
@@ -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) {
index 1be50eed681fb88c62654b8accaeb1af5a9539c3..0f6f836258c1006448d317f9df116d706bdc2646 100644 (file)
 #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);
        }
 }