align packer specs and loader
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
use super::*;
|
||||
use crate::fs::{FsBackend, FsEntry, FsError};
|
||||
use prometeu_bytecode::TRAP_TYPE;
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::assembler::assemble;
|
||||
use prometeu_bytecode::model::{BytecodeModule, ConstantPoolEntry, FunctionMeta, SyscallDecl};
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use crate::fs::{FsBackend, FsEntry, FsError};
|
||||
use prometeu_hal::AudioOpStatus;
|
||||
use prometeu_hal::asset::{AssetEntry, AssetLoadError, AssetOpStatus, BankType, LoadStatus};
|
||||
use prometeu_hal::GfxOpStatus;
|
||||
use prometeu_hal::InputSignals;
|
||||
use prometeu_hal::asset::{AssetEntry, AssetLoadError, AssetOpStatus, BankType, LoadStatus};
|
||||
use prometeu_hal::cartridge::{AssetsPayloadSource, Cartridge};
|
||||
use prometeu_hal::syscalls::caps;
|
||||
use prometeu_hal::tile_bank::TILE_BANK_PALETTE_COUNT_V1;
|
||||
use prometeu_vm::VmInitError;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -92,6 +93,14 @@ fn serialized_single_function_module_with_consts(
|
||||
.serialize()
|
||||
}
|
||||
|
||||
fn test_tile_payload_size(width: usize, height: usize) -> usize {
|
||||
(width * height).div_ceil(2) + (TILE_BANK_PALETTE_COUNT_V1 * 16 * std::mem::size_of::<u16>())
|
||||
}
|
||||
|
||||
fn test_tile_decoded_size(width: usize, height: usize) -> usize {
|
||||
width * height + (TILE_BANK_PALETTE_COUNT_V1 * 16 * std::mem::size_of::<u16>())
|
||||
}
|
||||
|
||||
fn test_tile_asset_entry(asset_name: &str, data_len: usize) -> AssetEntry {
|
||||
AssetEntry {
|
||||
asset_id: 7,
|
||||
@@ -99,19 +108,21 @@ fn test_tile_asset_entry(asset_name: &str, data_len: usize) -> AssetEntry {
|
||||
bank_type: BankType::TILES,
|
||||
offset: 0,
|
||||
size: data_len as u64,
|
||||
decoded_size: data_len as u64,
|
||||
decoded_size: test_tile_decoded_size(16, 16) as u64,
|
||||
codec: "RAW".to_string(),
|
||||
metadata: serde_json::json!({
|
||||
"tile_size": 16,
|
||||
"width": 16,
|
||||
"height": 16
|
||||
"height": 16,
|
||||
"palette_count": TILE_BANK_PALETTE_COUNT_V1
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn test_tile_asset_data() -> Vec<u8> {
|
||||
let mut data = vec![1u8; 256];
|
||||
data.extend_from_slice(&[0u8; 2048]);
|
||||
let mut data =
|
||||
vec![0x11u8; test_tile_payload_size(16, 16) - (TILE_BANK_PALETTE_COUNT_V1 * 16 * 2)];
|
||||
data.extend_from_slice(&[0u8; TILE_BANK_PALETTE_COUNT_V1 * 16 * 2]);
|
||||
data
|
||||
}
|
||||
|
||||
@@ -405,10 +416,7 @@ fn tick_gfx_set_sprite_invalid_index_returns_status_not_crash() {
|
||||
let report = runtime.tick(&mut vm, &signals, &mut hardware);
|
||||
assert!(report.is_none(), "invalid sprite index must not crash");
|
||||
assert!(vm.is_halted());
|
||||
assert_eq!(
|
||||
vm.operand_stack_top(1),
|
||||
vec![Value::Int64(GfxOpStatus::InvalidSpriteIndex as i64)]
|
||||
);
|
||||
assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(GfxOpStatus::InvalidSpriteIndex as i64)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -432,12 +440,19 @@ fn tick_gfx_set_sprite_invalid_range_returns_status_not_crash() {
|
||||
}],
|
||||
);
|
||||
let cartridge = cartridge_with_program(program, caps::GFX);
|
||||
let asset_data = test_tile_asset_data();
|
||||
|
||||
hardware.assets.initialize_for_cartridge(
|
||||
vec![test_tile_asset_entry("tile_asset", asset_data.len())],
|
||||
vec![prometeu_hal::asset::PreloadEntry { asset_id: 7, slot: 0 }],
|
||||
AssetsPayloadSource::from_bytes(asset_data),
|
||||
);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report = runtime.tick(&mut vm, &signals, &mut hardware);
|
||||
assert!(report.is_none(), "invalid gfx parameter range must not crash");
|
||||
assert!(vm.is_halted());
|
||||
assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(GfxOpStatus::BankInvalid as i64)]);
|
||||
assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(GfxOpStatus::ArgRangeInvalid as i64)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -446,8 +461,10 @@ fn tick_audio_play_sample_operational_error_returns_status_not_crash() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_I32 -1\nPUSH_I32 0\nPUSH_I32 255\nPUSH_I32 128\nPUSH_I32 1\nHOSTCALL 0\nHALT")
|
||||
.expect("assemble");
|
||||
let code = assemble(
|
||||
"PUSH_I32 -1\nPUSH_I32 0\nPUSH_I32 255\nPUSH_I32 128\nPUSH_I32 1\nHOSTCALL 0\nHALT",
|
||||
)
|
||||
.expect("assemble");
|
||||
let program = serialized_single_function_module(
|
||||
code,
|
||||
vec![SyscallDecl {
|
||||
@@ -548,9 +565,8 @@ fn tick_audio_play_type_mismatch_surfaces_trap_not_panic() {
|
||||
let cartridge = cartridge_with_program(program, caps::AUDIO);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report = runtime
|
||||
.tick(&mut vm, &signals, &mut hardware)
|
||||
.expect("type mismatch must surface as trap");
|
||||
let report =
|
||||
runtime.tick(&mut vm, &signals, &mut hardware).expect("type mismatch must surface as trap");
|
||||
match report {
|
||||
CrashReport::VmTrap { trap } => {
|
||||
assert_eq!(trap.code, TRAP_TYPE);
|
||||
@@ -593,7 +609,8 @@ fn tick_asset_load_missing_asset_returns_status_and_zero_handle() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_CONST 0\nPUSH_I32 0\nPUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let code =
|
||||
assemble("PUSH_CONST 0\nPUSH_I32 0\nPUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module_with_consts(
|
||||
code,
|
||||
vec![ConstantPoolEntry::String("missing_asset".into())],
|
||||
@@ -629,7 +646,8 @@ fn tick_asset_load_invalid_slot_returns_status_and_zero_handle() {
|
||||
vec![],
|
||||
AssetsPayloadSource::from_bytes(asset_data),
|
||||
);
|
||||
let code = assemble("PUSH_CONST 0\nPUSH_I32 0\nPUSH_I32 16\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let code =
|
||||
assemble("PUSH_CONST 0\nPUSH_I32 0\nPUSH_I32 16\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module_with_consts(
|
||||
code,
|
||||
vec![ConstantPoolEntry::String("tile_asset".into())],
|
||||
@@ -665,7 +683,8 @@ fn tick_asset_load_kind_mismatch_returns_status_and_zero_handle() {
|
||||
vec![],
|
||||
AssetsPayloadSource::from_bytes(asset_data),
|
||||
);
|
||||
let code = assemble("PUSH_CONST 0\nPUSH_I32 1\nPUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let code =
|
||||
assemble("PUSH_CONST 0\nPUSH_I32 1\nPUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module_with_consts(
|
||||
code,
|
||||
vec![ConstantPoolEntry::String("tile_asset".into())],
|
||||
@@ -721,10 +740,8 @@ fn tick_asset_commit_invalid_transition_returns_status_not_crash() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble(
|
||||
"PUSH_I32 1\nHOSTCALL 0\nPOP_N 1\nPUSH_I32 1\nHOSTCALL 1\nHALT",
|
||||
)
|
||||
.expect("assemble");
|
||||
let code = assemble("PUSH_I32 1\nHOSTCALL 0\nPOP_N 1\nPUSH_I32 1\nHOSTCALL 1\nHALT")
|
||||
.expect("assemble");
|
||||
let program = serialized_single_function_module(
|
||||
code,
|
||||
vec![
|
||||
@@ -849,7 +866,8 @@ fn tick_asset_load_invalid_kind_surfaces_trap_not_panic() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_CONST 0\nPUSH_I32 2\nPUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let code =
|
||||
assemble("PUSH_CONST 0\nPUSH_I32 2\nPUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module_with_consts(
|
||||
code,
|
||||
vec![ConstantPoolEntry::String("tile_asset".into())],
|
||||
@@ -892,9 +910,7 @@ fn tick_status_first_surface_smoke_across_gfx_audio_and_asset() {
|
||||
.expect("assemble");
|
||||
let program = serialized_single_function_module_with_consts(
|
||||
code,
|
||||
vec![
|
||||
ConstantPoolEntry::String("missing_asset".into()),
|
||||
],
|
||||
vec![ConstantPoolEntry::String("missing_asset".into())],
|
||||
vec![
|
||||
SyscallDecl {
|
||||
module: "gfx".into(),
|
||||
@@ -959,9 +975,8 @@ fn tick_gfx_set_sprite_type_mismatch_surfaces_trap_not_panic() {
|
||||
let cartridge = cartridge_with_program(program, caps::GFX);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report = runtime
|
||||
.tick(&mut vm, &signals, &mut hardware)
|
||||
.expect("type mismatch must surface as trap");
|
||||
let report =
|
||||
runtime.tick(&mut vm, &signals, &mut hardware).expect("type mismatch must surface as trap");
|
||||
match report {
|
||||
CrashReport::VmTrap { trap } => {
|
||||
assert_eq!(trap.code, TRAP_TYPE);
|
||||
|
||||
Reference in New Issue
Block a user