quality baseline
This commit is contained in:
@@ -7,7 +7,11 @@ use crate::stats::HostStats;
|
||||
use crate::utilities::draw_rgb565_to_rgba8;
|
||||
use pixels::wgpu::PresentMode;
|
||||
use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
|
||||
use prometeu_drivers::AudioCommand;
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use prometeu_firmware::{BootTarget, Firmware};
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::telemetry::CertificationConfig;
|
||||
use std::time::{Duration, Instant};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::dpi::LogicalSize;
|
||||
@@ -15,14 +19,10 @@ use winit::event::{ElementState, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, ControlFlow};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
use prometeu_hal::telemetry::CertificationConfig;
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use prometeu_drivers::AudioCommand;
|
||||
use prometeu_hal::color::Color;
|
||||
|
||||
/// The Desktop implementation of the PROMETEU Runtime.
|
||||
///
|
||||
/// This struct acts as the physical "chassis" of the virtual console. It is
|
||||
///
|
||||
/// This struct acts as the physical "chassis" of the virtual console. It is
|
||||
/// responsible for:
|
||||
/// - Creating and managing the OS window (via `winit`).
|
||||
/// - Initializing the GPU-accelerated framebuffer (via `pixels`).
|
||||
@@ -130,8 +130,18 @@ impl HostRunner {
|
||||
let color_warn = Color::RED;
|
||||
|
||||
self.hardware.gfx.fill_rect(5, 5, 175, 100, color_bg);
|
||||
self.hardware.gfx.draw_text(10, 10, &format!("FPS: {:.1}", self.stats.current_fps), color_text);
|
||||
self.hardware.gfx.draw_text(10, 18, &format!("HOST: {:.2}MS", tel.host_cpu_time_us as f64 / 1000.0), color_text);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
10,
|
||||
&format!("FPS: {:.1}", self.stats.current_fps),
|
||||
color_text,
|
||||
);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
18,
|
||||
&format!("HOST: {:.2}MS", tel.host_cpu_time_us as f64 / 1000.0),
|
||||
color_text,
|
||||
);
|
||||
self.hardware.gfx.draw_text(10, 26, &format!("STEPS: {}", tel.vm_steps), color_text);
|
||||
self.hardware.gfx.draw_text(10, 34, &format!("SYSC: {}", tel.syscalls), color_text);
|
||||
|
||||
@@ -140,25 +150,60 @@ impl HostRunner {
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
self.hardware.gfx.draw_text(10, 42, &format!("CYC: {}/{} ({:.1}%)", tel.cycles_used, tel.cycles_budget, cycles_pct), color_text);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
42,
|
||||
&format!("CYC: {}/{} ({:.1}%)", tel.cycles_used, tel.cycles_budget, cycles_pct),
|
||||
color_text,
|
||||
);
|
||||
|
||||
self.hardware.gfx.draw_text(10, 50, &format!("GFX: {}K/16M ({}S)", tel.gfx_used_bytes / 1024, tel.gfx_slots_occupied), color_text);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
50,
|
||||
&format!("GFX: {}K/16M ({}S)", tel.gfx_used_bytes / 1024, tel.gfx_slots_occupied),
|
||||
color_text,
|
||||
);
|
||||
if tel.gfx_inflight_bytes > 0 {
|
||||
self.hardware.gfx.draw_text(10, 58, &format!("LOAD GFX: {}KB", tel.gfx_inflight_bytes / 1024), color_warn);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
58,
|
||||
&format!("LOAD GFX: {}KB", tel.gfx_inflight_bytes / 1024),
|
||||
color_warn,
|
||||
);
|
||||
}
|
||||
|
||||
self.hardware.gfx.draw_text(10, 66, &format!("AUD: {}K/32M ({}S)", tel.audio_used_bytes / 1024, tel.audio_slots_occupied), color_text);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
66,
|
||||
&format!("AUD: {}K/32M ({}S)", tel.audio_used_bytes / 1024, tel.audio_slots_occupied),
|
||||
color_text,
|
||||
);
|
||||
if tel.audio_inflight_bytes > 0 {
|
||||
self.hardware.gfx.draw_text(10, 74, &format!("LOAD AUD: {}KB", tel.audio_inflight_bytes / 1024), color_warn);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
74,
|
||||
&format!("LOAD AUD: {}KB", tel.audio_inflight_bytes / 1024),
|
||||
color_warn,
|
||||
);
|
||||
}
|
||||
|
||||
let cert_color = if tel.violations > 0 { color_warn } else { color_text };
|
||||
self.hardware.gfx.draw_text(10, 82, &format!("CERT LAST: {}", tel.violations), cert_color);
|
||||
|
||||
if tel.violations > 0 {
|
||||
if let Some(event) = self.firmware.os.log_service.get_recent(10).into_iter().rev().find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA03) {
|
||||
if let Some(event) = self
|
||||
.firmware
|
||||
.os
|
||||
.log_service
|
||||
.get_recent(10)
|
||||
.into_iter()
|
||||
.rev()
|
||||
.find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA03)
|
||||
{
|
||||
let mut msg = event.msg.clone();
|
||||
if msg.len() > 30 { msg.truncate(30); }
|
||||
if msg.len() > 30 {
|
||||
msg.truncate(30);
|
||||
}
|
||||
self.hardware.gfx.draw_text(10, 90, &msg, color_warn);
|
||||
}
|
||||
}
|
||||
@@ -183,11 +228,12 @@ impl ApplicationHandler for HostRunner {
|
||||
let size = window.inner_size();
|
||||
let surface_texture = SurfaceTexture::new(size.width, size.height, window);
|
||||
|
||||
let mut pixels = PixelsBuilder::new(Hardware::W as u32, Hardware::H as u32, surface_texture)
|
||||
.present_mode(PresentMode::Fifo) // activate vsync
|
||||
.build()
|
||||
.expect("failed to create Pixels");
|
||||
|
||||
let mut pixels =
|
||||
PixelsBuilder::new(Hardware::W as u32, Hardware::H as u32, surface_texture)
|
||||
.present_mode(PresentMode::Fifo) // activate vsync
|
||||
.build()
|
||||
.expect("failed to create Pixels");
|
||||
|
||||
pixels.frame_mut().fill(0);
|
||||
|
||||
self.pixels = Some(pixels);
|
||||
@@ -197,7 +243,6 @@ impl ApplicationHandler for HostRunner {
|
||||
event_loop.set_control_flow(ControlFlow::Poll);
|
||||
}
|
||||
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
self.input.handle_event(&event, self.window());
|
||||
|
||||
@@ -232,7 +277,6 @@ impl ApplicationHandler for HostRunner {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let PhysicalKey::Code(code) = event.physical_key {
|
||||
let is_down = event.state == ElementState::Pressed;
|
||||
@@ -270,13 +314,13 @@ impl ApplicationHandler for HostRunner {
|
||||
}
|
||||
|
||||
// 3. Timing Management (The heart of determinism).
|
||||
// We measure the elapsed time since the last iteration and add it to an
|
||||
// accumulator. We then execute exactly as many 60Hz slices as the
|
||||
// We measure the elapsed time since the last iteration and add it to an
|
||||
// accumulator. We then execute exactly as many 60Hz slices as the
|
||||
// accumulator allows.
|
||||
let now = Instant::now();
|
||||
let mut frame_delta = now.duration_since(self.last_frame_time);
|
||||
|
||||
// Safety cap: if the OS freezes or we fall behind too much, we don't try
|
||||
// Safety cap: if the OS freezes or we fall behind too much, we don't try
|
||||
// to catch up indefinitely (avoiding the "death spiral").
|
||||
if frame_delta > Duration::from_millis(100) {
|
||||
frame_delta = Duration::from_millis(100);
|
||||
@@ -293,16 +337,13 @@ impl ApplicationHandler for HostRunner {
|
||||
}
|
||||
|
||||
// Sync pause state with audio.
|
||||
// We do this AFTER firmware.tick to avoid MasterPause/Resume commands
|
||||
// We do this AFTER firmware.tick to avoid MasterPause/Resume commands
|
||||
// being cleared by the OS if a new logical frame starts in this tick.
|
||||
let is_paused = self.firmware.os.paused || self.debugger.waiting_for_start;
|
||||
if is_paused != self.last_paused_state {
|
||||
self.last_paused_state = is_paused;
|
||||
let cmd = if is_paused {
|
||||
AudioCommand::MasterPause
|
||||
} else {
|
||||
AudioCommand::MasterResume
|
||||
};
|
||||
let cmd =
|
||||
if is_paused { AudioCommand::MasterPause } else { AudioCommand::MasterResume };
|
||||
self.hardware.audio.commands.push(cmd);
|
||||
}
|
||||
|
||||
@@ -331,7 +372,7 @@ impl ApplicationHandler for HostRunner {
|
||||
// 5. Rendering the Telemetry Overlay (if enabled).
|
||||
if self.overlay_enabled {
|
||||
// We temporarily swap buffers to draw over the current image.
|
||||
self.hardware.gfx.present();
|
||||
self.hardware.gfx.present();
|
||||
self.display_dbg_overlay();
|
||||
self.hardware.gfx.present();
|
||||
}
|
||||
@@ -345,9 +386,9 @@ impl ApplicationHandler for HostRunner {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use prometeu_firmware::BootTarget;
|
||||
use prometeu_hal::debugger_protocol::DEVTOOLS_PROTOCOL_VERSION;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
use prometeu_hal::debugger_protocol::DEVTOOLS_PROTOCOL_VERSION;
|
||||
|
||||
#[test]
|
||||
fn test_debug_port_opens() {
|
||||
@@ -364,10 +405,11 @@ mod tests {
|
||||
|
||||
// Check if we can connect
|
||||
{
|
||||
let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect");
|
||||
let mut stream =
|
||||
TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect");
|
||||
// Short sleep to ensure the OS processes
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
|
||||
|
||||
// Simulates the loop to accept the connection
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
assert!(runner.debugger.stream.is_some(), "Stream should have been kept open");
|
||||
@@ -375,24 +417,36 @@ mod tests {
|
||||
// Handshake Check
|
||||
let mut buf = [0u8; 2048];
|
||||
let n = stream.read(&mut buf).expect("Should read handshake");
|
||||
let resp: serde_json::Value = serde_json::from_slice(&buf[..n]).expect("Handshake should be valid JSON");
|
||||
let resp: serde_json::Value =
|
||||
serde_json::from_slice(&buf[..n]).expect("Handshake should be valid JSON");
|
||||
assert_eq!(resp["type"], "handshake");
|
||||
assert_eq!(resp["protocol_version"], DEVTOOLS_PROTOCOL_VERSION);
|
||||
|
||||
// Send start via JSON
|
||||
stream.write_all(b"{\"type\":\"start\"}\n").expect("Connection should be open for writing");
|
||||
stream
|
||||
.write_all(b"{\"type\":\"start\"}\n")
|
||||
.expect("Connection should be open for writing");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
|
||||
|
||||
// Process the received command
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
assert!(!runner.debugger.waiting_for_start, "Execution should have started after start command");
|
||||
assert!(runner.debugger.listener.is_some(), "Listener should remain open for reconnections");
|
||||
assert!(
|
||||
!runner.debugger.waiting_for_start,
|
||||
"Execution should have started after start command"
|
||||
);
|
||||
assert!(
|
||||
runner.debugger.listener.is_some(),
|
||||
"Listener should remain open for reconnections"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Now that the stream is out of the test scope, the runner should detect closure on next check
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
assert!(runner.debugger.stream.is_none(), "Stream should have been closed after client disconnected");
|
||||
assert!(
|
||||
runner.debugger.stream.is_none(),
|
||||
"Stream should have been closed after client disconnected"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -407,7 +461,8 @@ mod tests {
|
||||
|
||||
// 1. Connect and start
|
||||
{
|
||||
let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect 1");
|
||||
let mut stream =
|
||||
TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect 1");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
assert!(runner.debugger.stream.is_some());
|
||||
@@ -427,10 +482,13 @@ mod tests {
|
||||
// 3. Try to reconnect - SHOULD FAIL currently, but we want it to WORK
|
||||
let stream2 = TcpStream::connect(format!("127.0.0.1:{}", port));
|
||||
assert!(stream2.is_ok(), "Should accept new connection even after start");
|
||||
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
assert!(runner.debugger.stream.is_some(), "Stream should have been accepted on reconnection");
|
||||
assert!(
|
||||
runner.debugger.stream.is_some(),
|
||||
"Stream should have been accepted on reconnection"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -444,22 +502,27 @@ mod tests {
|
||||
});
|
||||
|
||||
// 1. First connection
|
||||
let mut _stream1 = TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect 1");
|
||||
let mut _stream1 =
|
||||
TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect 1");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
assert!(runner.debugger.stream.is_some());
|
||||
|
||||
// 2. Second connection
|
||||
let mut stream2 = TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect 2 (OS accepts)");
|
||||
let mut stream2 = TcpStream::connect(format!("127.0.0.1:{}", port))
|
||||
.expect("Should connect 2 (OS accepts)");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware); // Should accept and close stream2
|
||||
|
||||
|
||||
// Check if stream2 was closed by the server
|
||||
let mut buf = [0u8; 10];
|
||||
stream2.set_read_timeout(Some(std::time::Duration::from_millis(100))).unwrap();
|
||||
let res = stream2.read(&mut buf);
|
||||
assert!(matches!(res, Ok(0)) || res.is_err(), "Second connection should be closed by server");
|
||||
|
||||
assert!(
|
||||
matches!(res, Ok(0)) || res.is_err(),
|
||||
"Second connection should be closed by server"
|
||||
);
|
||||
|
||||
assert!(runner.debugger.stream.is_some(), "First connection should continue active");
|
||||
}
|
||||
|
||||
@@ -479,7 +542,7 @@ mod tests {
|
||||
|
||||
use std::io::BufRead;
|
||||
let mut reader = std::io::BufReader::new(stream);
|
||||
|
||||
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).expect("Should read handshake");
|
||||
assert!(line.contains("handshake"));
|
||||
@@ -487,15 +550,17 @@ mod tests {
|
||||
// Send getState
|
||||
reader.get_mut().write_all(b"{\"type\":\"getState\"}\n").expect("Should write getState");
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
|
||||
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
|
||||
// Check if response received (may have events/logs before)
|
||||
loop {
|
||||
line.clear();
|
||||
reader.read_line(&mut line).expect("Should read line");
|
||||
if line.is_empty() { break; }
|
||||
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Ok(resp) = serde_json::from_str::<serde_json::Value>(&line) {
|
||||
if resp["type"] == "getState" {
|
||||
return;
|
||||
@@ -517,10 +582,11 @@ mod tests {
|
||||
|
||||
// 1. Connect and pause
|
||||
{
|
||||
let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect");
|
||||
let mut stream =
|
||||
TcpStream::connect(format!("127.0.0.1:{}", port)).expect("Should connect");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
|
||||
|
||||
stream.write_all(b"{\"type\":\"pause\"}\n").expect("Should write pause");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
@@ -530,7 +596,7 @@ mod tests {
|
||||
// 2. Disconnect (stream goes out of scope)
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
runner.debugger.check_commands(&mut runner.firmware, &mut runner.hardware);
|
||||
|
||||
|
||||
// 3. Check if unpaused
|
||||
assert!(!runner.firmware.os.paused, "VM should have unpaused after disconnect");
|
||||
assert!(!runner.debugger.waiting_for_start, "VM should have left waiting_for_start state");
|
||||
|
||||
Reference in New Issue
Block a user