fix compiler issues

This commit is contained in:
Nilton Constantino
2026-01-30 17:28:04 +00:00
parent 926ad2a807
commit e2a5a08bf2
12 changed files with 137 additions and 37 deletions
@@ -51,6 +51,7 @@ pub struct PrometeuOS {
pub current_cartridge_title: String,
pub current_cartridge_app_version: String,
pub current_cartridge_app_mode: crate::model::AppMode,
pub current_entrypoint: String,
/// Rate-limiter to prevent apps from flooding the log buffer and killing performance.
pub logs_written_this_frame: HashMap<u32, u32>,
@@ -100,6 +101,7 @@ impl PrometeuOS {
current_cartridge_title: String::new(),
current_cartridge_app_version: String::new(),
current_cartridge_app_mode: crate::model::AppMode::Game,
current_entrypoint: String::new(),
logs_written_this_frame: HashMap::new(),
telemetry_current: TelemetryFrame::default(),
telemetry_last: TelemetryFrame::default(),
@@ -167,6 +169,7 @@ impl PrometeuOS {
self.current_cartridge_title = cartridge.title.clone();
self.current_cartridge_app_version = cartridge.app_version.clone();
self.current_cartridge_app_mode = cartridge.app_mode;
self.current_entrypoint = cartridge.entrypoint.clone();
}
/// Executes a single VM instruction (Debug).
@@ -204,6 +207,12 @@ impl PrometeuOS {
self.logical_frame_remaining_cycles = Self::CYCLES_PER_LOGICAL_FRAME;
self.begin_logical_frame(signals, hw);
// If the VM is not currently executing a function (e.g. at the start of the app
// or after the entrypoint function returned), we prepare a new call to the entrypoint.
if vm.call_stack.is_empty() {
vm.prepare_call(&self.current_entrypoint);
}
// Reset telemetry for the new logical frame
self.telemetry_current = TelemetryFrame {
frame_index: self.logical_frame_index,
@@ -236,8 +245,16 @@ impl PrometeuOS {
self.log(LogLevel::Info, LogSource::Vm, 0xDEB1, format!("Breakpoint hit at PC 0x{:X}", vm.pc));
}
// 4. Frame Finalization (FRAME_SYNC reached)
if run.reason == crate::virtual_machine::LogicalFrameEndingReason::FrameSync {
// Handle Panics
if let crate::virtual_machine::LogicalFrameEndingReason::Panic(err) = run.reason {
let err_msg = format!("PVM Fault: \"{}\"", err);
self.log(LogLevel::Error, LogSource::Vm, 0, err_msg.clone());
return Some(err_msg);
}
// 4. Frame Finalization (FRAME_SYNC reached or Entrypoint returned)
if run.reason == crate::virtual_machine::LogicalFrameEndingReason::FrameSync ||
run.reason == crate::virtual_machine::LogicalFrameEndingReason::EndOfRom {
// All drawing commands for this frame are now complete.
// Finalize the framebuffer.
hw.gfx_mut().render_all();
@@ -626,6 +643,45 @@ mod tests {
os.syscall(0x1001, &mut vm, &mut hw).unwrap(); // gfx.clear
assert_eq!(vm.pop().unwrap(), Value::Null);
}
#[test]
fn test_entrypoint_called_every_frame() {
let mut os = PrometeuOS::new(None);
let mut vm = VirtualMachine::default();
let mut hw = Hardware::new();
let signals = InputSignals::default();
// PushI32 0 (0x17), then Ret (0x51)
let rom = vec![
0x17, 0x00, // PushI32
0x00, 0x00, 0x00, 0x00, // value 0
0x51, 0x00 // Ret
];
let cartridge = Cartridge {
app_id: 1234,
title: "test".to_string(),
app_version: "1.0.0".to_string(),
app_mode: AppMode::Game,
entrypoint: "0".to_string(),
program: rom,
assets: vec![],
asset_table: vec![],
preload: vec![],
};
os.initialize_vm(&mut vm, &cartridge);
// First frame
os.tick(&mut vm, &signals, &mut hw);
assert_eq!(os.logical_frame_index, 1);
assert!(!os.logical_frame_active);
assert!(vm.call_stack.is_empty());
// Second frame - Should call entrypoint again
os.tick(&mut vm, &signals, &mut hw);
assert_eq!(os.logical_frame_index, 2);
assert!(!os.logical_frame_active);
assert!(vm.call_stack.is_empty());
}
}
impl NativeInterface for PrometeuOS {
@@ -137,6 +137,30 @@ impl VirtualMachine {
self.cycles = 0;
self.halted = false;
}
/// Prepares the VM to execute a specific entrypoint by setting the PC and
/// pushing an initial call frame.
pub fn prepare_call(&mut self, entrypoint: &str) {
let addr = if let Ok(addr) = entrypoint.parse::<usize>() {
addr
} else {
0
};
self.pc = addr;
self.halted = false;
// Pushing a sentinel frame so RET works at the top level.
// The return address is set to the end of ROM, which will naturally
// cause the VM to stop after returning from the entrypoint.
self.operand_stack.clear();
self.call_stack.clear();
self.scope_stack.clear();
self.call_stack.push(CallFrame {
return_pc: self.program.rom.len() as u32,
stack_base: 0,
});
}
}
impl Default for VirtualMachine {
@@ -1277,4 +1301,25 @@ mod tests {
_ => panic!("Expected Trap, got {:?}", report.reason),
}
}
#[test]
fn test_entry_point_ret_with_prepare_call() {
// PushI32 0 (0x17), then Ret (0x51)
let rom = vec![
0x17, 0x00, // PushI32
0x00, 0x00, 0x00, 0x00, // value 0
0x51, 0x00 // Ret
];
let mut vm = VirtualMachine::new(rom, vec![]);
let mut hw = crate::Hardware::new();
struct TestNative;
impl NativeInterface for TestNative {
fn syscall(&mut self, _id: u32, _vm: &mut VirtualMachine, _hw: &mut dyn HardwareBridge) -> Result<u64, String> { Ok(0) }
}
let mut native = TestNative;
vm.prepare_call("0");
let result = vm.run_budget(100, &mut native, &mut hw).expect("VM run failed");
assert_eq!(result.reason, LogicalFrameEndingReason::EndOfRom);
}
}