221 lines
7.3 KiB
C#
221 lines
7.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using SDL2;
|
|
|
|
namespace Core
|
|
{
|
|
public class Operations
|
|
{
|
|
public Operations(int pixBufSize) => WindowSize = pixBufSize;
|
|
public Dictionary<string, int> Variables = new Dictionary<string, int>();
|
|
public static int WindowSize;
|
|
public int Accumulator = 0;
|
|
public int StackX = 0;
|
|
public int StackY = 0;
|
|
public int[] Memory = new int[256];
|
|
public string Filename;
|
|
public int pointer = 0;
|
|
public int[] PixBufferR = new int[WindowSize];
|
|
public int[] PixBufferG = new int[WindowSize];
|
|
public int[] PixBufferB = new int[WindowSize];
|
|
|
|
|
|
private int ResolveValue(string shite)
|
|
{
|
|
if (shite.Any(char.IsLetter))
|
|
{
|
|
switch (shite)
|
|
{
|
|
case "ACC":
|
|
return Accumulator;
|
|
case "RAX":
|
|
return StackX;
|
|
case "RAY":
|
|
return StackY;
|
|
}
|
|
Variables.TryGetValue(shite, out int a);
|
|
return a;
|
|
}
|
|
else
|
|
{
|
|
int.TryParse(shite, out int a);
|
|
return a;
|
|
}
|
|
}
|
|
|
|
|
|
public void ExecuteCommand(ComponentType type, string[] args)
|
|
{
|
|
switch (type)
|
|
{
|
|
default: return;
|
|
// Variables
|
|
case ComponentType.NVAR:
|
|
string name = args[1];
|
|
int value = ResolveValue(args[2]);
|
|
Variables.Add(name, value);
|
|
return;
|
|
case ComponentType.RVAR:
|
|
string rname = args[1];
|
|
Variables.Remove(rname);
|
|
return;
|
|
case ComponentType.SVAR:
|
|
string sname = args[1];
|
|
int svalue = ResolveValue(args[2]);
|
|
Variables[sname] = svalue;
|
|
return;
|
|
|
|
|
|
// Memory/stack shite
|
|
case ComponentType.LDA:
|
|
Accumulator = Memory[ResolveValue(args[1])];
|
|
return;
|
|
case ComponentType.STA:
|
|
Memory[ResolveValue(args[1])] = Accumulator;
|
|
return;
|
|
case ComponentType.LDX:
|
|
StackX = Accumulator;
|
|
return;
|
|
case ComponentType.LDY:
|
|
StackY = Accumulator;
|
|
return;
|
|
case ComponentType.STX:
|
|
Accumulator = StackX;
|
|
return;
|
|
case ComponentType.STY:
|
|
Accumulator = StackY;
|
|
return;
|
|
case ComponentType.SEA:
|
|
Accumulator = ResolveValue(args[1]);
|
|
return;
|
|
|
|
|
|
// flow control
|
|
case ComponentType.JMP:
|
|
pointer = ResolveValue(args[1]) - 1;
|
|
return;
|
|
|
|
// math
|
|
case ComponentType.ADD:
|
|
Accumulator = StackX + StackY;
|
|
return;
|
|
case ComponentType.SUB:
|
|
Accumulator = StackX - StackY;
|
|
return;
|
|
case ComponentType.MUL:
|
|
Accumulator = StackX * StackY;
|
|
return;
|
|
case ComponentType.DIV:
|
|
Accumulator = StackX / StackY;
|
|
return;
|
|
|
|
|
|
|
|
// Debug commands
|
|
case ComponentType.PRA:
|
|
Console.WriteLine(Accumulator);
|
|
return;
|
|
case ComponentType.PRX:
|
|
Console.WriteLine(StackX);
|
|
return;
|
|
case ComponentType.PRY:
|
|
Console.WriteLine(StackY);
|
|
return;
|
|
case ComponentType.PRM:
|
|
int.TryParse(args[1], out int valB);
|
|
Console.WriteLine(Memory[valB]);
|
|
return;
|
|
case ComponentType.HLT:
|
|
while (true) { }
|
|
}
|
|
}
|
|
public void EquParserLoop(string equ)
|
|
{
|
|
string[] lines = equ.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
while (pointer < lines.Length)
|
|
{
|
|
string[] tokens = lines[pointer].Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
string command = tokens[0];
|
|
if (tokens.Length == 0)
|
|
{
|
|
pointer++;
|
|
continue;
|
|
}
|
|
|
|
switch (command)
|
|
{
|
|
default: break;
|
|
case "::":
|
|
break;
|
|
// variables
|
|
case "NVAR":
|
|
ExecuteCommand(ComponentType.NVAR, tokens);
|
|
break;
|
|
case "RVAR":
|
|
ExecuteCommand(ComponentType.RVAR, tokens);
|
|
break;
|
|
case "SVAR":
|
|
ExecuteCommand(ComponentType.SVAR, tokens);
|
|
break;
|
|
|
|
// memory
|
|
case "LDA":
|
|
ExecuteCommand(ComponentType.LDA, tokens);
|
|
break;
|
|
case "STA":
|
|
ExecuteCommand(ComponentType.STA, tokens);
|
|
break;
|
|
case "LDX":
|
|
ExecuteCommand(ComponentType.LDX, tokens);
|
|
break;
|
|
case "LDY":
|
|
ExecuteCommand(ComponentType.LDY, tokens);
|
|
break;
|
|
case "STX":
|
|
ExecuteCommand(ComponentType.STX, tokens);
|
|
break;
|
|
case "STY":
|
|
ExecuteCommand(ComponentType.STY, tokens);
|
|
break;
|
|
case "SEA":
|
|
ExecuteCommand(ComponentType.SEA, tokens);
|
|
break;
|
|
|
|
// flow control
|
|
case "JMP":
|
|
ExecuteCommand(ComponentType.JMP, tokens);
|
|
break;
|
|
|
|
// math
|
|
case "ADD":
|
|
ExecuteCommand(ComponentType.ADD, tokens);
|
|
break;
|
|
|
|
// debug
|
|
case "PRA":
|
|
ExecuteCommand(ComponentType.PRA, tokens);
|
|
break;
|
|
case "PRX":
|
|
ExecuteCommand(ComponentType.PRX, tokens);
|
|
break;
|
|
case "PRY":
|
|
ExecuteCommand(ComponentType.PRY, tokens);
|
|
break;
|
|
case "PRM":
|
|
ExecuteCommand(ComponentType.PRM, tokens);
|
|
break;
|
|
|
|
case "HLT":
|
|
ExecuteCommand(ComponentType.HLT, tokens);
|
|
break;
|
|
}
|
|
pointer++;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|