feat(runtime): add host wall clock and administrative UTC timestamps
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,14 +1,14 @@
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
|
||||
use prometeu_drivers::MemoryBanks;
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use prometeu_drivers::{ControlledWallClock, MemoryBanks};
|
||||
use prometeu_firmware::firmware_state::{AppCrashesStep, ResetStep};
|
||||
use prometeu_firmware::{BootTarget, Firmware, FirmwareState};
|
||||
use prometeu_hal::{
|
||||
AssetBridge, AudioBridge, FrameId, Game2DFrameComposer, InputPlatform, InputSignals,
|
||||
OwnedRgba8888Frame, RenderBackend, RenderSubmission, RenderSubmissionSink, RenderSubmitError,
|
||||
RuntimePlatform, TelemetryPlatform,
|
||||
RuntimePlatform, TelemetryPlatform, WallClock,
|
||||
};
|
||||
use prometeu_system::{CrashReport, LatestRenderFrameStore};
|
||||
|
||||
@@ -103,6 +103,7 @@ pub struct HeadlessHost {
|
||||
signals: InputSignals,
|
||||
boot_attempted: bool,
|
||||
last_completed_frame: Option<CompletedFrame>,
|
||||
controlled_wall_clock: Option<Arc<ControlledWallClock>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@@ -118,8 +119,21 @@ struct HeadlessPlatform {
|
||||
|
||||
impl HeadlessHost {
|
||||
pub fn new() -> Self {
|
||||
let clock = Arc::new(ControlledWallClock::default());
|
||||
Self::with_clock(clock.clone(), Some(clock))
|
||||
}
|
||||
|
||||
/// Supply a host-owned civil clock. The caller retains control of this provider.
|
||||
pub fn new_with_wall_clock(clock: Arc<dyn WallClock>) -> Self {
|
||||
Self::with_clock(clock, None)
|
||||
}
|
||||
|
||||
fn with_clock(
|
||||
clock: Arc<dyn WallClock>,
|
||||
controlled_wall_clock: Option<Arc<ControlledWallClock>>,
|
||||
) -> Self {
|
||||
let memory_banks = Arc::new(MemoryBanks::new());
|
||||
let hardware = Hardware::new_with_memory_banks(Arc::clone(&memory_banks));
|
||||
let hardware = Hardware::new_with_memory_banks(Arc::clone(&memory_banks), clock);
|
||||
let store = LatestRenderFrameStore::default();
|
||||
Self {
|
||||
firmware: Firmware::new(None),
|
||||
@@ -127,9 +141,20 @@ impl HeadlessHost {
|
||||
signals: InputSignals::default(),
|
||||
boot_attempted: false,
|
||||
last_completed_frame: None,
|
||||
controlled_wall_clock,
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the same clock supplied to the runtime platform.
|
||||
pub fn wall_clock(&self) -> &dyn WallClock {
|
||||
self.platform.wall_clock()
|
||||
}
|
||||
|
||||
/// Native controls for the default clock; custom providers are controlled by their owner.
|
||||
pub fn controlled_wall_clock(&self) -> Option<&ControlledWallClock> {
|
||||
self.controlled_wall_clock.as_deref()
|
||||
}
|
||||
|
||||
/// Load `path` and tick until the firmware is in `GameRunning`.
|
||||
///
|
||||
/// The transition into `GameRunning` is the last tick. The first game
|
||||
@@ -384,6 +409,10 @@ impl RenderSubmissionSink for HeadlessPlatform {
|
||||
}
|
||||
|
||||
impl RuntimePlatform for HeadlessPlatform {
|
||||
fn wall_clock(&self) -> &dyn WallClock {
|
||||
self.hardware.wall_clock()
|
||||
}
|
||||
|
||||
fn display_size(&self) -> (usize, usize) {
|
||||
(Hardware::W, Hardware::H)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,48 @@ const BLUE: u32 = Color::BLUE.raw();
|
||||
const GREEN: u32 = Color::GREEN.raw();
|
||||
const BLACK: u32 = Color::BLACK.raw();
|
||||
|
||||
#[test]
|
||||
fn controlled_civil_time_is_independent_of_frames_pause_and_reset() {
|
||||
let cartridge = color_cartridge();
|
||||
for _ in 0..2 {
|
||||
let mut host = HeadlessHost::new();
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 0);
|
||||
host.controlled_wall_clock().unwrap().set(1_000);
|
||||
host.boot(&cartridge).expect("boot");
|
||||
host.advance_frames(3).expect("frames");
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 1_000);
|
||||
host.pause().expect("pause");
|
||||
host.controlled_wall_clock().unwrap().advance(60);
|
||||
host.resume().expect("resume");
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 1_060);
|
||||
host.controlled_wall_clock().unwrap().set(10);
|
||||
host.advance_frame().expect("frame after clock adjustment");
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 10);
|
||||
host.reset();
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 10);
|
||||
host.boot(&cartridge).expect("reboot");
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 10);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn injected_civil_clock_remains_the_runtime_source_across_lifecycle() {
|
||||
use prometeu_drivers::ControlledWallClock;
|
||||
use std::sync::Arc;
|
||||
|
||||
let clock = Arc::new(ControlledWallClock::new(u64::MAX));
|
||||
let mut host = HeadlessHost::new_with_wall_clock(clock.clone());
|
||||
assert!(host.controlled_wall_clock().is_none());
|
||||
host.boot(color_cartridge()).expect("boot");
|
||||
host.advance_frame().expect("frame");
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), u64::MAX);
|
||||
clock.set(42);
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 42);
|
||||
host.reset();
|
||||
clock.advance(3);
|
||||
assert_eq!(host.wall_clock().utc_unix_seconds(), 45);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn headless_source_has_no_timing_wait_or_interactive_backend_dependency() {
|
||||
let source = include_str!("../src/host.rs");
|
||||
|
||||
Reference in New Issue
Block a user