mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Compare commits
93 Commits
83e6c0bc00
...
post-03
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93d0daa1e0 | ||
|
|
1f097bba71 | ||
|
|
99fbbad9b0 | ||
|
|
6f0be666f3 | ||
|
|
97de98df33 | ||
|
|
e2a3e76a32 | ||
|
|
df0c0b5783 | ||
|
|
2a61f88d8a | ||
|
|
f52872f3b9 | ||
|
|
07d6c9c84e | ||
|
|
3bc023ec2d | ||
|
|
1a53a115da | ||
|
|
ef9bb1a087 | ||
|
|
7288459bdf | ||
|
|
6c8fea8b00 | ||
|
|
3280a0afc7 | ||
|
|
a387a00c85 | ||
|
|
4c9cd3ae32 | ||
|
|
cbead25b43 | ||
|
|
2c25d55810 | ||
|
|
ec95ac0e5a | ||
|
|
e39c7d8ad8 | ||
|
|
f40717901b | ||
|
|
195f4489e6 | ||
|
|
fbdcd45d16 | ||
|
|
d12842bcd5 | ||
|
|
ea5aad0e0c | ||
|
|
1afa3af79d | ||
|
|
552eb97f75 | ||
|
|
eace075f2c | ||
|
|
2d5150f2a3 | ||
|
|
3200b7a68d | ||
|
|
f316bd1a08 | ||
|
|
1ddc27ca88 | ||
|
|
406c406d5f | ||
|
|
651adbc8bd | ||
|
|
a004e7da9c | ||
|
|
f23269ed93 | ||
|
|
0489c09d05 | ||
|
|
99b139ddff | ||
|
|
abd2f4c5e2 | ||
|
|
dbb3db2dc7 | ||
|
|
748365bf62 | ||
|
|
2b1f72ccd8 | ||
|
|
679e453509 | ||
|
|
4f90781b3f | ||
|
|
515d98645e | ||
|
|
a86fe5c2af | ||
|
|
ab10af6f99 | ||
|
|
bcafb60069 | ||
|
|
465eb45cfe | ||
|
|
c6d5a4d567 | ||
|
|
fe5e1fd92c | ||
|
|
a1060f79f0 | ||
|
|
3765c27020 | ||
|
|
70b339a4a5 | ||
|
|
153beec1c4 | ||
|
|
bfde53de79 | ||
|
|
298ec5f4b6 | ||
|
|
ff8afb814e | ||
|
|
fcd849f471 | ||
|
|
70c24b76d0 | ||
|
|
16a314ab8d | ||
|
|
f875a933e5 | ||
|
|
652d91154f | ||
|
|
9a879e69c5 | ||
|
|
186321623a | ||
|
|
1246fc4df9 | ||
|
|
03da22d8e1 | ||
|
|
d98a4d0cbc | ||
|
|
93b2134692 | ||
|
|
4d405d5098 | ||
|
|
e3f9cd42b0 | ||
|
|
61f524f040 | ||
|
|
3f9072e8b5 | ||
|
|
bac4a3c549 | ||
|
|
4bfcea2368 | ||
|
|
af0b258279 | ||
|
|
de2b99f0d2 | ||
|
|
8c1e98752a | ||
|
|
ceb5cf4eed | ||
|
|
1d5bc372a0 | ||
|
|
0874a50544 | ||
|
|
b5c05193e1 | ||
|
|
6c427e7cee | ||
|
|
c43d6191c5 | ||
|
|
c1e3fa57c6 | ||
|
|
3574958d5f | ||
|
|
cbfe6221bd | ||
|
|
d1897af515 | ||
|
|
d71e6a7cb6 | ||
|
|
27f2c4c2ff | ||
|
|
19de9e560d |
30
Cargo.lock
generated
30
Cargo.lock
generated
@@ -7,6 +7,9 @@ name = "blog_os"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bootloader",
|
||||
"lazy_static",
|
||||
"spin 0.5.2",
|
||||
"volatile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -14,3 +17,30 @@ name = "bootloader"
|
||||
version = "0.9.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7bdfddac270bbdd45903296bc1caf29a7fdce6b326aaf0bbab7f04c5f98b7447"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
dependencies = [
|
||||
"spin 0.9.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||
|
||||
[[package]]
|
||||
name = "volatile"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
|
||||
|
||||
@@ -6,6 +6,12 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bootloader = "0.9"
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
|
||||
[dependencies.lazy_static]
|
||||
version = "1.0"
|
||||
features = ["spin_no_std"]
|
||||
|
||||
[[bin]]
|
||||
name = "blog_os"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# Blog OS (A Minimal Rust Kernel)
|
||||
# Blog OS (VGA Text Mode)
|
||||
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-02)
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-03)
|
||||
|
||||
This repository contains the source code for the [A Minimal Rust Kernel][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
This repository contains the source code for the [VGA Text Mode][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
|
||||
[post]: https://os.phil-opp.com/minimal-rust-kernel/
|
||||
[post]: https://os.phil-opp.com/vga-text-mode/
|
||||
|
||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||
|
||||
|
||||
14
src/main.rs
14
src/main.rs
@@ -3,24 +3,18 @@
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
static HELLO: &[u8] = b"Hello World!";
|
||||
mod vga_buffer;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
let vga_buffer = 0xb8000 as *mut u8;
|
||||
|
||||
for (i, &byte) in HELLO.iter().enumerate() {
|
||||
unsafe {
|
||||
*vga_buffer.offset(i as isize * 2) = byte;
|
||||
*vga_buffer.offset(i as isize * 2 + 1) = 0xb;
|
||||
}
|
||||
}
|
||||
println!("Hello World{}", "!");
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
/// This function is called on panic.
|
||||
#[panic_handler]
|
||||
fn panic(_info: &PanicInfo) -> ! {
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
println!("{}", info);
|
||||
loop {}
|
||||
}
|
||||
|
||||
171
src/vga_buffer.rs
Normal file
171
src/vga_buffer.rs
Normal file
@@ -0,0 +1,171 @@
|
||||
use core::fmt;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use volatile::Volatile;
|
||||
|
||||
lazy_static! {
|
||||
/// A global `Writer` instance that can be used for printing to the VGA text buffer.
|
||||
///
|
||||
/// Used by the `print!` and `println!` macros.
|
||||
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||
column_position: 0,
|
||||
color_code: ColorCode::new(Color::Yellow, Color::Black),
|
||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
||||
});
|
||||
}
|
||||
|
||||
/// The standard color palette in VGA text mode.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Color {
|
||||
Black = 0,
|
||||
Blue = 1,
|
||||
Green = 2,
|
||||
Cyan = 3,
|
||||
Red = 4,
|
||||
Magenta = 5,
|
||||
Brown = 6,
|
||||
LightGray = 7,
|
||||
DarkGray = 8,
|
||||
LightBlue = 9,
|
||||
LightGreen = 10,
|
||||
LightCyan = 11,
|
||||
LightRed = 12,
|
||||
Pink = 13,
|
||||
Yellow = 14,
|
||||
White = 15,
|
||||
}
|
||||
|
||||
/// A combination of a foreground and a background color.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(transparent)]
|
||||
struct ColorCode(u8);
|
||||
|
||||
impl ColorCode {
|
||||
/// Create a new `ColorCode` with the given foreground and background colors.
|
||||
fn new(foreground: Color, background: Color) -> ColorCode {
|
||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||
}
|
||||
}
|
||||
|
||||
/// A screen character in the VGA text buffer, consisting of an ASCII character and a `ColorCode`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
struct ScreenChar {
|
||||
ascii_character: u8,
|
||||
color_code: ColorCode,
|
||||
}
|
||||
|
||||
/// The height of the text buffer (normally 25 lines).
|
||||
const BUFFER_HEIGHT: usize = 25;
|
||||
/// The width of the text buffer (normally 80 columns).
|
||||
const BUFFER_WIDTH: usize = 80;
|
||||
|
||||
/// A structure representing the VGA text buffer.
|
||||
#[repr(transparent)]
|
||||
struct Buffer {
|
||||
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||
}
|
||||
|
||||
/// A writer type that allows writing ASCII bytes and strings to an underlying `Buffer`.
|
||||
///
|
||||
/// Wraps lines at `BUFFER_WIDTH`. Supports newline characters and implements the
|
||||
/// `core::fmt::Write` trait.
|
||||
pub struct Writer {
|
||||
column_position: usize,
|
||||
color_code: ColorCode,
|
||||
buffer: &'static mut Buffer,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
/// Writes an ASCII byte to the buffer.
|
||||
///
|
||||
/// Wraps lines at `BUFFER_WIDTH`. Supports the `\n` newline character.
|
||||
pub fn write_byte(&mut self, byte: u8) {
|
||||
match byte {
|
||||
b'\n' => self.new_line(),
|
||||
byte => {
|
||||
if self.column_position >= BUFFER_WIDTH {
|
||||
self.new_line();
|
||||
}
|
||||
|
||||
let row = BUFFER_HEIGHT - 1;
|
||||
let col = self.column_position;
|
||||
|
||||
let color_code = self.color_code;
|
||||
self.buffer.chars[row][col].write(ScreenChar {
|
||||
ascii_character: byte,
|
||||
color_code,
|
||||
});
|
||||
self.column_position += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the given ASCII string to the buffer.
|
||||
///
|
||||
/// Wraps lines at `BUFFER_WIDTH`. Supports the `\n` newline character. Does **not**
|
||||
/// support strings with non-ASCII characters, since they can't be printed in the VGA text
|
||||
/// mode.
|
||||
fn write_string(&mut self, s: &str) {
|
||||
for byte in s.bytes() {
|
||||
match byte {
|
||||
// printable ASCII byte or newline
|
||||
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
||||
// not part of printable ASCII range
|
||||
_ => self.write_byte(0xfe),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shifts all lines one line up and clears the last row.
|
||||
fn new_line(&mut self) {
|
||||
for row in 1..BUFFER_HEIGHT {
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
let character = self.buffer.chars[row][col].read();
|
||||
self.buffer.chars[row - 1][col].write(character);
|
||||
}
|
||||
}
|
||||
self.clear_row(BUFFER_HEIGHT - 1);
|
||||
self.column_position = 0;
|
||||
}
|
||||
|
||||
/// Clears a row by overwriting it with blank characters.
|
||||
fn clear_row(&mut self, row: usize) {
|
||||
let blank = ScreenChar {
|
||||
ascii_character: b' ',
|
||||
color_code: self.color_code,
|
||||
};
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
self.buffer.chars[row][col].write(blank);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Write for Writer {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.write_string(s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Like the `print!` macro in the standard library, but prints to the VGA text buffer.
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ($crate::vga_buffer::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
/// Like the `println!` macro in the standard library, but prints to the VGA text buffer.
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
() => ($crate::print!("\n"));
|
||||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
/// Prints the given formatted string to the VGA text buffer through the global `WRITER` instance.
|
||||
#[doc(hidden)]
|
||||
pub fn _print(args: fmt::Arguments) {
|
||||
use core::fmt::Write;
|
||||
WRITER.lock().write_fmt(args).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user