Skip to content

Commit

Permalink
Added CS command
Browse files Browse the repository at this point in the history
The Clear And Subtract command
  • Loading branch information
jnosek committed Jul 4, 2015
1 parent a791f7c commit 3976b3e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 6 deletions.
1 change: 1 addition & 0 deletions AGC/AGC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="Base\OnesCompliment.cs" />
<Compile Include="Instructions\BranchZeroOrMinusToFixed.cs" />
<Compile Include="Instructions\ClearAndAdd.cs" />
<Compile Include="Instructions\ClearAndSubtract.cs" />
<Compile Include="Instructions\CountCompareAndSkip.cs" />
<Compile Include="Instructions\QuarterCode5.cs" />
<Compile Include="Instructions\TransferToStorage.cs" />
Expand Down
3 changes: 2 additions & 1 deletion AGC/Base/OnesCompliment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static ushort Add(this ushort left, ushort right)
// 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
// we need to ones compliment correct the negative number by adding 1 and taking the lower 16 bits
// this process is called "end around carry"
sum = sum + 1;
sum = sum & 0xFFFF;
}
Expand Down
1 change: 1 addition & 0 deletions AGC/InstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public InstructionSet(Processor cpu)
Add(new Add());
Add(new ClearAndAdd());
Add(new CountCompareAndSkip());
Add(new ClearAndSubtract());
}

public new IInstruction this[ushort code]
Expand Down
40 changes: 40 additions & 0 deletions AGC/Instructions/ClearAndSubtract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Apollo.Virtual.AGC.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// CS - 0100
///
/// The "Clear and Subtract" instruction moves the 1's-complement (i.e., the negative) of a memory location into the accumulator.
///
/// Also:
/// COM - 0100 0000 0000 0000
/// The "Complement the Contents of A" bitwise complements the accumulator
/// Assembles as CS A
/// </summary>
class ClearAndSubtract : IInstruction
{
public ushort Code
{
get { return 0x4; }
}

public Processor CPU { get; set; }

public void Execute(ushort K)
{
var value = CPU.Memory[K];

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

// if not the A register, re-write value to K
if (K != CPU.A.Address)
CPU.Memory[K] = value;
}
}
}
6 changes: 3 additions & 3 deletions AGC/Instructions/CountCompareAndSkip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public void Execute(ushort K)

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

// if negative, XOR 1's to get ABS
// if negative, NOT 1's to get ABS
var isNegative = (value & 0x8000) > 0;
var abs = isNegative ? value ^ 0xFFFF : value;
var abs = (ushort)(isNegative ? ~value : value);

if (abs > 1)
CPU.A.Write((ushort)(abs - 1));
CPU.A.Write(abs.Add(OnesCompliment.NegativeOne));
else
CPU.A.Write(0);

Expand Down
4 changes: 2 additions & 2 deletions AGC/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Processor
/// The lower product after MP instructions
/// also known as L
/// </summary>
internal IWord LP;
internal IWord L;

/// <summary>
/// Remainder from the DV instruction,
Expand Down Expand Up @@ -229,7 +229,7 @@ public Processor(MemoryMap memory)

// main registers?
A = memory.AddRegister<Accumulator>();
LP = memory.AddRegister<FullRegister>(0x01);
L = memory.AddRegister<FullRegister>(0x01);
Q = memory.AddRegister<FullRegister>(0x02);
EB = memory.GetWord(0x3);
FB = memory.GetWord(0x4);
Expand Down
1 change: 1 addition & 0 deletions Tests/AGC.Tests/AGC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Instructions\BranchZeroOrMinusToFixed.cs" />
<Compile Include="Instructions\BranchZeroToFixed.cs" />
<Compile Include="Instructions\ClearAndAdd.cs" />
<Compile Include="Instructions\ClearAndSubtract.cs" />
<Compile Include="Instructions\CountCompareAndSkip.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registers\CycleRightRegister.cs" />
Expand Down
48 changes: 48 additions & 0 deletions Tests/AGC.Tests/Instructions/ClearAndSubtract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGC.Tests.Instructions
{
[TestClass]
public class ClearAndSubtract : BaseTest
{
[TestMethod]
public void ClearAndSubtractAccumulator()
{
// arrange
Memory[0x0] = 0x0101;

Memory.LoadFixedRom(new ushort[] {
0x4000
});

// act
CPU.Execute();

// assert

Assert.AreEqual(0xFEFE, Memory[0x0]);
}

[TestMethod]
public void ClearAndSubtractMemory()
{
// arrange
Memory[0x200] = 0xF0F0;

Memory.LoadFixedRom(new ushort[] {
0x4000 | 0x0200
});

// act
CPU.Execute();

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

0 comments on commit 3976b3e

Please sign in to comment.