add repl environment to baremetal

This will help keep the command somewhat organized as things
are developed.

Also provides some templating for e.g. how to handle carriage
returns, etc.
This commit is contained in:
bunnie 2025-06-15 23:53:02 +08:00
parent 25afa24411
commit 1bff1eebf1
3 changed files with 111 additions and 14 deletions

View file

@ -6,6 +6,7 @@ extern crate alloc;
mod asm;
mod platform;
mod repl;
use alloc::collections::VecDeque;
use core::cell::RefCell;
@ -46,12 +47,12 @@ pub unsafe extern "C" fn rust_entry() -> ! {
rgb.wfo(utra::rgb::OUT_OUT, 0x002);
crate::platform::early_init();
crate::println!("\n~~Baremetal up!~~\n");
// select a BIO test to run
fifo_basic();
// hello_world();
let bio_ss = BioSharedState::new();
// The green LEDs flash whenever the FPGA is configured with the Arty BIO design.
// The RGB LEDs flash when the CPU is running this code.
let mut count = 0;
@ -59,25 +60,23 @@ pub unsafe extern "C" fn rust_entry() -> ! {
// provide some feedback on the run state of the BIO by peeking at the program counter
// value, and provide feedback on the CPU operation by flashing the RGB LEDs.
let mut repl = crate::repl::Repl::new();
loop {
crate::println!(
"pc: {:04x} {:04x} {:04x} {:04x}",
bio_ss.bio.r(utra::bio_bdma::SFR_DBG0),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG1),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG2),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG3)
);
rgb.wfo(utra::rgb::OUT_OUT, count);
delay(500);
count += 1;
// Handle keyboard events.
critical_section::with(|cs| {
let mut queue = UART_RX.borrow(cs).borrow_mut();
while let Some(byte) = queue.pop_front() {
crate::println!("got {}", unsafe { char::from_u32_unchecked(byte as u32) })
repl.rx_char(byte);
}
});
// Process any command line requests
repl.process();
// Animate the LED flashing to indicate repl loop is running
delay(1);
count += 1;
rgb.wfo(utra::rgb::OUT_OUT, ((count / 500) as u32) << 6);
}
}

View file

@ -164,7 +164,7 @@ pub extern "C" fn trap_handler(
_a7: usize,
) -> ! {
let cause: mcause::Mcause = mcause::read();
crate::println!("cause {:x}", cause.bits());
// crate::println!("cause {:x}", cause.bits());
// 2 is illegal instruction
if cause.bits() == 2 {
// skip past the illegal instruction, in case that's what we want to do...

98
baremetal/src/repl.rs Normal file
View file

@ -0,0 +1,98 @@
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use utralib::*;
use xous_bio_bdma::*;
pub struct Repl {
cmdline: String,
do_cmd: bool,
}
impl Repl {
pub fn new() -> Self { Self { cmdline: String::new(), do_cmd: false } }
pub fn rx_char(&mut self, c: u8) {
if c == b'\r' {
crate::println!("");
// carriage return
self.do_cmd = true;
} else if c == b'\x08' {
// backspace
crate::print!("\u{0008}");
if self.cmdline.len() != 0 {
self.cmdline.pop();
}
} else {
// everything else
match char::from_u32(c as u32) {
Some(c) => {
crate::print!("{}", c);
self.cmdline.push(c);
}
None => {
crate::println!("Warning: bad char received, ignoring")
}
}
}
}
pub fn process(&mut self) {
if !self.do_cmd {
return;
}
// crate::println!("got {}", self.cmdline);
let mut parts = self.cmdline.split_whitespace();
let cmd = parts.next().unwrap_or("").to_string();
let args: Vec<String> = parts.map(|s| s.to_string()).collect();
match cmd.as_str() {
"mon" => {
let bio_ss = BioSharedState::new();
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
let mut count = 0;
let mut quit = false;
const TICKS_PER_PRINT: usize = 5;
const TICK_MS: usize = 100;
while !quit {
// Hacky logic to create a 500ms interval on prints, but improve
// keyboard hit latency.
if count % TICKS_PER_PRINT == 0 {
crate::println!(
"pc: {:04x} {:04x} {:04x} {:04x}",
bio_ss.bio.r(utra::bio_bdma::SFR_DBG0),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG1),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG2),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG3)
);
rgb.wfo(utra::rgb::OUT_OUT, (count / TICKS_PER_PRINT) as u32);
}
crate::platform::delay(TICK_MS);
count += 1;
// just check and see if the keyboard was hit
critical_section::with(|cs| {
let queue = crate::UART_RX.borrow(cs).borrow_mut();
if queue.len() > 0 {
quit = true;
}
});
}
}
"echo" => {
for word in args {
crate::print!("{} ", word);
}
crate::println!("");
}
_ => {
crate::println!("Command not recognized: {}", cmd);
crate::println!("Commands include: echo, mon");
}
}
// reset for next loop
self.do_cmd = false;
self.cmdline.clear();
}
}