Skip to content

Commit

Permalink
Merge pull request #3 from RonnyA/ImplementAssembler
Browse files Browse the repository at this point in the history
Added support for unit testing
  • Loading branch information
RonnyA authored Dec 16, 2020
2 parents 454b13e + 7af84af commit 8dbacaf
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 57 deletions.
47 changes: 37 additions & 10 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static void Main(string[] args)

string defaultS8File = @"s8.s8";

S8Dissasembler s8d = new S8Dissasembler();
S8Dissasembler s8d = new S8Dissasembler();

s8d.Init(defaultS8File);

Expand All @@ -40,7 +40,7 @@ static void Main(string[] args)
{
Console.Write("s8 [");
}
Console.Write(currentAddress.ToString("X4") + "] ");
Console.Write(currentAddress.ToString("X3") + "] ");
string input = Console.ReadLine();

try
Expand Down Expand Up @@ -87,7 +87,7 @@ static void Main(string[] args)
{
Console.WriteLine("Assembly FAILED!!");
}
else
else
{
s8d = new S8Dissasembler();
s8d.InitFromMemory(s8prog);
Expand Down Expand Up @@ -140,7 +140,7 @@ static void Main(string[] args)
}
}
break;

case "INPUT":
case "FØDE":
if (cmd.Length > 1)
Expand All @@ -157,6 +157,19 @@ static void Main(string[] args)
currentAddress = s8d.SetPC(start);
break;

case "PC!": // set PC
currentAddress = s8d.SetPC(start, true);
break;


case "UNITTEST":
if (cmd.Length > 1)
{
S8UnitTest s8unit = new S8UnitTest();
currentAddress = s8unit.RunUnitTest(s8d, cmd[1]);
}
break;

case "R":
case "RUN":
currentAddress = s8d.Run();
Expand All @@ -176,7 +189,7 @@ static void Main(string[] args)
case "S":
case "STEP":
if (start > 0)
{
{
currentAddress = s8d.Step(start);
}
else
Expand All @@ -191,7 +204,7 @@ static void Main(string[] args)
{
s8d.SetMaxTicks(start);
}

Console.WriteLine("MaxTicks is set to " + s8d.GetMaxTicks().ToString());
break;
case ":":
Expand All @@ -203,9 +216,16 @@ static void Main(string[] args)
case "D":
currentAddress = s8d.Dissasemble(start, length, showAddress);
break;
case "D!":
currentAddress = s8d.Dissasemble(start, length, showAddress, true);
break;
case "M":
currentAddress = s8d.MemoryDump(start, length, showAddress);
break;
case "M!":
currentAddress = s8d.MemoryDump(start, length, showAddress, true);
break;

case "H":
case "HELP":
case "?":
Expand Down Expand Up @@ -266,7 +286,7 @@ private static byte[] Asm(string sledeFile)
Console.WriteLine("Can't find SLEDE8 file " + sledeFile);
}
S8Assembler s8 = new S8Assembler();

return s8.AssembleFile(sledeFile);
}

Expand Down Expand Up @@ -294,18 +314,25 @@ static void hard()
static void PrintHelp()
{
Console.WriteLine("D - Dissassemble [start] [length]");
Console.WriteLine("M - Memory Dump [start] [length]");
Console.WriteLine("M - Memory Dump [start] [length]");
Console.WriteLine("Limits itself to inside loaded image");
Console.WriteLine();

Console.WriteLine("D!- Dissassemble [start] [length]");
Console.WriteLine("M!- Memory Dump [start] [length]");
Console.WriteLine("Enables access to memory ourside loaded image"); ;
Console.WriteLine();

Console.WriteLine("");

Console.WriteLine("FØDE - SET INPUT hexhexhex");
Console.WriteLine("PC - SET pc = 0xNNNN");
Console.WriteLine("INPUT - SET INPUT hexhexhex");
Console.WriteLine("PC - SET pc = xxx");
Console.WriteLine("RUN - Run program from 0");
Console.WriteLine("REGS - Dump registers");
Console.WriteLine("RESET - Reset registers");
Console.WriteLine("STEP - Step PC [steps]");
Console.WriteLine("TICKS - Set Max Ticks 0xNN");
Console.WriteLine("UNITTEST [filename] - Run unit tests agains [filename]");
Console.WriteLine("! = Change showaddress flag");
Console.WriteLine("");

Expand Down
1 change: 1 addition & 0 deletions ReadMe-release.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotnet publish -r win-x64 -c release -p:PublishSingleFile=true --self-contained true
12 changes: 9 additions & 3 deletions S8CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public S8CPU()
public void SetMaxTicks(int Ticks)
{
state.maxTicks = Ticks;
ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted] = ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted].Replace("${maxTicks}", Ticks.ToString());

}

//const memory = load(executable);
Expand All @@ -91,8 +91,13 @@ public void SetMaxTicks(int Ticks)

public byte[] Load(byte[] executable, bool skipMagicHeader)
{
int oldMaxTicks = DEFAULT_MAX_STEPS;
if (state is not null)
{
oldMaxTicks = state.maxTicks;
}
state = new CpuState();
SetMaxTicks(DEFAULT_MAX_STEPS);
SetMaxTicks(oldMaxTicks);

ResetRegs();

Expand Down Expand Up @@ -190,7 +195,8 @@ internal bool RunUntil(int stop_pc_at)
{
if (++state.tick > state.maxTicks)
{
Console.WriteLine(ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted]);
var strErr = ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted].Replace("${maxTicks}", state.tick.ToString());
Console.WriteLine(strErr);
return false;
}

Expand Down
104 changes: 73 additions & 31 deletions S8Dissasembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace S8Debugger
{
public class S8Dissasembler
{
byte[] bytes = null;
{
byte[] bytes = new byte[4096];
S8CPU cpu = new S8CPU();

public bool Init(string fname, bool force = false)
Expand All @@ -39,8 +39,8 @@ internal bool InitFromMemory(byte[] s8prog)
return true;
}

public int MemoryDump(int start, int length, bool showAddress = false)
{
public int MemoryDump(int start, int length, bool showAddress = false, bool allowOutsideLoadedMemory = false)
{
int currentAddress = start;
int endAddress = currentAddress + length;

Expand All @@ -51,25 +51,30 @@ public int MemoryDump(int start, int length, bool showAddress = false)
//endAddress = cpu.state.memoryUsed;
}

if (endAddress > cpu.state.memoryUsed)
if (!allowOutsideLoadedMemory)
{
endAddress = cpu.state.memoryUsed;
if (endAddress > cpu.state.memoryUsed)
{
endAddress = cpu.state.memoryUsed;
}
}



int lineCounter = 0;

string line1 = "";
string line2 = "";


while (currentAddress < endAddress)
{

if (lineCounter == 0)
{
string sHexAddress = currentAddress.ToString("X3");
Console.WriteLine("m" + sHexAddress + ":");
line1 = ".DATA ";
line2 = ";" + currentAddress.ToString("X4") + ": ";
line2 = ";" + currentAddress.ToString("X3") + ": ";
}
else
{
Expand Down Expand Up @@ -104,22 +109,22 @@ public int MemoryDump(int start, int length, bool showAddress = false)

lineCounter = 0;
Console.WriteLine();
}
}
}

Console.WriteLine();

return currentAddress;
}



internal void Reset()
{
cpu.ResetRegs();
}

public int Dissasemble(int start, int length, bool showAddress = false)
public int Dissasemble(int start, int length, bool showAddress = false, bool allowOutsideLoadedMemory = false)
{
S8Instruction s8i;

Expand All @@ -132,29 +137,45 @@ public int Dissasemble(int start, int length, bool showAddress = false)
endAddress = currentAddress + 8;
//endAddress = cpu.state.memoryUsed;
}

if (endAddress > cpu.state.memoryUsed)
if (!allowOutsideLoadedMemory)
{
endAddress = cpu.state.memoryUsed;
if (endAddress > cpu.state.memoryUsed)
{
endAddress = cpu.state.memoryUsed;
}
}

while (currentAddress < endAddress)
{
string sHexAddress = currentAddress.ToString("X4");
string sHexAddress = currentAddress.ToString("X3");

byte opcode = bytes[currentAddress++];
byte param = bytes[currentAddress++];

s8i = new S8Instruction(opcode, param);
s8i.DecodeInstruction();

if (s8i.ValidInstruction)
{
if (!showAddress)
Console.WriteLine("a" + sHexAddress + ":");
}
else
{
if (!showAddress)
Console.WriteLine("m" + sHexAddress + ":");
}

if (showAddress)
{
string sOpcode = opcode.ToString("X2");
string sParam = param.ToString("X2");
Console.Write("A["+sHexAddress + "] | I["+sOpcode + " " + sParam + "] ");
Console.Write("A[" + sHexAddress + "] | I[" + sOpcode + " " + sParam + "] ");
}





if (s8i.ValidInstruction)
{
Console.WriteLine(s8i.DecodedInstruction);
Expand All @@ -169,28 +190,47 @@ public int Dissasemble(int start, int length, bool showAddress = false)

}

internal int SetPC(int start)
internal int SetPC(int start, bool allowOutsideLoadedMemory = false)
{
if (start > cpu.state.memoryUsed)
{
if (start > 0xFFF)
start = 0;

if (!allowOutsideLoadedMemory)
{
if (start > cpu.state.memoryUsed)
{
start = 0;
}
}
cpu.state.pc = start;

return cpu.state.pc;
}

internal void SetInput(byte[] inputBuffer)
{
cpu.state.stdin = inputBuffer;
}

internal void SetInput(string v)
{
string s = ConvertHex2Asii(v);

cpu.state.stdin = new byte[s.Length];
for (int i=0;i<s.Length;i++)
for (int i = 0; i < s.Length; i++)
{
cpu.state.stdin[i] = (byte)s[i];
}
}
}
internal string GetOutput()
{
return cpu.state.stdout;
}

internal void ClearOutput()
{
cpu.state.stdout = "";
}

public void SetMaxTicks(int Ticks)
{
Expand All @@ -202,18 +242,20 @@ public int GetMaxTicks()
return cpu.state.maxTicks;
}

public int Run()

public int Run(bool ShowOppgulp = true)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
stopwatch.Start();

cpu.Run();

stopwatch.Stop();
var elapsed_time = stopwatch.ElapsedMilliseconds;

Console.WriteLine("Elapsed time " + elapsed_time + "ms, Ticks " + cpu.state.tick);
Oppgulp();
if (ShowOppgulp)
Oppgulp();
return cpu.state.pc;
}

Expand Down Expand Up @@ -286,9 +328,9 @@ private void Oppgulp()

public void Regs()
{
Console.WriteLine("PC [" + cpu.state.pc.ToString("X4") +"]");
Console.WriteLine("FLAG [" + cpu.state.flag +"]");
for (int i=0; i<16;i++)
Console.WriteLine("PC [" + cpu.state.pc.ToString("X3") + "]");
Console.WriteLine("FLAG [" + cpu.state.flag + "]");
for (int i = 0; i < 16; i++)
{
Console.Write("R" + i + "[" + cpu.state.regs[i].ToString("X2") + "] ");
}
Expand Down
Loading

0 comments on commit 8dbacaf

Please sign in to comment.