feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good

This commit is contained in:
2026-10-13 15:24:01 +01:00
parent 0af8b2b1fe
commit 6c2a07e579
65 changed files with 6222 additions and 985 deletions
@@ -38,6 +38,14 @@ struct Cli {
#[arg(long, value_name = "PATH")]
games_root: Option<String>,
/// Install a memory-card image (never converted or overwritten).
#[arg(long, value_name = "PATH")]
memcard: Option<std::path::PathBuf>,
/// Create and fully allocate the named new memory card before installation.
#[arg(long, requires = "memcard")]
create_memcard: bool,
/// Path to the capability configuration file
#[arg(long, value_name = "PATH")]
cap: Option<String>,
@@ -63,6 +71,11 @@ pub fn run() -> Result<(), Box<dyn std::error::Error>> {
if let Some(games_root) = cli.games_root {
runner.set_games_root(games_root);
}
if let Some(path) = cli.memcard {
runner
.install_memcard(path, cli.create_memcard)
.map_err(|e| format!("Cannot install memory card: {e:?}"))?;
}
runner.set_boot_target(boot_target);
event_loop.run_app(&mut runner)?;
@@ -156,6 +156,26 @@ pub struct HostRunner {
}
impl HostRunner {
pub fn install_memcard(
&mut self,
path: std::path::PathBuf,
create: bool,
) -> prometeu_system::memcard::image::Result<()> {
self.firmware.os.memcard().mount_card_with(move || {
use prometeu_drivers::memcard::FileMemcard;
use prometeu_system::memcard::{image::IMAGE_BYTES, transaction::Card};
let storage = if create {
FileMemcard::create(&path, IMAGE_BYTES, |storage, id| {
Card::format(storage, id)
.map_err(|_| prometeu_hal::memcard::MemcardIoError::IoError)
})?
} else {
FileMemcard::open(&path, false)?
};
Ok(Box::new(storage))
})
}
/// Configures the boot target (Hub or specific Cartridge).
pub(crate) fn set_boot_target(&mut self, boot_target: BootTarget) {
self.firmware.boot_target = boot_target.clone();
+61 -1
View File
@@ -12,7 +12,7 @@ use prometeu_hal::{
};
use prometeu_system::{CrashReport, LatestRenderFrameStore};
const BOOT_TICK_LIMIT: usize = 64;
const BOOT_TICK_LIMIT: usize = 5000;
const HOST_FRAME_ADVANCE_TICK_LIMIT: usize = 100_000;
/// One published logical frame plus the automation counter.
@@ -118,6 +118,17 @@ struct HeadlessPlatform {
}
impl HeadlessHost {
/// Install storage through the shared worker. Progress is driven by ordinary host ticks.
pub fn install_memcard(
&mut self,
storage: Box<dyn prometeu_hal::memcard::MemcardStorage>,
) -> prometeu_system::memcard::image::Result<()> {
self.firmware.os.memcard().mount_card(storage)
}
pub fn memcard_preparation(&mut self) -> prometeu_system::memcard::admission::PreparationState {
self.firmware.os.memcard().admission.state
}
pub fn new() -> Self {
let clock = Arc::new(ControlledWallClock::default());
Self::with_clock(clock.clone(), Some(clock))
@@ -160,6 +171,20 @@ impl HeadlessHost {
/// The transition into `GameRunning` is the last tick. The first game
/// logical frame stays available for [`Self::advance_frame`].
pub fn boot(&mut self, path: impl Into<String>) -> Result<(), HostError> {
self.boot_with_choice(path, None)
}
pub fn boot_with_memcard_choice(
&mut self,
path: impl Into<String>,
choice: prometeu_system::memcard::admission::Choice,
) -> Result<(), HostError> {
self.boot_with_choice(path, Some(choice))
}
fn boot_with_choice(
&mut self,
path: impl Into<String>,
choice: Option<prometeu_system::memcard::admission::Choice>,
) -> Result<(), HostError> {
if self.boot_attempted {
return Err(HostError::AlreadyBooted);
}
@@ -167,6 +192,7 @@ impl HeadlessHost {
let path = path.into();
self.firmware.boot_target = BootTarget::Cartridge { path, debug: false, debug_port: 7777 };
let boot_deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
for _ in 0..BOOT_TICK_LIMIT {
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
return Ok(());
@@ -185,7 +211,41 @@ impl HeadlessHost {
report: None,
});
}
let progress = self.firmware.os.memcard().lane.progress_stamp();
let waiting = matches!(&self.firmware.state,FirmwareState::LoadCartridge(step) if step.needs_memcard_choice());
if waiting {
let Some(choice) = choice else {
return Err(HostError::BootFailed {
reason: "Memory-card choice required; use boot_with_memcard_choice".into(),
report: None,
});
};
self.signals = InputSignals::default();
self.tick_once();
self.signals = match choice {
prometeu_system::memcard::admission::Choice::Persistent => {
InputSignals { a_signal: true, ..Default::default() }
}
prometeu_system::memcard::admission::Choice::MemoryFresh => {
InputSignals { x_signal: true, ..Default::default() }
}
prometeu_system::memcard::admission::Choice::MemoryExisting => {
InputSignals { y_signal: true, ..Default::default() }
}
};
}
self.tick_once();
self.signals = InputSignals::default();
if matches!(self.firmware.state, FirmwareState::LoadCartridge(_)) {
if std::time::Instant::now() >= boot_deadline {
break;
}
self.firmware
.os
.memcard()
.lane
.wait_for_progress(progress, std::time::Duration::from_millis(16));
}
}
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {