Skip to content

Commit

Permalink
Added Branching and Clear Instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
jnosek committed Aug 7, 2014
1 parent d2125d3 commit c51b2c4
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 25 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,8 @@
<Compile Include="Base\FixedMemory.cs" />
<Compile Include="Base\IRegister.cs" />
<Compile Include="Base\SinglePrecision.cs" />
<Compile Include="Instructions\BranchZeroOrMinusToFixed.cs" />
<Compile Include="Instructions\ClearAndAdd.cs" />
<Compile Include="Registers\FullRegister.cs" />
<Compile Include="Base\IWord.cs" />
<Compile Include="Base\MemoryAddress.cs" />
Expand Down
2 changes: 1 addition & 1 deletion AGC/Base/FixedMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public FixedMemory(MemoryAddress memory)

public void Write(ushort value)
{
throw new InvalidOperationException("Cannot write to a fixed memory location");
//throw new InvalidOperationException("Cannot write to a fixed memory location");
}

public ushort Address
Expand Down
1 change: 1 addition & 0 deletions AGC/ExtraCodeInstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public ExtraCodeInstructionSet(Processor cpu)

Add(new BranchZeroToFixed { CPU = CPU });
Add(new ExtraQuarterCode(CPU));
Add(new BranchZeroOrMinusToFixed { CPU = CPU });
}

public new IInstruction this[ushort code]
Expand Down
3 changes: 2 additions & 1 deletion AGC/InstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public InstructionSet(Processor cpu)
CPU = cpu;

Add(new TransferControl { CPU = CPU });
Add(new QuarterCode { CPU = CPU });
Add(new QuarterCode(CPU));
Add(new ClearAndAdd { CPU = CPU });
Add(new Add { CPU = CPU });
}

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

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// BZMF - EX 0110
///
/// Jumps to a fixed memory location if the accumulator is 0 or negative
/// </summary>
class BranchZeroOrMinusToFixed : IInstruction
{
public ushort Code
{
get { return 0x06; }
}

public Processor CPU { get; set; }

public void Execute(ushort K)
{
var value = CPU.A.Read();

// if +0 or negative, jump
if (value == 0 || (value & 0x8000) > 0)
CPU.Z.Write(K);
}
}
}
11 changes: 9 additions & 2 deletions AGC/Instructions/BranchZeroToFixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Apollo.Virtual.AGC.Instructions
/// <summary>
/// BZF - EX 0001
///
/// Jumps to a memory location in the fixed bank if the accumulator is 0
/// Jumps to a fixed memory location if the accumulator is 0
/// </summary>
class BranchZeroToFixed : IInstruction
{
Expand All @@ -22,7 +22,14 @@ public ushort Code

public void Execute(ushort K)
{
if (CPU.A.Read() == 0)
// if in overflow, no jump
if(CPU.A.IsOverflow)
return;

var value = CPU.A.Read();

// if +0 or -0, then jump
if (value == 0 || value == SinglePrecision.NegativeZero)
CPU.Z.Write(K);
}
}
Expand Down
34 changes: 34 additions & 0 deletions AGC/Instructions/ClearAndAdd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Apollo.Virtual.AGC.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// CA - 0011
///
/// Moves the contents of memory at location K into the accumulator
/// </summary>
class ClearAndAdd : IInstruction
{
public ushort Code
{
get { return 0x3; }
}

public Processor CPU { get; set; }

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

// set value in accumulator
CPU.A.Write(value);

// value in K is re-written
CPU.Memory[K] = value;
}
}
}
4 changes: 3 additions & 1 deletion AGC/Instructions/QuarterCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace Apollo.Virtual.AGC.Instructions
/// </summary>
class QuarterCode : InstructionList, IInstruction
{
public QuarterCode() : base(3)
public QuarterCode(Processor CPU) : base(3)
{
this.CPU = CPU;

Add(new AddToStorage { CPU = this.CPU });
}

Expand Down
11 changes: 11 additions & 0 deletions AGC/Registers/FullRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public ushort Read()
return memory.Read();
}

public bool IsOverflow
{
get
{
// look at bits 16 and 15 to see if they are different
var value = (ushort)(Read() & 0xC000);

return value == 0x8000 || value == 0x4000;
}
}

MemoryAddress IRegister.Memory
{
set { this.memory = value; }
Expand Down
4 changes: 4 additions & 0 deletions Tests/AGC.Tests/AGC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
<ItemGroup>
<Compile Include="BaseTest.cs" />
<Compile Include="Instructions\Add.cs" />
<Compile Include="Instructions\AddToStorage.cs" />
<Compile Include="Instructions\Augment.cs" />
<Compile Include="Instructions\BranchZeroOrMinusToFixed.cs" />
<Compile Include="Instructions\BranchZeroToFixed.cs" />
<Compile Include="Instructions\UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
60 changes: 42 additions & 18 deletions Tests/AGC.Tests/Instructions/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public void AddTwoPositiveNumbers()
Memory[0x200] = 10;
Memory[0x201] = 15;

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -39,13 +39,13 @@ public void AddTwoNegativeNumbers()
Memory[0x200] = SinglePrecision.To(-10);
Memory[0x201] = SinglePrecision.To(-15);

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -60,13 +60,13 @@ public void AddPostive1AndNegative1()
Memory[0x200] = SinglePrecision.To(-10);
Memory[0x201] = 15;

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -81,13 +81,13 @@ public void AddTwoNumbersThatEqualNegativeZero()
Memory[0x200] = 0xC000; // most negative number 15 bit number
Memory[0x201] = SinglePrecision.To(-1);

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -105,13 +105,13 @@ public void AddTwoNumbersThatEqualPositiveZero()
Memory[0x200] = 0x3FFF; // most positive number 15 bit number
Memory[0x201] = 1;

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -129,13 +129,13 @@ public void AddTwoNegativeNumbersThatCauseUnderflow()
Memory[0x200] = 0xC000; // most negative number 15 bit number
Memory[0x201] = SinglePrecision.To(-3);

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -153,13 +153,13 @@ public void AddTwoPositiveNumbersThatCauseOverflow()
Memory[0x200] = 0x3FFF; // most positive number 15 bit number
Memory[0x201] = 3;

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -177,13 +177,13 @@ public void AddNegativeZeroAndPositiveNumber()
Memory[0x200] = SinglePrecision.NegativeZero;
Memory[0x201] = 4;

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -201,13 +201,13 @@ public void AddNegativeZeroAndNegativeNumber()
Memory[0x200] = SinglePrecision.NegativeZero;
Memory[0x201] = SinglePrecision.To(-4);

// insert add instructions
// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

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

Expand All @@ -217,5 +217,29 @@ public void AddNegativeZeroAndNegativeNumber()
// assert
Assert.AreEqual(SinglePrecision.To(-4), Memory[0x202]);
}

[TestMethod]
public void Add1AndNegative1EqualsNegative0()
{
// arrange
Memory[0x200] = 0x01;
Memory[0x201] = SinglePrecision.To(-1);

// insert instructions
Memory.LoadFixedRom(new ushort[] {
0x06000 | 0x200,
0x06000 | 0x201
});

// act - run the instructions
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(SinglePrecision.NegativeZero, Memory[0x202]);
}
}
}
Loading

0 comments on commit c51b2c4

Please sign in to comment.