Skip to content

Commit

Permalink
Remaining OnesCompliment changes, needs fixes to for broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jnosek committed Jul 14, 2020
1 parent 9da0a8d commit 6c4aa2e
Show file tree
Hide file tree
Showing 32 changed files with 241 additions and 242 deletions.
4 changes: 2 additions & 2 deletions AGC.dev/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ static void Main(string[] args)
var computer = new Computer();
var memory = computer.Memory;

memory[0x800] = (0x6301).ToOnesCompliment();
memory[0x801] = (0x02).ToOnesCompliment();
memory[0x800] = 0x6301;
memory[0x801] = 0x02;
computer.Start();

Console.WriteLine("A: {0}", memory[0x0]);
Expand Down
2 changes: 1 addition & 1 deletion AGC/Instructions/BranchZeroToFixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void IInstruction.Execute(ushort K)

// if +0 or -0, then jump
if (value == 0 || value == OnesCompliment.NegativeZero)
cpu.Z.Write(new OnesCompliment(K));
cpu.Z.Write(K);
}
}
}
2 changes: 1 addition & 1 deletion AGC/Instructions/ClearAndSubtract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void IInstruction.Execute(ushort K)
var value = cpu.Memory[K];

// write the compliment to the accumulator
cpu.A.Write(~value);
cpu.A.Write((ushort)~value);

// if not the A register, re-write value to K
if (K != cpu.A.Address)
Expand Down
12 changes: 7 additions & 5 deletions AGC/Instructions/CountCompareAndSkip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Apollo.Virtual.AGC.Instructions
using Apollo.Virtual.AGC.Math;

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// CCS - 0001
Expand Down Expand Up @@ -28,7 +30,7 @@ void IInstruction.Execute(ushort K)

// 1) compute the Diminished ABSolute value found at K and set in A

var dabs = value.GetDiminishedAbsoluteValue();
var dabs = OnesCompliment.GetDiminishedAbsoluteValue(value);


// 2) K is edited, then write dabs to A
Expand All @@ -44,17 +46,17 @@ void IInstruction.Execute(ushort K)
// if greater than +0 we do nothing, continue to next instruction as usual

// if == +0 increment by 1
if (value.IsPositiveZero)
if (OnesCompliment.IsPositiveZero(value))
cpu.Z.Increment();
// if == -0 increment by 3
else if(value.IsNegativeZero)
else if(OnesCompliment.IsNegativeZero(value))
{
cpu.Z.Increment();
cpu.Z.Increment();
cpu.Z.Increment();
}
// if < 0 increment by 2
else if (value.IsNegative)
else if (OnesCompliment.IsNegative(value))
{
cpu.Z.Increment();
cpu.Z.Increment();
Expand Down
6 changes: 3 additions & 3 deletions AGC/Instructions/Diminish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ void IInstruction.Execute(ushort K)
var value = cpu.Memory[K];

// if negative
if(value.IsNegative)
if (OnesCompliment.IsNegative(value))
{
cpu.Memory[K] = value + OnesCompliment.PositiveOne;
cpu.Memory[K] = OnesCompliment.AddPositiveOne(value);
}
// is positive
else
{
cpu.Memory[K] = value + OnesCompliment.NegativeOne;
cpu.Memory[K] = OnesCompliment.AddNegativeOne(value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions AGC/Instructions/DoubleAddToStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ void IInstruction.Execute(ushort K0)
cpu.L.Write(OnesCompliment.PositiveZero);

// A set based upon overflow
if (sum.MostSignificantWord.IsPositiveOverflow)
if (OnesCompliment.IsPositiveOverflow(sum.MostSignificantWord))
cpu.A.Write(OnesCompliment.PositiveOne);
else if (sum.MostSignificantWord.IsNegativeOverflow)
else if (OnesCompliment.IsNegativeOverflow(sum.MostSignificantWord))
cpu.A.Write(OnesCompliment.NegativeOne);
else
cpu.A.Write(OnesCompliment.PositiveZero);
Expand Down
4 changes: 2 additions & 2 deletions AGC/Instructions/DoubleClearAndSubtract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ void IInstruction.Execute(ushort K0)
var K1 = (ushort)(K0 - 1);

// compliment least significant word into L
cpu.L.Write(~cpu.Memory[K0]);
cpu.L.Write((ushort)~cpu.Memory[K0]);

// rewrite K0
cpu.Memory[K0] = cpu.Memory[K0];

// compliment most significant word into A
cpu.A.Write(~cpu.Memory[K1]);
cpu.A.Write((ushort)~cpu.Memory[K1]);

// rewrite K1
cpu.Memory[K1] = cpu.Memory[K1];
Expand Down
2 changes: 1 addition & 1 deletion AGC/Instructions/TransferControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void IInstruction.Execute(ushort K)
cpu.Q.Write(cpu.Z.Read());

// set control to K
cpu.Z.Write(new OnesCompliment(K));
cpu.Z.Write(K);

// clear the extra code flag
cpu.ExtraCodeFlag = false;
Expand Down
8 changes: 4 additions & 4 deletions AGC/Math/DoublePrecision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public DoublePrecision(ushort most, ushort least)
public static DoublePrecision operator +(DoublePrecision left, DoublePrecision right)
{
// single preceision add the least significant word and most significant word
var lsw = left.LeastSignificantWord + right.LeastSignificantWord;
var msw = left.MostSignificantWord + right.MostSignificantWord;
ushort lsw = (ushort)(left.LeastSignificantWord + right.LeastSignificantWord);
ushort msw = (ushort)(left.MostSignificantWord + right.MostSignificantWord);

// check for overflow and adjust
if (lsw.IsPositiveOverflow)
if (OnesCompliment.IsPositiveOverflow(lsw))
msw += OnesCompliment.PositiveOne;
else if (lsw.IsNegativeOverflow)
else if (OnesCompliment.IsNegativeOverflow(lsw))
msw += OnesCompliment.NegativeOne;

// in case lsw is in overflow, correct it and extend it back to 16 bits
Expand Down
18 changes: 16 additions & 2 deletions AGC/Math/OnesCompliment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Xml.Schema;

namespace Apollo.Virtual.AGC.Math
{
Expand Down Expand Up @@ -59,8 +60,21 @@ public static ushort GetDiminishedAbsoluteValue(ushort value)
return PositiveZero;
}

public static ushort Convert(short value) => throw new NotImplementedException();
/// <summary>
/// Converstion helper, mainly to handle negative values
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static ushort Convert(ushort value) => value < 0 ? (ushort)~value : value;

public static ushort Convert(int value) => throw new NotImplementedException();
/// <summary>
/// Converstion helper, mainly to handle negative values
/// </summary>
/// <remarks>
/// Make the negative int positive, and then compliment the binary value
/// </remarks>
/// <param name="value"></param>
/// <returns></returns>
public static ushort Convert(int value) => (ushort)(value < 0 ? ~(-value): value);
}
}
1 change: 0 additions & 1 deletion AGC/Memory/FixedMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public FixedMemory(ushort address, MemoryBank bank)
/// <param name="value"></param>
public override void Write(ushort value)
{
Debug.Fail("Cannot write to a fixed memory location");
}
}
}
2 changes: 2 additions & 0 deletions AGC/Memory/MemoryWord.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Apollo.Virtual.AGC.Math;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Apollo.Virtual.AGC.Memory
{
Expand Down
6 changes: 3 additions & 3 deletions AGC/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,19 @@ public Processor(IMemoryBus memory)
INLINK = memory.MapRegister<ErasableMemory>(0x25);

// prime Z to start at the boot interrupt
Z.Write(new OnesCompliment(0x800));
Z.Write(0x800);
}

public void Execute()
{
// get address of instruction to run
ushort address = Z.Read().NativeValue;
ushort address = Z.Read();

// update Z
Z.Increment();

// get instruction
ushort instruction = Memory[address].NativeValue;
ushort instruction = Memory[address];

// we only care about 15-bit instructions
instruction = MaskInstruction(instruction);
Expand Down
18 changes: 0 additions & 18 deletions Tests/AGC.Tests/CustomAssert.cs

This file was deleted.

46 changes: 23 additions & 23 deletions Tests/AGC.Tests/Instructions/AddTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,49 @@ public class AddTests : BaseTest
public void Add_TwoPositiveNumbers()
{
// arrange
Memory[0x200] = (10).ToOnesCompliment();
Memory[0x201] = (15).ToOnesCompliment();
Memory[0x200] = 10;
Memory[0x201] = 15;

// act - run the instructions
RunProgram(baseProgram);

// assert
CustomAssert.AreEqual(25, Memory[0x0]);
Assert.AreEqual(25, Memory[0x0]);
}

[TestMethod]
public void Add_TwoNegativeNumbers()
{
// arrange
Memory[0x200] = (~10).ToOnesCompliment(); // -10
Memory[0x201] = (~15).ToOnesCompliment(); // -15
Memory[0x200] = OnesCompliment.Convert(-10);
Memory[0x201] = OnesCompliment.Convert(-15);

// act - run the instructions
RunProgram(baseProgram);

// assert
CustomAssert.AreEqual(~25, Memory[0x0]); // -25
Assert.AreEqual(OnesCompliment.Convert(-25), Memory[0x0]);
}

[TestMethod]
public void Add_Postive1AndNegative1()
{
// arrange
Memory[0x200] = (~10).ToOnesCompliment(); // -10
Memory[0x201] = (15).ToOnesCompliment();
Memory[0x200] = OnesCompliment.Convert(-10);
Memory[0x201] = 15;

// act - run the instructions
RunProgram(baseProgram);

// assert
CustomAssert.AreEqual(5, Memory[0x0]);
Assert.AreEqual(5, Memory[0x0]);
}

[TestMethod]
public void Add_TwoNumbersThatEqualNegativeZero()
{
// arrange
Memory[0x200] = (0xC000).ToOnesCompliment(); // most negative number 15 bit number
Memory[0x200] = 0xC000; // most negative number 15 bit number
Memory[0x201] = OnesCompliment.NegativeOne;

// act - run the instructions
Expand All @@ -75,7 +75,7 @@ public void Add_TwoNumbersThatEqualNegativeZero()
public void Add_TwoNumbersThatEqualPositiveZero()
{
// arrange
Memory[0x200] = (0x3FFF).ToOnesCompliment(); // most positive number 15 bit number
Memory[0x200] = 0x3FFF; // most positive number 15 bit number
Memory[0x201] = OnesCompliment.PositiveOne;

// act - run the instructions
Expand All @@ -92,8 +92,8 @@ public void Add_TwoNumbersThatEqualPositiveZero()
public void Add_TwoNegativeNumbersThatCauseUnderflow()
{
// arrange
Memory[0x200] = (0xC000).ToOnesCompliment(); // most negative number 15 bit number
Memory[0x201] = (~3).ToOnesCompliment(); // -3
Memory[0x200] = 0xC000; // most negative number 15 bit number
Memory[0x201] = OnesCompliment.Convert(-3);

// act - run the instructions
RunProgram(baseProgram);
Expand All @@ -102,15 +102,15 @@ public void Add_TwoNegativeNumbersThatCauseUnderflow()
Memory[0x202] = Memory[0x0];

// assert
CustomAssert.AreEqual(~2, Memory[0x202]); // -2
Assert.AreEqual(OnesCompliment.Convert(-2), Memory[0x202]);
}

[TestMethod]
public void Add_TwoPositiveNumbersThatCauseOverflow()
{
// arrange
Memory[0x200] = (0x3FFF).ToOnesCompliment(); // most positive number 15 bit number
Memory[0x201] = (3).ToOnesCompliment();
Memory[0x200] = 0x3FFF; // most positive number 15 bit number
Memory[0x201] = 3;

// act - run the instructions
RunProgram(baseProgram);
Expand All @@ -119,15 +119,15 @@ public void Add_TwoPositiveNumbersThatCauseOverflow()
Memory[0x202] = Memory[0x0];

// assert
CustomAssert.AreEqual(2, Memory[0x202]);
Assert.AreEqual(2, Memory[0x202]);
}

[TestMethod]
public void Add_NegativeZeroAndPositiveNumber()
{
// arrange
Memory[0x200] = OnesCompliment.NegativeZero;
Memory[0x201] = (4).ToOnesCompliment();
Memory[0x201] = 4;

// act - run the instructions
RunProgram(baseProgram);
Expand All @@ -136,15 +136,15 @@ public void Add_NegativeZeroAndPositiveNumber()
Memory[0x202] = Memory[0x0];

// assert
CustomAssert.AreEqual(4, Memory[0x202]);
Assert.AreEqual(4, Memory[0x202]);
}

[TestMethod]
public void Add_NegativeZeroAndNegativeNumber()
{
// arrange
Memory[0x200] = OnesCompliment.NegativeZero;
Memory[0x201] = (~4).ToOnesCompliment();
Memory[0x201] = OnesCompliment.Convert(-4);

// act - run the instructions
RunProgram(baseProgram);
Expand All @@ -153,7 +153,7 @@ public void Add_NegativeZeroAndNegativeNumber()
Memory[0x202] = Memory[0x0];

// assert
CustomAssert.AreEqual(~4, Memory[0x202]); // -4
Assert.AreEqual(OnesCompliment.Convert(-4), Memory[0x202]);
}

[TestMethod]
Expand All @@ -177,13 +177,13 @@ public void Add_1AndNegative1EqualsNegative0()
public void Add_Double()
{
// arrange
Memory[0x000] = new OnesCompliment(2);
Memory[0x000] = 2;

// act - run the instructions
RunInstruction(Double.Instruction);

// assert
CustomAssert.AreEqual(4, Memory[0x000]);
Assert.AreEqual(4, Memory[0x000]);
}
}
}
Loading

0 comments on commit 6c4aa2e

Please sign in to comment.