-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated Instructions to separate classes
Added instruction list for managing instructions instead of big switch statements
- Loading branch information
Showing
8 changed files
with
220 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
interface IInstruction | ||
{ | ||
ushort Code { get; } | ||
|
||
Processor CPU { get; set; } | ||
|
||
void Execute(ushort K); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Apollo.Virtual.AGC.Base | ||
{ | ||
class InstructionList | ||
{ | ||
private IInstruction[] array; | ||
|
||
public InstructionList(int count) | ||
{ | ||
array = new IInstruction[0]; | ||
} | ||
|
||
public InstructionList() | ||
: this(0) | ||
{ | ||
} | ||
|
||
public void Add(IInstruction instruction) | ||
{ | ||
this[instruction.Code] = instruction; | ||
} | ||
|
||
public IInstruction this[ushort code] | ||
{ | ||
get | ||
{ | ||
if (code > array.Length - 1) | ||
throw new IndexOutOfRangeException(); | ||
|
||
return array[code]; | ||
} | ||
set | ||
{ | ||
if(code > array.Length - 1) | ||
Array.Resize(ref array, code + 1); | ||
|
||
array[code] = value; | ||
} | ||
} | ||
|
||
public void Clear() | ||
{ | ||
array = new IInstruction[0]; | ||
} | ||
|
||
public int Count | ||
{ | ||
get { return array.Length; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Apollo.Virtual.AGC.Base; | ||
using Apollo.Virtual.AGC.Instructions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Apollo.Virtual.AGC | ||
{ | ||
class InstructionSet | ||
{ | ||
private Processor CPU; | ||
private InstructionList instructions; | ||
|
||
public InstructionSet(Processor cpu) | ||
{ | ||
CPU = cpu; | ||
instructions = new InstructionList(7); | ||
|
||
instructions.Add(new QuarterCode { CPU = CPU }); | ||
instructions.Add(new Add { CPU = CPU }); | ||
} | ||
|
||
public IInstruction this[ushort code] | ||
{ | ||
get | ||
{ | ||
return instructions[code]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
/// AD - 0110 | ||
/// | ||
/// Adds the value located in K to the accumulator | ||
/// </summary> | ||
class Add : IInstruction | ||
{ | ||
public Processor CPU { get; set; } | ||
|
||
public ushort Code { get { return 0x06; } } | ||
|
||
public void Execute(ushort K) | ||
{ | ||
var value = CPU.Memory[K]; | ||
CPU.A.Add(CPU.Memory[K]); | ||
|
||
// value in K is re-written | ||
CPU.Memory[K] = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
/// ADS - 0010 11 | ||
/// QuaterCode Instruction | ||
/// | ||
/// Adds the accumulator to an eraseable memory location and vice versa | ||
/// </summary> | ||
class AddToStorage : IInstruction | ||
{ | ||
public ushort Code | ||
{ | ||
get { return 0x02; } | ||
} | ||
|
||
public Processor CPU { get; set; } | ||
|
||
public void Execute(ushort K) | ||
{ | ||
var value = CPU.Memory[K]; | ||
CPU.A.Add(value); | ||
|
||
CPU.Memory[K] = CPU.A.Read(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
/// QC - 0010 | ||
/// | ||
/// Operation on 10-bit address space operand restricted to eraseable memory | ||
/// Followed by 2-bit quarter code for instruction | ||
/// </summary> | ||
class QuarterCode : IInstruction | ||
{ | ||
private InstructionList instuctions; | ||
|
||
public QuarterCode() | ||
{ | ||
instuctions = new InstructionList(3); | ||
instuctions.Add(new AddToStorage { 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); | ||
|
||
instuctions[quarterCode].Execute(K); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters