From 269a2b51bef9b5706c1ef6cd790a381cc13cf98f Mon Sep 17 00:00:00 2001 From: Creeper Lv Date: Fri, 1 Aug 2025 00:06:32 +1000 Subject: [PATCH] Working on a BSD-Style syscall wrapper. --- src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs | 53 +++++++++++++++++++ src/SVM.Advanced/SVM.Advanced.csproj | 12 +++++ src/SVM.Assembler/ISA.xml | 16 ++++++ src/SVM.Core/SimpleVirtualMachine.cs | 17 +++++- src/SVM.Standalone/Program.cs | 5 +- src/SVM.Standalone/SVM.Standalone.csproj | 1 + src/SVM.sln | 14 +++++ 7 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs create mode 100644 src/SVM.Advanced/SVM.Advanced.csproj diff --git a/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs b/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs new file mode 100644 index 0000000..f9af9da --- /dev/null +++ b/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs @@ -0,0 +1,53 @@ +using SVM.Core; +using System; +using System.Collections.Generic; +using System.IO; + +namespace SVM.Advanced.BSDStyleVM +{ + public class BSDLikeWrapper + { + public SimpleVirtualMachine baseVM; + public Dictionary FDs = new Dictionary(); + public BSDLikeWrapper(SimpleVirtualMachine baseVM) + { + this.baseVM = baseVM; + baseVM.AdditionalData = this; + baseVM.Config = BSDConfig.CreateConfig(); + } + } + public class BSDConfig + { + public static SVMConfig CreateConfig() + { + var config = new SVMConfig(); + config.PCRegisterID = 1; + config.SPRegisterID = 2; + + SetupSyscall(config); + return config; + } + public static void SetupSyscall(SVMConfig config) + { + config.FuncCalls.Add(1, BSDStyleFunctions0.__exit); + } + } + public static class BSDStyleFunctions0 + { + public static void __exit(SimpleVirtualMachine machine) + { + var status = machine.registers.GetData(10); + Console.WriteLine("Bye-bye!"); + Environment.Exit(status); + } + } + public class FileDescripter + { + public Stream stream; + + public FileDescripter(Stream stream) + { + this.stream = stream; + } + } +} diff --git a/src/SVM.Advanced/SVM.Advanced.csproj b/src/SVM.Advanced/SVM.Advanced.csproj new file mode 100644 index 0000000..43ba0d5 --- /dev/null +++ b/src/SVM.Advanced/SVM.Advanced.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.1 + enable + + + + + + + diff --git a/src/SVM.Assembler/ISA.xml b/src/SVM.Assembler/ISA.xml index ecc099d..ff1f6ff 100644 --- a/src/SVM.Assembler/ISA.xml +++ b/src/SVM.Assembler/ISA.xml @@ -73,5 +73,21 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SVM.Core/SimpleVirtualMachine.cs b/src/SVM.Core/SimpleVirtualMachine.cs index 3dece13..336597d 100644 --- a/src/SVM.Core/SimpleVirtualMachine.cs +++ b/src/SVM.Core/SimpleVirtualMachine.cs @@ -25,6 +25,8 @@ namespace SVM.Core public MState MachineState; public SVMProgram* Program = null; public static uint InitGPMemorySize = 696320; + public Object? AdditionalData = null; + public ErrorIDs ErrorIDs = new ErrorIDs(); public void Init(uint StackSize = 1024 * 1024, uint RegisterSize = 512, uint GPMemory = uint.MaxValue) { registers.Init(RegisterSize); @@ -181,11 +183,19 @@ namespace SVM.Core case PrimaryInstruction.System: if (Config != null) { - var target = Instruction.GetData(1); + var target = Instruction.GetData(4); if (Config.FuncCalls.TryGetValue(target, out var func)) { func(this); } + else + { + registers.SetData((int)ErrorIDOffset,ErrorIDs.SyscallCallNotExist); + } + } + else + { + registers.SetData((int)ErrorIDOffset, ErrorIDs.SyscallCallNotExist); } break; case PrimaryInstruction.SIMD: @@ -359,6 +369,11 @@ namespace SVM.Core /// public uint EIDRegisterID; } + public class ErrorIDs + { + public int SyscallCallNotExist = 1; + public int MAX_ERROR_ID = short.MaxValue; + } public delegate void FuncCall(SimpleVirtualMachine machine); [StructLayout(LayoutKind.Sequential)] public struct MState diff --git a/src/SVM.Standalone/Program.cs b/src/SVM.Standalone/Program.cs index b121b47..3114ff8 100644 --- a/src/SVM.Standalone/Program.cs +++ b/src/SVM.Standalone/Program.cs @@ -1,4 +1,5 @@ -using SVM.Core; +using SVM.Advanced.BSDStyleVM; +using SVM.Core; namespace SVM.Standalone; @@ -25,10 +26,10 @@ class Program { Program = program }; + BSDLikeWrapper wrapper = new BSDLikeWrapper(svm); svm.Init(); while (!svm.isReachBinaryEnd()) { - Console.WriteLine("Step"); svm.Step(); } } diff --git a/src/SVM.Standalone/SVM.Standalone.csproj b/src/SVM.Standalone/SVM.Standalone.csproj index f1af48c..6889a86 100644 --- a/src/SVM.Standalone/SVM.Standalone.csproj +++ b/src/SVM.Standalone/SVM.Standalone.csproj @@ -1,6 +1,7 @@  + diff --git a/src/SVM.sln b/src/SVM.sln index c3c67da..87280da 100644 --- a/src/SVM.sln +++ b/src/SVM.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Assembler.Core", "SVM.A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Assembler", "SVM.Assembler\SVM.Assembler.csproj", "{E2B82455-3947-4C11-82D0-178E8D8C3D64}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Advanced", "SVM.Advanced\SVM.Advanced.csproj", "{F9F2F699-49F7-4427-BA22-B444DD6259FD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -69,6 +71,18 @@ Global {E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x64.Build.0 = Release|Any CPU {E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x86.ActiveCfg = Release|Any CPU {E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x86.Build.0 = Release|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Debug|x64.ActiveCfg = Debug|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Debug|x64.Build.0 = Debug|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Debug|x86.ActiveCfg = Debug|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Debug|x86.Build.0 = Debug|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Release|Any CPU.Build.0 = Release|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Release|x64.ActiveCfg = Release|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Release|x64.Build.0 = Release|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Release|x86.ActiveCfg = Release|Any CPU + {F9F2F699-49F7-4427-BA22-B444DD6259FD}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE