Implement integer stack opcodes
This commit is contained in:
104
engine/Age.Engine.Tests/IntegerStackOpsTests.cs
Normal file
104
engine/Age.Engine.Tests/IntegerStackOpsTests.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using Age.Engine.Model;
|
||||
using Age.Engine.Sys4;
|
||||
using Age.Engine.Vm;
|
||||
|
||||
public class IntegerStackOpsTests
|
||||
{
|
||||
private static readonly OpcodeTable Table = OpcodeTableJson.Load(Paths.OpcodesJson);
|
||||
private static Operand I(long value) => new(0, value);
|
||||
private static Operand G(long address) => new(3, address);
|
||||
private static Operand L(long address) => new(9, address);
|
||||
private static (int, Operand[]) Exit() => (0x2, Array.Empty<Operand>());
|
||||
|
||||
[Fact]
|
||||
public void IntegerStacksAreIndependentLifosAndResetDiscardsPendingValues()
|
||||
{
|
||||
Script script = ScriptAssembler.Assemble(Table, "INT_STACK",
|
||||
[
|
||||
(0x138, [I(0), I(11)]),
|
||||
(0x138, [I(0), I(-22)]),
|
||||
(0x138, [I(1), I(33)]),
|
||||
|
||||
(0x139, [I(0), L(0), L(1)]),
|
||||
(0x55, [G(0x100), L(0)]),
|
||||
(0x55, [G(0x101), L(1)]),
|
||||
(0x139, [I(0), L(0), L(1)]),
|
||||
(0x55, [G(0x102), L(0)]),
|
||||
(0x55, [G(0x103), L(1)]),
|
||||
(0x139, [I(1), L(0), L(1)]),
|
||||
(0x55, [G(0x104), L(0)]),
|
||||
(0x55, [G(0x105), L(1)]),
|
||||
|
||||
(0x55, [L(1), I(777)]),
|
||||
(0x139, [I(0), L(0), L(1)]),
|
||||
(0x55, [G(0x106), L(0)]),
|
||||
(0x55, [G(0x107), L(1)]),
|
||||
(0x138, [I(1), I(44)]),
|
||||
(0x137, [I(1)]),
|
||||
(0x139, [I(1), L(0), L(1)]),
|
||||
(0x55, [G(0x108), L(0)]),
|
||||
Exit(),
|
||||
], []);
|
||||
var vm = new VirtualMachine(script, Table, new RecordingHost());
|
||||
|
||||
vm.Run();
|
||||
|
||||
Assert.Equal(1, vm.Globals[0x100]);
|
||||
Assert.Equal(-22, vm.Globals[0x101]);
|
||||
Assert.Equal(1, vm.Globals[0x102]);
|
||||
Assert.Equal(11, vm.Globals[0x103]);
|
||||
Assert.Equal(1, vm.Globals[0x104]);
|
||||
Assert.Equal(33, vm.Globals[0x105]);
|
||||
Assert.Equal(0, vm.Globals[0x106]);
|
||||
Assert.Equal(777, vm.Globals[0x107]);
|
||||
Assert.Equal(0, vm.Globals[0x108]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandlerAddressableSlotTenIsSafeIndependentState()
|
||||
{
|
||||
Script script = ScriptAssembler.Assemble(Table, "INT_STACK_SLOT_TEN",
|
||||
[
|
||||
(0x138, [I(0), I(10)]),
|
||||
(0x138, [I(10), I(1010)]),
|
||||
(0x139, [I(10), G(0x100), G(0x101)]),
|
||||
(0x139, [I(0), G(0x102), G(0x103)]),
|
||||
Exit(),
|
||||
], []);
|
||||
var vm = new VirtualMachine(script, Table, new RecordingHost());
|
||||
|
||||
vm.Run();
|
||||
|
||||
Assert.Equal(1, vm.Globals[0x100]);
|
||||
Assert.Equal(1010, vm.Globals[0x101]);
|
||||
Assert.Equal(1, vm.Globals[0x102]);
|
||||
Assert.Equal(10, vm.Globals[0x103]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RootSceneReloadReconstructsEmptyStacks()
|
||||
{
|
||||
Script reloadedSystem4 = ScriptAssembler.Assemble(Table, "SYSTEM4.BIN",
|
||||
[
|
||||
(0x55, [L(1), I(777)]),
|
||||
(0x139, [I(0), L(0), L(1)]),
|
||||
(0x55, [G(0x100), L(0)]),
|
||||
(0x55, [G(0x101), L(1)]),
|
||||
Exit(),
|
||||
], []);
|
||||
Script initial = ScriptAssembler.Assemble(Table, "INITIAL",
|
||||
[
|
||||
(0x138, [I(0), I(123)]),
|
||||
(0x9, Array.Empty<Operand>()),
|
||||
], []);
|
||||
var provider = new MapProvider(new Dictionary<long, Script> { [0] = reloadedSystem4 });
|
||||
var host = new RecordingHost();
|
||||
var vm = new VirtualMachine(initial, Table, host, provider: provider);
|
||||
|
||||
vm.Run();
|
||||
|
||||
Assert.Equal(0, vm.Globals[0x100]);
|
||||
Assert.Equal(777, vm.Globals[0x101]);
|
||||
Assert.Equal(1, host.SceneContextResets);
|
||||
}
|
||||
}
|
||||
@@ -95,9 +95,12 @@ public sealed class VirtualMachine
|
||||
// returns the same dword through the VM's signed integer-cell representation.
|
||||
private uint _systemMenuShowDelayMilliseconds;
|
||||
private readonly Dictionary<string, int> _valueSwitchTargets = new(StringComparer.Ordinal);
|
||||
// Native EngineCtx owns 11 lazily allocated integer FIFOs at +0x55130. ATSEEK/MVSEEK use
|
||||
// slot zero as their packed-coordinate flood-fill worklist; op 0x132 replaces a slot.
|
||||
// Eleven safely isolated handler-addressable FIFO slots. Native physically owns ten at
|
||||
// +0x55130; its admitted id 10 aliases stack slot zero. ATSEEK/MVSEEK use FIFO slot zero.
|
||||
private readonly Queue<int>?[] _intQueues = new Queue<int>?[11];
|
||||
// Eleven safely isolated handler-addressable LIFO slots. Native constructs ten at +0x55158
|
||||
// on every scene reset; its admitted id 10 aliases numeric-glyph-style storage.
|
||||
private readonly Stack<int>[] _intStacks = CreateIntegerStacks();
|
||||
// Opcodes 0x06/0x08 load scripts into numbered EngineCtx frame slots and invoke them later.
|
||||
// Unlike ordinary call-script frames, native non-adjacent slots survive return with locals intact.
|
||||
private readonly Dictionary<int, PreloadedScriptSlot> _preloadedScriptSlots = new();
|
||||
@@ -766,6 +769,7 @@ public sealed class VirtualMachine
|
||||
Gfx.ResetSceneContext();
|
||||
_valueSwitchTargets.Clear();
|
||||
_preloadedScriptSlots.Clear();
|
||||
ResetIntegerStacks();
|
||||
lock (_interactiveLock)
|
||||
{
|
||||
_interactiveFrame = null;
|
||||
@@ -797,6 +801,20 @@ public sealed class VirtualMachine
|
||||
_host.ResetSceneContext();
|
||||
}
|
||||
|
||||
private static Stack<int>[] CreateIntegerStacks()
|
||||
{
|
||||
var stacks = new Stack<int>[11];
|
||||
for (int i = 0; i < stacks.Length; i++)
|
||||
stacks[i] = new Stack<int>(0x100);
|
||||
return stacks;
|
||||
}
|
||||
|
||||
private void ResetIntegerStacks()
|
||||
{
|
||||
for (int i = 0; i < _intStacks.Length; i++)
|
||||
_intStacks[i] = new Stack<int>(0x100);
|
||||
}
|
||||
|
||||
private void RestartBgm(long requestedTrackId, int startMode)
|
||||
{
|
||||
if (requestedTrackId != 0)
|
||||
@@ -1672,6 +1690,52 @@ public sealed class VirtualMachine
|
||||
}
|
||||
return pc + 1;
|
||||
}
|
||||
case "u0041F1C0":
|
||||
case "reset-int-stack": // 0x137 (stack_id)
|
||||
{
|
||||
int stackId = unchecked((int)Read(a[0]));
|
||||
if ((uint)stackId >= (uint)_intStacks.Length)
|
||||
{
|
||||
HaltReason ??= $"int-stack-id-out-of-range:{stackId}";
|
||||
return HALT;
|
||||
}
|
||||
_intStacks[stackId] = new Stack<int>(0x100);
|
||||
return pc + 1;
|
||||
}
|
||||
case "u0041F2B0":
|
||||
case "push-int-stack": // 0x138 (stack_id, value)
|
||||
{
|
||||
int stackId = unchecked((int)Read(a[0]));
|
||||
if ((uint)stackId >= (uint)_intStacks.Length)
|
||||
{
|
||||
HaltReason ??= $"int-stack-id-out-of-range:{stackId}";
|
||||
return HALT;
|
||||
}
|
||||
_intStacks[stackId].Push(unchecked((int)Read(a[1])));
|
||||
return pc + 1;
|
||||
}
|
||||
case "u0041F310":
|
||||
case "try-pop-int-stack": // 0x139 (stack_id, out_success, out_value)
|
||||
{
|
||||
int stackId = unchecked((int)Read(a[0]));
|
||||
if ((uint)stackId >= (uint)_intStacks.Length)
|
||||
{
|
||||
HaltReason ??= $"int-stack-id-out-of-range:{stackId}";
|
||||
return HALT;
|
||||
}
|
||||
if (_intStacks[stackId].TryPop(out int value))
|
||||
{
|
||||
Write(a[1], 1);
|
||||
Write(a[2], value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Native writes success=0 and leaks an internal EngineCtx pointer through
|
||||
// operand 3. Preserve the destination instead of exposing host garbage.
|
||||
Write(a[1], 0);
|
||||
}
|
||||
return pc + 1;
|
||||
}
|
||||
case "bit-set":
|
||||
{
|
||||
long bit = Read(a[1]);
|
||||
|
||||
Reference in New Issue
Block a user