From 4747f04b089ee8f6dbaadaadd39efe96f9d64dc7 Mon Sep 17 00:00:00 2001 From: jnosek Date: Wed, 30 Jul 2014 12:26:11 -0400 Subject: [PATCH] Fixed Add Tests and Accumulator Instruction --- AGC/Registers/Accumulator.cs | 8 ++ Tests/AGC.Tests/BaseTest.cs | 17 ++++ Tests/AGC.Tests/Instructions/Add.cs | 138 ++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) diff --git a/AGC/Registers/Accumulator.cs b/AGC/Registers/Accumulator.cs index 65e0d57..be35afe 100644 --- a/AGC/Registers/Accumulator.cs +++ b/AGC/Registers/Accumulator.cs @@ -12,6 +12,14 @@ public void Add(ushort value) { var sum = Read() + value; + // if we have overflow, most likely from subtracting negative numbers + if((sum & 0x10000) > 0) + { + // we need to Single Precision correct the negative number by adding 1 and taking the lower 16 bits + sum = sum + 1; + sum = sum & 0xFFFF; + } + this.Write((ushort)sum); } } diff --git a/Tests/AGC.Tests/BaseTest.cs b/Tests/AGC.Tests/BaseTest.cs index 899325a..cf62441 100644 --- a/Tests/AGC.Tests/BaseTest.cs +++ b/Tests/AGC.Tests/BaseTest.cs @@ -19,5 +19,22 @@ public void BackGround() Memory = new MemoryMap(); CPU = new Processor(Memory); } + + protected ushort ToSP(short value) + { + // if this is negative, + // return the 14 lower bits of the 1's compliment of the positive value + // (this is just subtracting 1 from the value), + // and append a 1 at bit-15 + if(value < 0) + { + return (ushort)(0x4000 | (value - 1)); + } + // else return the positive value + else + return (ushort)value; + } + + protected const ushort NegativeZero = 0xFFFF; } } diff --git a/Tests/AGC.Tests/Instructions/Add.cs b/Tests/AGC.Tests/Instructions/Add.cs index 56a820d..a1966b2 100644 --- a/Tests/AGC.Tests/Instructions/Add.cs +++ b/Tests/AGC.Tests/Instructions/Add.cs @@ -30,5 +30,143 @@ public void AddTwoPositiveNumbers() // assert Assert.AreEqual(25, Memory[0x0]); } + + [TestMethod] + public void AddTwoNegativeNumbers() + { + // arrange + Memory[0x200] = ToSP(-10); + Memory[0x201] = ToSP(-15); + + // insert add instructions + Memory.LoadFixedRom(new ushort[] { + 0x06000 | 0x200, + 0x06000 | 0x201 + }); + + // act - run the additions + CPU.Execute(); + CPU.Execute(); + + // assert + Assert.AreEqual(ToSP(-25), Memory[0x0]); + } + + [TestMethod] + public void AddPostive1AndNegative1() + { + // arrange + Memory[0x200] = ToSP(-10); + Memory[0x201] = 15; + + // insert add instructions + Memory.LoadFixedRom(new ushort[] { + 0x06000 | 0x200, + 0x06000 | 0x201 + }); + + // act - run the additions + CPU.Execute(); + CPU.Execute(); + + // assert + Assert.AreEqual(5, Memory[0x0]); + } + + [TestMethod] + public void AddTwoNumbersThatEqualNegativeZero() + { + // arrange + Memory[0x200] = 0xC000; // most negative number 15 bit number + Memory[0x201] = ToSP(-1); + + // insert add instructions + Memory.LoadFixedRom(new ushort[] { + 0x06000 | 0x200, + 0x06000 | 0x201 + }); + + // act - run the additions + CPU.Execute(); + CPU.Execute(); + + //save acumulator value to a place in memory so we can view the overflow corrected value, for now + Memory[0x202] = Memory[0x0]; + + // assert + Assert.AreEqual(NegativeZero, Memory[0x202]); + } + + [TestMethod] + public void AddTwoNumbersThatEqualPositiveZero() + { + // arrange + Memory[0x200] = 0x3FFF; // most positive number 15 bit number + Memory[0x201] = 1; + + // insert add instructions + Memory.LoadFixedRom(new ushort[] { + 0x06000 | 0x200, + 0x06000 | 0x201 + }); + + // act - run the additions + CPU.Execute(); + CPU.Execute(); + + //save acumulator value to a place in memory so we can view the overflow corrected value, for now + Memory[0x202] = Memory[0x0]; + + // assert + Assert.AreEqual(0, Memory[0x202]); + } + + [TestMethod] + public void AddTwoNegativeNumbersThatCauseUnderflow() + { + // arrange + Memory[0x200] = 0xC000; // most negative number 15 bit number + Memory[0x201] = ToSP(-3); + + // insert add instructions + Memory.LoadFixedRom(new ushort[] { + 0x06000 | 0x200, + 0x06000 | 0x201 + }); + + // act - run the additions + CPU.Execute(); + CPU.Execute(); + + //save acumulator value to a place in memory so we can view the overflow corrected value, for now + Memory[0x202] = Memory[0x0]; + + // assert + Assert.AreEqual(ToSP(-2), Memory[0x202]); + } + + [TestMethod] + public void AddTwoPositiveNumbersThatCauseOverflow() + { + // arrange + Memory[0x200] = 0x3FFF; // most positive number 15 bit number + Memory[0x201] = 3; + + // insert add instructions + Memory.LoadFixedRom(new ushort[] { + 0x06000 | 0x200, + 0x06000 | 0x201 + }); + + // act - run the additions + CPU.Execute(); + CPU.Execute(); + + //save acumulator value to a place in memory so we can view the overflow corrected value, for now + Memory[0x202] = Memory[0x0]; + + // assert + Assert.AreEqual(2, Memory[0x202]); + } } }