Skip to content

Commit

Permalink
Fixed Add Tests and Accumulator Instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
jnosek committed Jul 30, 2014
1 parent 8bdbe7f commit 4747f04
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
8 changes: 8 additions & 0 deletions AGC/Registers/Accumulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/AGC.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
138 changes: 138 additions & 0 deletions Tests/AGC.Tests/Instructions/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}

0 comments on commit 4747f04

Please sign in to comment.