-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] replace deprecated sbi_rt legacy console operations in riscv (#…
…195) * [feat] replace deprecated sbi_rt legacy console operations in riscv platform * [refactor] use ax_console_read_bytes to replace ax_console_read_byte * [fix] do not expose putchar and getchar, set MAX_READ_SIZE in riscv console read * [fix] set MAX_RW_SIZE in riscv console read and write * [fix] use while in riscv console write_bytes * [feat] add try_write_bytes in riscv console
- Loading branch information
Showing
10 changed files
with
140 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,47 @@ | ||
use memory_addr::VirtAddr; | ||
|
||
use crate::mem::virt_to_phys; | ||
|
||
/// The maximum number of bytes that can be read at once. | ||
const MAX_RW_SIZE: usize = 256; | ||
|
||
/// Writes a byte to the console. | ||
pub fn putchar(c: u8) { | ||
#[allow(deprecated)] | ||
sbi_rt::legacy::console_putchar(c as usize); | ||
sbi_rt::console_write_byte(c); | ||
} | ||
|
||
/// Reads a byte from the console, or returns [`None`] if no input is available. | ||
pub fn getchar() -> Option<u8> { | ||
#[allow(deprecated)] | ||
match sbi_rt::legacy::console_getchar() as isize { | ||
-1 => None, | ||
c => Some(c as u8), | ||
/// Tries to write bytes to the console from input u8 slice. | ||
/// Returns the number of bytes written. | ||
fn try_write_bytes(bytes: &[u8]) -> usize { | ||
sbi_rt::console_write(sbi_rt::Physical::new( | ||
// A maximum of 256 bytes can be written at a time | ||
// to prevent SBI from disabling IRQs for too long. | ||
bytes.len().min(MAX_RW_SIZE), | ||
virt_to_phys(VirtAddr::from_ptr_of(bytes.as_ptr())).as_usize(), | ||
0, | ||
)) | ||
.value | ||
} | ||
|
||
/// Writes bytes to the console from input u8 slice. | ||
pub fn write_bytes(bytes: &[u8]) { | ||
let mut write_len = 0; | ||
while write_len < bytes.len() { | ||
let len = try_write_bytes(&bytes[write_len..]); | ||
if len == 0 { | ||
break; | ||
} | ||
write_len += len; | ||
} | ||
} | ||
|
||
/// Reads bytes from the console into the given mutable slice. | ||
/// Returns the number of bytes read. | ||
pub fn read_bytes(bytes: &mut [u8]) -> usize { | ||
sbi_rt::console_read(sbi_rt::Physical::new( | ||
bytes.len().min(MAX_RW_SIZE), | ||
virt_to_phys(VirtAddr::from_mut_ptr_of(bytes.as_mut_ptr())).as_usize(), | ||
0, | ||
)) | ||
.value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters