Skip to content

Commit

Permalink
Major refactoring for 16 bit registers
Browse files Browse the repository at this point in the history
Allows overflow correction, sign extending, and overlapping custom
register actions with the memory map
  • Loading branch information
jnosek committed Jul 29, 2014
1 parent c4a2b76 commit 3379a0a
Show file tree
Hide file tree
Showing 21 changed files with 507 additions and 131 deletions.
4 changes: 2 additions & 2 deletions AGC.dev/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ static void Main(string[] args)
{
var computer = new Computer();

computer.Memory.GetAddress(0x300).Write(0x6301);
computer.Memory.GetAddress(0x301).Write(0x02);
computer.Memory.GetWord(0x300).Write(0x6301);
computer.Memory.GetWord(0x301).Write(0x02);
computer.CPU.Z.Write(0x300);
computer.CPU.Execute();

Expand Down
14 changes: 12 additions & 2 deletions AGC/AGC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@
<!-- A reference to the entire .NET Framework is automatically included -->
</ItemGroup>
<ItemGroup>
<Compile Include="Base\MemoryBank.cs" />
<Compile Include="Base\FixedMemory.cs" />
<Compile Include="Base\IRegister.cs" />
<Compile Include="Base\Word.cs" />
<Compile Include="Registers\FullRegister.cs" />
<Compile Include="Base\IWord.cs" />
<Compile Include="Base\MemoryAddress.cs" />
<Compile Include="Base\Register.cs" />
<Compile Include="Base\MemoryBank.cs" />
<Compile Include="Base\ErasableMemory.cs" />
<Compile Include="Base\Computer.cs" />
<Compile Include="ExtraCodeInstructionSet.cs" />
<Compile Include="InstructionSet.cs" />
Expand All @@ -48,11 +53,16 @@
<Compile Include="Base\IInstruction.cs" />
<Compile Include="Base\InstructionList.cs" />
<Compile Include="Instructions\Augment.cs" />
<Compile Include="Instructions\BranchZeroToFixed.cs" />
<Compile Include="Instructions\ExtraQuarterCode.cs" />
<Compile Include="Instructions\QuarterCode.cs" />
<Compile Include="Instructions\TransferControl.cs" />
<Compile Include="MemoryMap.cs" />
<Compile Include="Processor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registers\Accumulator.cs" />
<Compile Include="Registers\ProgramCounter.cs" />
<Compile Include="Registers\Register.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
56 changes: 56 additions & 0 deletions AGC/Base/ErasableMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Base
{
class ErasableMemory : IWord
{
protected MemoryAddress memory;

public ErasableMemory()
{
}

public ErasableMemory(MemoryAddress memory)
{
this.memory = memory;
}

public void Write(IWord word)
{
// if 16-bit, apply overflow correction and write 15-bit value
if (word.Is16Bit)
{
uint value = word.Read();

// get lower 14 bits
uint lowerBits = value & 0x3FFF;

// move 16-th bit, into 15th position, isolate it, and set it in above value;
value = (value >> 1 & 0x4000) | lowerBits;

memory.Write((ushort)value);
}
else
memory.Write(word.Read());
}

public bool Is16Bit
{
get { return false; }
}


public ushort Address
{
get { return memory.Address; }
}

public ushort Read()
{
return memory.Read();
}
}
}
37 changes: 37 additions & 0 deletions AGC/Base/FixedMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Base
{
class FixedMemory : IWord
{
private MemoryAddress memory;

public FixedMemory(MemoryAddress memory)
{
this.memory = memory;
}

public bool Is16Bit
{
get { return false; }
}

public void Write(IWord word)
{
throw new InvalidOperationException("Cannot write to a fixed memory location");
}

public ushort Address
{
get { return memory.Address; }
}

public ushort Read()
{
return memory.Read();
}
}
}
12 changes: 12 additions & 0 deletions AGC/Base/IRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Base
{
interface IRegister
{
MemoryAddress Memory { set; }
}
}
16 changes: 16 additions & 0 deletions AGC/Base/IWord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Base
{
public interface IWord
{
bool Is16Bit { get; }
ushort Address { get; }

ushort Read();
void Write(IWord word);
}
}
13 changes: 8 additions & 5 deletions AGC/Base/MemoryAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ namespace Apollo.Virtual.AGC.Base
public class MemoryAddress
{
private MemoryBank m;
private uint address;
private ushort index;

public MemoryAddress(MemoryBank memory, uint address)
public ushort Address { get; private set; }

public MemoryAddress(MemoryBank memory, ushort address, ushort index)
{
this.m = memory;
this.address = address;
this.index = index;
this.Address = address;
}

public ushort Read()
{
return m[address];
return m[index];
}

public void Write(ushort value)
{
m[address] = value;
m[index] = value;
}
}
}
38 changes: 0 additions & 38 deletions AGC/Base/Register.cs

This file was deleted.

34 changes: 34 additions & 0 deletions AGC/Base/Word.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Base
{
class Word : IWord
{
public Word()
{
}

public Word(ushort value)
{
this.Value = value;
this.Is16Bit = true;
}

public ushort Value { get; set; }
public bool Is16Bit { get; set; }
public ushort Address { get;set; }

public ushort Read()
{
return Value;
}

public void Write(IWord word)
{
Value = word.Read();
}
}
}
3 changes: 2 additions & 1 deletion AGC/ExtraCodeInstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public ExtraCodeInstructionSet(Processor cpu)
{
CPU = cpu;

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

public new IInstruction this[ushort code]
Expand Down
2 changes: 1 addition & 1 deletion AGC/Instructions/AddToStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Execute(ushort K)
var value = CPU.Memory[K];
CPU.A.Add(value);

CPU.Memory[K] = CPU.A.Read();
CPU.Memory[K].Write(CPU.A);
}
}
}
13 changes: 11 additions & 2 deletions AGC/Instructions/Augment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// AUG - EX 0010 10
/// </summary>
class Augment: IInstruction
{
public ushort Code
{
get { throw new NotImplementedException(); }
get { return 0x02; }
}

public Processor CPU { get; set; }

public void Execute(ushort K)
{
throw new NotImplementedException();
var word =
// if positive, add 1
(K & 0x4000) == 0 ? new Word((ushort)(CPU.Memory[K].Read() + 1)) :
// else negative, subtract 1
new Word((ushort)(CPU.Memory[K].Read() - 1));

CPU.Memory[K].Write(word);
}
}
}
29 changes: 29 additions & 0 deletions AGC/Instructions/BranchZeroToFixed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Apollo.Virtual.AGC.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// BZF - EX 0001
///
/// Jumps to a memory location in the fixed bank if the accumulator is 0
/// </summary>
class BranchZeroToFixed : IInstruction
{
public ushort Code
{
get { return 0x01; }
}

public Processor CPU { get; set; }

public void Execute(ushort K)
{
if (CPU.A.Read() == 0)
CPU.Z.Write(K);
}
}
}
36 changes: 36 additions & 0 deletions AGC/Instructions/ExtraQuarterCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Apollo.Virtual.AGC.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Apollo.Virtual.AGC.Instructions
{
/// <summary>
/// EQC - EX 0010
/// 10-bit memroy instructions in the extra code set
/// </summary>
class ExtraQuarterCode: InstructionList, IInstruction
{
public ExtraQuarterCode()
: base(3)
{
Add(new Augment { CPU = this.CPU });
}

public ushort Code
{
get { return 0x02; }
}

public Processor CPU { get; set; }

public void Execute(ushort K)
{
var quarterCode = (ushort)(K >> 10);
K = (ushort)(K & 0x3FF);

base[quarterCode].Execute(K);
}
}
}
2 changes: 1 addition & 1 deletion AGC/Instructions/TransferControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void Execute(ushort K)
// else process as a TC command
{
// set Q to the next instruction, for when we return
CPU.Q.Write(CPU.Z.Read());
CPU.Q.Write(CPU.Z);

// set control to K
CPU.Z.Write(K);
Expand Down
Loading

0 comments on commit 3379a0a

Please sign in to comment.