Skip to content

Commit d9c491c

Browse files
authored
Page 03: VGA Text Mode (#6)
* Working `println!` and `print!` macros * Colors! * A Very Normal Sphere * Small Tweaks 🤏 * Update `README.md` * Fix a few typos
1 parent 0b29541 commit d9c491c

File tree

8 files changed

+343
-27
lines changed

8 files changed

+343
-27
lines changed

Cargo.lock

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
# rust-os
1+
# rust-os
2+
3+
[![Verify](https://github.com/c1m50c/rust-os/actions/workflows/verify.yaml/badge.svg?branch=main&event=push)](https://github.com/c1m50c/rust-os/actions/workflows/verify.yaml)
4+
5+
Repository following the ["Writing an Operating System in Rust"] blog by Philipp Oppermann.
6+
7+
["Writing an Operating System in Rust"]: https://os.phil-opp.com/
8+
9+
## Installing Requirements
10+
11+
### Compilation Tools
12+
13+
This operating system is written in Rust so you'll need to install the language's compilation tools by running the following commmands:
14+
15+
#### Windows
16+
17+
```bash
18+
$ choco install rust
19+
```
20+
21+
#### MacOS
22+
23+
```bash
24+
$ brew install rust
25+
```
26+
27+
### Nightly Channel
28+
29+
Alongside the normal compilation toolkit you'll need to add the [`nightly`] channel to your [`rustup`] installation, you can do so by running the following commands:
30+
31+
```bash
32+
$ rustup update
33+
> ...
34+
35+
$ rustup toolchain install nightly
36+
> ...
37+
38+
# Also add the `x86_64-unknown-none` build target for compiling our kernel.
39+
$ rustup target add x86_64-unknown-none
40+
```
41+
42+
[`rustup`]: https://rust-lang.github.io/rustup/index.html
43+
[`nightly`]: https://rust-lang.github.io/rustup/concepts/channels.html#working-with-nightly-rust
44+
45+
### QEMU
46+
47+
Lastly, you'll need to install [QEMU] to emulate our operating system. To do so, follow the [installation instructions] for the platform you're currently using.
48+
49+
[QEMU]: https://www.qemu.org/
50+
[installation instructions]: https://www.qemu.org/download/
51+
52+
## Running
53+
54+
After you've installed all of the requirements, running the operating system should be as simple as executing any of the following commands:
55+
56+
```bash
57+
$ cargo run --release -- help
58+
> ... # Help menu will pop up displaying a valid list of commands
59+
60+
$ cargo run --release -- uefi
61+
> ... # QEMU Window should pop up launching our kernel's UEFI image
62+
63+
$ cargo run --release -- bios
64+
> ... # QEMU Window should pop up launching our kernel's BIOS image
65+
```

kernel/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ edition = "2021"
66
publish = false
77

88
[dependencies]
9+
lazy_static = { version = "1.4.0", features = [ "spin_no_std" ] }
910
noto-sans-mono-bitmap = "0.2.0"
1011
bootloader_api = "0.11.3"
12+
fixed-vectors = "3.2.1"
13+
spin = "0.9.8"
14+
libm = "0.2.6"

kernel/src/macros/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use core::fmt::{self, Write};
2+
use core::ops::DerefMut;
3+
4+
use crate::FRAME_BUFFER_WRITER;
5+
6+
7+
#[macro_export]
8+
macro_rules! print {
9+
($($arg: tt)*) => {
10+
$crate::macros::_print(
11+
format_args!( $($arg)* )
12+
);
13+
};
14+
}
15+
16+
17+
#[macro_export]
18+
macro_rules! println {
19+
() => {
20+
$crate::print!("\n");
21+
};
22+
23+
($($arg: tt)*) => {
24+
$crate::print!("{}\n", format_args!($($arg)*));
25+
}
26+
}
27+
28+
29+
#[doc(hidden)]
30+
pub fn _print(args: fmt::Arguments) {
31+
let mut lock = FRAME_BUFFER_WRITER.lock();
32+
33+
if let Some(writer) = lock.deref_mut() {
34+
writer.write_fmt(args)
35+
.expect("In `_print()`, the `write_fmt()` function is expected not to fail");
36+
}
37+
}

kernel/src/main.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
1-
#![allow(clippy::empty_loop)]
1+
#![allow(clippy::empty_loop, unused_labels)]
22
#![no_main]
33
#![no_std]
44

5-
65
use bootloader_api::info::Optional;
76
use bootloader_api::BootInfo;
7+
8+
use lazy_static::lazy_static;
9+
use fixed_vectors::Vector2;
10+
use spin::Mutex;
11+
12+
use core::ops::DerefMut;
13+
#[cfg(not(test))]
814
use core::panic::PanicInfo;
9-
use core::fmt::Write;
1015

16+
pub mod raytracer;
1117
pub mod writer;
12-
pub mod math;
18+
pub mod macros;
19+
20+
21+
lazy_static!{
22+
pub static ref FRAME_BUFFER_WRITER: Mutex<Option<writer::FrameBufferWriter<'static>>> = Mutex::new(
23+
// The `FRAME_BUFFER_WRITER` needs to be initialized in `main` with the `info.framebuffer`
24+
None
25+
);
26+
}
1327

1428

1529
#[panic_handler]
16-
fn panic(_info: &PanicInfo) -> ! {
30+
#[cfg(not(test))]
31+
fn panic(info: &PanicInfo) -> ! {
32+
println!("PANIC: {}", info);
33+
1734
loop { }
1835
}
1936

2037

2138
fn main(info: &'static mut BootInfo) -> ! {
39+
// Initialize `FRAME_BUFFER_WRITER` with `info.framebuffer`
2240
if let Optional::Some(buffer) = &mut info.framebuffer {
2341
let frame_buffer_info = buffer.info();
2442
let frame_buffer = buffer.buffer_mut();
2543

26-
let mut font_writer = writer::FontWriter::new(frame_buffer_info, frame_buffer);
27-
font_writer.clear();
44+
let mut lock = FRAME_BUFFER_WRITER.lock();
45+
46+
let mut writer = writer::FrameBufferWriter::new(frame_buffer_info, frame_buffer);
47+
writer.clear();
2848

29-
let _ = write!(&mut font_writer, "Hello, World!\n");
30-
let _ = write!(&mut font_writer, "https://github.com/c1m50c/rust-os");
49+
*lock = Some(writer);
3150
}
3251

52+
let mut lock = FRAME_BUFFER_WRITER.lock();
53+
let Some(writer) = lock.deref_mut() else { unreachable!() };
54+
55+
let resolution = Vector2::new(
56+
writer.frame_buffer_info.width, writer.frame_buffer_info.height
57+
);
58+
59+
let mut raytracer = raytracer::Raytracer::new(resolution, writer);
60+
raytracer.run();
61+
3362
loop { }
3463
}
3564

kernel/src/math.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)