feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
This commit is contained in:
@@ -36,6 +36,21 @@ impl<'a> LifecycleFacade<'a> {
|
||||
}
|
||||
|
||||
pub fn set_game_foreground_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
if self.os.memcard.host_faulted()
|
||||
|| self.os.memcard.session.removal_pending()
|
||||
|| matches!(
|
||||
self.os.memcard.admission.state,
|
||||
crate::memcard::admission::PreparationState::Pending
|
||||
| crate::memcard::admission::PreparationState::Failed(_)
|
||||
)
|
||||
{
|
||||
return Err(LifecycleError::InvalidTransition {
|
||||
task_id,
|
||||
from: self.task_state(task_id).unwrap_or(TaskState::Closed),
|
||||
operation: LifecycleOperation::SetForeground,
|
||||
});
|
||||
}
|
||||
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
|
||||
if task_kind_for_task(self.os, task_id)? != TaskKind::Game {
|
||||
@@ -242,6 +257,21 @@ impl<'a> LifecycleFacade<'a> {
|
||||
}
|
||||
|
||||
pub fn resume_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
if self.os.memcard.host_faulted()
|
||||
|| self.os.memcard.session.removal_pending()
|
||||
|| matches!(
|
||||
self.os.memcard.admission.state,
|
||||
crate::memcard::admission::PreparationState::Pending
|
||||
| crate::memcard::admission::PreparationState::Failed(_)
|
||||
)
|
||||
{
|
||||
return Err(LifecycleError::InvalidTransition {
|
||||
task_id,
|
||||
from: self.task_state(task_id).unwrap_or(TaskState::Closed),
|
||||
operation: LifecycleOperation::Resume,
|
||||
});
|
||||
}
|
||||
|
||||
let task =
|
||||
self.os.task_manager.get(task_id).ok_or(LifecycleError::TaskNotFound(task_id))?;
|
||||
|
||||
|
||||
@@ -203,7 +203,16 @@ impl<'a> VmFacade<'a> {
|
||||
});
|
||||
};
|
||||
|
||||
session.runtime.debug_step_instruction(&mut self.os.log_service, &mut session.vm, platform)
|
||||
session.runtime.debug_step_instruction(
|
||||
&mut self.os.log_service,
|
||||
&mut self.os.fs,
|
||||
&mut session.fs_state,
|
||||
&mut self.os.memcard,
|
||||
&mut session.open_files,
|
||||
&mut session.next_handle,
|
||||
&mut session.vm,
|
||||
platform,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn active_pc(&self) -> Option<usize> {
|
||||
|
||||
@@ -28,6 +28,8 @@ pub struct SystemOS {
|
||||
pub(super) memcard: MemcardService,
|
||||
pub(super) open_files: HashMap<u32, String>,
|
||||
pub(super) next_handle: u32,
|
||||
io_controller: Option<crate::AsyncWorkLaneController>,
|
||||
io_bound: bool,
|
||||
}
|
||||
|
||||
impl SystemOS {
|
||||
@@ -50,6 +52,8 @@ impl SystemOS {
|
||||
memcard: MemcardService::new(),
|
||||
open_files: HashMap::new(),
|
||||
next_handle: 1,
|
||||
io_controller: None,
|
||||
io_bound: false,
|
||||
};
|
||||
|
||||
os.log(LogLevel::Info, LogSource::Pos, 0, "PrometeuOS starting...".to_string());
|
||||
@@ -57,6 +61,55 @@ impl SystemOS {
|
||||
os
|
||||
}
|
||||
|
||||
pub fn memcard(&mut self) -> &mut MemcardService {
|
||||
&mut self.memcard
|
||||
}
|
||||
|
||||
/// Host/player operations are admitted only while gameplay is suspended.
|
||||
pub fn replace_memcard(
|
||||
&mut self,
|
||||
storage: Box<dyn prometeu_hal::memcard::MemcardStorage>,
|
||||
load_existing: bool,
|
||||
) -> crate::memcard::image::Result<()> {
|
||||
if let Some(task) = self.foreground_stack.resident_game_task()
|
||||
&& self
|
||||
.task_manager
|
||||
.get(task)
|
||||
.is_none_or(|t| t.state != crate::task::TaskState::Suspended)
|
||||
{
|
||||
return Err(crate::memcard::image::Error::AccessDenied);
|
||||
}
|
||||
self.memcard.replace_card(storage, load_existing)
|
||||
}
|
||||
pub fn detach_memcard(&mut self) -> crate::memcard::image::Result<()> {
|
||||
if let Some(task) = self.foreground_stack.resident_game_task()
|
||||
&& self
|
||||
.task_manager
|
||||
.get(task)
|
||||
.is_none_or(|t| t.state != crate::task::TaskState::Suspended)
|
||||
{
|
||||
return Err(crate::memcard::image::Error::AccessDenied);
|
||||
}
|
||||
if self.memcard.host_faulted() {
|
||||
return Err(crate::memcard::image::Error::ReadOnly);
|
||||
}
|
||||
self.memcard.session.detach()?;
|
||||
self.memcard.admission.state = crate::memcard::admission::PreparationState::Ready;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called outside guest/frame execution, including while a game is suspended.
|
||||
pub fn poll_background_io(&mut self, platform: &mut dyn prometeu_hal::RuntimePlatform) {
|
||||
if !self.io_bound && platform.assets_mut().bind_background_work(self.memcard.lane.clone()) {
|
||||
self.io_controller = Some(crate::AsyncWorkLaneController::start(
|
||||
crate::AsyncWorkLaneConfig::default(),
|
||||
self.memcard.lane.clone(),
|
||||
));
|
||||
self.io_bound = true;
|
||||
}
|
||||
self.memcard.poll(std::time::Instant::now());
|
||||
}
|
||||
|
||||
pub fn log(&mut self, level: LogLevel, source: LogSource, tag: u16, msg: String) {
|
||||
self.vm_runtime.log(&mut self.log_service, level, source, tag, msg);
|
||||
}
|
||||
@@ -136,7 +189,7 @@ impl SystemOS {
|
||||
pub(super) fn clear_cartridge_service_state(&mut self) {
|
||||
self.open_files.clear();
|
||||
self.next_handle = 1;
|
||||
self.memcard.clear_all_staging();
|
||||
self.memcard.session.end_session();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +211,7 @@ mod tests {
|
||||
|
||||
fn session_test_cartridge(app_mode: AppMode, app_id: u32, title: &str) -> Cartridge {
|
||||
Cartridge {
|
||||
memcard_slots: None,
|
||||
app_id,
|
||||
title: title.to_string(),
|
||||
app_version: "1.0.0".to_string(),
|
||||
|
||||
Reference in New Issue
Block a user