Skip to content

Commit

Permalink
use os terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyukai0403 committed Jan 23, 2025
1 parent 6b83307 commit c145687
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 138 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ raw-cpuid = "11.3.0"
spin = "0.9.8"
x2apic = "0.4.3"
x86_64 = "0.15.2"
os-terminal = "0.5.8"
54 changes: 24 additions & 30 deletions kernel/src/console/framebuffer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use limine::framebuffer::Framebuffer as LimineFrameBuffer;
use spin::Mutex;

Check warning on line 2 in kernel/src/console/framebuffer.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `spin::Mutex`
use os_terminal::{DrawTarget, Rgb};
use limine::request::FramebufferRequest;

struct FrameBuffer {
width: u64,
height: u64,
addr: u64,
pitch: u64,
#[used]
#[link_section = ".requests"]
pub static FRAMEBUFFER_REQUEST: FramebufferRequest = FramebufferRequest::new();

pub struct FrameBuffer {
width: usize,
height: usize,
addr: usize,
pitch: usize,
}

impl FrameBuffer {
Expand All @@ -20,36 +26,24 @@ impl FrameBuffer {

pub fn from_limine(fb: &LimineFrameBuffer) -> FrameBuffer {
FrameBuffer {
width: fb.width(),
height: fb.height(),
addr: fb.addr() as u64,
pitch: fb.pitch(),
width: fb.width() as usize,
height: fb.height() as usize,
addr: fb.addr() as usize,
pitch: fb.pitch() as usize,
}
}
}

pub static display: Mutex<FrameBuffer> = Mutex::new(FrameBuffer::null());

pub fn init(fb: &LimineFrameBuffer) {
*display.lock() = FrameBuffer::from_limine(fb);
}

pub fn display_fill(r: u8, g: u8, b: u8) {
let lock = display.lock();
for i in 0..(*lock).height {
for j in 0..(*lock).width {
unsafe {
*(((*lock).addr as * mut u8).offset((i * (*lock).pitch + j * 4) as isize) as *mut u32) =
((r as u32) << 16) + ((g as u32) << 8) + (b as u32);
}
}
impl DrawTarget for FrameBuffer {
fn size(&self) -> (usize, usize) {
(self.width as usize, self.height as usize)
}
}

pub fn display_setpixel(x: u64, y: u64, r: u8, g: u8, b: u8) {
let lock = display.lock();
unsafe {
*(((*lock).addr as * mut u8).offset((x * (*lock).pitch + y * 4) as isize) as *mut u32) =
((r as u32) << 16) + ((g as u32) << 8) + (b as u32);
#[inline(always)]
fn draw_pixel(&mut self, x: usize, y: usize, color: Rgb) {
unsafe {
*((self.addr as * mut u8).offset((y * self.pitch + x * 4) as isize) as *mut u32) =
((color.0 as u32) << 16) + ((color.1 as u32) << 8) + (color.2 as u32);
}
}
}
106 changes: 17 additions & 89 deletions kernel/src/console/mod.rs
Original file line number Diff line number Diff line change
@@ -1,94 +1,22 @@
mod framebuffer;

use limine::framebuffer::Framebuffer as LimineFrameBuffer;

Check warning on line 3 in kernel/src/console/mod.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `limine::framebuffer::Framebuffer as LimineFrameBuffer`
use noto_sans_mono_bitmap::{get_raster, FontWeight, RasterHeight};
use spin::Mutex;

struct Console {
width: u64,
height: u64,
cursor_x: u64,
cursor_y: u64,
}

impl Console {
pub const fn null() -> Console {
Console {
width: 0,
height: 0,
cursor_x: 0,
cursor_y: 0,
}
}

pub fn clear(&mut self) {
framebuffer::display_fill(0, 0, 0);
self.cursor_x = 0;
self.cursor_y = 0;
}

pub fn setchar(&mut self, x: u64, y: u64, c: char) {
let rc = get_raster(c, FontWeight::Regular, RasterHeight::Size16).unwrap();
for (i, di) in rc.raster().iter().enumerate() {
for (j, dj) in di.iter().enumerate() {
framebuffer::display_setpixel(x * 16 + i as u64, y * 7 + j as u64, *dj, *dj, *dj);
}
}
}

pub fn cr(&mut self) {
self.cursor_y = 0;
}

pub fn newline(&mut self) {
self.cr();
self.cursor_x += 1;
if self.cursor_x == self.width {
self.cursor_x = 0;
self.clear();
}
}

pub fn inc(&mut self) {
self.cursor_y += 1;
if self.cursor_y == self.width {
self.newline();
}
}

pub fn putchar(&mut self, c: char) {
match c {
'\n' => self.newline(),
'\r' => self.cr(),
oc => {
self.setchar(self.cursor_x, self.cursor_y, oc);
self.inc();
}
}
}

pub fn puts(&mut self, s: &str) {
for c in s.chars() {
self.putchar(c);
}
}
}

pub static console: Mutex<Console> = Mutex::new(Console::null());

pub fn init(fb: &LimineFrameBuffer) {
let mut lock = console.lock();
framebuffer::init(fb);
(*lock).width = fb.width() / 7;
(*lock).height = fb.height() / 16;
(*lock).clear();
}

impl core::fmt::Write for Console {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.puts(s);
Ok(())
}
use spin::{Lazy, Mutex};
use os_terminal::{Terminal, font::BitmapFont};
use framebuffer::{FrameBuffer, FRAMEBUFFER_REQUEST};
use alloc::boxed::Box;
use core::fmt::Write;

pub static TERMINAL: Lazy<Mutex<Terminal<FrameBuffer>>> = Lazy::new(|| {
let framebuffer_response = FRAMEBUFFER_REQUEST.get_response().unwrap();
let framebuffer = framebuffer_response.framebuffers().next().unwrap();
let mut terminal = Terminal::new(FrameBuffer::from_limine(&framebuffer));
terminal.set_font_manager(Box::new(BitmapFont));
Mutex::new(terminal)
});

pub fn init() {
Lazy::force(&TERMINAL);
}

#[macro_export]
Expand All @@ -103,5 +31,5 @@ macro_rules! println {
}

pub fn _print(args: core::fmt::Arguments) {
core::fmt::write(&mut *console.lock(), args);
TERMINAL.lock().write_fmt(args).unwrap();
}
2 changes: 1 addition & 1 deletion kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#![feature(abi_x86_interrupt)]
#![feature(slice_take)]

extern crate alloc;
pub mod acpi;
pub mod apic;
pub mod console;
pub mod cpu;
pub mod int;
pub mod mm;
pub mod pcie;
pub mod ata;
18 changes: 3 additions & 15 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ use limine::request::{
FramebufferRequest, HhdmRequest, RequestsEndMarker, RequestsStartMarker, RsdpRequest,
};
use limine::BaseRevision;
use DoglinkOS_2nd::console::init as init_terminal;
use DoglinkOS_2nd::acpi::{init as init_acpi, parse_madt};
use DoglinkOS_2nd::apic::{io::init as init_ioapic, local::init as init_lapic};
use DoglinkOS_2nd::console::init as init_console;
use DoglinkOS_2nd::cpu::show_cpu_info;
use DoglinkOS_2nd::int::init as init_interrupt;
use DoglinkOS_2nd::mm::init as init_mm;
use DoglinkOS_2nd::pcie::enumrate::doit;
use DoglinkOS_2nd::ata::init_and_test as test_ata;
use DoglinkOS_2nd::println;

#[used]
#[link_section = ".requests"]
static BASE_REVISION: BaseRevision = BaseRevision::new();

#[used]
#[link_section = ".requests"]
static FRAMEBUFFER_REQUEST: FramebufferRequest = FramebufferRequest::new();

#[used]
#[link_section = ".requests"]
static HHDM_REQUEST: HhdmRequest = HhdmRequest::new();
Expand All @@ -44,24 +39,17 @@ static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
#[no_mangle]
extern "C" fn kmain() -> ! {
assert!(BASE_REVISION.is_supported());

if let Some(framebuffer_response) = FRAMEBUFFER_REQUEST.get_response() {
if let Some(framebuffer) = framebuffer_response.framebuffers().next() {
init_console(&framebuffer);
}
}

println!("[INFO] Loading DoglinkOS GNU/MicroFish...");
let hhdm_response = HHDM_REQUEST.get_response().unwrap();
init_mm(&hhdm_response);
init_terminal();
println!("[INFO] Loading DoglinkOS GNU/MicroFish...");
init_interrupt();
init_lapic();
let rsdp_response = RSDP_REQUEST.get_response().unwrap();
unsafe { init_acpi(&rsdp_response) };
init_ioapic(parse_madt());
show_cpu_info();
doit();
test_ata();
hang();
}

Expand Down
Loading

0 comments on commit c145687

Please sign in to comment.