Skip to content

Commit

Permalink
Added ShiftRight and Erasable Bank Register with unit tests
Browse files Browse the repository at this point in the history
Cleaned up and refactore some code
  • Loading branch information
jnosek committed Jul 6, 2017
1 parent 149b478 commit 5649543
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 22 deletions.
2 changes: 2 additions & 0 deletions AGC/AGC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="Core\DoublePrecision.cs" />
<Compile Include="Architecture\QuarterCodeInstructionList.cs" />
<Compile Include="Core\FixedMemory.cs" />
<Compile Include="Registers\ErasableBankRegister.cs" />
<Compile Include="Registers\FullRegister.cs" />
<Compile Include="Core\MemoryBankOffset.cs" />
<Compile Include="Core\OnesCompliment.cs" />
Expand Down Expand Up @@ -73,6 +74,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registers\Accumulator.cs" />
<Compile Include="Registers\ProgramCounter.cs" />
<Compile Include="Registers\ShiftRightRegister.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
4 changes: 3 additions & 1 deletion AGC/Core/DoublePrecision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace Apollo.Virtual.AGC.Core
{
// https://github.com/rburkey2005/virtualagc/blob/master/yaAGC/agc_engine.c
/// <summary>
/// https://github.com/rburkey2005/virtualagc/blob/master/yaAGC/agc_engine.c
/// </summary>
public class DoublePrecision
{
public SinglePrecision MostSignificantWord { get; protected set; }
Expand Down
8 changes: 0 additions & 8 deletions AGC/ExtraCodeInstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,5 @@ public ExtraCodeInstructionSet()
Add(new ExtraQuarterCode3());
Add(new BranchZeroOrMinusToFixed());
}

public new IInstruction this[ushort code]
{
get
{
return base[code];
}
}
}
}
8 changes: 0 additions & 8 deletions AGC/InstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,5 @@ public InstructionSet()
Add(new CountCompareAndSkip());
Add(new ClearAndSubtract());
}

public new IInstruction this[ushort code]
{
get
{
return base[code];
}
}
}
}
4 changes: 2 additions & 2 deletions AGC/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public Processor(MemoryMap memory)
A = memory.AddRegister<Accumulator>();
L = memory.AddRegister<FullRegister>(0x01);
Q = memory.AddRegister<FullRegister>(0x02);
EB = memory.GetWord(0x3);
EB = memory.AddRegister<ErasableBankRegister>();
FB = memory.GetWord(0x4);
Z = memory.AddRegister<ProgramCounter>();
BB = memory.GetWord(0x6);
Expand All @@ -249,7 +249,7 @@ public Processor(MemoryMap memory)

// editing registers
CYR = memory.AddRegister<CycleRightRegister>();
SR = memory.GetWord(0x11);
SR = memory.AddRegister<ShiftRightRegister>();
CYL = memory.GetWord(0x12);
EDOP = memory.GetWord(0x13);

Expand Down
29 changes: 29 additions & 0 deletions AGC/Registers/ErasableBankRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Apollo.Virtual.AGC.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Registers
{
class ErasableBankRegister : MemoryAddress, IWord
{
public ErasableBankRegister(MemoryBank bank) :
base(0x03, bank)
{
}

public ushort Read()
{
var v = new OnesCompliment(Get());
v.SignExtend();
return v;
}

public void Write(ushort value)
{
// can only set the 3 bits for the erasable memory bank selection
Set(value & 0x0700);
}
}
}
38 changes: 38 additions & 0 deletions AGC/Registers/ShiftRightRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Apollo.Virtual.AGC.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Registers
{
class ShiftRightRegister : MemoryAddress, IWord
{
public ShiftRightRegister(MemoryBank bank)
: base(0x11, bank)
{
}

public ushort Read()
{
var v = new OnesCompliment(Get());
v.SignExtend();
return v;
}

public void Write(ushort value)
{
// first overflow correct the value
var v = new OnesCompliment(value);
v.OverflowCorrect();

value = v;

// preserve MSB and OR with shifted bits
var bits = (value & 0x4000) | (value >> 1);

// write the shifted value into memory
Set(bits);
}
}
}
2 changes: 2 additions & 0 deletions Tests/AGC.Tests/AGC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<Compile Include="Instructions\DoubleAddToStorage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registers\CycleRightRegister.cs" />
<Compile Include="Registers\ErasableBankRegister.cs" />
<Compile Include="Registers\ShiftRightRegister.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AGC\AGC.csproj">
Expand Down
6 changes: 3 additions & 3 deletions Tests/AGC.Tests/Registers/CycleRightRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AGC.Tests.Registers
public class CycleRightRegister : BaseTest
{
[TestMethod]
public void CycleRightOneWrapAround()
public void CycleRight_OneWrapAround()
{
// arrange
Memory[0x200] = 51;
Expand All @@ -27,11 +27,11 @@ public void CycleRightOneWrapAround()
CPU.Execute();

// assert
Assert.AreEqual(0XC019, Memory[0x10]);
Assert.AreEqual(0xC019, Memory[0x10]);
}

[TestMethod]
public void CycleRightZeroWrapAround()
public void CycleRight_ZeroWrapAround()
{
// arrange
Memory[0x200] = 50;
Expand Down
48 changes: 48 additions & 0 deletions Tests/AGC.Tests/Registers/ErasableBankRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AGC.Tests.Registers
{
[TestClass]
public class ErasableBankRegister : BaseTest
{
[TestMethod]
public void ErasableBank_SetWithinMask()
{
// arrange
Memory[0x200] = 0x0200;

// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200, // add to the accumulator
0x05800 | 0x03 // transfer to storage, the EB register
});

// act - run the instructions
CPU.Execute();
CPU.Execute();

// assert
Assert.AreEqual(0x0200, Memory[0x3]);
}

[TestMethod]
public void ErasableBank_SetOutsideMask()
{
// arrange
Memory[0x200] = 0x0002;

// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200, // add to the accumulator
0x05800 | 0x03 // transfer to storage, the EB register
});

// act - run the instructions
CPU.Execute();
CPU.Execute();

// assert
Assert.AreEqual(0x0, Memory[0x3]);
}
}
}
75 changes: 75 additions & 0 deletions Tests/AGC.Tests/Registers/ShiftRightRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Apollo.Virtual.AGC.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGC.Tests.Registers
{
[TestClass]
public class ShiftRightRegister : BaseTest
{
[TestMethod]
public void ShiftRight_Default()
{
// arrange
Memory[0x200] = 0x202;

// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200, // add to the accumulator
0x05800 | 0x011 // transfer to storage, the SR register
});

// act - run the instructions
CPU.Execute();
CPU.Execute();

// assert
Assert.AreEqual(0x0101, Memory[0x11]);
}

[TestMethod]
public void ShiftRight_ClearLSB()
{
// arrange
Memory[0x200] = 0x201;

// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200, // add to the accumulator
0x05800 | 0x011 // transfer to storage, the SR register
});

// act - run the instructions
CPU.Execute();
CPU.Execute();

// assert
Assert.AreEqual(0x0100, Memory[0x11]);
}

[TestMethod]
public void ShiftRight_DuplicateMSB()
{
// arrange
Memory[0x200] = 0xC000; // sign extended 0x4000 value

// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200, // add to the accumulator
0x05800 | 0x011 // transfer to storage, the SR register
});

// act - run the instructions
CPU.Execute();
CPU.Execute();

// assert
// sign extended 0x6000 value
Assert.AreEqual(0xE000, Memory[0x11]);
}
}
}

0 comments on commit 5649543

Please sign in to comment.