feat(vga): add simple vga interface
Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
parent
b9db05978f
commit
77f6a31381
26
Cargo.lock
generated
26
Cargo.lock
generated
@ -5,3 +5,29 @@ version = 3
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "julios"
|
name = "julios"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"spin",
|
||||||
|
"volatile",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
dependencies = [
|
||||||
|
"spin",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spin"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "volatile"
|
||||||
|
version = "0.2.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
|
||||||
|
@ -7,3 +7,11 @@ edition = "2018"
|
|||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["staticlib"]
|
crate-type = ["staticlib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
volatile = "0.2.6"
|
||||||
|
spin = "0.5.2"
|
||||||
|
|
||||||
|
[dependencies.lazy_static]
|
||||||
|
version = "1.0"
|
||||||
|
features = ["spin_no_std"]
|
||||||
|
38
src/lib.rs
38
src/lib.rs
@ -2,40 +2,16 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(_info: &core::panic::PanicInfo) -> !
|
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||||
{
|
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
static HELLO: &[u8] = b"Welcome to the JuliOS";
|
use core::fmt::Write;
|
||||||
|
mod vga;
|
||||||
static STAR: &[u8] = b"|/-\\";
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn julios_main() -> !
|
pub extern "C" fn julios_main() -> ! {
|
||||||
{
|
vga::WRITER.lock().write_str("Hello").unwrap();
|
||||||
let vga_buffer: *mut u8 = 0xb8000 as *mut u8;
|
write!(vga::WRITER.lock(), " {}!\n{}", "World", "***JuliOS***").unwrap();
|
||||||
|
loop {}
|
||||||
let mut k: usize = 0;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
k = i * 2 + 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut j: usize = 0;
|
|
||||||
loop
|
|
||||||
{
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
*vga_buffer.offset(k as isize) = STAR[j];
|
|
||||||
*vga_buffer.offset(k as isize + 1) = 0xb;
|
|
||||||
j += 1;
|
|
||||||
j %= 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
125
src/vga.rs
Normal file
125
src/vga.rs
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
use volatile::Volatile;
|
||||||
|
use core::fmt::{self, Write};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use spin::Mutex;
|
||||||
|
|
||||||
|
const BUFFER_HEIGHT: usize = 25;
|
||||||
|
const BUFFER_WIDTH: usize = 80;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||||
|
column: 0,
|
||||||
|
color_code: ColorCode::new(Color::Green, Color::Black),
|
||||||
|
buffer: unsafe { &mut *(0xb8000 as *mut VgaBuffer) }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
#[repr(u8)]
|
||||||
|
pub enum Color {
|
||||||
|
Black = 0x0,
|
||||||
|
Blue = 0x1,
|
||||||
|
Green = 0x2,
|
||||||
|
Cyan = 0x3,
|
||||||
|
Red = 0x4,
|
||||||
|
Magenta = 0x5,
|
||||||
|
Brown = 0x6,
|
||||||
|
LightGray = 0x7,
|
||||||
|
DarkGray = 0x8,
|
||||||
|
LightBlue = 0x9,
|
||||||
|
LightGreen = 0xa,
|
||||||
|
LightCyan = 0xb,
|
||||||
|
LightRed = 0xc,
|
||||||
|
Pink = 0xd,
|
||||||
|
Yellow = 0xe,
|
||||||
|
White = 0xf,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
#[repr(transparent)]
|
||||||
|
struct ColorCode(u8);
|
||||||
|
|
||||||
|
impl ColorCode {
|
||||||
|
fn new(fg: Color, bg: Color) -> ColorCode {
|
||||||
|
ColorCode((bg as u8) << 4 | (fg as u8))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
#[repr(C)]
|
||||||
|
struct VgaChar {
|
||||||
|
character: u8,
|
||||||
|
color_code: ColorCode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(transparent)]
|
||||||
|
struct VgaBuffer {
|
||||||
|
chars: [[Volatile<VgaChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Writer {
|
||||||
|
column: usize,
|
||||||
|
color_code: ColorCode,
|
||||||
|
buffer: &'static mut VgaBuffer,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Writer {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_byte(&mut self, byte: u8) {
|
||||||
|
match byte {
|
||||||
|
b'\n' => self.new_line(),
|
||||||
|
byte => {
|
||||||
|
if self.column >= BUFFER_WIDTH {
|
||||||
|
self.new_line();
|
||||||
|
}
|
||||||
|
|
||||||
|
let row = BUFFER_HEIGHT - 1;
|
||||||
|
let col = self.column;
|
||||||
|
|
||||||
|
let color_code = self.color_code;
|
||||||
|
self.buffer.chars[row][col].write(VgaChar {
|
||||||
|
character: byte,
|
||||||
|
color_code
|
||||||
|
});
|
||||||
|
self.column += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_row(&mut self, row: usize) {
|
||||||
|
let blank = VgaChar {
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user