rewrite, its now a vm for a shitty subset of assembly
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
public class DebugDumper
|
||||
{
|
||||
public static void GenerateDumpFile(string asmCode, string outputPath)
|
||||
{
|
||||
string[] lines = asmCode.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
||||
StringBuilder dump = new StringBuilder();
|
||||
Dictionary<string, int> variables = new Dictionary<string, int>();
|
||||
int accumulator = 0;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
string line = lines[i].Trim();
|
||||
if (string.IsNullOrEmpty(line) || line.StartsWith("::"))
|
||||
{
|
||||
dump.AppendLine($"[{i}] {line}");
|
||||
continue;
|
||||
}
|
||||
|
||||
string[] tokens = line.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (tokens.Length == 0) continue;
|
||||
|
||||
string command = tokens[0].ToUpper();
|
||||
string comment = "";
|
||||
int ResolveValue(string val)
|
||||
{
|
||||
if (variables.ContainsKey(val)) return variables[val];
|
||||
if (int.TryParse(val, out int num)) return num;
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (command)
|
||||
{
|
||||
// Variables
|
||||
case "NVAR":
|
||||
string newVar = tokens[1];
|
||||
int newVal = ResolveValue(tokens[2]);
|
||||
variables[newVar] = newVal;
|
||||
comment = $"Create '{newVar}' with value of '{newVal}'";
|
||||
break;
|
||||
case "SVAR":
|
||||
string setVar = tokens[1];
|
||||
int setVal = ResolveValue(tokens[2]);
|
||||
variables[setVar] = setVal;
|
||||
comment = $"Set variable '{setVar}' to '{setVal}'";
|
||||
break;
|
||||
case "RVAR":
|
||||
variables.Remove(tokens[1]);
|
||||
comment = $"Remove variable '{tokens[1]}'";
|
||||
break;
|
||||
|
||||
// Memory / Registers
|
||||
case "LDA":
|
||||
comment = $"Load memory address {tokens[1]} into Accumulator";
|
||||
break;
|
||||
case "STA":
|
||||
comment = $"Store the Accumulator ({accumulator}) into Memory address {tokens[1]}";
|
||||
break;
|
||||
case "LDX":
|
||||
x = accumulator;
|
||||
comment = $"Load Accumulator ({accumulator}) into X";
|
||||
break;
|
||||
case "LDY":
|
||||
y = accumulator;
|
||||
comment = $"Load Accumulator ({accumulator}) into Y";
|
||||
break;
|
||||
case "STX":
|
||||
accumulator = x;
|
||||
comment = $"Store X ({x}) into Accumulator";
|
||||
break;
|
||||
case "STY":
|
||||
accumulator = y;
|
||||
comment = $"Store Y ({y}) into Accumulator";
|
||||
break;
|
||||
case "SEA":
|
||||
int seaVal = ResolveValue(tokens[1]);
|
||||
accumulator = seaVal;
|
||||
// If they passed a variable name, show both the name and the value
|
||||
if (variables.ContainsKey(tokens[1]))
|
||||
comment = $"Set Accumulator to '{tokens[1]}' ({seaVal})";
|
||||
else
|
||||
comment = $"Set Accumulator to {seaVal}";
|
||||
break;
|
||||
|
||||
// Arithmetic
|
||||
case "ADD":
|
||||
accumulator = x + y;
|
||||
comment = $"Add X ({x}) and Y ({y}) and store into the Accumulator ({accumulator})";
|
||||
break;
|
||||
case "SUB":
|
||||
accumulator = x - y;
|
||||
comment = $"Subtract Y ({y}) from X ({x}) and store into the Accumulator ({accumulator})";
|
||||
break;
|
||||
case "MUL":
|
||||
accumulator = x * y;
|
||||
comment = $"Multiply X ({x}) and Y ({y}) and store into the Accumulator ({accumulator})";
|
||||
break;
|
||||
case "DIV":
|
||||
if (y != 0) accumulator = x / y;
|
||||
comment = $"Divide X ({x}) by Y ({y}) and store into the Accumulator ({accumulator})";
|
||||
break;
|
||||
|
||||
// Flow Control
|
||||
case "JZE":
|
||||
comment = $"Jump to line {tokens[1]} if Zero flag is true";
|
||||
break;
|
||||
case "JMP":
|
||||
comment = $"Jump to line {tokens[1]}";
|
||||
break;
|
||||
case "SFG":
|
||||
comment = $"Set flag '{tokens[1]}' to true";
|
||||
break;
|
||||
case "RFG":
|
||||
comment = $"Set flag '{tokens[1]}' to false";
|
||||
break;
|
||||
|
||||
// Debug
|
||||
case "PRA":
|
||||
comment = $"Print Accumulator ({accumulator})";
|
||||
break;
|
||||
case "PRX":
|
||||
comment = $"Print X ({x})";
|
||||
break;
|
||||
case "PRY":
|
||||
comment = $"Print Y ({y})";
|
||||
break;
|
||||
case "PRM":
|
||||
comment = $"Print value of memory address {tokens[1]}";
|
||||
break;
|
||||
case "HLT":
|
||||
comment = "Halt execution";
|
||||
break;
|
||||
|
||||
default:
|
||||
comment = "Unknown command";
|
||||
break;
|
||||
}
|
||||
dump.AppendLine($"[{i}] {line,-20} :: {comment}");
|
||||
}
|
||||
File.WriteAllText(outputPath, dump.ToString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user