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:
@@ -12,7 +12,7 @@ use prometeu_hal::cartridge::AssetsPayloadSource;
|
||||
use prometeu_hal::sprite::Sprite;
|
||||
use prometeu_hal::{
|
||||
AssetBridge, AudioBridge, InputPlatform, NoopTelemetryPlatform, PadBridge, RenderBackend,
|
||||
RuntimePlatform, TelemetryPlatform, TouchBridge,
|
||||
RuntimePlatform, TelemetryPlatform, TouchBridge, WallClock,
|
||||
};
|
||||
use prometeu_hal::{
|
||||
Game2DFrameComposer, Game2DFramePacket, RenderSubmission, RenderSubmissionPacket,
|
||||
@@ -33,6 +33,7 @@ use std::sync::Arc;
|
||||
/// - **Audio**: Stereo, Command-based mixing.
|
||||
/// - **Input**: 12-button Digital Gamepad + Absolute Touch/Mouse.
|
||||
pub struct Hardware {
|
||||
wall_clock: Arc<dyn WallClock>,
|
||||
/// Shared physical memory bank ownership used by peripherals and worker backends.
|
||||
pub memory_banks: Arc<MemoryBanks>,
|
||||
/// The Graphics Processing Unit (GPU). Handles drawing primitives, sprites, and tilemaps.
|
||||
@@ -49,12 +50,6 @@ pub struct Hardware {
|
||||
pub assets: AssetManager,
|
||||
}
|
||||
|
||||
impl Default for Hardware {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Hardware {
|
||||
fn consume_render_submission(&mut self, submission: &RenderSubmission) {
|
||||
match &submission.packet {
|
||||
@@ -159,6 +154,10 @@ impl InputPlatform for Hardware {
|
||||
static NOOP_TELEMETRY: NoopTelemetryPlatform = NoopTelemetryPlatform;
|
||||
|
||||
impl RuntimePlatform for Hardware {
|
||||
fn wall_clock(&self) -> &dyn WallClock {
|
||||
self.wall_clock.as_ref()
|
||||
}
|
||||
|
||||
fn render_submission_sink(&mut self) -> &mut dyn RenderSubmissionSink {
|
||||
self
|
||||
}
|
||||
@@ -207,12 +206,15 @@ impl Hardware {
|
||||
pub const H: usize = 270;
|
||||
|
||||
/// Creates a fresh hardware instance with default settings.
|
||||
pub fn new() -> Self {
|
||||
Self::new_with_memory_banks(Arc::new(MemoryBanks::new()))
|
||||
pub fn new(wall_clock: Arc<dyn WallClock>) -> Self {
|
||||
Self::new_with_memory_banks(Arc::new(MemoryBanks::new()), wall_clock)
|
||||
}
|
||||
|
||||
/// Creates hardware with explicit shared bank ownership.
|
||||
pub fn new_with_memory_banks(memory_banks: Arc<MemoryBanks>) -> Self {
|
||||
pub fn new_with_memory_banks(
|
||||
memory_banks: Arc<MemoryBanks>,
|
||||
wall_clock: Arc<dyn WallClock>,
|
||||
) -> Self {
|
||||
let assets = AssetManager::new(
|
||||
vec![],
|
||||
AssetsPayloadSource::empty(),
|
||||
@@ -222,6 +224,7 @@ impl Hardware {
|
||||
);
|
||||
let glyph_slot_index = assets.glyph_asset_slot_index();
|
||||
Self {
|
||||
wall_clock,
|
||||
memory_banks: Arc::clone(&memory_banks),
|
||||
gfx: Gfx::new(
|
||||
Self::W,
|
||||
@@ -304,7 +307,10 @@ mod tests {
|
||||
banks.install_glyph_bank(0, Arc::new(make_glyph_bank()));
|
||||
banks.install_scene_bank(0, Arc::new(make_scene_with_glyph_asset_id(0)));
|
||||
|
||||
let mut hardware = Hardware::new_with_memory_banks(Arc::clone(&banks));
|
||||
let mut hardware = Hardware::new_with_memory_banks(
|
||||
Arc::clone(&banks),
|
||||
Arc::new(crate::ControlledWallClock::default()),
|
||||
);
|
||||
let scene = banks.scene_bank_slot(0).expect("scene bank slot 0 should be resident");
|
||||
let mut cache = SceneViewportCache::new(&scene, 4, 4);
|
||||
cache.materialize_all_layers(&scene);
|
||||
@@ -323,7 +329,8 @@ mod tests {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
banks.install_scene_bank(2, Arc::new(make_scene()));
|
||||
|
||||
let hardware = Hardware::new_with_memory_banks(banks);
|
||||
let hardware =
|
||||
Hardware::new_with_memory_banks(banks, Arc::new(crate::ControlledWallClock::default()));
|
||||
let scene = hardware
|
||||
.frame_composer
|
||||
.scene_bank_slot(2)
|
||||
@@ -340,7 +347,8 @@ mod tests {
|
||||
banks.install_glyph_bank(0, Arc::new(make_glyph_bank()));
|
||||
banks.install_scene_bank(0, Arc::new(make_scene_with_glyph_asset_id(0)));
|
||||
|
||||
let mut hardware = Hardware::new_with_memory_banks(banks);
|
||||
let mut hardware =
|
||||
Hardware::new_with_memory_banks(banks, Arc::new(crate::ControlledWallClock::default()));
|
||||
let mut glyph_slots = [None; 16];
|
||||
glyph_slots[0] = Some(0);
|
||||
hardware.assets.glyph_asset_slot_index().rebuild_from_slots(&glyph_slots);
|
||||
@@ -370,7 +378,8 @@ mod tests {
|
||||
banks.install_glyph_bank(0, Arc::new(make_glyph_bank()));
|
||||
banks.install_scene_bank(0, Arc::new(make_scene_with_glyph_asset_id(0)));
|
||||
|
||||
let mut hardware = Hardware::new_with_memory_banks(banks);
|
||||
let mut hardware =
|
||||
Hardware::new_with_memory_banks(banks, Arc::new(crate::ControlledWallClock::default()));
|
||||
let mut glyph_slots = [None; 16];
|
||||
glyph_slots[0] = Some(0);
|
||||
hardware.assets.glyph_asset_slot_index().rebuild_from_slots(&glyph_slots);
|
||||
@@ -411,7 +420,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn render_submission_sink_accepts_owned_game2d_submission() {
|
||||
let mut hardware = Hardware::new();
|
||||
let mut hardware = Hardware::new(Arc::new(crate::ControlledWallClock::default()));
|
||||
let packet = Game2DFramePacket::new(
|
||||
ComposerFramePacket::default(),
|
||||
vec![Gfx2dCommand::Clear { color: Color::BLUE }],
|
||||
@@ -426,7 +435,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn render_submission_sink_accepts_owned_shell_ui_submission() {
|
||||
let mut hardware = Hardware::new();
|
||||
let mut hardware = Hardware::new(Arc::new(crate::ControlledWallClock::default()));
|
||||
let packet = ShellUiFramePacket::new(vec![GfxUiCommand::Clear { color: Color::RED }]);
|
||||
let submission = RenderSubmission::shell_ui(FrameId::ZERO, packet);
|
||||
|
||||
@@ -440,7 +449,8 @@ mod tests {
|
||||
fn emit_sprite_rejects_palette_id_outside_loaded_glyph_bank() {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
banks.install_glyph_bank(0, Arc::new(make_glyph_bank()));
|
||||
let mut hardware = Hardware::new_with_memory_banks(banks);
|
||||
let mut hardware =
|
||||
Hardware::new_with_memory_banks(banks, Arc::new(crate::ControlledWallClock::default()));
|
||||
|
||||
let status = Game2DFrameComposer::emit_sprite(
|
||||
&mut hardware,
|
||||
|
||||
@@ -8,6 +8,7 @@ mod memory_banks;
|
||||
mod pad;
|
||||
mod test_platform;
|
||||
mod touch;
|
||||
mod wall_clock;
|
||||
|
||||
pub use crate::asset::AssetManager;
|
||||
pub use crate::audio::{Audio, AudioCommand, Channel, MAX_CHANNELS, OUTPUT_SAMPLE_RATE};
|
||||
@@ -20,3 +21,4 @@ pub use crate::memory_banks::{
|
||||
};
|
||||
pub use crate::pad::Pad;
|
||||
pub use crate::test_platform::TestPlatform;
|
||||
pub use crate::wall_clock::ControlledWallClock;
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
use crate::ControlledWallClock;
|
||||
use crate::hardware::Hardware;
|
||||
use crate::memory_banks::MemoryBanks;
|
||||
use prometeu_hal::{
|
||||
AssetBridge, AudioBridge, Game2DFrameComposer, InputPlatform, RenderBackend,
|
||||
RenderSubmissionSink, RuntimePlatform, TelemetryPlatform,
|
||||
RenderSubmissionSink, RuntimePlatform, TelemetryPlatform, WallClock,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct TestPlatform {
|
||||
hardware: Hardware,
|
||||
controlled_wall_clock: Option<Arc<ControlledWallClock>>,
|
||||
}
|
||||
|
||||
impl Default for TestPlatform {
|
||||
@@ -18,11 +20,24 @@ impl Default for TestPlatform {
|
||||
|
||||
impl TestPlatform {
|
||||
pub fn new() -> Self {
|
||||
Self { hardware: Hardware::new() }
|
||||
Self::new_with_memory_banks(Arc::new(MemoryBanks::new()))
|
||||
}
|
||||
|
||||
pub fn new_with_memory_banks(memory_banks: Arc<MemoryBanks>) -> Self {
|
||||
Self { hardware: Hardware::new_with_memory_banks(memory_banks) }
|
||||
let clock = Arc::new(ControlledWallClock::default());
|
||||
Self {
|
||||
hardware: Hardware::new_with_memory_banks(memory_banks, clock.clone()),
|
||||
controlled_wall_clock: Some(clock),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_wall_clock(clock: Arc<dyn WallClock>) -> Self {
|
||||
Self { hardware: Hardware::new(clock), controlled_wall_clock: None }
|
||||
}
|
||||
|
||||
/// Native controls for the default clock. Injected providers are controlled by their owner.
|
||||
pub fn controlled_wall_clock(&self) -> Option<&ControlledWallClock> {
|
||||
self.controlled_wall_clock.as_deref()
|
||||
}
|
||||
|
||||
pub fn local_hardware(&self) -> &Hardware {
|
||||
@@ -35,6 +50,10 @@ impl TestPlatform {
|
||||
}
|
||||
|
||||
impl RuntimePlatform for TestPlatform {
|
||||
fn wall_clock(&self) -> &dyn WallClock {
|
||||
self.hardware.wall_clock()
|
||||
}
|
||||
|
||||
fn render_submission_sink(&mut self) -> &mut dyn RenderSubmissionSink {
|
||||
self.hardware.render_submission_sink()
|
||||
}
|
||||
@@ -97,4 +116,22 @@ mod tests {
|
||||
assert!(!platform.input().pad().any());
|
||||
assert!(platform.telemetry().telemetry_snapshot().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_clock_is_shared_with_hardware_and_can_be_injected() {
|
||||
let platform = TestPlatform::new();
|
||||
assert_eq!(platform.wall_clock().utc_unix_seconds(), 0);
|
||||
let clock = platform.controlled_wall_clock().expect("default controls");
|
||||
clock.set(42);
|
||||
assert_eq!(platform.local_hardware().wall_clock().utc_unix_seconds(), 42);
|
||||
clock.advance(3);
|
||||
assert_eq!(platform.wall_clock().utc_unix_seconds(), 45);
|
||||
|
||||
let custom = Arc::new(ControlledWallClock::new(900));
|
||||
let platform = TestPlatform::new_with_wall_clock(custom.clone());
|
||||
assert!(platform.controlled_wall_clock().is_none());
|
||||
assert_eq!(platform.wall_clock().utc_unix_seconds(), 900);
|
||||
custom.set(7);
|
||||
assert_eq!(platform.local_hardware().wall_clock().utc_unix_seconds(), 7);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
use prometeu_hal::WallClock;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
/// A native harness-controlled civil clock. Reads and runtime ticks never advance it.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ControlledWallClock {
|
||||
seconds: AtomicU64,
|
||||
}
|
||||
|
||||
impl ControlledWallClock {
|
||||
pub fn new(seconds: u64) -> Self {
|
||||
Self { seconds: AtomicU64::new(seconds) }
|
||||
}
|
||||
|
||||
/// Set civil time explicitly, including backwards adjustments.
|
||||
pub fn set(&self, seconds: u64) {
|
||||
self.seconds.store(seconds, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Advance civil time explicitly, saturating at the largest timestamp.
|
||||
pub fn advance(&self, seconds: u64) {
|
||||
// Harness control may retry on a concurrent update; reads remain a single load.
|
||||
let _ = self.seconds.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
|
||||
Some(current.saturating_add(seconds))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl WallClock for ControlledWallClock {
|
||||
fn utc_unix_seconds(&self) -> u64 {
|
||||
self.seconds.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn explicit_changes_allow_regression_and_saturate_without_advancing_on_read() {
|
||||
let clock = ControlledWallClock::default();
|
||||
assert_eq!(clock.utc_unix_seconds(), 0);
|
||||
clock.set(100);
|
||||
for _ in 0..10 {
|
||||
assert_eq!(clock.utc_unix_seconds(), 100);
|
||||
}
|
||||
clock.advance(9);
|
||||
assert_eq!(clock.utc_unix_seconds(), 109);
|
||||
clock.set(3);
|
||||
assert_eq!(clock.utc_unix_seconds(), 3);
|
||||
clock.advance(u64::MAX);
|
||||
clock.advance(1);
|
||||
assert_eq!(clock.utc_unix_seconds(), u64::MAX);
|
||||
clock.set(0);
|
||||
assert_eq!(clock.utc_unix_seconds(), 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user