Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
92533cb5d2 | ||
|
084945688b | ||
|
79efbdc5a0 | ||
|
7cac67b0fc | ||
|
cd685e79c7 | ||
|
ad88a0784b | ||
|
56f003e8cc | ||
|
aa03bdad88 | ||
|
a5fb50ffb5 | ||
|
4d7aa79434 | ||
|
1cfd133cb2 | ||
|
5c0ea91e65 | ||
|
2889490657 |
@ -1,2 +1,10 @@
|
||||
[unstable]
|
||||
build-std = ["core", "compiler_builtins"]
|
||||
build-std-features = ["compiler-builtins-mem"]
|
||||
|
||||
[build]
|
||||
target = "os.json"
|
||||
target = "x86_64_target.json"
|
||||
|
||||
# Facilitates running tests with qemu
|
||||
[target.'cfg(target_os = "none")']
|
||||
runner = "bootimage runner"
|
||||
|
98
Cargo.lock
generated
Normal file
98
Cargo.lock
generated
Normal file
@ -0,0 +1,98 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bootloader"
|
||||
version = "0.9.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a62c8f6168cd106687ee36a2b71a46c4144d73399f72814104d33094b8092fd2"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
dependencies = [
|
||||
"spin 0.5.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustos"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bootloader",
|
||||
"lazy_static",
|
||||
"spin 0.9.2",
|
||||
"uart_16550",
|
||||
"x86_64",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[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.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "511254be0c5bcf062b019a6c89c01a664aa359ded62f78aa72c6fc137c0590e5"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uart_16550"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af81448a9a53c7b0f66198381f80912fd18f2c8965f9da4319e6f92e740bca5b"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"x86_64",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "volatile"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
|
||||
|
||||
[[package]]
|
||||
name = "x86_64"
|
||||
version = "0.14.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "958ab3202b01bc43ba2eb832102c4a487ed93151667a2289062e5f2b00058be2"
|
||||
dependencies = [
|
||||
"bit_field",
|
||||
"bitflags",
|
||||
"volatile",
|
||||
]
|
21
Cargo.toml
21
Cargo.toml
@ -2,17 +2,22 @@
|
||||
name = "rustos"
|
||||
version = "0.1.0"
|
||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bootloader = { version = "0.6.4", features = ["map_physical_memory"] }
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.0"
|
||||
x86_64 = "0.7.2"
|
||||
pic8259_simple = "0.1.1"
|
||||
pc-keyboard = "0.5.0"
|
||||
#bootloader = "0.10.12"
|
||||
bootloader = "0.9.8"
|
||||
spin = "0.9.2"
|
||||
x86_64 = "0.14.8"
|
||||
uart_16550 = "0.2.16"
|
||||
|
||||
[dependencies.lazy_static]
|
||||
version = "1.3.0"
|
||||
version = "1.4.0"
|
||||
features = ["spin_no_std"]
|
||||
|
||||
|
||||
[package.metadata.bootimage]
|
||||
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||
"-display", "none"]
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||
test-timeout = 10 # timeout in seconds
|
||||
|
7
justfile
Normal file
7
justfile
Normal file
@ -0,0 +1,7 @@
|
||||
default:
|
||||
just --list
|
||||
|
||||
run_os:
|
||||
cargo bootimage
|
||||
qemu-system-x86_64 -drive format=raw,file=target/x86_64_target/debug/bootimage-rustos.bin
|
||||
|
5
run.sh
5
run.sh
@ -1,5 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
cargo bootimage
|
||||
qemu-system-x86_64 -drive format=raw,file=target/os/debug/bootimage-rustos.bin
|
||||
|
32
src/gdt.rs
32
src/gdt.rs
@ -1,18 +1,15 @@
|
||||
use x86_64::VirtAddr;
|
||||
use x86_64::structures::tss::TaskStateSegment;
|
||||
use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector};
|
||||
use lazy_static::lazy_static;
|
||||
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
|
||||
use x86_64::structures::tss::TaskStateSegment;
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
//double fault interrupt stack table
|
||||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
||||
|
||||
lazy_static! {
|
||||
static ref TSS: TaskStateSegment = {
|
||||
let mut tss = TaskStateSegment::new();
|
||||
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
||||
const STACK_SIZE: usize = 4096; //4k stack
|
||||
|
||||
//this static mut is temporarily the stack until proper allocation can be implemented
|
||||
const STACK_SIZE: usize = 4096 * 5;
|
||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
|
||||
let stack_end = stack_start + STACK_SIZE;
|
||||
@ -22,26 +19,15 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
struct Selectors {
|
||||
code_selector: SegmentSelector,
|
||||
data_selector: SegmentSelector,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref GDT: (GlobalDescriptorTable, Selectors) = {
|
||||
static ref GDT: GlobalDescriptorTable = {
|
||||
let mut gdt = GlobalDescriptorTable::new();
|
||||
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
|
||||
let data_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
|
||||
(gdt, Selectors { code_selector, data_selector })
|
||||
gdt.add_entry(Descriptor::kernel_code_segment());
|
||||
gdt.add_entry(Descriptor::tss_segment(&TSS));
|
||||
gdt
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
use x86_64::instructions::segmentation::set_cs;
|
||||
use x86_64::instructions::tables::load_tss;
|
||||
GDT.0.load();
|
||||
unsafe {
|
||||
set_cs(GDT.1.code_selector);
|
||||
load_tss(GDT.1.data_selector);
|
||||
}
|
||||
GDT.load();
|
||||
}
|
||||
|
@ -1,98 +1,26 @@
|
||||
use lazy_static::lazy_static;
|
||||
use x86_64::structures::idt::{InterruptStackFrame, InterruptDescriptorTable};
|
||||
use crate::println;
|
||||
use pic8259_simple::ChainedPics;
|
||||
use spin;
|
||||
use crate::gdt;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[repr(u8)]
|
||||
pub enum InterruptIndex {
|
||||
Timer = PIC_1_OFFSET,
|
||||
Keyboard,
|
||||
}
|
||||
|
||||
impl InterruptIndex {
|
||||
fn as_u8(self) -> u8 {
|
||||
self as u8
|
||||
}
|
||||
fn as_usize(self) -> usize {
|
||||
usize::from(self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const PIC_1_OFFSET: u8 = 32;
|
||||
const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
||||
static PICS: spin::Mutex<ChainedPics> =
|
||||
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
||||
|
||||
lazy_static! {
|
||||
static ref IDT: InterruptDescriptorTable = {
|
||||
let mut idt = InterruptDescriptorTable::new();
|
||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||
unsafe {
|
||||
idt.double_fault.set_handler_fn(double_fault_handler)
|
||||
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
||||
}
|
||||
idt[InterruptIndex::Timer.as_usize()]
|
||||
.set_handler_fn(timer_interrupt_handler);
|
||||
idt[InterruptIndex::Keyboard.as_usize()]
|
||||
.set_handler_fn(keyboard_interrupt_handler);
|
||||
idt.double_fault.set_handler_fn(double_fault_handler);
|
||||
idt
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init_idt() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
pub fn initalize_pics() {
|
||||
unsafe {
|
||||
PICS.lock().initialize();
|
||||
}
|
||||
x86_64::instructions::interrupts::enable();
|
||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
||||
println!("EXCEPTION: Breakpoint\n{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
|
||||
|
||||
println!("EXCEPTION - BREAKPOINT\n{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn double_fault_handler(stack_frame: &mut InterruptStackFrame, code: u64) {
|
||||
panic!("EXCEPTION - DOUBLE FAULT (code {})\n{:#?}", code, stack_frame);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||
print!(".");
|
||||
unsafe {
|
||||
PICS.lock().notify_end_of_interrupt(InterruptIndex::Timer.as_u8())
|
||||
}
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
||||
use x86_64::instructions::port::Port;
|
||||
use pc_keyboard::{Keyboard, ScancodeSet1, DecodedKey, HandleControl, layouts};
|
||||
use spin::Mutex;
|
||||
|
||||
lazy_static! {
|
||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
|
||||
Mutex::new(Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore));
|
||||
}
|
||||
|
||||
let mut keyboard = KEYBOARD.lock();
|
||||
let mut port = Port::new(0x60);
|
||||
let scancode: u8 = unsafe { port.read() };
|
||||
|
||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
||||
match key {
|
||||
DecodedKey::Unicode(character) => print!("{}", character),
|
||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
||||
}
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
PICS.lock().notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8())
|
||||
}
|
||||
// The error code of the double-fault handler is always 0
|
||||
extern "x86-interrupt" fn double_fault_handler(
|
||||
stack_frame: InterruptStackFrame,
|
||||
_error_code: u64,
|
||||
) -> ! {
|
||||
panic!("Exception: Double fault\n{:#?}", stack_frame)
|
||||
}
|
||||
|
54
src/main.rs
54
src/main.rs
@ -1,41 +1,51 @@
|
||||
#![feature(abi_x86_interrupt, asm)]
|
||||
#![feature(abi_x86_interrupt)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![test_runner(crate::test_utils::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
#[macro_use]
|
||||
mod vga_buffer;
|
||||
mod interrupts;
|
||||
#[macro_use]
|
||||
mod serial;
|
||||
mod gdt;
|
||||
mod interrupts;
|
||||
mod test_utils;
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
println!("{}", info);
|
||||
halt_loop();
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
interrupts::initalize_pics();
|
||||
gdt::init();
|
||||
interrupts::init_idt();
|
||||
fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
println!("{info}");
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
#[cfg(test)]
|
||||
test_main();
|
||||
|
||||
println!("Gamarjoba, munde!");
|
||||
init();
|
||||
|
||||
// Deliberate breakpoint exception
|
||||
x86_64::instructions::interrupts::int3();
|
||||
|
||||
|
||||
for i in 1..10 {
|
||||
println!("Gamarjoba, munde: {}", i);
|
||||
}
|
||||
halt_loop();
|
||||
println!("We're here now");
|
||||
loop {}
|
||||
}
|
||||
|
||||
pub fn halt_loop() -> ! {
|
||||
loop {
|
||||
x86_64::instructions::hlt();
|
||||
}
|
||||
#[test_case]
|
||||
fn basic_test() {
|
||||
assert_eq!(5, 5);
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn basic_test2() {
|
||||
assert_eq!(4, 4);
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
gdt::init();
|
||||
interrupts::init_idt();
|
||||
}
|
||||
|
32
src/serial.rs
Normal file
32
src/serial.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use uart_16550::SerialPort;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SERIAL1: Mutex<SerialPort> = {
|
||||
let mut serial_port = unsafe { SerialPort::new(0x3f8) }; // 0x3f8 is standard address of 1st serial port
|
||||
serial_port.init();
|
||||
Mutex::new(serial_port)
|
||||
};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn _print(args: ::core::fmt::Arguments) {
|
||||
use core::fmt::Write;
|
||||
SERIAL1.lock().write_fmt(args).expect("Serial print failed");
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! serial_print {
|
||||
($($arg:tt)*) => {
|
||||
$crate::serial::_print(format_args!($($arg)*));
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! serial_println {
|
||||
() => ($crate::serial_print!("\n"));
|
||||
($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
|
||||
($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
|
||||
concat!($fmt, "\n"), $($arg)*));
|
||||
}
|
54
src/test_utils.rs
Normal file
54
src/test_utils.rs
Normal file
@ -0,0 +1,54 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
serial_println!("[failed]\n");
|
||||
serial_println!("Error: {}", info);
|
||||
exit_qemu(QemuExitCode::Failed);
|
||||
loop {}
|
||||
}
|
||||
|
||||
/// This function is hooked up to the #![test_runner] attribute made possible by the
|
||||
/// https://doc.rust-lang.org/unstable-book/language-features/custom-test-frameworks.html unstable
|
||||
/// feature. cf. https://os.phil-opp.com/testing/
|
||||
pub fn test_runner(tests: &[&dyn TestFunction]) {
|
||||
serial_println!("Running {} test(s)", tests.len());
|
||||
for test in tests {
|
||||
test.run();
|
||||
}
|
||||
|
||||
exit_qemu(QemuExitCode::Success);
|
||||
}
|
||||
|
||||
pub trait TestFunction {
|
||||
fn run(&self) -> ();
|
||||
}
|
||||
|
||||
impl<T> TestFunction for T
|
||||
where
|
||||
T: Fn(),
|
||||
{
|
||||
fn run(&self) {
|
||||
serial_print!("{}...\t", core::any::type_name::<T>());
|
||||
self();
|
||||
serial_println!("[ok]");
|
||||
}
|
||||
}
|
||||
|
||||
/// Exits qemu by writing to a special qemu emulated port.
|
||||
pub fn exit_qemu(exit_code: QemuExitCode) {
|
||||
use x86_64::instructions::port::Port;
|
||||
unsafe {
|
||||
let mut port = Port::new(0xf4); // isa-debug-exit port, see Cargo.toml
|
||||
port.write(exit_code as u32);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u32)]
|
||||
pub enum QemuExitCode {
|
||||
Success = 0x10,
|
||||
Failed = 0x11,
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
use volatile::Volatile;
|
||||
use core::fmt;
|
||||
use spin::Mutex;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use spin::Mutex;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
@ -18,10 +16,13 @@ macro_rules! println {
|
||||
#[doc(hidden)]
|
||||
pub fn _print(args: fmt::Arguments) {
|
||||
use core::fmt::Write;
|
||||
WRITER.lock().write_fmt(args).unwrap();
|
||||
/*
|
||||
use x86_64::instructions::interrupts;
|
||||
interrupts::without_interrupts(|| {
|
||||
WRITER.lock().write_fmt(args).unwrap();
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@ -48,10 +49,10 @@ pub enum Color {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(transparent)]
|
||||
struct ColorCode(u8);
|
||||
pub struct ColorCode(u8);
|
||||
|
||||
impl ColorCode {
|
||||
fn new(foreground: Color, background: Color) -> ColorCode {
|
||||
pub fn new(foreground: Color, background: Color) -> ColorCode {
|
||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||
}
|
||||
}
|
||||
@ -67,8 +68,8 @@ const BUFFER_WIDTH: usize = 80;
|
||||
const BUFFER_HEIGHT: usize = 25;
|
||||
|
||||
#[repr(transparent)]
|
||||
struct Buffer {
|
||||
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||
pub struct Buffer {
|
||||
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||
}
|
||||
|
||||
pub struct Writer {
|
||||
@ -78,11 +79,18 @@ pub struct Writer {
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn new(color_code: ColorCode) -> Self {
|
||||
Writer {
|
||||
column_position: 0,
|
||||
color_code,
|
||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
||||
}
|
||||
}
|
||||
pub fn write_string(&mut self, s: &str) {
|
||||
for byte in s.bytes() {
|
||||
match byte {
|
||||
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
||||
_ => self.write_byte(0xfe), // a ■
|
||||
_ => self.write_byte(0xfe), // 0xfe is the character ■
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,9 +105,17 @@ impl Writer {
|
||||
let row = BUFFER_HEIGHT - 1;
|
||||
let col = self.column_position;
|
||||
let color_code = self.color_code;
|
||||
self.buffer.chars[row][col].write(ScreenChar {
|
||||
ascii_char: byte, color_code,
|
||||
});
|
||||
|
||||
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
|
||||
let screenchar = ScreenChar {
|
||||
ascii_char: byte,
|
||||
color_code,
|
||||
};
|
||||
|
||||
unsafe {
|
||||
core::ptr::write_volatile(ptr, screenchar);
|
||||
}
|
||||
|
||||
self.column_position += 1;
|
||||
}
|
||||
}
|
||||
@ -108,8 +124,14 @@ impl Writer {
|
||||
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);
|
||||
let character = unsafe {
|
||||
core::ptr::read_volatile(&self.buffer.chars[row][col] as *const ScreenChar)
|
||||
};
|
||||
|
||||
let ptr = &mut self.buffer.chars[row - 1][col] as *mut ScreenChar;
|
||||
unsafe {
|
||||
core::ptr::write_volatile(ptr, character);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.clear_row(BUFFER_HEIGHT - 1);
|
||||
@ -122,7 +144,10 @@ impl Writer {
|
||||
color_code: self.color_code,
|
||||
};
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
self.buffer.chars[row][col].write(blank);
|
||||
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
|
||||
unsafe {
|
||||
core::ptr::write_volatile(ptr, blank);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -134,11 +159,31 @@ impl fmt::Write for Writer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lazy_static! {
|
||||
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) },
|
||||
});
|
||||
pub static ref WRITER: Mutex<Writer> =
|
||||
Mutex::new(Writer::new(ColorCode::new(Color::Yellow, Color::Black)));
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_simple() {
|
||||
println!("Does this work?");
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_many() {
|
||||
for _ in 0..200 {
|
||||
println!("Test many output");
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_output() {
|
||||
let s = "Test string that fits on a single line";
|
||||
println!("{}", s);
|
||||
for (i, c) in s.chars().enumerate() {
|
||||
let screen_char_ptr =
|
||||
&WRITER.lock().buffer.chars[BUFFER_HEIGHT - 2][i] as *const ScreenChar;
|
||||
let screen_char = unsafe { core::ptr::read_volatile(screen_char_ptr) };
|
||||
assert_eq!(char::from(screen_char.ascii_char), c);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user