Skip to content

Commit

Permalink
Decrease size of ModelEmulated from ~50k to 192 bytes.
Browse files Browse the repository at this point in the history
This should cut back on stack consumption and reduce memcpy bloat
when running tests.
  • Loading branch information
korran committed Oct 29, 2024
1 parent 22f8362 commit 2d01247
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ caliptra-emu-cpu = { path = "sw-emulator/lib/cpu" }
caliptra-emu-crypto = { path = "sw-emulator/lib/crypto" }
caliptra-emu-derive = { path = "sw-emulator/lib/derive" }
caliptra-emu-periph = { path = "sw-emulator/lib/periph" }
caliptra-emu-types = { path = "sw-emulator/lib/types" }
caliptra-emu-types = { path = "sw-emulator/lib/types", features = ["std"] }
caliptra-error = { path = "error", default-features = false }
caliptra-fpga-boss = { path = "ci-tools/fpga-boss" }
caliptra-gen-linker-scripts = { path = "cpu/gen" }
Expand Down
4 changes: 2 additions & 2 deletions hw-model/src/model_emulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> Bus for EmulatedApbBus<'a> {

/// Emulated model
pub struct ModelEmulated {
cpu: Cpu<BusLogger<CaliptraRootBus>>,
cpu: Box<Cpu<BusLogger<CaliptraRootBus>>>,
soc_to_caliptra_bus: SocToCaliptraBus,
output: Output,
trace_fn: Option<Box<InstrTracer<'static>>>,
Expand Down Expand Up @@ -187,7 +187,7 @@ impl HwModel for ModelEmulated {
dccm_dest.copy_from_slice(params.dccm);
}
let soc_to_caliptra_bus = root_bus.soc_to_caliptra_bus();
let cpu = Cpu::new(BusLogger::new(root_bus), clock);
let cpu = Box::new(Cpu::new(BusLogger::new(root_bus), clock));

let mut hasher = DefaultHasher::new();
std::hash::Hash::hash_slice(params.rom, &mut hasher);
Expand Down
7 changes: 5 additions & 2 deletions sw-emulator/lib/cpu/src/csr_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Csr {
/// Configuration and status register file
pub struct CsrFile {
/// CSRS
csrs: [Csr; CsrFile::CSR_COUNT],
csrs: Box<[Csr; CsrFile::CSR_COUNT]>,
/// Timer
timer: Timer,
}
Expand All @@ -114,7 +114,10 @@ impl CsrFile {
/// Create a new Configuration and status register file
pub fn new(clock: &Clock) -> Self {
let mut csrs = Self {
csrs: [Csr::new(0, 0); CsrFile::CSR_COUNT],
csrs: vec![Csr::new(0, 0); CsrFile::CSR_COUNT]
.try_into()
.map_err(|_| ())
.unwrap(),
timer: Timer::new(clock),
};

Expand Down
4 changes: 2 additions & 2 deletions sw-emulator/lib/periph/src/root_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub struct CaliptraRootBus {
pub sha256: HashSha256,

#[peripheral(offset = 0x1003_0000, mask = 0x0000_7fff)] // TODO update when known
pub ml_dsa87: MlDsa87,
pub ml_dsa87: Box<MlDsa87>,

#[peripheral(offset = 0x4000_0000, mask = 0x0fff_ffff)]
pub iccm: Iccm,
Expand Down Expand Up @@ -331,7 +331,7 @@ impl CaliptraRootBus {
key_vault: key_vault.clone(),
sha512,
sha256: HashSha256::new(clock),
ml_dsa87: MlDsa87::new(clock),
ml_dsa87: Box::new(MlDsa87::new(clock)),
iccm,
dccm: Ram::new(vec![0; Self::DCCM_SIZE]),
uart: Uart::new(),
Expand Down
3 changes: 3 additions & 0 deletions sw-emulator/lib/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[features]
std = []
23 changes: 23 additions & 0 deletions sw-emulator/lib/types/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,26 @@ pub trait Bus {
// By default, do nothing
}
}

#[cfg(feature = "std")]
impl<T: Bus> Bus for Box<T> {
fn read(&mut self, size: RvSize, addr: RvAddr) -> Result<RvData, BusError> {
T::read(self, size, addr)
}

fn write(&mut self, size: RvSize, addr: RvAddr, val: RvData) -> Result<(), BusError> {
T::write(self, size, addr, val)
}

fn poll(&mut self) {
T::poll(self)
}

fn warm_reset(&mut self) {
T::warm_reset(self)
}

fn update_reset(&mut self) {
T::update_reset(self)
}
}
2 changes: 1 addition & 1 deletion sw-emulator/lib/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Abstract:
File contains exports for for Caliptra Emulator Types library.
--*/
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(any(test, feature = "std")), no_std)]

pub mod bus;
mod exception;
Expand Down

0 comments on commit 2d01247

Please sign in to comment.