aboutsummaryrefslogtreecommitdiff
path: root/libk
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2021-02-12 17:55:31 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2021-02-12 17:55:31 +0200
commit46a5f1ee4db70a037af5038deaa1d64ad0f7862c (patch)
tree0250b88f2eb69a7a0987a78d23a06e2125df74a2 /libk
parentAdd comments to explain CGA ports better (diff)
downloadkernel.cpp-46a5f1ee4db70a037af5038deaa1d64ad0f7862c.tar.xz
Add serial0 console output
Diffstat (limited to 'libk')
-rw-r--r--libk/stdlib.h37
-rw-r--r--libk/stdlib/console.cc12
2 files changed, 35 insertions, 14 deletions
diff --git a/libk/stdlib.h b/libk/stdlib.h
index a336586..7213384 100644
--- a/libk/stdlib.h
+++ b/libk/stdlib.h
@@ -2,15 +2,32 @@
#include <string.h>
+template <typename T, bool reverse = false>
+class Iterator {
+public:
+ T next() { return buffer[reverse ? pos-- : pos++]; }
+ operator bool() const { return reverse ? (pos >= 0) : (pos < length); }
+
+ explicit Iterator(T* b, size_t l) : buffer(b), length(l) {
+ if (reverse)
+ pos = length - 1;
+ };
+
+private:
+ ssize_t pos = 0;
+ T* const buffer;
+ const size_t length;
+};
+
class Console {
- public:
- virtual ~Console() = default;
- virtual void write(char c) = 0;
- virtual void write(ViewIterator& msg) = 0;
- virtual void update_cursor() = 0;
-
- static void set(Console* ptr);
- static Console* get();
+public:
+ virtual ~Console() = default;
+ virtual void write(char c) = 0;
+ virtual void write(ViewIterator& msg) = 0;
+ virtual void update_cursor() = 0;
+
+ static void set(Console* ptr);
+ static Iterator<Console*> begin();
};
template <typename T>
@@ -37,7 +54,9 @@ void print_c(Console* console, const T& a, const Args&... x) {
template <typename T, typename... Args>
void printk(const T& a, const Args&... x) {
- if (auto* c = Console::get()) {
+ auto iter = Console::begin();
+ while (iter) {
+ auto* c = iter.next();
print_c(c, a, x...);
c->update_cursor();
}
diff --git a/libk/stdlib/console.cc b/libk/stdlib/console.cc
index 308f055..a234e6b 100644
--- a/libk/stdlib/console.cc
+++ b/libk/stdlib/console.cc
@@ -1,15 +1,17 @@
#include "stdlib.h"
-static Console* global_console = nullptr;
+constexpr size_t max_consoles = 4;
+static Console* global_console[max_consoles] = {nullptr};
+static size_t last_console = 0;
void Console::set(Console* ptr) {
- if (ptr != nullptr) {
- global_console = ptr;
+ if (ptr != nullptr && last_console < max_consoles) {
+ global_console[last_console++] = ptr;
}
}
-Console* Console::get() {
- return global_console;
+Iterator<Console*> Console::begin() {
+ return Iterator<Console*>(global_console, last_console);
}
void abort() {