Update library

Changed memory I/O layout a bit
master
D4VID 3 months ago
parent 2c8863208e
commit 866c53bfb6

@ -8,18 +8,18 @@ pub const NUMBER_DISPLAY_OFFSET : u32 = 0x00;
pub const RANDOM_NUMBER_OFFSET : u32 = 0x04;
pub const BUTTONS_OFFSET : u32 = 0x08;
pub const KEYBOARD_OFFSET : u32 = 0x10;
pub const SCREEN_CONTROL_OFFSET : u32 = 0x20;
pub const TEXT_CONTROL_OFFSET : u32 = 0x24;
pub const TEXT_DISPLAY_OFFSET : u32 = 0x28;
pub const CHAR_CONTROL_OFFSET : u32 = 0x20;
pub const SCREEN_CONTROL_OFFSET : u32 = 0x22;
pub const CHAR_DISPLAY_OFFSET : u32 = 0x24;
pub const SCREEN_OFFSET : u32 = 0x80;
pub const NUMBER_DISPLAY_POINTER : *mut u32 = (BASE_POINTER + NUMBER_DISPLAY_OFFSET) as *mut u32;
pub const RANDOM_NUMBER_POINTER : *mut u32 = (BASE_POINTER + RANDOM_NUMBER_OFFSET) as *mut u32;
pub const BUTTONS_POINTER : *mut u8 = (BASE_POINTER + BUTTONS_OFFSET) as *mut u8;
pub const TEXT_DISPLAY_POINTER : *mut u8 = (BASE_POINTER + TEXT_DISPLAY_OFFSET) as *mut u8;
pub const CHAR_DISPLAY_POINTER : *mut u8 = (BASE_POINTER + CHAR_DISPLAY_OFFSET) as *mut u8;
pub const KEYBOARD_POINTER : *mut u32 = (BASE_POINTER + KEYBOARD_OFFSET) as *mut u32;
pub const SCREEN_CONTROL_POINTER : *mut u32 = (BASE_POINTER + SCREEN_CONTROL_OFFSET) as *mut u32;
pub const TEXT_CONTROL_POINTER : *mut u32 = (BASE_POINTER + TEXT_CONTROL_OFFSET) as *mut u32;
pub const SCREEN_CONTROL_POINTER : *mut u16 = (BASE_POINTER + SCREEN_CONTROL_OFFSET) as *mut u16;
pub const CHAR_CONTROL_POINTER : *mut u16 = (BASE_POINTER + CHAR_CONTROL_OFFSET) as *mut u16;
pub const SCREEN_POINTER : *mut u32 = (BASE_POINTER + SCREEN_OFFSET) as *mut u32;
@ -30,16 +30,16 @@ pub enum ScreenMode {
pub struct Screen {}
impl Screen {
const SCREEN_CLEAR : u32 = 0x0000_0001;
const SCREEN_DISPLAY : u32 = 0x0000_0100;
const CLEAR : u16 = 0x0001;
const DISPLAY : u16 = 0x0100;
#[inline]
pub fn init(mode: ScreenMode) {
unsafe {
write_volatile(SCREEN_CONTROL_POINTER, Self::SCREEN_CLEAR | Self::SCREEN_DISPLAY);
write_volatile(SCREEN_CONTROL_POINTER, Self::CLEAR | Self::DISPLAY);
match mode {
ScreenMode::AlwaysDisplay => {
write_volatile(SCREEN_CONTROL_POINTER, Self::SCREEN_DISPLAY);
write_volatile(SCREEN_CONTROL_POINTER, Self::DISPLAY);
},
ScreenMode::Buffered => {
write_volatile(SCREEN_CONTROL_POINTER, 0);
@ -98,32 +98,42 @@ impl Screen {
}
}
pub struct TextDisplay {}
impl TextDisplay {
const TEXT_CLEAR : u32 = 0x0000_0001;
const TEXT_DISPLAY : u32 = 0x0000_0100;
pub struct CharDisplay {}
impl CharDisplay {
const CLEAR : u16 = 0x0001;
const DISPLAY : u16 = 0x0100;
#[inline]
pub fn init(&self) {
unsafe {
write_volatile(TEXT_CONTROL_POINTER, Self::TEXT_CLEAR); // hide contents and clear
write_volatile(TEXT_CONTROL_POINTER, Self::TEXT_DISPLAY); // display contents
write_volatile(CHAR_CONTROL_POINTER, Self::CLEAR); // hide contents and clear
write_volatile(CHAR_CONTROL_POINTER, Self::DISPLAY); // display contents
}
}
#[inline]
pub fn put_char(ch: u8) {
pub fn put_char(&self, ch: u8) {
unsafe {
write_volatile(TEXT_DISPLAY_POINTER, ch);
write_volatile(CHAR_DISPLAY_POINTER, ch);
}
}
#[inline]
pub fn hide(&self) {
unsafe {
write_volatile((CHAR_CONTROL_POINTER as *mut u8).byte_offset(1), 0);
}
}
#[inline]
pub fn show(&self) {
unsafe {
write_volatile((CHAR_CONTROL_POINTER as *mut u8).byte_offset(1), 1);
}
}
}
impl core::fmt::Write for TextDisplay {
impl core::fmt::Write for CharDisplay {
#[inline]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for ch in s.bytes() {
unsafe {
write_volatile(TEXT_DISPLAY_POINTER, ch);
}
self.put_char(ch);
}
return Ok(());
}

@ -1,8 +1,9 @@
#![no_std]
#![no_main]
use core::{panic::PanicInfo};
use lwcpu::{NumberDisplay, TextDisplay};
use core::fmt::Write;
use core::panic::PanicInfo;
use lwcpu::{NumberDisplay, CharDisplay};
use riscv_rt::entry;
const SEGMENT_MAX: usize = 8;
@ -60,8 +61,8 @@ fn segment_sieve(primes: &[usize], prime_multiple: &mut [usize]) {
#[panic_handler]
fn panic_handler(_info: &PanicInfo) -> ! {
NumberDisplay::display_number(0xDEADC0DE);
// let mut text = TextDisplay{};
// let _ = write!(text, "Panic: {}", _info.message());
let mut text = CharDisplay{};
let _ = write!(text, "Panic: {}", _info.message());
loop {}
}

@ -2,13 +2,14 @@
#![no_main]
use core::{fmt::Write, panic::PanicInfo, ptr::{read_volatile, write_volatile}};
use lwcpu::{NumberDisplay, Screen, ScreenMode, TextDisplay, SCREEN_POINTER};
use lwcpu::{NumberDisplay, Screen, ScreenMode, CharDisplay, SCREEN_POINTER};
use riscv_rt::entry;
#[panic_handler]
fn panic_handler(_info: &PanicInfo) -> ! {
NumberDisplay::display_number(0xDEADC0DE);
let mut text = TextDisplay{};
let mut text = CharDisplay{};
text.init();
let _ = write!(text, "Panic: {} {}", _info.message(), 1234);
loop {}
}
@ -19,7 +20,7 @@ const HELLO_WORLD: &str = " Hello world!";
fn main() -> ! {
NumberDisplay::display_number(0);
Screen::init(ScreenMode::Buffered);
let mut text = TextDisplay{};
let mut text = CharDisplay{};
text.init();
for i in 0..32 {
Screen::set_line(i, 1 << i);
@ -35,8 +36,12 @@ fn main() -> ! {
let b = read_volatile(ptr.offset(1));
write_volatile(ptr.offset(5), b);
}
let _ = text.write_str(HELLO_WORLD);
for _ in 0..4 {
text.hide();
text.show();
}
loop {
let _ = text.write_str(HELLO_WORLD);
panic!("Lmao")
}
}

Loading…
Cancel
Save