//! Real core playback and executed allocation evidence, including error paths. use super::*; use prometeu_vm::LogicalFrameEndingReason as End; use prometeu_bytecode::{decode_next, isa::core::CoreOpCode as Op}; pub fn verify(root: &std::path::Path) { let manifest = std::fs::read_to_string(root.join("pbs-animation-manifest.txt")).expect("mandatory animation manifest"); let names: Vec<_> = manifest.lines().collect(); assert_eq!(names, ["basic", "loop", "once", "negative", "overflow", "draft", "limits", "matrix", "errors"]); for name in names { for suffix in ["", "-optimized"] { let bytes = std::fs::read(root.join(format!("pbs-animation-{name}{suffix}.pbx"))).expect("mandatory animation fixture"); let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE) .unwrap_or_else(|e| panic!("animation/{name}{suffix}: load {e:?}")); let mut vm = VirtualMachine::from_image(image.clone(), Default::default()); vm.prepare_boot_call().unwrap(); let mut watching = false; let mut started = 0; let mut ended = 0; let mut allocations = 0; let mut measured_steps=0; for step in 0..2_000_000 { let instruction = decode_next(vm.pc(), image.code()).unwrap(); if instruction.opcode == Op::PushI32 { if instruction.imm_i32().unwrap() == 162000001 { watching = true; started += 1; } if instruction.imm_i32().unwrap() == 162000002 { watching = false; ended += 1; } } if watching { measured_steps+=1; } if watching && matches!(instruction.opcode, Op::StructNew | Op::ArrayNew | Op::MakeClosure) { allocations += 1; } let report = vm.run_budget(1, &mut NoNative, &mut HostContext::new(None)) .unwrap_or_else(|e| panic!("animation/{name}{suffix}: run {e:?}")); if matches!(report.reason, End::Halted | End::EndOfRom) { break; } assert!(matches!(report.reason, End::BudgetExhausted), "animation/{name}{suffix}: {report:?}"); assert!(step < 1_999_999, "animation failed to terminate"); } assert_eq!(vm.operand_stack_top(2), vec![Value::Int32(42), Value::Int32(0)], "animation/{name}{suffix}"); if !matches!(name, "basic" | "draft" | "limits" | "matrix") { assert_eq!((started, ended), (1, 1)); } assert!(measured_steps<50000,"bounded frame traversal: {measured_steps}"); assert_eq!(allocations, 0, "animation/{name}{suffix}: executed advance/sprite heap allocations"); if name=="basic" { use prometeu_vm::resource_budget::{RuntimeLimits,RuntimeResource}; for resource in [RuntimeResource::GuestBytes,RuntimeResource::HandleSlots] { let mut refused=VirtualMachine::from_image(image.clone(),RuntimeLimits::default().lowered(resource,0).unwrap()); refused.prepare_boot_call().unwrap();let report=refused.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap(); assert!(matches!(report.reason,End::Trap(ref fault) if fault.code==prometeu_bytecode::TRAP_RESOURCE_LIMIT)); assert_eq!(refused.heap().len(),0);println!("ANIMATION QUOTA TRAP PASS {suffix} {resource:?}"); } } println!("ANIMATION PASS {name}{suffix} watched={started} executed_allocations={allocations}"); } } verify_gc(root); verify_installed(root); verify_composer(root); } fn verify_gc(root: &std::path::Path) { for suffix in ["", "-optimized"] { let bytes=std::fs::read(root.join(format!("pbs-animation-gc{suffix}.pbx"))).unwrap(); let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap(); let mut vm=VirtualMachine::from_image(image.clone(),Default::default());vm.prepare_boot_call().unwrap(); for round in 0..2 { let report=vm.run_budget(30_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap(); assert!(matches!(report.reason,End::FrameSync),"animation GC: {report:?}"); let objects=vm.heap().diagnostic_typed_storage(); for name in ["Animator","_AnimationClip","_AnimationFrame"] { let count=objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some(name)).count(); if round==0 {assert!(count>0,"live animation root {name}");} else {assert_eq!(count,0,"unowned cycle {name}");} } assert!(vm.resource_snapshot().gc.reclaimed>1000); println!("ANIMATION GC PASS {suffix} round={round} collections={} reclaimed={}",vm.resource_snapshot().gc.collections,vm.resource_snapshot().gc.reclaimed); } } } fn verify_installed(root: &std::path::Path) { use prometeu_drivers::TestPlatform; use prometeu_firmware::firmware::Firmware; use prometeu_hal::{InputSignals,app_mode::AppMode,cartridge::{Cartridge,AssetsPayloadSource}}; for suffix in ["", "-optimized"] { let name=format!("pbs-animation-installed{suffix}"); let mut fw=Firmware::new(None);let mut platform=TestPlatform::new(); fw.load_cartridge(Cartridge{app_id:84,title:"Studio animation".into(),app_version:"1.0.0".into(),app_mode:AppMode::Game,capabilities:caps::NONE,memcard_slots:None,program:std::fs::read(root.join(format!("{name}.pbx"))).unwrap(),assets:AssetsPayloadSource::empty(),asset_table:vec![],preload:vec![]}); for _ in 0..5 {fw.tick(&InputSignals::default(),&mut platform);assert!(fw.os.vm().last_crash_report().is_none());} let task=fw.os.lifecycle().resident_game_task().unwrap();let mut sessions=fw.os.sessions();let session=sessions.vm_session_for_task_mut(task).unwrap(); let id=std::fs::read_to_string(root.join(format!("{name}.probe"))).unwrap();session.vm.prepare_call(id.trim()).unwrap(); let report=session.vm.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap();assert!(matches!(report.reason,End::Halted|End::EndOfRom));assert_eq!(session.vm.operand_stack_top(1),vec![Value::Int32(42)]); println!("ANIMATION INSTALLED GENERATED/FIRMWARE PASS {suffix}"); } } fn verify_composer(root: &std::path::Path) { use prometeu_drivers::TestPlatform; use prometeu_firmware::firmware::Firmware; use prometeu_hal::{InputSignals,app_mode::AppMode,cartridge::{Cartridge,AssetsPayloadSource,AssetsPackPrelude}}; for suffix in ["", "-optimized"] { let prefix=format!("pbs-animation-composer{suffix}"); let manifest:serde_json::Value=serde_json::from_slice(&std::fs::read(root.join(format!("{prefix}.manifest.json"))).unwrap()).unwrap(); let oracle:serde_json::Value=serde_json::from_slice(&std::fs::read(root.join(format!("{prefix}.oracle.json"))).unwrap()).unwrap(); let packed=std::fs::read(root.join(format!("{prefix}.assets.pa"))).unwrap();let prelude=AssetsPackPrelude::from_bytes(&packed).unwrap(); let mut fw=Firmware::new(None);let mut platform=TestPlatform::new(); fw.load_cartridge(Cartridge{app_id:84,title:"Studio animation Composer".into(),app_version:"1.0.0".into(),app_mode:AppMode::Game, capabilities:caps::COMPOSER,memcard_slots:None, program:std::fs::read(root.join(format!("{prefix}.pbx"))).unwrap(),assets:AssetsPayloadSource::from_bytes(packed[prelude.payload_offset as usize..].to_vec()), asset_table:serde_json::from_value(manifest["asset_table"].clone()).unwrap(),preload:serde_json::from_value(manifest["preload"].clone()).unwrap()}); let mut observed=0; for _ in 0..50000 { fw.tick(&InputSignals::default(),&mut platform); assert!(fw.os.vm().last_crash_report().is_none(),"Composer {suffix}: {:?}",fw.os.vm().last_crash_report()); if let Some(task)=fw.os.lifecycle().resident_game_task() { let completed={let sessions=fw.os.sessions();sessions.vm_session_for_task(task).map(|s|s.runtime.atomic_telemetry.completed_logical_frames.load(std::sync::atomic::Ordering::Relaxed)).unwrap_or(0)}; if completed>observed { assert_eq!(completed,observed+1,"one native frame per observation"); let sample=&oracle[observed as usize];let pixels=sample["rgba"].as_array().unwrap();let front=platform.local_hardware().gfx.front_buffer();let width=platform.local_hardware().gfx.size().0; for x in [32,64] {for y in 0..16 {for dx in 0..16 {assert_eq!(front[(32+y)*width+x+dx],pixels[y*16+dx].as_u64().unwrap() as u32,"{prefix} tick={observed} x={x} pixel={dx},{y}");}}} println!("ANIMATION COMPOSER PIXELS PASS {suffix} tick={observed} glyph={} palette={}",sample["glyph"],sample["palette"]); observed=completed;if observed==8 {break;} } } std::thread::yield_now(); } assert_eq!(observed,8,"native Composer did not produce all frames"); } }