IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
vmem.cpp
Go to the documentation of this file.
1
11
12
13// C++
14#include <algorithm>
15#include <bit>
16#include <span>
17#include <string_view>
18// IgrOS-Kernel arch
19#include <arch/io.hpp>
20// IgrOS-Kernel drivers
21#include <drivers/vga/vmem.hpp>
22// IgrOS-Kernel library
23#include <klib/kString.hpp>
24// IgrOS-Kernel platform
25#include <platform/platform.hpp>
26
27
28// Arch-dependent code zone
29namespace igros::arch {
30
31
32 // VGA cursor control ports
33 constexpr auto VGA_CURSOR_CONTROL {static_cast<port_t>(0x03D4_u16)};
34 constexpr auto VGA_CURSOR_DATA {static_cast<port_t>(VGA_CURSOR_CONTROL + 1_u16)};
35
36
38 constexpr auto VIDEO_MEM_PHYS {0x000B8000_usize};
39
42 [[nodiscard]]
43 static auto vmemScreen() noexcept -> std::span<vmemSymbol, VIDEO_MEM_SIZE> {
44 return std::span<vmemSymbol, VIDEO_MEM_SIZE> {
45 std::bit_cast<vmemSymbol*>(VIDEO_MEM_PHYS + platform::Platform::kernelOffset()),
47 };
48 }
49
51 constinit static auto vmemBkgColor {vmemColor::Green};
52
54 constinit static vmemCursor cursorPos {};
55
56
59 [[nodiscard]]
60 static auto vmemBlank() noexcept -> vmemSymbol {
61 return vmemSymbol {
62 .symbol = ' ',
63 .color = static_cast<igros_byte_t>(vmemBkgColor),
64 };
65 }
66
67
68 // Set cursor position
69 void vmemCursorSet(const igros_byte_t x, const igros_byte_t y) noexcept {
70 // Calculate VGA console offset
71 const auto position {(y * VIDEO_MEM_WIDTH) + x};
72 // Choose cursor location high register
74 // Write cursor position high byte
75 io::writePort8(VGA_CURSOR_DATA, ((position & 0xFF00_u16) >> 8));
76 // Choose cursor location low register
78 // Write cursor position low byte
79 io::writePort8(VGA_CURSOR_DATA, (position & 0x00FF_u16));
80 // Save cursor data
81 cursorPos.x = x;
82 cursorPos.y = y;
83 }
84
85 // Set VGA memory cursor position
86 void vmemCursorSet(const vmemCursor cursor) noexcept {
87 vmemCursorSet(cursor.x, cursor.y);
88 }
89
90 // Get VGA memory cursor position
91 [[nodiscard]]
92 auto vmemCursorGet() noexcept -> vmemCursor {
93 // Choose cursor location high register
95 // Write cursor position high byte
96 auto position {static_cast<igros_word_t>(io::readPort8(VGA_CURSOR_DATA))};
97 // Choose cursor location low register
99 // Write cursor position low byte
100 position = static_cast<igros_word_t>((static_cast<igros_dword_t>(position) << 8) | io::readPort8(VGA_CURSOR_DATA));
101 // Return cursor data
102 return vmemCursor {
103 .x = static_cast<igros_byte_t>(position % VIDEO_MEM_WIDTH),
104 .y = static_cast<igros_byte_t>(position / VIDEO_MEM_WIDTH),
105 };
106 }
107
108
109 // Disable VGA memory cursor
110 void vmemCursorDisable() noexcept {
111 // Choose cursor start register
113 // Send control word to disable cursor
115 }
116
117 // Enable VGA memory cursor
118 void vmemCursorEnable() noexcept {
119 // Choose cursor start register
121 // Get current register value
122 const auto cursorStartReg {io::readPort8(VGA_CURSOR_DATA)};
123 // Send control word to disable cursor
124 io::writePort8(VGA_CURSOR_DATA, static_cast<igros_byte_t>(static_cast<igros_dword_t>(cursorStartReg) & ~0x20_u32));
125 }
126
127 // Set VGA memory color
128 void vmemSetColor(const igros_byte_t background, const igros_byte_t foreground) noexcept {
129 // Background is first 4 bits and foreground is next 4
130 vmemBkgColor = static_cast<vmemColor>((static_cast<igros_dword_t>(background) << 4) | foreground);
131 }
132
133 // Write symbol to VGA memory
134 void vmemWrite(const char symbol) noexcept {
135 // Backspace symbol
136 if (symbol == '\b') {
137 // If we are not at start
138 if (0_u8 != cursorPos.x) {
139 // Move 1 symbol backward
140 --cursorPos.x;
141 } else {
142 cursorPos.x = VIDEO_MEM_WIDTH - 1_u16;
143 // Move 1 line up
144 --cursorPos.y;
145 }
146 // Tabulation symbol
147 } else if (symbol == '\t') {
148 // calculate new tab offset
149 cursorPos.x = static_cast<igros_byte_t>((cursorPos.x + VIDEO_MEM_TAB_SIZE) & ~(VIDEO_MEM_TAB_SIZE - 1_usize));
150 // Carret return
151 } else if (symbol == '\r') {
152 // Move to start of the row
153 cursorPos.x = 0_u8;
154 // Carret new line
155 } else if (symbol == '\n') {
156 // Move to start of the row
157 cursorPos.x = 0_u8;
158 // Move to next row
159 ++cursorPos.y;
160 // If non-control (printable) character
161 } else if (symbol >= ' ') {
162 // Write symbol to VGA console
164 .symbol = symbol,
165 .color = static_cast<igros_byte_t>(vmemBkgColor),
166 };
167 // Move cursor 1 symbol right
168 ++cursorPos.x;
169 }
170 // Check if we are not out of columns
171 if (cursorPos.x >= VIDEO_MEM_WIDTH) {
172 // Move to start of the row
173 cursorPos.x = 0_u8;
174 // Move to next row
175 ++cursorPos.y;
176 }
177 // Chech if we are not out of rows
179 // Move cursor to the last line
180 cursorPos.y = VIDEO_MEM_HEIGHT - 1_u16;
181 // Move screen 1 line up
182 const auto screen {vmemScreen()};
183 std::shift_left(screen.begin(), screen.end(), VIDEO_MEM_WIDTH); // NOLINT(modernize-use-ranges): std::ranges::shift_left is C++23
184 // Clear bottom line
185 std::ranges::fill(screen.last<VIDEO_MEM_WIDTH>(), vmemBlank());
186 }
187 // Set new cursor position
189 }
190
191 // Write string to VGA memory
192 void vmemWrite(const char* const message) noexcept {
193 // Write symbols one by one
194 vmemWrite(klib::kstrview(message));
195 }
196
197 // Write fixed-width string to VGA memory
198 void vmemWrite(const std::string_view message) noexcept {
199 // Write symbols one by one
200 for (const auto symbol : message) {
201 vmemWrite(symbol);
202 }
203 }
204
205
206 // Clear VGA memory
207 void vmemClear() noexcept {
208 // Set whole screen with whitespace with default background
209 std::ranges::fill(vmemScreen(), vmemBlank());
210 }
211
212
213 // Init VGA memory
214 [[nodiscard]]
215 auto vmemInit() noexcept -> bool {
216 // Clear screen
217 vmemClear();
218 // Place cursor at (0, 0)
219 vmemCursorSet(0_u8, 0_u8);
220 // Disable cursor
222 // Success
223 return true;
224 }
225
226
227} // namespace igros::arch
228
static auto readPort8(const port_t addr) noexcept -> igros_byte_t
Read byte from port.
Definition io.hpp:160
static void writePort8(const port_t addr, const igros_byte_t value) noexcept
Write byte to port.
Definition io.hpp:178
static auto kernelOffset() noexcept -> igros_usize_t
Get kernel virtual memory offset (kernel virtual address = physical address + offset).
Definition platform.hpp:346
std::uint16_t igros_word_t
16-bit unsigned integer
Definition types.hpp:42
std::uint8_t igros_byte_t
8-bit unsigned integer
Definition types.hpp:41
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
I/O operations.
Kernel string functions.
Architecture-independent interface: drivers and aliases of current architecture types.
auto vmemCursorGet() noexcept -> vmemCursor
Get VGA memory cursor position.
Definition vmem.cpp:92
static constinit auto vmemBkgColor
VGA memory background symbol.
Definition vmem.cpp:51
void vmemWrite(const char symbol) noexcept
Write symbol to VGA memory.
Definition vmem.cpp:134
constexpr auto VIDEO_MEM_PHYS
VGA text buffer physical address.
Definition vmem.cpp:38
constexpr auto VGA_CURSOR_CONTROL
CRT controller register select port.
Definition vmem.cpp:33
constexpr auto VGA_CURSOR_DATA
CRT controller data port.
Definition vmem.cpp:34
void vmemCursorDisable() noexcept
Disable VGA memory cursor.
Definition vmem.cpp:110
static auto vmemScreen() noexcept -> std::span< vmemSymbol, VIDEO_MEM_SIZE >
VGA text buffer (low memory is mapped at kernel virtual offset).
Definition vmem.cpp:43
auto vmemInit() noexcept -> bool
Init VGA console.
Definition vmem.cpp:215
static constexpr auto VIDEO_MEM_WIDTH
VGA console max width.
Definition vmem.hpp:28
vmemColor
VGA memory colors enumeration.
Definition vmem.hpp:42
@ Green
Green VGA color.
Definition vmem.hpp:45
void vmemCursorEnable() noexcept
Enable VGA memory cursor.
Definition vmem.cpp:118
void vmemCursorSet(const igros_byte_t x, const igros_byte_t y) noexcept
Set VGA memory cursor position.
Definition vmem.cpp:69
static constinit vmemCursor cursorPos
Current cursor coordinates.
Definition vmem.cpp:54
void vmemClear() noexcept
Clear VGA console.
Definition vmem.cpp:207
static constexpr auto VIDEO_MEM_HEIGHT
VGA console max height.
Definition vmem.hpp:30
static constexpr auto VIDEO_MEM_TAB_SIZE
TAB size.
Definition vmem.hpp:35
void vmemSetColor(const igros_byte_t background, const igros_byte_t foreground) noexcept
Set VGA memory color.
Definition vmem.cpp:128
static auto vmemBlank() noexcept -> vmemSymbol
Blank symbol (space with current background).
Definition vmem.cpp:60
static constexpr auto VIDEO_MEM_SIZE
VGA console size.
Definition vmem.hpp:32
constexpr auto kstrview(const char *const src) noexcept -> std::string_view
View of null-terminated string (null pointer is empty string).
Definition kString.hpp:28
igros_word_t port_t
Define port data type which is 16bit word.
Definition io.hpp:25
IgrOS platform description.
VGA memory cursor struct.
Definition vmem.hpp:68
igros_byte_t x
Cursor X coordinate.
Definition vmem.hpp:69
igros_byte_t y
Cursor Y coordinate.
Definition vmem.hpp:70
VGA memory symbol representation.
Definition vmem.hpp:62
igros_sbyte_t symbol
Symbol.
Definition vmem.hpp:63
VGA memory low-level operations.