summaryrefslogtreecommitdiff
path: root/Q1.tests/Core/ChipTests.cs
diff options
context:
space:
mode:
authorMelodi <62381002+Melodi17@users.noreply.github.com>2026-03-18 23:08:58 +1000
committerMelodi <62381002+Melodi17@users.noreply.github.com>2026-03-18 23:08:58 +1000
commit7d20ff2a8e89e646fa79be69c8d07a512081daba (patch)
tree67433c74d55f778065363010a593c44a0f65296b /Q1.tests/Core/ChipTests.cs
parent0bb62fc65526a0981cd328dea1b09aba8ce0a576 (diff)
test: Add tests for all control instructions and base CPU implementation
Diffstat (limited to 'Q1.tests/Core/ChipTests.cs')
-rw-r--r--Q1.tests/Core/ChipTests.cs87
1 files changed, 87 insertions, 0 deletions
diff --git a/Q1.tests/Core/ChipTests.cs b/Q1.tests/Core/ChipTests.cs
new file mode 100644
index 0000000..2b0d20f
--- /dev/null
+++ b/Q1.tests/Core/ChipTests.cs
@@ -0,0 +1,87 @@
+namespace Q1.tests.Core;
+
+using core.Arch;
+using core.Arch.Constants;
+
+[TestFixture]
+public class ChipTests : ChipTestBase
+{
+ [TestCase(ChipRegisters.AX)]
+ [TestCase(ChipRegisters.DX)]
+ [TestCase(ChipRegisters.LX)]
+ public void Register_ShouldRoundTrip(u8 register)
+ {
+ var chip = this.CreateChip();
+
+ u16 value = (u16) (register * 0x1111); // unique value for each register
+ chip.SetRegister(register, value);
+ u16 readValue = chip.GetRegister(register);
+
+ Assert.That(readValue, Is.EqualTo(value), $"Register {register:X2} should round trip");
+ }
+
+ [Test]
+ public void PushPop_ShouldRoundTrip()
+ {
+ var chip = this.CreateChip();
+
+ u16 value = 0xABCD;
+ chip.Push(value);
+ u16 poppedValue = chip.Pop();
+
+ Assert.That(poppedValue, Is.EqualTo(value), "Popped value should match pushed value");
+ }
+
+ [Test]
+ public void StackOverflow_ShouldThrow()
+ {
+ var chip = this.CreateChip();
+
+ // Fill the stack to its limit
+ for (int i = 0; i < ChipLayout.STACK_COUNT; i++)
+ chip.Push((u16) i);
+
+ // Next push should cause overflow
+ Assert.Throws<InvalidOperationException>(() => chip.Push(0xFFFF), "Expected stack overflow to throw");
+ }
+
+ [Test]
+ public void StackUnderflow_ShouldThrow()
+ {
+ var chip = this.CreateChip();
+
+ // Attempt to pop from an empty stack
+ Assert.Throws<InvalidOperationException>(() => chip.Pop(), "Expected stack underflow to throw");
+ }
+
+ [Test]
+ public void InvalidRegisterAccess_ShouldThrow()
+ {
+ var chip = this.CreateChip();
+
+ // Attempt to access an invalid register index
+ Assert.Throws<InvalidOperationException>(() => chip.GetRegister(0xFF), "Expected invalid register access to throw");
+ Assert.Throws<InvalidOperationException>(() => chip.SetRegister(0xFF, 0x1234), "Expected invalid register access to throw");
+ }
+
+ [Test]
+ public void InterruptHandling_ShouldJumpToInterruptVector()
+ {
+ var chip = this.CreateChip();
+
+ var originalPc = chip.Pc;
+
+ // Set up an interrupt vector at 0xFFFE
+ u16 interruptHandlerAddress = 0x1234;
+ chip.RegisterInterruptHandler(8, interruptHandlerAddress); // Register handler for interrupt 8
+
+ // Trigger the interrupt
+ chip.Interrupt(8);
+
+ Assert.Multiple(() =>
+ {
+ Assert.That(chip.Pc, Is.EqualTo(interruptHandlerAddress), "PC should jump to interrupt handler address");
+ Assert.That(chip.Pop(), Is.EqualTo(originalPc), "PC should be pushed onto the stack during interrupt");
+ });
+ }
+} \ No newline at end of file