Skip to content

Commit

Permalink
seq is loading from JSON, aka works
Browse files Browse the repository at this point in the history
  • Loading branch information
rbaker26 committed Jun 17, 2020
1 parent 0acf912 commit b913300
Show file tree
Hide file tree
Showing 16 changed files with 630 additions and 162 deletions.
16 changes: 11 additions & 5 deletions SAP1EMU.Assembler/Assemble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using SAP1EMU.Lib;
using SAP1EMU.Lib.Components;
using SAP1EMU.Lib.Utilities;

Expand All @@ -14,6 +15,9 @@ public static class Assemble
public static List<string> Parse(List<string> unchecked_assembly)
{

// Get Instruction Set
InstructionSet iset = OpCodeLoader.GetSet("SAP1Emu");

// *********************************************************************
// Sanitize *
// *********************************************************************
Expand Down Expand Up @@ -60,7 +64,7 @@ public static List<string> Parse(List<string> unchecked_assembly)


//if is not valid, will throw execptions for CLI to catch and display to user
_ = IsValid(unchecked_assembly);
_ = IsValid(unchecked_assembly, iset);
// *********************************************************************


Expand Down Expand Up @@ -96,9 +100,9 @@ public static List<string> Parse(List<string> unchecked_assembly)


// Convert Upper Nibble
if (SEQ.Instance().SupportedCommandsBinTable.ContainsKey(upper_nibble_asm)) // Is instruction
if (InstructionValidator.IsValidInstruction(upper_nibble_asm, iset)) // Is instruction
{
upper_nibble_bin = SEQ.Instance().SupportedCommandsBinTable[line.Substring(0, 3)];
upper_nibble_bin = InstructionValidator.GetUpperNibble(line.Substring(0, 3), iset);
}
else if (Regex.IsMatch(upper_nibble_asm, "^0[xX][0-9a-fA-F]$")) // Is Data
{
Expand All @@ -125,7 +129,7 @@ public static List<string> Parse(List<string> unchecked_assembly)
return binary;
}

private static bool IsValid(List<string> unchecked_assembly)
private static bool IsValid(List<string> unchecked_assembly, InstructionSet iset)
{
bool dot_macro_used = false;

Expand Down Expand Up @@ -156,7 +160,9 @@ private static bool IsValid(List<string> unchecked_assembly)
{
throw new ParseException($"SAP1ASM: invalid intruction on line {line_number}", new ParseException($"{instruction} is not a recognized instruction"));
}
if (!SEQ.Instance().SupportedCommandsBinTable.ContainsKey(instruction.ToUpper())) // Check if is valid instruction


if (!InstructionValidator.IsValidInstruction(instruction.ToUpper(),iset)) // Check if is valid instruction
{
if (!Regex.IsMatch(instruction, "^0[xX][0-9a-fA-F]$")) // Make sure it isnt data
{
Expand Down
35 changes: 35 additions & 0 deletions SAP1EMU.Assembler/InstructionValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

using SAP1EMU.Lib;

namespace SAP1EMU.Assembler
{
public class InstructionValidator
{
public static bool IsValidInstruction(string instruction, InstructionSet iset)
{
if(instruction.ToLower() == "nop")
{
return true;
}
try
{
return !string.IsNullOrEmpty(iset.instructions.Find(x => x.OpCode.ToLower().Equals(instruction.ToLower())).OpCode);
}
catch(NullReferenceException)
{
return false;
}
}
public static string GetUpperNibble(string instruction, InstructionSet iset)
{
if (instruction.ToLower() == "nop")
{
return "0000";
}
return iset.instructions.Find(x => x.OpCode.ToLower().Equals(instruction.ToLower())).BinCode;
}
}
}
1 change: 1 addition & 0 deletions SAP1EMU.Assembler/SAP1EMU.Assembler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SAP1EMU.Engine\SAP1EMU.Engine.csproj" />
<ProjectReference Include="..\SAP1EMU.Lib\SAP1EMU.Lib.csproj" />
</ItemGroup>

Expand Down
180 changes: 180 additions & 0 deletions SAP1EMU.Engine-CLI/InstructionSets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
[
{
"SetName": "SAP1Emu",
"SetDescription": "The full intruction set for the SAP1Emu Project.",
"instructions": [
{
"OpCode": "LDA",
"BinCode": "0000",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00011010001111",
"00101100001111",
"00111110001111"
]
},
{
"OpCode": "ADD",
"BinCode": "0001",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00011010001111",
"00101110000111",
"00111100011111"
]
},
{
"OpCode": "SUB",
"BinCode": "0010",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00011010001111",
"00101110000111",
"00111100111111"
]
},
{
"OpCode": "STA",
"BinCode": "0011",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00011010001111",
"00111111001101",
"00111110001111"
]
},
{
"OpCode": "JMP",
"BinCode": "0100",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00111010001100",
"00111110001111",
"00111110001111"
]
},
{
"OpCode": "JEQ",
"BinCode": "0101",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00111010001100",
"00111110001111",
"00111110001111"
]
},
{
"OpCode": "JIC",
"BinCode": "1001",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00111010001100",
"00111110001111",
"00111110001111"
]
},
{
"OpCode": "OUT",
"BinCode": "1110",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00111111001011",
"00111110001111",
"00111110001111"
]
},
{
"OpCode": "HLT",
"BinCode": "1111",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00111110001111",
"00111110001111",
"00111110001111"
]
},
{
"OpCode": "NOP",
"BinCode": "0000",
"MicroCode": [
"00111110001111",
"00111110001111",
"00111110001111",
"00111110001111",
"00111110001111",
"00111110001111"
]
}
]
},
{
"SetName": "Malvino",
"SetDescription": "The instruction set outlined in Digital Computer Electronics by Malvino & Brown.\nThe SAP1EMU Set is a superset of the Malvino Set.",
"instructions": [
{
"OpCode": "LDA",
"BinCode": "0000",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00011010001111",
"00101100001111",
"00111110001111"
]
},
{
"OpCode": "ADD",
"BinCode": "0001",
"MicroCode": [
"01011110001111",
"10111110001111",
"00100110001111",
"00011010001111",
"00101110000111",
"00111100011111"
]
}
]
},
{
"SetName": "BenEater",
"SetDescription": "The instruction set outlined by Ben Eater.",
"instructions": [
{
"OpCode": "ADD",
"BinCode": "1111",
"MicroCode": [
"10101",
"00011"
]
},
{
"OpCode": "LDA",
"BinCode": "0000",
"MicroCode": [
"01010",
"11111"
]
}
]
}
]
2 changes: 1 addition & 1 deletion SAP1EMU.Engine-CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static void Main(string[] args)


string engine_output = "************************************************************\n"
+ "Final Output Register Value: " + engine.GetOutput()
+ "Final Output Register Value: " + engine.GetOutputReg()
+ "\n************************************************************\n\n";

List<Frame> FrameStack = engine.FrameStack();
Expand Down
13 changes: 13 additions & 0 deletions SAP1EMU.Engine-CLI/SAP1EMU.Engine-CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<Copyright>2020 Bob Baker</Copyright>
</PropertyGroup>

<ItemGroup>
<None Remove="Makefile" />
<None Remove="packages-microsoft-prod.deb" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>
Expand All @@ -26,5 +31,13 @@
<ProjectReference Include="..\SAP1EMU.Engine\SAP1EMU.Engine.csproj" />
<ProjectReference Include="..\SAP1EMU.Lib\SAP1EMU.Lib.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="InstructionSets.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>



</Project>
16 changes: 11 additions & 5 deletions SAP1EMU.Engine/EngineProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ public class EngineProc
string OutPutRegContents = "";
private readonly List<Frame> _FrameStack = new List<Frame>();
private RAMProgram Program { get; set; }
private InstructionSet InstructionSet { get; set; }

// *************************************************************************
// Init Engine
// *************************************************************************
public void Init(RAMProgram program)
{
// Get Instruction Set
InstructionSet = OpCodeLoader.GetSet("SAP1Emu");

// Init RAM
if (program == null)
{
this.Program = new RAMProgram(new List<string>());
Expand Down Expand Up @@ -71,7 +76,9 @@ public void Run()
// Load the program into the RAM
ram.LoadProgram(Program);


// Load the intsructionSet into the SEQ
seq.Load(InstructionSet);



Frame tempFrame;
Expand Down Expand Up @@ -140,6 +147,8 @@ public void Run()



// *************************************************************************
// Output Functions
// *************************************************************************
public List<Frame> FrameStack()
{
Expand All @@ -159,13 +168,10 @@ public Frame FinalFrame()
}
}

public string GetOutput()
public string GetOutputReg()
{
return OutPutRegContents;
}



// *************************************************************************


Expand Down
12 changes: 0 additions & 12 deletions SAP1EMU.Engine/OpCodeLoader.cs

This file was deleted.

Loading

0 comments on commit b913300

Please sign in to comment.