mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-10 20:39:54 +00:00
Working on a BSD-Style syscall wrapper.
This commit is contained in:
53
src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs
Normal file
53
src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs
Normal file
@@ -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<int, FileDescripter> FDs = new Dictionary<int, FileDescripter>();
|
||||
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<int>(10);
|
||||
Console.WriteLine("Bye-bye!");
|
||||
Environment.Exit(status);
|
||||
}
|
||||
}
|
||||
public class FileDescripter
|
||||
{
|
||||
public Stream stream;
|
||||
|
||||
public FileDescripter(Stream stream)
|
||||
{
|
||||
this.stream = stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/SVM.Advanced/SVM.Advanced.csproj
Normal file
12
src/SVM.Advanced/SVM.Advanced.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SVM.Core\SVM.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -73,5 +73,21 @@
|
||||
</InstructionParameter>
|
||||
</Parameters>
|
||||
</InstructionDefinition>
|
||||
<InstructionDefinition PrimaryInstruction="System" InstructionCount="1">
|
||||
<Aliases>
|
||||
<Alias Name="system"/>
|
||||
<Alias Name="sys"/>
|
||||
<Alias Name="syscall"/>
|
||||
</Aliases>
|
||||
<Parameters>
|
||||
<InstructionParameter>
|
||||
<MatchingItems>
|
||||
<Item Id="Number"/>
|
||||
<Item Id="Word"/>
|
||||
</MatchingItems>
|
||||
<ExpectedValue Type="Int32" Pos="4" Converter="Integer32" />
|
||||
</InstructionParameter>
|
||||
</Parameters>
|
||||
</InstructionDefinition>
|
||||
</Definitions>
|
||||
</ISARoot>
|
||||
@@ -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<uint>(1);
|
||||
var target = Instruction.GetData<uint>(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
|
||||
/// </summary>
|
||||
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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SVM.Advanced\SVM.Advanced.csproj" />
|
||||
<ProjectReference Include="..\SVM.Core\SVM.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
14
src/SVM.sln
14
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
|
||||
|
||||
Reference in New Issue
Block a user