dev/vm-improvements (#3)

Co-authored-by: Nilton Constantino <nilton.constantino@visma.com>
Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2026-01-20 10:18:44 +00:00
co-authored by Nilton Constantino
parent 7e99eed6b4
commit 694d6c8be6
24 changed files with 1477 additions and 194 deletions
@@ -290,7 +290,7 @@ impl PrometeuOS {
// Helper para syscalls
fn syscall_log_write(&mut self, level_val: i64, tag: u16, msg: String) -> Result<u64, String> {
fn syscall_log_write(&mut self, vm: &mut VirtualMachine, level_val: i64, tag: u16, msg: String) -> Result<u64, String> {
let level = match level_val {
0 => LogLevel::Trace,
1 => LogLevel::Debug,
@@ -308,6 +308,7 @@ impl PrometeuOS {
self.logs_written_this_frame.insert(app_id, count + 1);
self.log(LogLevel::Warn, LogSource::App { app_id }, 0, "App exceeded log limit per frame".to_string());
}
vm.push(Value::Null);
return Ok(50);
}
@@ -320,6 +321,7 @@ impl PrometeuOS {
self.log(level, LogSource::App { app_id }, tag, final_msg);
vm.push(Value::Null);
Ok(100)
}
@@ -471,7 +473,7 @@ mod tests {
os.current_app_id = 123;
// 1. Normal log test
vm.push(Value::Integer(2)); // Info
vm.push(Value::Int64(2)); // Info
vm.push(Value::String("Hello Log".to_string()));
let res = os.syscall(0x5001, &mut vm, &mut hw);
assert!(res.is_ok());
@@ -483,7 +485,7 @@ mod tests {
// 2. Truncation test
let long_msg = "A".repeat(300);
vm.push(Value::Integer(3)); // Warn
vm.push(Value::Int64(3)); // Warn
vm.push(Value::String(long_msg));
os.syscall(0x5001, &mut vm, &mut hw).unwrap();
@@ -494,13 +496,13 @@ mod tests {
// 3. Rate Limit Test
// We already made 2 logs. The limit is 10.
for i in 0..8 {
vm.push(Value::Integer(2));
vm.push(Value::Int64(2));
vm.push(Value::String(format!("Log {}", i)));
os.syscall(0x5001, &mut vm, &mut hw).unwrap();
}
// The 11th log should be ignored (and generate a system warning)
vm.push(Value::Integer(2));
vm.push(Value::Int64(2));
vm.push(Value::String("Eleventh log".to_string()));
os.syscall(0x5001, &mut vm, &mut hw).unwrap();
@@ -513,7 +515,7 @@ mod tests {
// 4. Rate limit reset test in the next frame
os.begin_logical_frame(&InputSignals::default(), &mut hw);
vm.push(Value::Integer(2));
vm.push(Value::Int64(2));
vm.push(Value::String("New frame log".to_string()));
os.syscall(0x5001, &mut vm, &mut hw).unwrap();
@@ -521,14 +523,20 @@ mod tests {
assert_eq!(recent[0].msg, "New frame log");
// 5. LOG_WRITE_TAG test
vm.push(Value::Integer(2)); // Info
vm.push(Value::Integer(42)); // Tag
vm.push(Value::Int64(2)); // Info
vm.push(Value::Int64(42)); // Tag
vm.push(Value::String("Tagged Log".to_string()));
os.syscall(0x5002, &mut vm, &mut hw).unwrap();
let recent = os.log_service.get_recent(1);
assert_eq!(recent[0].msg, "Tagged Log");
assert_eq!(recent[0].tag, 42);
assert_eq!(vm.pop().unwrap(), Value::Null);
// 6. GFX Syscall return test
vm.push(Value::Int64(1)); // color_idx
os.syscall(0x1001, &mut vm, &mut hw).unwrap(); // gfx.clear
assert_eq!(vm.pop().unwrap(), Value::Null);
}
}
@@ -552,24 +560,27 @@ impl NativeInterface for PrometeuOS {
// system.has_cart() -> bool
0x0001 => {
// Returns true if a cartridge is available.
vm.push(Value::Boolean(true)); // For now, assume true or check state
Ok(10)
}
// system.run_cart()
// system.run_cart() -> null
0x0002 => {
// Triggers loading and execution of the current cartridge.
vm.push(Value::Null);
Ok(100)
}
// --- GFX Syscalls ---
// gfx.clear(color_index)
// gfx.clear(color_index) -> null
0x1001 => {
let color_idx = vm.pop_integer()? as usize;
let color = self.get_color(color_idx, hw);
hw.gfx_mut().clear(color);
vm.push(Value::Null);
Ok(100)
}
// gfx.draw_rect(x, y, w, h, color_index)
// gfx.draw_rect(x, y, w, h, color_index) -> null
0x1002 => {
let color_idx = vm.pop_integer()? as usize;
let h = vm.pop_integer()? as i32;
@@ -578,9 +589,10 @@ impl NativeInterface for PrometeuOS {
let x = vm.pop_integer()? as i32;
let color = self.get_color(color_idx, hw);
hw.gfx_mut().fill_rect(x, y, w, h, color);
vm.push(Value::Null);
Ok(200)
}
// gfx.draw_line(x1, y1, x2, y2, color_index)
// gfx.draw_line(x1, y1, x2, y2, color_index) -> null
0x1003 => {
let color_idx = vm.pop_integer()? as usize;
let y2 = vm.pop_integer()? as i32;
@@ -589,9 +601,10 @@ impl NativeInterface for PrometeuOS {
let x1 = vm.pop_integer()? as i32;
let color = self.get_color(color_idx, hw);
hw.gfx_mut().draw_line(x1, y1, x2, y2, color);
vm.push(Value::Null);
Ok(200)
}
// gfx.draw_circle(x, y, r, color_index)
// gfx.draw_circle(x, y, r, color_index) -> null
0x1004 => {
let color_idx = vm.pop_integer()? as usize;
let r = vm.pop_integer()? as i32;
@@ -599,9 +612,10 @@ impl NativeInterface for PrometeuOS {
let x = vm.pop_integer()? as i32;
let color = self.get_color(color_idx, hw);
hw.gfx_mut().draw_circle(x, y, r, color);
vm.push(Value::Null);
Ok(200)
}
// gfx.draw_disc(x, y, r, border_color_idx, fill_color_idx)
// gfx.draw_disc(x, y, r, border_color_idx, fill_color_idx) -> null
0x1005 => {
let fill_color_idx = vm.pop_integer()? as usize;
let border_color_idx = vm.pop_integer()? as usize;
@@ -611,9 +625,10 @@ impl NativeInterface for PrometeuOS {
let fill_color = self.get_color(fill_color_idx, hw);
let border_color = self.get_color(border_color_idx, hw);
hw.gfx_mut().draw_disc(x, y, r, border_color, fill_color);
vm.push(Value::Null);
Ok(300)
}
// gfx.draw_square(x, y, w, h, border_color_idx, fill_color_idx)
// gfx.draw_square(x, y, w, h, border_color_idx, fill_color_idx) -> null
0x1006 => {
let fill_color_idx = vm.pop_integer()? as usize;
let border_color_idx = vm.pop_integer()? as usize;
@@ -624,6 +639,7 @@ impl NativeInterface for PrometeuOS {
let fill_color = self.get_color(fill_color_idx, hw);
let border_color = self.get_color(border_color_idx, hw);
hw.gfx_mut().draw_square(x, y, w, h, border_color, fill_color);
vm.push(Value::Null);
Ok(200)
}
@@ -657,6 +673,7 @@ impl NativeInterface for PrometeuOS {
if let Some(s) = sample {
hw.audio_mut().play(s, voice_id, volume, pan, pitch, 0, crate::hardware::LoopMode::Off);
}
vm.push(Value::Null);
Ok(300)
}
@@ -670,13 +687,13 @@ impl NativeInterface for PrometeuOS {
_ => return Err("Expected string path".into()),
};
if self.fs_state != FsState::Mounted {
vm.push(Value::Integer(-1));
vm.push(Value::Int64(-1));
return Ok(100);
}
let handle = self.next_handle;
self.open_files.insert(handle, path);
self.next_handle += 1;
vm.push(Value::Integer(handle as i64));
vm.push(Value::Int64(handle as i64));
Ok(200)
}
// FS_READ(handle) -> content
@@ -718,6 +735,7 @@ impl NativeInterface for PrometeuOS {
0x4004 => {
let handle = vm.pop_integer()? as u32;
self.open_files.remove(&handle);
vm.push(Value::Null);
Ok(100)
}
// FS_LISTDIR(path)
@@ -770,7 +788,7 @@ impl NativeInterface for PrometeuOS {
_ => return Err("Expected string message".into()),
};
let level = vm.pop_integer()?;
self.syscall_log_write(level, 0, msg)
self.syscall_log_write(vm, level, 0, msg)
}
// LOG_WRITE_TAG(level, tag, msg)
0x5002 => {
@@ -780,7 +798,7 @@ impl NativeInterface for PrometeuOS {
};
let tag = vm.pop_integer()? as u16;
let level = vm.pop_integer()?;
self.syscall_log_write(level, tag, msg)
self.syscall_log_write(vm, level, tag, msg)
}
_ => Err(format!("Unknown syscall: 0x{:08X}", id)),