diff --git a/see/src/extensions.rs b/see/src/extensions.rs index 87a3276..1e3b8ab 100644 --- a/see/src/extensions.rs +++ b/see/src/extensions.rs @@ -1,3 +1,4 @@ +use core::sync::atomic::{AtomicU8, Ordering}; use hal::{ clint::{msip, mtimecmp}, pac::UART0, @@ -17,11 +18,7 @@ pub fn init() { rustsbi::init_ipi(&Ipi); } -impl rustsbi::legacy_stdio::LegacyStdio for LegacyConsole { - fn getchar(&self) -> u8 { - unimplemented!() - } - +impl LegacyConsole { fn putchar(&self, ch: u8) { let uart = unsafe { &*UART0::ptr() }; // 等待 FIFO 空位 @@ -32,6 +29,20 @@ impl rustsbi::legacy_stdio::LegacyStdio for LegacyConsole { } } +impl rustsbi::legacy_stdio::LegacyStdio for LegacyConsole { + fn getchar(&self) -> u8 { + unimplemented!() + } + + fn putchar(&self, ch: u8) { + static LAST: AtomicU8 = AtomicU8::new(0); + if ch == b'\n' && LAST.swap(ch, Ordering::Relaxed) != b'\r' { + self.putchar(b'\r'); + } + self.putchar(ch); + } +} + impl rustsbi::Timer for Timer { fn set_timer(&self, stime_value: u64) { mtimecmp::write(stime_value); diff --git a/spl/src/logging.rs b/spl/src/logging.rs index e80ecd4..3729f8a 100644 --- a/spl/src/logging.rs +++ b/spl/src/logging.rs @@ -1,4 +1,7 @@ -use core::ops::Shl; +use core::{ + ops::Shl, + sync::atomic::{AtomicU8, Ordering}, +}; use hal::pac::UART0; pub struct Out; @@ -10,17 +13,27 @@ pub enum Hex { Fmt(usize), } +impl Out { + fn putchar(&self, ch: u8) { + let uart = unsafe { &*UART0::ptr() }; + // 等待 FIFO 空位 + while uart.usr.read().tfnf().is_full() { + core::hint::spin_loop(); + } + uart.thr().write(|w| w.thr().variant(ch)); + } +} + impl Shl for Out { type Output = Self; #[inline] fn shl(self, rhs: u8) -> Self::Output { - let uart = unsafe { &*UART0::ptr() }; - // 等待 FIFO 空位 - while uart.usr.read().tfnf().is_full() { - core::hint::spin_loop(); + static LAST: AtomicU8 = AtomicU8::new(0); + if rhs == b'\n' && LAST.swap(rhs, Ordering::Relaxed) != b'\r' { + self.putchar(b'\r'); } - uart.thr().write(|w| w.thr().variant(rhs)); + self.putchar(rhs); self } } diff --git a/xtask/src/components.rs b/xtask/src/components.rs index f0850d8..cf5208c 100644 --- a/xtask/src/components.rs +++ b/xtask/src/components.rs @@ -1,5 +1,5 @@ use crate::{xfel::Xfel, AsmArg, FlashArgs, Package, Target, XError, DIRS}; -use command_ext::{CommandExt, Ext}; +use command_ext::{dir, CommandExt, Ext}; use common::uninit; use std::{ ffi::OsStr, @@ -68,6 +68,7 @@ impl Components { .target .join(dt.file_stem().unwrap_or_else(|| OsStr::new("nezha"))) .with_extension("dtb"); + dir::create_parent(&dtb).unwrap(); Ext::new("dtc").arg("-o").arg(&dtb).arg(&dt).invoke(); ans.dtb.replace(dtb); } else { @@ -98,6 +99,7 @@ impl Components { let path = if output.is_dir() { output.join(package.name()).with_extension("asm") } else { + dir::create_parent(&output).unwrap(); output }; // 保存