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:
@@ -1,6 +1,6 @@
|
||||
use super::*;
|
||||
use crate::fs::{FsState, VirtualFS};
|
||||
use crate::services::memcard::{MemcardService, MemcardSlotState, MemcardStatus};
|
||||
use crate::services::memcard::MemcardService;
|
||||
use prometeu_bytecode::{TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_TYPE, Value};
|
||||
use prometeu_hal::app_mode::AppMode;
|
||||
use prometeu_hal::asset::{AssetId, AssetOpStatus, BankType, SlotRef};
|
||||
@@ -605,101 +605,24 @@ impl NativeInterface for VmRuntimeHost<'_> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Syscall::MemSlotCount => {
|
||||
if self.current_cartridge_app_mode != AppMode::Game {
|
||||
ret.push_int(MemcardStatus::AccessDenied as i64);
|
||||
ret.push_int(0);
|
||||
return Ok(());
|
||||
}
|
||||
ret.push_int(MemcardStatus::Ok as i64);
|
||||
ret.push_int(self.memcard.slot_count() as i64);
|
||||
Ok(())
|
||||
}
|
||||
Syscall::MemSlotStat => {
|
||||
let slot = expect_slot_index(args, 0)?;
|
||||
if self.current_cartridge_app_mode != AppMode::Game {
|
||||
ret.push_int(MemcardStatus::AccessDenied as i64);
|
||||
ret.push_int(MemcardSlotState::Empty as i64);
|
||||
ret.push_int(0);
|
||||
ret.push_int(0);
|
||||
ret.push_int(0);
|
||||
return Ok(());
|
||||
}
|
||||
let stat = self.memcard.slot_stat(self.fs, self.current_app_id, slot);
|
||||
let status = if stat.state == MemcardSlotState::Corrupt {
|
||||
MemcardStatus::Corrupt
|
||||
} else {
|
||||
MemcardStatus::Ok
|
||||
};
|
||||
ret.push_int(status as i64);
|
||||
ret.push_int(stat.state as i64);
|
||||
ret.push_int(stat.used_bytes as i64);
|
||||
ret.push_int(stat.generation as i64);
|
||||
ret.push_int(stat.checksum as i64);
|
||||
Ok(())
|
||||
}
|
||||
Syscall::MemSlotRead => {
|
||||
let slot = expect_slot_index(args, 0)?;
|
||||
let offset = expect_non_negative_usize(args, 1, "offset")?;
|
||||
let max_bytes = expect_non_negative_usize(args, 2, "max_bytes")?;
|
||||
if self.current_cartridge_app_mode != AppMode::Game {
|
||||
ret.push_int(MemcardStatus::AccessDenied as i64);
|
||||
ret.push_string("")?;
|
||||
ret.push_int(0);
|
||||
return Ok(());
|
||||
}
|
||||
let read =
|
||||
self.memcard.slot_read(self.fs, self.current_app_id, slot, offset, max_bytes);
|
||||
ret.push_int(read.status as i64);
|
||||
ret.push_hex(&read.bytes)?;
|
||||
ret.push_int(read.bytes_read as i64);
|
||||
Ok(())
|
||||
}
|
||||
Syscall::MemSlotWrite => {
|
||||
let slot = expect_slot_index(args, 0)?;
|
||||
let offset = expect_non_negative_usize(args, 1, "offset")?;
|
||||
let payload_hex = expect_string(args, 2, "payload_hex")?;
|
||||
if self.current_cartridge_app_mode != AppMode::Game {
|
||||
ret.push_int(MemcardStatus::AccessDenied as i64);
|
||||
ret.push_int(0);
|
||||
return Ok(());
|
||||
}
|
||||
let payload = hex_decode(&payload_hex)?;
|
||||
Syscall::MemBootstrap
|
||||
| Syscall::MemSlotCount
|
||||
| Syscall::MemSlotStat
|
||||
| Syscall::MemSlotRead
|
||||
| Syscall::MemSlotWrite
|
||||
| Syscall::MemSlotCommit
|
||||
| Syscall::MemSlotClear
|
||||
| Syscall::MemQuery
|
||||
| Syscall::MemRelease
|
||||
| Syscall::MemInfo
|
||||
| Syscall::MemMetadata
|
||||
| Syscall::MemPresentation
|
||||
| Syscall::MemDiagnostic
|
||||
| Syscall::MemPublishPresentation => {
|
||||
let timestamp = platform.wall_clock().utc_unix_seconds();
|
||||
let app_id = self.current_app_id;
|
||||
let write = self.memcard.slot_write(self.fs, app_id, slot, offset, &payload);
|
||||
ret.push_int(write.status as i64);
|
||||
ret.push_int(write.bytes_written as i64);
|
||||
Ok(())
|
||||
}
|
||||
Syscall::MemSlotCommit => {
|
||||
let slot = expect_slot_index(args, 0)?;
|
||||
if self.current_cartridge_app_mode != AppMode::Game {
|
||||
ret.push_int(MemcardStatus::AccessDenied as i64);
|
||||
return Ok(());
|
||||
}
|
||||
let status = {
|
||||
let app_id = self.current_app_id;
|
||||
let memcard = &mut self.memcard;
|
||||
let fs = &mut self.fs;
|
||||
memcard.slot_commit(fs, app_id, slot)
|
||||
};
|
||||
ret.push_int(status as i64);
|
||||
Ok(())
|
||||
}
|
||||
Syscall::MemSlotClear => {
|
||||
let slot = expect_slot_index(args, 0)?;
|
||||
if self.current_cartridge_app_mode != AppMode::Game {
|
||||
ret.push_int(MemcardStatus::AccessDenied as i64);
|
||||
return Ok(());
|
||||
}
|
||||
let status = {
|
||||
let app_id = self.current_app_id;
|
||||
let memcard = &mut self.memcard;
|
||||
let fs = &mut self.fs;
|
||||
memcard.slot_clear(fs, app_id, slot)
|
||||
};
|
||||
ret.push_int(status as i64);
|
||||
Ok(())
|
||||
let game_profile = self.current_cartridge_app_mode == AppMode::Game;
|
||||
self.memcard.guest(syscall, args, ret, app_id, timestamp, game_profile)
|
||||
}
|
||||
Syscall::LogWrite => {
|
||||
self.syscall_log_write(
|
||||
@@ -849,14 +772,6 @@ fn expect_number(args: &[Value], index: usize, field: &str) -> Result<f64, VmFau
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_slot_index(args: &[Value], index: usize) -> Result<u8, VmFault> {
|
||||
let slot = expect_int(args, index)?;
|
||||
if !(0..32).contains(&slot) {
|
||||
return Err(VmFault::Trap(TRAP_OOB, format!("slot index out of bounds: {}", slot)));
|
||||
}
|
||||
Ok(slot as u8)
|
||||
}
|
||||
|
||||
fn expect_non_negative_usize(args: &[Value], index: usize, field: &str) -> Result<usize, VmFault> {
|
||||
let val = expect_int(args, index)?;
|
||||
if val < 0 {
|
||||
@@ -864,32 +779,3 @@ fn expect_non_negative_usize(args: &[Value], index: usize, field: &str) -> Resul
|
||||
}
|
||||
Ok(val as usize)
|
||||
}
|
||||
|
||||
fn hex_decode(s: &str) -> Result<Vec<u8>, VmFault> {
|
||||
fn nibble(c: u8) -> Option<u8> {
|
||||
match c {
|
||||
b'0'..=b'9' => Some(c - b'0'),
|
||||
b'a'..=b'f' => Some(10 + c - b'a'),
|
||||
b'A'..=b'F' => Some(10 + c - b'A'),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
let bytes = s.as_bytes();
|
||||
if !bytes.len().is_multiple_of(2) {
|
||||
return Err(VmFault::Trap(TRAP_TYPE, "payload_hex must have even length".to_string()));
|
||||
}
|
||||
let mut out = Vec::with_capacity(bytes.len() / 2);
|
||||
let mut i = 0usize;
|
||||
while i < bytes.len() {
|
||||
let hi = nibble(bytes[i]).ok_or_else(|| {
|
||||
VmFault::Trap(TRAP_TYPE, "payload_hex contains invalid hex".to_string())
|
||||
})?;
|
||||
let lo = nibble(bytes[i + 1]).ok_or_else(|| {
|
||||
VmFault::Trap(TRAP_TYPE, "payload_hex contains invalid hex".to_string())
|
||||
})?;
|
||||
out.push((hi << 4) | lo);
|
||||
i += 2;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user