1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 00:16:17 +04:00

aarch64/fb: add 8x16 font

This commit is contained in:
equation314 2018-12-18 17:51:22 +08:00
parent b47c4758bf
commit 7b8359eeef
6 changed files with 4689 additions and 51 deletions

View File

@ -198,6 +198,6 @@ pub fn init() {
info!("framebuffer: init end\n{:#x?}", fb);
*FRAME_BUFFER.lock() = Some(fb);
}
Err(err) => error!("framebuffer init failed: {}", err),
Err(err) => warn!("framebuffer init failed: {}", err),
}
}

View File

@ -1,9 +1,12 @@
//! Framebuffer console display driver for ARM64
use super::board::fb::{FramebufferInfo, FRAME_BUFFER};
use super::fb::{FramebufferInfo, FRAME_BUFFER};
use super::fonts::{Font, Font8x16};
use alloc::vec::Vec;
use core::fmt::{self, Write};
use core::fmt;
use core::marker::PhantomData;
use lazy_static::lazy_static;
use log::*;
use spin::Mutex;
#[derive(Debug, Clone, Copy)]
@ -25,41 +28,35 @@ impl Default for ConsoleChar {
}
}
const CHAR_WIDTH: u16 = 8;
const CHAR_HEIGHT: u16 = 16;
/// Character buffer
struct ConsoleBuffer {
num_row: u16,
num_col: u16,
struct ConsoleBuffer<F: Font> {
num_row: usize,
num_col: usize,
buf: Vec<Vec<ConsoleChar>>,
font: PhantomData<F>,
}
impl ConsoleBuffer {
fn new(num_row: u16, num_col: u16) -> ConsoleBuffer {
impl<F: Font> ConsoleBuffer<F> {
fn new(num_row: usize, num_col: usize) -> ConsoleBuffer<F> {
ConsoleBuffer {
num_row,
num_col,
buf: vec![vec![ConsoleChar::default(); num_col as usize]; num_row as usize],
buf: vec![vec![ConsoleChar::default(); num_col]; num_row],
font: PhantomData,
}
}
/// Read one character at `(row, col)`.
fn read(&self, row: u16, col: u16) -> ConsoleChar {
self.buf[row as usize][col as usize]
}
/// Write one character at `(row, col)`.
/// TODO: font & color
fn write(&mut self, row: u16, col: u16, ch: ConsoleChar) {
self.buf[row as usize][col as usize] = ch;
/// TODO: color
fn write(&mut self, row: usize, col: usize, ch: ConsoleChar) {
self.buf[row][col] = ch;
let off_x = col * CHAR_WIDTH;
let off_y = row * CHAR_HEIGHT;
let off_x = col * F::WIDTH;
let off_y = row * F::HEIGHT;
if let Some(fb) = FRAME_BUFFER.lock().as_mut() {
for y in 0..CHAR_HEIGHT {
for x in 0..CHAR_WIDTH {
let pixel = if ch.color.0 == 0 { 0 } else { !0 };
for y in 0..F::HEIGHT {
for x in 0..F::WIDTH {
let pixel = if ch.color.0 != 0 && F::get(ch.ascii_char, x, y) { !0 } else { 0 };
fb.write((off_x + x) as u32, (off_y + y) as u32, pixel);
}
}
@ -67,7 +64,7 @@ impl ConsoleBuffer {
}
/// Delete one character at `(row, col)`.
fn delete(&mut self, row: u16, col: u16) {
fn delete(&mut self, row: usize, col: usize) {
self.write(row, col, ConsoleChar::default());
}
@ -76,7 +73,7 @@ impl ConsoleBuffer {
fn new_line(&mut self) {
for i in 1..self.num_row {
for j in 0..self.num_col {
self.write(i - 1, j, self.read(i, j));
self.write(i - 1, j, self.buf[i][j]);
}
}
for j in 0..self.num_col {
@ -86,8 +83,8 @@ impl ConsoleBuffer {
/// Clear the entire buffer and screen.
fn clear(&mut self) {
for i in 0..self.num_row as usize {
for j in 0..self.num_col as usize {
for i in 0..self.num_row {
for j in 0..self.num_col {
self.buf[i][j] = ConsoleChar::default()
}
}
@ -98,38 +95,32 @@ impl ConsoleBuffer {
}
/// Console structure
pub struct Console {
pub struct Console<F: Font> {
/// current color
color: Color,
/// cursor row
row: u16,
row: usize,
/// cursor column
col: u16,
/// number of rows
num_row: u16,
/// number of columns
num_col: u16,
col: usize,
/// character buffer
buf: ConsoleBuffer,
buf: ConsoleBuffer<F>,
}
impl Console {
fn new(fb: &FramebufferInfo) -> Console {
let num_row = fb.yres as u16 / CHAR_HEIGHT;
let num_col = fb.xres as u16 / CHAR_WIDTH;
impl<F: Font> Console<F> {
fn new(fb: &FramebufferInfo) -> Console<F> {
let num_row = fb.yres as usize / F::HEIGHT;
let num_col = fb.xres as usize / F::WIDTH;
Console {
color: Color(1),
row: 0,
col: 0,
num_row,
num_col,
buf: ConsoleBuffer::new(num_row, num_col),
}
}
fn new_line(&mut self) {
self.col = 0;
if self.row < self.num_row - 1 {
if self.row < self.buf.num_row - 1 {
self.row += 1;
} else {
self.buf.new_line();
@ -144,14 +135,14 @@ impl Console {
self.buf.delete(self.row, self.col);
} else if self.row > 0 {
self.row -= 1;
self.col = self.num_col - 1;
self.col = self.buf.num_col - 1;
self.buf.delete(self.row, self.col);
}
}
b'\n' => self.new_line(),
b'\r' => self.col = 0,
byte => {
if self.col >= self.num_col {
if self.col >= self.buf.num_col {
self.new_line();
}
@ -173,7 +164,7 @@ impl Console {
}
}
impl fmt::Write for Console {
impl<F: Font> fmt::Write for Console<F> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for byte in s.bytes() {
self.write_byte(byte)
@ -183,7 +174,7 @@ impl fmt::Write for Console {
}
lazy_static! {
pub static ref CONSOLE: Mutex<Option<Console>> = Mutex::new(None);
pub static ref CONSOLE: Mutex<Option<Console<Font8x16>>> = Mutex::new(None);
}
/// Initialize console driver
@ -192,7 +183,9 @@ pub fn init() {
*CONSOLE.lock() = Some(Console::new(&fb.fb_info));
}
if let Some(console) = CONSOLE.lock().as_mut() {
console.write_str("Hello Raspberry Pi!\n").unwrap();
if !CONSOLE.lock().is_none() {
info!("console: init end");
} else {
warn!("console: init failed");
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
//! Console font
mod font8x16;
pub use self::font8x16::Font8x16;
pub trait Font {
const HEIGHT: usize;
const WIDTH: usize;
fn get(byte: u8, x: usize, y: usize) -> bool;
}

View File

@ -3,6 +3,10 @@
use super::board;
use once::*;
pub use self::board::fb;
pub use self::board::serial;
pub mod fonts;
pub mod console;
/// Initialize ARM64 common drivers

View File

@ -1,6 +1,6 @@
//! Serial driver for aarch64.
use super::board::serial::*;
use super::driver::serial::*;
use super::driver::console::CONSOLE;
use core::fmt::{Arguments, Write};