re architectures drivers

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
juju committed 2022-12-10 18:41:26 +01:00
1 parent 9ae31eb2f6
commit c85e7b5e5a
6 files changed
+7 -6

No files matched your search

View File
Whitespace-only changes.
+2
View File
@@ -0,0 +1,2 @@
pub mod serial;
pub mod vga;
+138
View File
@@ -0,0 +1,138 @@
use core::fmt;
use lazy_static::lazy_static;
use spin::Mutex;
use x86_64::instructions::port::Port;
const COM1: u16 = 0x3f8;
#[allow(dead_code)]
const COM2: u16 = 0x2f8;
#[allow(dead_code)]
const COM3: u16 = 0x3e8;
#[allow(dead_code)]
const COM4: u16 = 0x2e8;
const ENABLE_TRANSMITTER: u8 = 0x1 << 1;
const FIFO: u8 = 0x1;
const TRIGGER_LVL_14: u8 = 0x3 << 6;
const CLEAR_TRANSMIT_FIFO: u8 = 0x1 << 2;
const CLEAR_RECEIVE_FIFO: u8 = 0x1 << 1;
const NO_PARITY: u8 = 0x0;
const EIGHT_BITS_LENGTH: u8 = 0x3;
const EMPTY_TRANSMITTER: u8 = 0x1 << 5;
const DLAB: u8 = 0x1 << 7;
lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = {
let mut serial_port = SerialPort::new(COM1);
serial_port.init();
Mutex::new(serial_port)
};
}
#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
SERIAL1
.lock()
.write_fmt(args)
.expect("Printing to serial failed");
});
}
/// Prints to the host through the serial interface.
#[macro_export]
macro_rules! serial_print {
($($arg:tt)*) => {
$crate::drivers::serial::_print(format_args!($($arg)*));
};
}
/// Prints to the host through the serial interface, appending a newline.
#[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)*));
}
pub struct SerialPort {
base: Port<u8>,
interrupt_enable: Port<u8>,
fifo_control: Port<u8>,
line_control: Port<u8>,
line_status: Port<u8>,
}
impl SerialPort {
pub fn new(port: u16) -> SerialPort {
SerialPort {
base: Port::new(port),
interrupt_enable: Port::new(port + 1),
fifo_control: Port::new(port + 2),
line_control: Port::new(port + 3),
line_status: Port::new(port + 5),
}
}
fn write_byte(&mut self, byte: u8) -> bool {
unsafe {
let status: u8 = self.line_status.read();
match status & EMPTY_TRANSMITTER {
0 => false,
_ => {
self.base.write(byte);
true
}
}
}
}
fn write_string(&mut self, s: &str) -> usize {
let mut len: usize = 0;
for byte in s.bytes() {
let mut written: bool = self.write_byte(byte);
while !written {
written = self.write_byte(byte);
}
len += 1;
}
len
}
fn init_baud_rate(&mut self) {
let line: u8;
unsafe {
line = self.line_control.read();
self.line_control.write(line | DLAB);
self.base.write(3);
self.interrupt_enable.write(0);
self.line_control.write(line);
}
}
fn init(&mut self) {
self.init_baud_rate();
unsafe {
self.line_control.write(NO_PARITY | EIGHT_BITS_LENGTH);
self.fifo_control
.write(FIFO | TRIGGER_LVL_14 | CLEAR_TRANSMIT_FIFO | CLEAR_RECEIVE_FIFO);
self.interrupt_enable.write(ENABLE_TRANSMITTER);
}
}
}
impl fmt::Write for SerialPort {
fn write_str(&mut self, s: &str) -> fmt::Result {
let len: usize = self.write_string(s);
match len {
l if l == s.len() => Ok(()),
_ => Err(fmt::Error),
}
}
}
+160
View File
@@ -0,0 +1,160 @@
use core::fmt;
use lazy_static::lazy_static;
use spin::Mutex;
use volatile::Volatile;
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::LightGreen, Color::Black),
buffer: unsafe { &mut *(0xb8000 as *mut VgaBuffer) }
});
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::drivers::vga::_print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
WRITER.lock().write_fmt(args).unwrap();
});
}
pub fn change_color(color: ColorCode) {
WRITER.lock().change_color(color)
}
pub fn get_color() -> ColorCode {
WRITER.lock().get_color()
}
#[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)]
pub struct ColorCode(u8);
impl ColorCode {
pub 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);
}
}
fn change_color(&mut self, color: ColorCode) {
self.color_code = color;
}
fn get_color(&mut self) -> ColorCode {
self.color_code
}
}
impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_string(s);
Ok(())
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.