realign and clean up
This commit is contained in:
@@ -5,7 +5,7 @@ use prometeu_hal::log::{LogLevel, LogService, LogSource};
|
||||
use prometeu_hal::telemetry::{CertificationConfig, Certifier, TelemetryFrame};
|
||||
use std::collections::HashMap;
|
||||
use std::time::Instant;
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::{Value, TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_TYPE};
|
||||
use prometeu_hal::{expect_bool, expect_int, HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
use prometeu_hal::asset::{BankType, LoadStatus, SlotRef};
|
||||
use prometeu_hal::button::Button;
|
||||
@@ -352,7 +352,7 @@ impl VirtualMachineRuntime {
|
||||
2 => LogLevel::Info,
|
||||
3 => LogLevel::Warn,
|
||||
4 => LogLevel::Error,
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, format!("Invalid log level: {}", level_val))),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, format!("Invalid log level: {}", level_val))),
|
||||
};
|
||||
|
||||
let app_id = self.current_app_id;
|
||||
@@ -422,427 +422,6 @@ impl VirtualMachineRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use prometeu_hal::{HostReturn, InputSignals};
|
||||
use super::*;
|
||||
|
||||
fn call_syscall(os: &mut VirtualMachineRuntime, id: u32, vm: &mut VirtualMachine, hw: &mut dyn HardwareBridge) -> Result<(), VmFault> {
|
||||
let args_count = Syscall::from_u32(id).expect(&format!("Invalid syscall id: 0x{:08X}", id)).args_count();
|
||||
let mut args = Vec::new();
|
||||
for _ in 0..args_count {
|
||||
// Protege contra underflow/erros de pilha durante testes
|
||||
match vm.pop() {
|
||||
Ok(v) => args.push(v),
|
||||
Err(e) => return Err(VmFault::Panic(e)),
|
||||
}
|
||||
}
|
||||
args.reverse();
|
||||
let mut ret = HostReturn::new(&mut vm.operand_stack);
|
||||
os.syscall(id, &args, &mut ret, &mut HostContext::new(Some(hw)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_infinite_loop_budget_reset_bug() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
|
||||
let rom = prometeu_bytecode::BytecodeModule {
|
||||
version: 0,
|
||||
const_pool: vec![],
|
||||
functions: vec![prometeu_bytecode::FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 6,
|
||||
param_slots: 0,
|
||||
local_slots: 0,
|
||||
return_slots: 0,
|
||||
max_stack_slots: 0,
|
||||
}],
|
||||
code: vec![0x02, 0x00, 0x00, 0x00, 0x00, 0x00],
|
||||
debug_info: None,
|
||||
exports: vec![prometeu_bytecode::Export { symbol: "main".into(), func_idx: 0 }],
|
||||
}.serialize();
|
||||
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 tick
|
||||
os.tick(&mut vm, &signals, &mut hw);
|
||||
let cycles_after_tick_1 = vm.cycles;
|
||||
assert!(cycles_after_tick_1 >= VirtualMachineRuntime::CYCLES_PER_LOGICAL_FRAME);
|
||||
|
||||
// Second tick - Now it SHOULD NOT gain more budget
|
||||
os.tick(&mut vm, &signals, &mut hw);
|
||||
let cycles_after_tick_2 = vm.cycles;
|
||||
|
||||
// FIX: It should not have consumed cycles in the second tick because the logical frame budget ended
|
||||
println!("Cycles after tick 1: {}, tick 2: {}", cycles_after_tick_1, cycles_after_tick_2);
|
||||
assert_eq!(cycles_after_tick_2, cycles_after_tick_1, "VM should NOT have consumed more cycles in the second tick because logical frame budget is exhausted");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_budget_reset_on_frame_sync() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
|
||||
// Loop that calls FrameSync:
|
||||
// PUSH_CONST 0 (dummy)
|
||||
// FrameSync (0x80)
|
||||
// JMP 0
|
||||
let rom = prometeu_bytecode::BytecodeModule {
|
||||
version: 0,
|
||||
const_pool: vec![],
|
||||
functions: vec![prometeu_bytecode::FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 8,
|
||||
param_slots: 0,
|
||||
local_slots: 0,
|
||||
return_slots: 0,
|
||||
max_stack_slots: 0,
|
||||
}],
|
||||
code: vec![
|
||||
0x80, 0x00, // FrameSync (2 bytes opcode)
|
||||
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // JMP 0 (2 bytes opcode + 4 bytes u32)
|
||||
],
|
||||
debug_info: None,
|
||||
exports: vec![prometeu_bytecode::Export { symbol: "main".into(), func_idx: 0 }],
|
||||
}.serialize();
|
||||
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 tick
|
||||
os.tick(&mut vm, &signals, &mut hw);
|
||||
let cycles_after_tick_1 = vm.cycles;
|
||||
|
||||
// Should have stopped at FrameSync
|
||||
assert!(cycles_after_tick_1 > 0, "VM should have consumed some cycles");
|
||||
assert!(cycles_after_tick_1 < VirtualMachineRuntime::CYCLES_PER_LOGICAL_FRAME);
|
||||
|
||||
// Second tick - Should reset the budget and run a bit more until the next FrameSync
|
||||
os.tick(&mut vm, &signals, &mut hw);
|
||||
let cycles_after_tick_2 = vm.cycles;
|
||||
|
||||
assert!(cycles_after_tick_2 > cycles_after_tick_1, "VM should have consumed more cycles because FrameSync reset the budget");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_telemetry_cycles_budget() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
|
||||
// Standard budget
|
||||
os.tick(&mut vm, &signals, &mut hw);
|
||||
assert_eq!(os.telemetry_current.cycles_budget, VirtualMachineRuntime::CYCLES_PER_LOGICAL_FRAME);
|
||||
|
||||
// Custom budget via CAP
|
||||
let mut config = CertificationConfig::default();
|
||||
config.enabled = true;
|
||||
config.cycles_budget_per_frame = Some(50000);
|
||||
|
||||
let mut os_custom = VirtualMachineRuntime::new(Some(config));
|
||||
os_custom.tick(&mut vm, &signals, &mut hw);
|
||||
assert_eq!(os_custom.telemetry_current.cycles_budget, 50000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_color_logic() {
|
||||
let os = VirtualMachineRuntime::new(None);
|
||||
|
||||
// Deve retornar a cor raw diretamente
|
||||
assert_eq!(os.get_color(0x07E0), Color::GREEN);
|
||||
assert_eq!(os.get_color(0xF800), Color::RED);
|
||||
assert_eq!(os.get_color(0x001F), Color::BLUE);
|
||||
assert_eq!(os.get_color(3), Color::from_raw(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gfx_set_sprite_syscall_pops() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
|
||||
// Push arguments in order 1 to 10
|
||||
vm.push(Value::String("mouse_cursor".to_string())); // arg1: assetName
|
||||
vm.push(Value::Int32(0)); // arg2: id
|
||||
|
||||
// Simulating touch.x and touch.y syscalls
|
||||
vm.push(Value::Int32(10)); // arg3: x (returned from syscall)
|
||||
vm.push(Value::Int32(20)); // arg4: y (returned from syscall)
|
||||
|
||||
vm.push(Value::Int32(0)); // arg5: tileId
|
||||
vm.push(Value::Int32(0)); // arg6: paletteId
|
||||
vm.push(Value::Boolean(true)); // arg7: active
|
||||
vm.push(Value::Boolean(false)); // arg8: flipX
|
||||
vm.push(Value::Boolean(false)); // arg9: flipY
|
||||
vm.push(Value::Int32(4)); // arg10: priority
|
||||
|
||||
let res = call_syscall(&mut os, 0x1007, &mut vm, &mut hw);
|
||||
assert!(res.is_ok(), "GfxSetSprite syscall should succeed, but got: {:?}", res.err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gfx_set_sprite_with_swapped_arguments_repro() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
|
||||
// Repro: what if the compiler is pushing in reverse order?
|
||||
vm.push(Value::Int32(4)); // arg10?
|
||||
vm.push(Value::Boolean(false));
|
||||
vm.push(Value::Boolean(false));
|
||||
vm.push(Value::Boolean(true));
|
||||
vm.push(Value::Int32(0));
|
||||
vm.push(Value::Int32(0));
|
||||
vm.push(Value::Int32(20));
|
||||
vm.push(Value::Int32(10));
|
||||
vm.push(Value::Int32(0));
|
||||
vm.push(Value::String("mouse_cursor".to_string())); // arg1?
|
||||
|
||||
let res = call_syscall(&mut os, 0x1007, &mut vm, &mut hw);
|
||||
assert!(res.is_err());
|
||||
// Because it tries to pop priority but gets a string
|
||||
match res.err().unwrap() {
|
||||
VmFault::Trap(code, _) => assert_eq!(code, prometeu_bytecode::abi::TRAP_TYPE),
|
||||
_ => panic!("Expected Trap"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_syscall_log_write_and_rate_limit() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
|
||||
os.current_app_id = 123;
|
||||
|
||||
// 1. Normal log test
|
||||
vm.push(Value::Int64(2)); // Info
|
||||
vm.push(Value::String("Hello Log".to_string()));
|
||||
let res = call_syscall(&mut os, 0x5001, &mut vm, &mut hw);
|
||||
assert!(res.is_ok());
|
||||
|
||||
let recent = os.log_service.get_recent(1);
|
||||
assert_eq!(recent[0].msg, "Hello Log");
|
||||
assert_eq!(recent[0].level, LogLevel::Info);
|
||||
assert_eq!(recent[0].source, LogSource::App { app_id: 123 });
|
||||
|
||||
// 2. Truncation test
|
||||
let long_msg = "A".repeat(300);
|
||||
vm.push(Value::Int64(3)); // Warn
|
||||
vm.push(Value::String(long_msg));
|
||||
call_syscall(&mut os, 0x5001, &mut vm, &mut hw).unwrap();
|
||||
|
||||
let recent = os.log_service.get_recent(1);
|
||||
assert_eq!(recent[0].msg.len(), 256);
|
||||
assert!(recent[0].msg.starts_with("AAAAA"));
|
||||
|
||||
// 3. Rate Limit Test
|
||||
// We already made 2 logs. The limit is 10.
|
||||
for i in 0..8 {
|
||||
vm.push(Value::Int64(2));
|
||||
vm.push(Value::String(format!("Log {}", i)));
|
||||
call_syscall(&mut os, 0x5001, &mut vm, &mut hw).unwrap();
|
||||
}
|
||||
|
||||
// The 11th log should be ignored (and generate a system warning)
|
||||
vm.push(Value::Int64(2));
|
||||
vm.push(Value::String("Eleventh log".to_string()));
|
||||
call_syscall(&mut os, 0x5001, &mut vm, &mut hw).unwrap();
|
||||
|
||||
let recent = os.log_service.get_recent(2);
|
||||
// The last log should be the rate limit warning (came after the 10th log attempted)
|
||||
assert_eq!(recent[1].msg, "App exceeded log limit per frame");
|
||||
assert_eq!(recent[1].level, LogLevel::Warn);
|
||||
// The log "Eleventh log" should not be there
|
||||
assert_ne!(recent[0].msg, "Eleventh log");
|
||||
|
||||
// 4. Rate limit reset test in the next frame
|
||||
os.begin_logical_frame(&InputSignals::default(), &mut hw);
|
||||
vm.push(Value::Int64(2));
|
||||
vm.push(Value::String("New frame log".to_string()));
|
||||
call_syscall(&mut os, 0x5001, &mut vm, &mut hw).unwrap();
|
||||
|
||||
let recent = os.log_service.get_recent(1);
|
||||
assert_eq!(recent[0].msg, "New frame log");
|
||||
|
||||
// 5. LOG_WRITE_TAG test
|
||||
vm.push(Value::Int64(2)); // Info
|
||||
vm.push(Value::Int64(42)); // Tag
|
||||
vm.push(Value::String("Tagged Log".to_string()));
|
||||
call_syscall(&mut os, 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);
|
||||
// Syscall de log é void: não empurra valor na pilha
|
||||
|
||||
// 6. GFX Syscall return test
|
||||
vm.push(Value::Int64(1)); // color_idx
|
||||
call_syscall(&mut os, 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 = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
|
||||
// PushI32 0 (0x17), then Ret (0x51)
|
||||
let rom = prometeu_bytecode::BytecodeModule {
|
||||
version: 0,
|
||||
const_pool: vec![],
|
||||
functions: vec![prometeu_bytecode::FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 10,
|
||||
param_slots: 0,
|
||||
local_slots: 0,
|
||||
return_slots: 0,
|
||||
max_stack_slots: 0,
|
||||
}],
|
||||
code: vec![
|
||||
0x17, 0x00, // PushI32
|
||||
0x00, 0x00, 0x00, 0x00, // value 0
|
||||
0x11, 0x00, // Pop
|
||||
0x51, 0x00 // Ret
|
||||
],
|
||||
debug_info: None,
|
||||
exports: vec![prometeu_bytecode::Export { symbol: "main".into(), func_idx: 0 }],
|
||||
}.serialize();
|
||||
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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_os_unknown_syscall_returns_trap() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let mut ret = HostReturn::new(&mut vm.operand_stack);
|
||||
|
||||
let res = os.syscall(0xDEADBEEF, &[], &mut ret, &mut HostContext::new(Some(&mut hw)));
|
||||
assert!(res.is_err());
|
||||
match res.err().unwrap() {
|
||||
VmFault::Trap(code, _) => assert_eq!(code, prometeu_bytecode::abi::TRAP_INVALID_SYSCALL),
|
||||
_ => panic!("Expected Trap"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gfx_clear565_syscall() {
|
||||
let mut hw = Hardware::new();
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut stack = Vec::new();
|
||||
|
||||
// Success case
|
||||
let args = vec![Value::Bounded(0xF800)]; // Red
|
||||
{
|
||||
let mut ret = HostReturn::new(&mut stack);
|
||||
os.syscall(Syscall::GfxClear565 as u32, &args, &mut ret, &mut HostContext::new(Some(&mut hw))).unwrap();
|
||||
}
|
||||
assert_eq!(stack.len(), 0); // void return
|
||||
|
||||
// OOB case
|
||||
let args = vec![Value::Bounded(0x10000)];
|
||||
{
|
||||
let mut ret = HostReturn::new(&mut stack);
|
||||
let res = os.syscall(Syscall::GfxClear565 as u32, &args, &mut ret, &mut HostContext::new(Some(&mut hw)));
|
||||
assert!(res.is_err());
|
||||
match res.err().unwrap() {
|
||||
VmFault::Trap(trap, _) => assert_eq!(trap, prometeu_bytecode::abi::TRAP_OOB),
|
||||
_ => panic!("Expected Trap OOB"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_input_snapshots_syscalls() {
|
||||
let mut hw = Hardware::new();
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
|
||||
// Pad snapshot
|
||||
let mut stack = Vec::new();
|
||||
{
|
||||
let mut ret = HostReturn::new(&mut stack);
|
||||
os.syscall(Syscall::InputPadSnapshot as u32, &[], &mut ret, &mut HostContext::new(Some(&mut hw))).unwrap();
|
||||
}
|
||||
assert_eq!(stack.len(), 48);
|
||||
|
||||
// Touch snapshot
|
||||
let mut stack = Vec::new();
|
||||
{
|
||||
let mut ret = HostReturn::new(&mut stack);
|
||||
os.syscall(Syscall::InputTouchSnapshot as u32, &[], &mut ret, &mut HostContext::new(Some(&mut hw))).unwrap();
|
||||
}
|
||||
assert_eq!(stack.len(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_os_syscall_without_hardware_fails_graciously() {
|
||||
let mut os = VirtualMachineRuntime::new(None);
|
||||
let mut stack = Vec::new();
|
||||
let mut ret = HostReturn::new(&mut stack);
|
||||
|
||||
// GfxClear needs hardware
|
||||
let res = os.syscall(Syscall::GfxClear as u32, &[Value::Int64(0)], &mut ret, &mut HostContext::new(None));
|
||||
assert_eq!(res, Err(VmFault::Unavailable));
|
||||
|
||||
// SystemHasCart DOES NOT need hardware
|
||||
let res = os.syscall(Syscall::SystemHasCart as u32, &[], &mut ret, &mut HostContext::new(None));
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
impl NativeInterface for VirtualMachineRuntime {
|
||||
/// Dispatches a syscall from the VM to the native implementation.
|
||||
///
|
||||
@@ -857,7 +436,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
/// Each syscall returns the number of virtual cycles it consumed.
|
||||
fn syscall(&mut self, id: SyscallId, args: &[Value], ret: &mut HostReturn, ctx: &mut HostContext) -> Result<(), VmFault> {
|
||||
self.telemetry_current.syscalls += 1;
|
||||
let syscall = Syscall::from_u32(id).ok_or_else(|| VmFault::Trap(prometeu_bytecode::abi::TRAP_INVALID_SYSCALL, format!(
|
||||
let syscall = Syscall::from_u32(id).ok_or_else(|| VmFault::Trap(TRAP_INVALID_SYSCALL, format!(
|
||||
"Unknown syscall: 0x{:08X}", id
|
||||
)))?;
|
||||
|
||||
@@ -958,7 +537,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::GfxSetSprite => {
|
||||
let asset_name = match args.get(0).ok_or_else(|| VmFault::Panic("Missing asset_name".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string asset_name".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string asset_name".into())),
|
||||
};
|
||||
let index = expect_int(args, 1)? as usize;
|
||||
let x = expect_int(args, 2)? as i32;
|
||||
@@ -971,7 +550,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let priority = expect_int(args, 9)? as u8;
|
||||
|
||||
let bank_id = hw.assets().find_slot_by_name(&asset_name, BankType::TILES).unwrap_or(0);
|
||||
|
||||
|
||||
if index < 512 {
|
||||
*hw.gfx_mut().sprite_mut(index) = Sprite {
|
||||
tile: Tile { id: tile_id, flip_x: false, flip_y: false, palette_id },
|
||||
@@ -992,7 +571,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let y = expect_int(args, 1)? as i32;
|
||||
let msg = match args.get(2).ok_or_else(|| VmFault::Panic("Missing message".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string message".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string message".into())),
|
||||
};
|
||||
let color_val = expect_int(args, 3)?;
|
||||
let color = self.get_color(color_val);
|
||||
@@ -1004,7 +583,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::GfxClear565 => {
|
||||
let color_val = expect_int(args, 0)? as u32;
|
||||
if color_val > 0xFFFF {
|
||||
return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_OOB, "Color value out of bounds (bounded)".into()));
|
||||
return Err(VmFault::Trap(TRAP_OOB, "Color value out of bounds (bounded)".into()));
|
||||
}
|
||||
let color = Color::from_raw(color_val as u16);
|
||||
hw.gfx_mut().clear(color);
|
||||
@@ -1209,7 +788,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Value::Int32(i) => *i as f64,
|
||||
Value::Int64(i) => *i as f64,
|
||||
Value::Bounded(b) => *b as f64,
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected number for pitch".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected number for pitch".into())),
|
||||
};
|
||||
|
||||
hw.audio_mut().play(0, sample_id as u16, voice_id, volume, pan, pitch, 0, prometeu_hal::LoopMode::Off);
|
||||
@@ -1221,7 +800,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::AudioPlay => {
|
||||
let asset_name = match args.get(0).ok_or_else(|| VmFault::Panic("Missing asset_name".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string asset_name".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string asset_name".into())),
|
||||
};
|
||||
let sample_id = expect_int(args, 1)? as u16;
|
||||
let voice_id = expect_int(args, 2)? as usize;
|
||||
@@ -1232,7 +811,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Value::Int32(i) => *i as f64,
|
||||
Value::Int64(i) => *i as f64,
|
||||
Value::Bounded(b) => *b as f64,
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected number for pitch".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected number for pitch".into())),
|
||||
};
|
||||
let loop_mode = match expect_int(args, 6)? {
|
||||
0 => prometeu_hal::LoopMode::Off,
|
||||
@@ -1252,7 +831,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::FsOpen => {
|
||||
let path = match args.get(0).ok_or_else(|| VmFault::Panic("Missing path".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string path".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string path".into())),
|
||||
};
|
||||
if self.fs_state != FsState::Mounted {
|
||||
ret.push_int(-1);
|
||||
@@ -1282,7 +861,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let handle = expect_int(args, 0)? as u32;
|
||||
let content = match args.get(1).ok_or_else(|| VmFault::Panic("Missing content".into()))? {
|
||||
Value::String(s) => s.as_bytes().to_vec(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string content".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string content".into())),
|
||||
};
|
||||
let path = self.open_files.get(&handle).ok_or_else(|| VmFault::Panic("Invalid handle".into()))?;
|
||||
match self.fs.write_file(path, &content) {
|
||||
@@ -1302,7 +881,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::FsListDir => {
|
||||
let path = match args.get(0).ok_or_else(|| VmFault::Panic("Missing path".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string path".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string path".into())),
|
||||
};
|
||||
match self.fs.list_dir(&path) {
|
||||
Ok(entries) => {
|
||||
@@ -1317,7 +896,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::FsExists => {
|
||||
let path = match args.get(0).ok_or_else(|| VmFault::Panic("Missing path".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string path".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string path".into())),
|
||||
};
|
||||
ret.push_bool(self.fs.exists(&path));
|
||||
Ok(())
|
||||
@@ -1326,7 +905,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::FsDelete => {
|
||||
let path = match args.get(0).ok_or_else(|| VmFault::Panic("Missing path".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string path".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string path".into())),
|
||||
};
|
||||
match self.fs.delete(&path) {
|
||||
Ok(_) => ret.push_bool(true),
|
||||
@@ -1342,7 +921,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let level = expect_int(args, 0)?;
|
||||
let msg = match args.get(1).ok_or_else(|| VmFault::Panic("Missing message".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string message".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string message".into())),
|
||||
};
|
||||
self.syscall_log_write(level, 0, msg)?;
|
||||
// void
|
||||
@@ -1354,7 +933,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let tag = expect_int(args, 1)? as u16;
|
||||
let msg = match args.get(2).ok_or_else(|| VmFault::Panic("Missing message".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string message".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string message".into())),
|
||||
};
|
||||
self.syscall_log_write(level, tag, msg)?;
|
||||
// void
|
||||
@@ -1365,15 +944,15 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
Syscall::AssetLoad => {
|
||||
let asset_id = match args.get(0).ok_or_else(|| VmFault::Panic("Missing asset_id".into()))? {
|
||||
Value::String(s) => s.clone(),
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Expected string asset_id".into())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Expected string asset_id".into())),
|
||||
};
|
||||
let asset_type_val = expect_int(args, 1)? as u32;
|
||||
let slot_index = expect_int(args, 2)? as usize;
|
||||
|
||||
|
||||
let asset_type = match asset_type_val {
|
||||
0 => BankType::TILES,
|
||||
1 => BankType::SOUNDS,
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Invalid asset type".to_string())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Invalid asset type".to_string())),
|
||||
};
|
||||
let slot = SlotRef { asset_type, index: slot_index };
|
||||
|
||||
@@ -1416,7 +995,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let asset_type = match asset_type_val {
|
||||
0 => BankType::TILES,
|
||||
1 => BankType::SOUNDS,
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Invalid asset type".to_string())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Invalid asset type".to_string())),
|
||||
};
|
||||
let info = hw.assets().bank_info(asset_type);
|
||||
let json = serde_json::to_string(&info).unwrap_or_default();
|
||||
@@ -1429,7 +1008,7 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
let asset_type = match asset_type_val {
|
||||
0 => BankType::TILES,
|
||||
1 => BankType::SOUNDS,
|
||||
_ => return Err(VmFault::Trap(prometeu_bytecode::abi::TRAP_TYPE, "Invalid asset type".to_string())),
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Invalid asset type".to_string())),
|
||||
};
|
||||
let slot = SlotRef { asset_type, index: slot_index };
|
||||
let info = hw.assets().slot_info(slot);
|
||||
|
||||
Reference in New Issue
Block a user