os::color os::operator*(color c1, color c2) {
return {
- .r = uint8_t(int(c1.r) * int(c2.r) / 256),
- .g = uint8_t(int(c1.g) * int(c2.g) / 256),
- .b = uint8_t(int(c1.b) * int(c2.b) / 256),
+ .r = std::uint8_t(int(c1.r) * int(c2.r) / 256),
+ .g = std::uint8_t(int(c1.g) * int(c2.g) / 256),
+ .b = std::uint8_t(int(c1.b) * int(c2.b) / 256),
};
}
-os::framebuffer::framebuffer(uint64_t addr, uint32_t pitch, uint32_t width, uint32_t height, uint8_t bpp, uint8_t type, const uint8_t* color_info) {
- address = phys_ptr<volatile uint8_t>(addr);
+os::framebuffer::framebuffer(std::uint64_t addr, std::uint32_t pitch, std::uint32_t width, std::uint32_t height, std::uint8_t bpp, std::uint8_t type, const std::uint8_t* color_info) {
+ address = phys_ptr<volatile std::uint8_t>(addr);
this->pitch = pitch;
this->width = width;
this->height = height;
b_pos = color_info[4];
b_mask = (1 << color_info[5]) - 1;
}
-void os::framebuffer::putpixel(size_t x, size_t y, color c) {
- *reinterpret_cast<volatile uint32_t*>(&address[pitch * y + x * bpp / 8]) = color_to_data(c);
+void os::framebuffer::putpixel(std::size_t x, std::size_t y, color c) {
+ *reinterpret_cast<volatile std::uint32_t*>(&address[pitch * y + x * bpp / 8]) = color_to_data(c);
}
void os::framebuffer::clear(color c) {
- for (size_t x = 0; x < width; x++) {
- for (size_t y = 0; y < height; y++) {
+ for (std::size_t x = 0; x < width; x++) {
+ for (std::size_t y = 0; y < height; y++) {
putpixel(x, y, c);
}
}
}
-size_t os::framebuffer::get_width() const {
+std::size_t os::framebuffer::get_width() const {
return width;
}
-size_t os::framebuffer::get_height() const {
+std::size_t os::framebuffer::get_height() const {
return height;
}
-uint32_t os::framebuffer::color_to_data(color c) const {
-return ((uint32_t(c.r) & r_mask) << r_pos) | ((uint32_t(c.g) & g_mask) << g_pos) | ((uint32_t(c.b) & b_mask) << b_pos);
+std::uint32_t os::framebuffer::color_to_data(color c) const {
+return ((std::uint32_t(c.r) & r_mask) << r_pos) | ((std::uint32_t(c.g) & g_mask) << g_pos) | ((std::uint32_t(c.b) & b_mask) << b_pos);
}
#pragma once
-#include <stdint.h>
+#include <cstdint>
#include "utils.hpp"
namespace os {
struct color {
- uint8_t r;
- uint8_t g;
- uint8_t b;
+ std::uint8_t r;
+ std::uint8_t g;
+ std::uint8_t b;
};
color operator*(color c1, color c2);
class framebuffer {
public:
framebuffer() = default;
- framebuffer(uint64_t addr, uint32_t pitch, uint32_t width, uint32_t height, uint8_t bpp, uint8_t type, const uint8_t* color_info);
- void putpixel(size_t x, size_t y, color c);
+ framebuffer(std::uint64_t addr, std::uint32_t pitch, std::uint32_t width, std::uint32_t height, std::uint8_t bpp, std::uint8_t type, const std::uint8_t* color_info);
+ void putpixel(std::size_t x, std::size_t y, color c);
void clear(color c);
- size_t get_width() const;
- size_t get_height() const;
+ std::size_t get_width() const;
+ std::size_t get_height() const;
private:
- uint32_t color_to_data(color c) const;
+ std::uint32_t color_to_data(color c) const;
- phys_ptr<volatile uint8_t> address = nullptr;
- size_t pitch;
- size_t width;
- size_t height;
- uint8_t bpp;
- uint8_t r_pos;
- uint8_t r_mask;
- uint8_t g_pos;
- uint8_t g_mask;
- uint8_t b_pos;
- uint8_t b_mask;
+ phys_ptr<volatile std::uint8_t> address = nullptr;
+ std::size_t pitch;
+ std::size_t width;
+ std::size_t height;
+ std::uint8_t bpp;
+ std::uint8_t r_pos;
+ std::uint8_t r_mask;
+ std::uint8_t g_pos;
+ std::uint8_t g_mask;
+ std::uint8_t b_pos;
+ std::uint8_t b_mask;
};
}
#pragma once
#ifdef __cplusplus
-# include <stdint.h>
+# include <cstdint>
# include "utils.hpp"
namespace multiboot2 {
#endif // __cplusplus
#ifdef __cplusplus
- constexpr uint32_t magic = 0xE85250D6;
+ constexpr std::uint32_t magic = 0xE85250D6;
#else
# define multiboot2_magic 0xE85250D6
#endif // __cplusplus
#ifdef __cplusplus
- constexpr uint32_t arch_i386_32bit = 0;
- constexpr uint32_t arch_mips_32bit = 4;
+ constexpr std::uint32_t arch_i386_32bit = 0;
+ constexpr std::uint32_t arch_mips_32bit = 4;
#else
# define multiboot2_arch_i386_32bit 0
# define multiboot2_arch_mips_32bit 4
#endif // __cplusplus
#ifdef __cplusplus
- constexpr uint32_t checksum(uint32_t arch, uint32_t length) { return -(magic + arch + length); }
+ constexpr std::uint32_t checksum(std::uint32_t arch, std::uint32_t length) { return -(magic + arch + length); }
#else
# define multiboot2_checksum(arch, length) -(multiboot2_magic + (arch) + (length))
#endif // __cplusplus
#ifdef __cplusplus
struct __attribute__((packed)) info_start {
- uint32_t total_size;
- uint32_t reserved;
+ std::uint32_t total_size;
+ std::uint32_t reserved;
};
struct __attribute__((packed)) info {
- enum class type_t : uint32_t {
+ enum class type_t : std::uint32_t {
end = 0,
basic_memory_info = 4,
bios_boot_device = 5,
image_load_base_physical_address = 21,
};
type_t type;
- uint32_t size;
- uint8_t rest[];
+ std::uint32_t size;
+ std::uint8_t rest[];
};
inline os::phys_ptr<const info> next(os::phys_ptr<const info_start> ptr) {
return os::phys_ptr<const info>((ptr.get_phys_addr() + ptr->size + 7) / 8 * 8); // + 7) / 8 * 8 is required for alignment.
}
- uint64_t framebuffer_addr(os::phys_ptr<const info> ptr) {
- return *reinterpret_cast<const uint64_t*>(&ptr->rest[0]);
+ std::uint64_t framebuffer_addr(os::phys_ptr<const info> ptr) {
+ return *reinterpret_cast<const std::uint64_t*>(&ptr->rest[0]);
}
- uint32_t framebuffer_pitch(os::phys_ptr<const info> ptr) {
- return *reinterpret_cast<const uint32_t*>(&ptr->rest[8]);
+ std::uint32_t framebuffer_pitch(os::phys_ptr<const info> ptr) {
+ return *reinterpret_cast<const std::uint32_t*>(&ptr->rest[8]);
}
- uint32_t framebuffer_width(os::phys_ptr<const info> ptr) {
- return *reinterpret_cast<const uint32_t*>(&ptr->rest[12]);
+ std::uint32_t framebuffer_width(os::phys_ptr<const info> ptr) {
+ return *reinterpret_cast<const std::uint32_t*>(&ptr->rest[12]);
}
- uint32_t framebuffer_height(os::phys_ptr<const info> ptr) {
- return *reinterpret_cast<const uint32_t*>(&ptr->rest[16]);
+ std::uint32_t framebuffer_height(os::phys_ptr<const info> ptr) {
+ return *reinterpret_cast<const std::uint32_t*>(&ptr->rest[16]);
}
- uint8_t framebuffer_bpp(os::phys_ptr<const info> ptr) {
- return *reinterpret_cast<const uint8_t*>(&ptr->rest[20]);
+ std::uint8_t framebuffer_bpp(os::phys_ptr<const info> ptr) {
+ return *reinterpret_cast<const std::uint8_t*>(&ptr->rest[20]);
}
- uint8_t framebuffer_type(os::phys_ptr<const info> ptr) {
- return *reinterpret_cast<const uint8_t*>(&ptr->rest[21]);
+ std::uint8_t framebuffer_type(os::phys_ptr<const info> ptr) {
+ return *reinterpret_cast<const std::uint8_t*>(&ptr->rest[21]);
}
- const uint8_t* color_info(os::phys_ptr<const info> ptr) {
- return reinterpret_cast<const uint8_t*>(&ptr->rest[24]);
+ const std::uint8_t* color_info(os::phys_ptr<const info> ptr) {
+ return reinterpret_cast<const std::uint8_t*>(&ptr->rest[24]);
}
#endif // __cplusplus
-#include <stddef.h>
+#include <cstddef>
#include "serial.hpp"
namespace {
- void outb(uint16_t port, uint8_t data) {
+ void outb(std::uint16_t port, std::uint8_t data) {
asm volatile ("outb %1,%0" : : "dN"(port), "a"(data));
}
- uint8_t inb(uint16_t port) {
- uint8_t data;
+ std::uint8_t inb(std::uint16_t port) {
+ std::uint8_t data;
asm volatile ("inb %1,%0" : "=a"(data) : "dN"(port));
return data;
}
bool os::serial_received() {
return (inb(serial_port + 5) & 0x01) != 0;
}
-uint8_t os::read_serial() {
+std::uint8_t os::read_serial() {
while (!serial_received()) {}
return inb(serial_port + 0);
}
bool os::serial_transmit_empty() {
return (inb(serial_port + 5) & 0x20) != 0;
}
-void os::write_serial(uint8_t v) {
+void os::write_serial(std::uint8_t v) {
while (!serial_transmit_empty()) {}
outb(serial_port + 0, v);
}
write_serial(c);
}
void os::print(const char* str) {
- for (size_t i = 0; str[i] != '\0'; i++) {
+ for (std::size_t i = 0; str[i] != '\0'; i++) {
os::printc(str[i]);
}
}
-void os::print(uint64_t v) {
+void os::print(std::uint64_t v) {
for (int i = 60; i >= 0; i -= 4) {
const int c = (v >> i) & 0xF;
os::printc(c < 10 ? c + '0' : c + 'A' - 10);
}
}
-void os::print(uint32_t v) {
+void os::print(std::uint32_t v) {
for (int i = 28; i >= 0; i -= 4) {
const int c = (v >> i) & 0xF;
os::printc(c < 10 ? c + '0' : c + 'A' - 10);
}
}
-void os::print(uint16_t v) {
+void os::print(std::uint16_t v) {
for (int i = 12; i >= 0; i -= 4) {
const int c = (v >> i) & 0xF;
os::printc(c < 10 ? c + '0' : c + 'A' - 10);
}
}
-void os::print(uint8_t v) {
+void os::print(std::uint8_t v) {
for (int i = 4; i >= 0; i -= 4) {
const int c = (v >> i) & 0xF;
os::printc(c < 10 ? c + '0' : c + 'A' - 10);
}
}
-void os::print(int64_t v) {
+void os::print(std::int64_t v) {
if (v < 0) {
os::printc('-');
- os::print(uint64_t(-v));
+ os::print(std::uint64_t(-v));
} else {
os::printc(' ');
- os::print(uint64_t(v));
+ os::print(std::uint64_t(v));
}
}
-void os::print(int32_t v) {
+void os::print(std::int32_t v) {
if (v < 0) {
os::printc('-');
- os::print(uint32_t(-v));
+ os::print(std::uint32_t(-v));
} else {
os::printc(' ');
- os::print(uint32_t(v));
+ os::print(std::uint32_t(v));
}
}
-void os::print(int16_t v) {
+void os::print(std::int16_t v) {
if (v < 0) {
os::printc('-');
- os::print(uint16_t(-v));
+ os::print(std::uint16_t(-v));
} else {
os::printc(' ');
- os::print(uint16_t(v));
+ os::print(std::uint16_t(v));
}
}
-void os::print(int8_t v) {
+void os::print(std::int8_t v) {
if (v < 0) {
os::printc('-');
- os::print(uint8_t(-v));
+ os::print(std::uint8_t(-v));
} else {
os::printc(' ');
- os::print(uint8_t(v));
+ os::print(std::uint8_t(v));
}
}
void os::println(const char* str) {
#pragma once
-#include <stdint.h>
+#include <cstdint>
namespace os {
-constexpr uint16_t serial_port{0x3F8};
+constexpr std::uint16_t serial_port{0x3F8};
bool init_serial_port();
bool serial_received();
-uint8_t read_serial();
+std::uint8_t read_serial();
bool serial_transmit_empty();
-void write_serial(uint8_t v);
+void write_serial(std::uint8_t v);
void printc(char c);
void print(const char* str);
-void print(uint64_t v);
-void print(uint32_t v);
-void print(uint16_t v);
-void print(uint8_t v);
-void print(int64_t v);
-void print(int32_t v);
-void print(int16_t v);
-void print(int8_t v);
+void print(std::uint64_t v);
+void print(std::uint32_t v);
+void print(std::uint16_t v);
+void print(std::uint8_t v);
+void print(std::int64_t v);
+void print(std::int32_t v);
+void print(std::int16_t v);
+void print(std::int8_t v);
void println(const char* str);
void assert(bool cond, const char* diagnostic);
#pragma once
-#include <stddef.h>
-#include <stdint.h>
+#include <cstddef>
+#include <cstdint>
#include <compare>
namespace os {
template <typename T>
class phys_ptr {
public:
- constexpr explicit phys_ptr(uintptr_t phys_addr): phys_addr(phys_addr) {}
- constexpr phys_ptr(nullptr_t): phys_addr(0) {}
+ constexpr explicit phys_ptr(std::uintptr_t phys_addr): phys_addr(phys_addr) {}
+ constexpr phys_ptr(std::nullptr_t): phys_addr(0) {}
- constexpr T& operator[](size_t i) const {
+ constexpr T& operator[](std::size_t i) const {
return get_virt_addr()[i];
}
constexpr T& operator*() const {
operator--();
return old;
}
- constexpr phys_ptr<T>& operator+=(ptrdiff_t offset) {
+ constexpr phys_ptr<T>& operator+=(std::ptrdiff_t offset) {
phys_addr += offset * sizeof(T);
return *this;
}
- constexpr phys_ptr<T>& operator-=(ptrdiff_t offset) {
+ constexpr phys_ptr<T>& operator-=(std::ptrdiff_t offset) {
phys_addr -= offset * sizeof(T);
return *this;
}
friend constexpr auto operator<=>(phys_ptr<T> a, phys_ptr<T> b) { return a.phys_addr <=> b.phys_addr; }
friend constexpr auto operator== (phys_ptr<T> a, phys_ptr<T> b) { return a.phys_addr == b.phys_addr; }
- friend constexpr phys_ptr<T> operator+(phys_ptr<T> ptr, ptrdiff_t offset) {
+ friend constexpr phys_ptr<T> operator+(phys_ptr<T> ptr, std::ptrdiff_t offset) {
return phys_ptr<T>{ptr.phys_addr + offset * sizeof(T)};
}
- friend constexpr phys_ptr<T> operator+(ptrdiff_t offset, phys_ptr<T> ptr) {
+ friend constexpr phys_ptr<T> operator+(std::ptrdiff_t offset, phys_ptr<T> ptr) {
return ptr + offset;
}
- friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, ptrdiff_t offset) {
+ friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, std::ptrdiff_t offset) {
return phys_ptr<T>{ptr.phys_addr - offset * sizeof(T)};
}
- constexpr uintptr_t get_phys_addr() const {
+ constexpr std::uintptr_t get_phys_addr() const {
return phys_addr;
}
return reinterpret_cast<T*>(phys_addr + 0xFFFF800000000000);
}
- uintptr_t phys_addr;
+ std::uintptr_t phys_addr;
};
void halt();