From 7d9a3baffd4db825badd68211efa2799cd9e8bf7 Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Sat, 26 Oct 2024 15:15:37 +0200 Subject: [PATCH] feat: Add SysCalls --- .../Wrappers/ControlUnitWrapper.axaml | 13 ++++---- .../MimaSim/MIMA/Components/ControlUnit.cs | 20 +++++++++++- .../MIMA/Instructions/SysCallInstruction.cs | 22 +++++++++++++ src/MimaSim/MimaSim/MIMA/Mnemnonics.cs | 3 +- src/MimaSim/MimaSim/MIMA/OpCodes.cs | 5 +-- .../Parsers/Assembler/AssemblyVisitor.cs | 32 +++++++------------ src/MimaSim/MimaSim/MIMA/Registers.cs | 1 + src/MimaSim/MimaSim/MIMA/SysCall.cs | 6 ++++ .../Resources/Highligting/Assembler.xshd | 2 ++ 9 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 src/MimaSim/MimaSim/MIMA/Instructions/SysCallInstruction.cs create mode 100644 src/MimaSim/MimaSim/MIMA/SysCall.cs diff --git a/src/MimaSim/MimaSim/Controls/MimaComponents/Wrappers/ControlUnitWrapper.axaml b/src/MimaSim/MimaSim/Controls/MimaComponents/Wrappers/ControlUnitWrapper.axaml index 0917309..e6c8036 100644 --- a/src/MimaSim/MimaSim/Controls/MimaComponents/Wrappers/ControlUnitWrapper.axaml +++ b/src/MimaSim/MimaSim/Controls/MimaComponents/Wrappers/ControlUnitWrapper.axaml @@ -10,15 +10,16 @@ x:Class="MimaSim.Controls.MimaComponents.Wrappers.ControlUnitWrapper"> + - + - + - - + + - - + + \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/Components/ControlUnit.cs b/src/MimaSim/MimaSim/MIMA/Components/ControlUnit.cs index 0e3f1d4..367d0b5 100644 --- a/src/MimaSim/MimaSim/MIMA/Components/ControlUnit.cs +++ b/src/MimaSim/MimaSim/MIMA/Components/ControlUnit.cs @@ -1,4 +1,7 @@ -namespace MimaSim.MIMA.Components; +using System; +using System.Collections.Generic; + +namespace MimaSim.MIMA.Components; public class ControlUnit { @@ -6,6 +9,21 @@ public class ControlUnit public Register FLAG = new("FLAG"); public Register IAR = new("IAR"); public Register SP = new("SP", 49); + public Register SCR = new("SCR"); + + private Dictionary _syscalls = new(); + + public void AddSysCall(SysCall syscall, Action action) + { + _syscalls.TryAdd(syscall, action); + } + + public void InvokeSysCall(SysCall syscall) + { + _syscalls.TryGetValue(syscall, out Action? action); + + action!(); + } public bool HasFlag(Flags flag) { diff --git a/src/MimaSim/MimaSim/MIMA/Instructions/SysCallInstruction.cs b/src/MimaSim/MimaSim/MIMA/Instructions/SysCallInstruction.cs new file mode 100644 index 0000000..6b54916 --- /dev/null +++ b/src/MimaSim/MimaSim/MIMA/Instructions/SysCallInstruction.cs @@ -0,0 +1,22 @@ +using System.Text; +using MimaSim.Core; +using MimaSim.MIMA.Components; + +namespace MimaSim.MIMA.Instructions; + +public class SysCallInstruction : IInstruction, IDisassemblyInstruction +{ + public OpCodes OpCode => OpCodes.SYSCALL; + + public bool Invoke(CPU cpu) + { + cpu.ControlUnit.InvokeSysCall((SysCall)cpu.Fetch16()); + + return true; + } + + public void Dissassemble(StringBuilder builder, Disassembler disassembler) + { + builder.AppendLine($"syscall 0x{disassembler.Fetch16():X2}"); + } +} \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/Mnemnonics.cs b/src/MimaSim/MimaSim/MIMA/Mnemnonics.cs index 478c6bc..1582456 100644 --- a/src/MimaSim/MimaSim/MIMA/Mnemnonics.cs +++ b/src/MimaSim/MimaSim/MIMA/Mnemnonics.cs @@ -18,5 +18,6 @@ public enum Mnemnonics CLK, FLAG, UNFLAG, - HASFLAG + HASFLAG, + SYSCALL } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/OpCodes.cs b/src/MimaSim/MimaSim/MIMA/OpCodes.cs index 3774032..a4550c9 100644 --- a/src/MimaSim/MimaSim/MIMA/OpCodes.cs +++ b/src/MimaSim/MimaSim/MIMA/OpCodes.cs @@ -20,7 +20,6 @@ public enum OpCodes //Shifting LSHIFT = 0x20, - RSHIFT = 0x21, // Logical @@ -53,5 +52,7 @@ public enum OpCodes FLAG = 0x46, HASFLAG = 0x47, - UNFLAG = 0x48 + UNFLAG = 0x48, + + SYSCALL = 0x49, } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs b/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs index 21dcfe6..5fccaf4 100644 --- a/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs +++ b/src/MimaSim/MimaSim/MIMA/Parsing/Parsers/Assembler/AssemblyVisitor.cs @@ -43,6 +43,11 @@ public AssemblyVisitor() { Scope.Root.Define(flag.ToUpper(), (short)Enum.Parse(flag)); } + + foreach (var syscall in Enum.GetNames()) + { + Scope.Root.Define(syscall.ToUpper(), (short)Enum.Parse(syscall)); + } } private void VisitMacroInvocation(InstructionNode invocation, Scope scope) @@ -187,27 +192,12 @@ public void Visit(InstructionNode instruction, Scope scope) private OpCodes SelectOpCode(InstructionNode instruction, Scope scope, Mnemnonics mnemnonic) { - return mnemnonic switch - { - Mnemnonics.MOV => SelectMovOpCode(instruction, scope), - Mnemnonics.LOAD => OpCodes.LOAD, - Mnemnonics.JMP => OpCodes.JMP, - Mnemnonics.JMPC => OpCodes.JMPC, - Mnemnonics.CMPNE => OpCodes.CMPNEQ, - Mnemnonics.CMPE => OpCodes.CMPEQ, - Mnemnonics.CMPLT => OpCodes.CMPLT, - Mnemnonics.CMPGT => OpCodes.CMPGT, - Mnemnonics.ADD => OpCodes.ADD, - Mnemnonics.SUB => OpCodes.SUB, - Mnemnonics.PUSH => OpCodes.PUSH, - Mnemnonics.POP => OpCodes.POP, - Mnemnonics.EXIT => OpCodes.EXIT, - Mnemnonics.CLK => OpCodes.CLK, - Mnemnonics.FLAG => OpCodes.FLAG, - Mnemnonics.HASFLAG => OpCodes.HASFLAG, - Mnemnonics.UNFLAG => OpCodes.UNFLAG, - _ => throw new ArgumentOutOfRangeException(nameof(instruction.Mnemnonic), $@"No opcode defined for mnemonic: {instruction.Mnemnonic}") - }; + if (mnemnonic == Mnemnonics.MOV) + { + return SelectMovOpCode(instruction, scope); + } + + return Enum.Parse(mnemnonic.ToString()); } private OpCodes SelectMovOpCode(InstructionNode instruction, Scope scope) diff --git a/src/MimaSim/MimaSim/MIMA/Registers.cs b/src/MimaSim/MimaSim/MIMA/Registers.cs index ef5e916..9320917 100644 --- a/src/MimaSim/MimaSim/MIMA/Registers.cs +++ b/src/MimaSim/MimaSim/MIMA/Registers.cs @@ -14,4 +14,5 @@ public enum Registers DX, DY, FLAG, + SCR // Register to store syscall result } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/MIMA/SysCall.cs b/src/MimaSim/MimaSim/MIMA/SysCall.cs new file mode 100644 index 0000000..7158586 --- /dev/null +++ b/src/MimaSim/MimaSim/MIMA/SysCall.cs @@ -0,0 +1,6 @@ +namespace MimaSim.MIMA; + +public enum SysCall +{ + +} \ No newline at end of file diff --git a/src/MimaSim/MimaSim/Resources/Highligting/Assembler.xshd b/src/MimaSim/MimaSim/Resources/Highligting/Assembler.xshd index fefabc9..bcbdd16 100644 --- a/src/MimaSim/MimaSim/Resources/Highligting/Assembler.xshd +++ b/src/MimaSim/MimaSim/Resources/Highligting/Assembler.xshd @@ -54,6 +54,8 @@ unflag hasflag + syscall + macro