Merge pull request #683 from betrusted-io/verilate-fixes

Verilate fixes
This commit is contained in:
bunnie 2025-09-20 22:41:45 +08:00 committed by GitHub
commit 8d19be2784
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 105 additions and 30 deletions

View file

@ -2,7 +2,11 @@
// SPDX-FileCopyrightText: 2024 bunnie <bunnie@kosagi.com>
// SPDX-License-Identifier: Apache-2.0
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "verilator-only")))]
#[path = "rand.rs"]
pub mod rand;
#[cfg(all(feature = "bao1x", feature = "verilator-only"))]
#[path = "rand_fake.rs"]
pub mod rand;
#[cfg(any(feature = "debug-print", feature = "print-panics"))]

View file

@ -24,7 +24,9 @@ static LOCAL_RNG_STATE: [AtomicU32; 8] = [
pub fn init() {}
/// This a fully deterministic PRNG that relies on Chacha8 for state evolution.
/// This a fully deterministic PRNG that relies on Chacha8 for state evolution. This is
/// used in verilator simulation environments.
#[cfg(feature = "verilator-only")]
pub fn get_u32() -> u32 {
// Local storage for the seed.
let mut seed = [0u8; 32];

View file

@ -51,6 +51,7 @@ board-baosec = [
"bmp180",
"utralib/bao1x",
"ux-api/board-baosec",
"ux-api/std",
] # USB form factor token
loader-baosec = [
"utralib/bao1x",
@ -66,9 +67,11 @@ kernel-dabao = ["utralib/bao1x"]
test-baosec = ["ux-api/loader-baosec", "camera-gc2145"]
hosted-baosec = ["std"] # emulated hardware on x86 platform
board-baosor = [
"std",
"camera-gc2145",
"axp2101",
"utralib/bao1x",
"ux-api/std",
] # Precursor form factor
loader-baosor = []
board-dabao = ["utralib/bao1x"] # Dev board form factor
@ -89,7 +92,6 @@ std = [
"xous-api-names",
"usb-device",
"xous-api-ticktimer",
"ux-api/std",
"bao1x-api/std",
]
derive-rkyv = ["rkyv"]

View file

@ -1,6 +1,6 @@
// Constants that define pin locations, RAM offsets, etc. for the DaBao basic breakout board
use crate::iox::IoSetup;
use crate::iox::*;
use bao1x_api::*;
// console uart buffer
pub const UART_DMA_TX_BUF_PHYS: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4096;
@ -42,7 +42,7 @@ pub fn setup_usb_pins<T: IoSetup + IoGpio>(iox: &T) -> (IoxPort, u8) {
(IoxPort::PB, SE0_PIN)
}
pub fn setup_i2c_pins(iox: &dyn IoSetup) -> crate::udma::I2cChannel {
pub fn setup_i2c_pins(iox: &dyn IoSetup) -> bao1x_api::I2cChannel {
// I2C_SCL_B[0]
iox.setup_pin(
IoxPort::PB,
@ -65,5 +65,12 @@ pub fn setup_i2c_pins(iox: &dyn IoSetup) -> crate::udma::I2cChannel {
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
crate::udma::I2cChannel::Channel0
bao1x_api::I2cChannel::Channel0
}
// these are a bodge to allow some UDMA imports to work - they need to be present even if
// there currently no SPINOR devices attached.
pub const SPINOR_PAGE_LEN: u32 = 0x100;
pub const SPINOR_ERASE_SIZE: u32 = 0x1000; // this is the smallest sector size.
pub const SPINOR_BULK_ERASE_SIZE: u32 = 0x1_0000; // this is the bulk erase size.
pub const SPINOR_LEN: u32 = 16384 * 1024;

View file

@ -16,9 +16,9 @@ pub use baosec::*;
pub mod baosor;
#[cfg(any(feature = "board-baosor", feature = "loader-baosor"))]
pub use baosor::*;
#[cfg(any(feature = "board-dabao", feature = "loader-dabao"))]
#[cfg(any(feature = "board-dabao", feature = "loader-dabao", feature = "kernel-dabao"))]
pub mod dabao;
#[cfg(any(feature = "board-dabao", feature = "loader-dabao"))]
#[cfg(any(feature = "board-dabao", feature = "loader-dabao", feature = "kernel-dabao"))]
pub use dabao::*;
#[cfg(feature = "hosted-baosec")]
pub mod hosted;

View file

@ -66,13 +66,13 @@ impl Uart {
use core::fmt::{Error, Write};
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
fn write_str(&mut self, _s: &str) -> Result<(), Error> {
#[cfg(not(all(feature = "std", not(feature = "debug-print-usb"))))]
for c in s.bytes() {
for c in _s.bytes() {
self.putc(c);
}
// #[cfg(all(feature = "std", not(feature = "debug-print-usb")))]
// log::info!("{}", s);
// log::info!("{}", _s);
Ok(())
}
}

View file

@ -106,7 +106,7 @@ board-baosec = [
"ux-api/loader-baosec",
]
board-baosor = ["bao1x-hal/loader-baosor"]
board-dabao = ["bao1x-hal/loader-dabao"]
board-dabao = ["bao1x-hal/loader-dabao", "bao1x-hal/board-dabao", "linked_list_allocator",]
atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"]
# precursor flags

View file

@ -10,6 +10,7 @@ use bao1x_hal::udma;
#[cfg(any(feature = "board-baosec", feature = "board-baosor"))]
use bao1x_hal::{axp2101::Axp2101, udma::GlobalConfig};
use utralib::generated::*;
#[cfg(not(feature = "verilator-only"))]
use utralib::utra::sysctrl;
#[cfg(feature = "qr")]
@ -2199,6 +2200,7 @@ pub fn coreuser_config() {
/// Takes in the top clock in MHz, desired perclk in MHz, and returns a tuple of
/// (min cycle, fd, actual freq)
/// *tested*
#[allow(dead_code)]
pub fn clk_to_per(top_in_mhz: u32, perclk_in_mhz: u32) -> Option<(u8, u8, u32)> {
let fd_platonic = ((256 * perclk_in_mhz) / (top_in_mhz / 2)).min(256);
if fd_platonic > 0 {
@ -2218,6 +2220,7 @@ pub fn clk_to_per(top_in_mhz: u32, perclk_in_mhz: u32) -> Option<(u8, u8, u32)>
}
/// Delay with a given system clock frequency. Useful during power mode switching.
#[allow(dead_code)]
pub fn delay_at_sysfreq(ms: usize, sysclk_freq: u32) {
let mut timer = utralib::CSR::new(utra::timer0::HW_TIMER0_BASE as *mut u32);
timer.wfo(utra::timer0::EN_EN, 0b0); // disable the timer

View file

@ -44,6 +44,7 @@ board-baosec = ["bao1x-hal/board-baosec"]
board-baosor = ["bao1x-hal/board-baosor"]
board-dabao = ["bao1x-hal/board-dabao"]
hosted-baosec = []
hwsim = []
precursor = ["utralib/precursor"]
hosted = ["utralib/hosted"]

View file

@ -1,6 +1,6 @@
use core::fmt::{Error, Write};
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use bao1x_hal::udma;
#[macro_export]
@ -30,7 +30,7 @@ pub struct Uart {}
// this is a hack to bypass an explicit initialization/allocation step for the debug structure
pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
impl Uart {
pub fn putc(&self, c: u8) {
// check that we've been initialized before attempting to send any characters...
@ -56,6 +56,18 @@ impl Uart {
uart.write(&[c]);
}
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
impl Uart {
pub fn putc(&self, c: u8) {
// check that we've been initialized before attempting to send any characters...
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
return;
}
let mut uart_csr = unsafe { utralib::CSR::new(DEFAULT_UART_ADDR) };
while uart_csr.r(utralib::utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utralib::utra::duart::SFR_TXD, c as usize);
}
}
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {

View file

@ -1,28 +1,28 @@
use core::fmt::{Error, Write};
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use std::pin::Pin;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use bao1x_hal::board::UART_DMA_TX_BUF_PHYS;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use bao1x_hal::udma;
use utralib::generated::*;
pub struct Output {}
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
pub static mut UART_DMA_TX_BUF_VIRT: *mut u8 = 0x0000_0000 as *mut u8;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
pub static mut UART_IRQ: Option<Pin<Box<bao1x_hal::udma::UartIrq>>> = None;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
pub static mut KBD_CONN: u32 = 0;
pub fn init() -> Output {
// TODO: migrate this to a "proper" UART that is available on SoC hardware, but for now all we have access
// to is the DUART.
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::udma_uart_2::HW_UDMA_UART_2_BASE),
None,
@ -30,8 +30,11 @@ pub fn init() -> Output {
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map serial port");
unsafe { crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ };
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
unsafe {
crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _
};
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
{
// Note: for the TX buf, we allocate a pre-reserved portion of IFRAM as our
// DMA buffer. We do *not* use the IFRAM allocator in `bao1x-hal-service` because
@ -65,13 +68,25 @@ pub fn init() -> Output {
};
udma_uart.setup_async_read();
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::duart::HW_DUART_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map UDMA buffer");
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
unsafe {
crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _
};
println!("Mapped UART @ {:08x}", uart.as_ptr() as usize);
println!("Process: map success!");
Output {}
}
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
fn uart_handler(_irq_no: usize, _arg: *mut usize) {
let mut uart = unsafe {
udma::Uart::get_handle(
@ -117,7 +132,7 @@ pub struct OutputWriter {}
#[allow(dead_code)]
impl OutputWriter {
pub fn putc(&self, c: u8) {
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
{
// safety: safe to call as long as the raw parts are initialized and we exclusively
// own it; and the UART has been initialized. For this peripheral, initialization
@ -134,13 +149,19 @@ impl OutputWriter {
// enqueue our character to send via DMA
uart.write(&[c]);
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
{
let mut uart_csr = unsafe { CSR::new(crate::platform::debug::DEFAULT_UART_ADDR) };
while uart_csr.r(utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utra::duart::SFR_TXD, c as usize);
}
}
/// Write a buffer to the output and return the number of
/// bytes written. This is mostly compatible with `std::io::Write`,
/// except it is infallible.
pub fn write(&mut self, buf: &[u8]) -> usize {
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
{
// safety: safe to call as long as the raw parts are initialized and we exclusively
// own it; and the UART has been initialized. For this peripheral, initialization
@ -155,6 +176,15 @@ impl OutputWriter {
};
uart.write(buf)
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
{
let mut uart_csr = unsafe { CSR::new(crate::platform::debug::DEFAULT_UART_ADDR) };
for &c in buf.iter() {
while uart_csr.r(utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utra::duart::SFR_TXD, c as usize);
}
buf.len()
}
}
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> { Ok(self.write(buf)) }

2
utralib/bao1x/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
# this is only present during simulations
soc.svd

View file

@ -123,7 +123,11 @@ fn main() {
let generated_filename = "src/generated/atsama5d27.rs";
#[cfg(feature = "bao1x")]
let svd_filenames = vec!["bao1x/core.svd", "bao1x/bao1x_peri.svd"];
let mut svd_filenames = vec!["bao1x/core.svd", "bao1x/bao1x_peri.svd"];
#[cfg(feature = "bao1x")]
if std::path::Path::new("bao1x/soc.svd").exists() {
svd_filenames.push("bao1x/soc.svd");
}
#[cfg(feature = "bao1x")]
let generated_filename = "src/generated/bao1x.rs";

View file

@ -224,7 +224,7 @@ impl Builder {
self
}
/// Disable default features on the loader
/// Disable default features on the kernel
#[allow(dead_code)]
pub fn kernel_disable_defaults(&mut self) -> &mut Builder {
self.kernel_disable_defaults = true;

View file

@ -499,7 +499,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
builder.add_loader_feature(board);
builder.add_kernel_feature(board);
}
_ => (), // don't add any board because this is simulation
Some("bao1x-sim") => {
let board = "board-dabao";
// select the board
builder.add_feature(board);
builder.add_loader_feature(board);
builder.add_kernel_feature(board);
builder.kernel_disable_defaults(); // need to turn of kernel DUART exclusive access so console can work!
}
_ => panic!("unhandled configuration"),
};
// placement in flash is a tension between dev convenience and RAM usage. Things in flash