Initial Commit.

This commit is contained in:
2025-07-16 20:09:34 +08:00
parent be67738526
commit 1ca24171fb
9 changed files with 346 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
namespace SVM.Core
{
[Serializable]
public class DebugSymbol
{
public Dictionary<string, uint> Functions = new Dictionary<string, uint>();
}
public class RuntimeBinary
{
public uint offset;
public SimpleVirtualMachine? BindedMachine;
public DebugSymbol? Symbol;
public bool Invoke(string funcName)
{
if (BindedMachine == null) return false;
if (Symbol == null) return false;
if (!Symbol.Functions.TryGetValue(funcName, out var func))
{
return false;
}
return false;
}
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
</Project>

68
src/SVM.Core/SVMInst.cs Normal file
View File

@@ -0,0 +1,68 @@
namespace SVM.Core
{
public enum SVMInst : byte
{
// 0 1 2 3 4
// Add [I]Type [R]L [R]R [R]T
Add,
Sub,
Mul,
Div,
Mod,
// Set Conditional Register to 1 if condition met.
// 0 1 2 3 4
// Cmp [R]Op [R]L [R]R [R]T
Cmp,
// 0 1
// JAL RD
// [I]Address (int32)
JAL,
// Jump And Link If Conditional Register is set.
// JALF RD
// [I]Address (int32)
JALF,
// 0 1 2 3
// Load [R]Address [I]Len [R]T
Load,
Save,
// 0
// Call
// [I]Address (int64)
Call,
// Return
Return,
// System
// [I]CallID (uint64)
System,
// 0 1 2 3 4 5
// SIMD Op [R]LAddr [R]RAddr [R]TAddr [R]Len
SIMD,
// 0 1 2 3 4
// AdvMath Op [R]L [R]R [R]T
AdvMath,
}
public enum CmpOperator : byte
{
Eq, Ne, LT, GT, LE, GE
}
public enum SVMNativeTypes : byte
{
Int8,
Int16,
Int32,
Int64,
UInt8,
UInt16,
UInt32,
UInt64,
Float,
Double,
}
public enum SIMDOperator : byte
{
Add,
Sub,
Mul,
Div,
}
}

View File

@@ -0,0 +1,85 @@
using System;
using static SVM.Core.stdc.stdlib;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using SVM.Core.Utils;
namespace SVM.Core
{
public unsafe class SimpleVirtualMachine : IDisposable
{
public Registers registers;
public MemoryBlock Stack;
public MemoryBlock* GPMemory;
public SVMConfig? Config = null;
public void Init(uint StackSize = 1024 * 1024, uint RegisterSize = 512)
{
GPMemory = null;
}
public void Step()
{
uint SPOffset = 4;
uint PCOffset = 0;
if (Config != null)
{
SPOffset = Config.SPRegisterOffset;
PCOffset = Config.PCRegisterOffset;
}
}
public void SetGPMemory(MemoryBlock* ptr)
{
GPMemory = ptr;
}
public void Dispose()
{
registers.Dispose();
Stack.Dispose();
}
}
public class SVMConfig
{
public Dictionary<uint, FuncCall> FuncCalls = new Dictionary<uint, FuncCall>();
public uint SPRegisterOffset;
public uint PCRegisterOffset;
}
public delegate void FuncCall(SimpleVirtualMachine machine);
[StructLayout(LayoutKind.Sequential)]
public struct MState
{
public uint PC;
}
[StructLayout(LayoutKind.Sequential)]
public struct MemoryBlock : IDisposable
{
public IntPtr StartAddress;
public int Size;
public void Dispose()
{
free(StartAddress);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Registers : IDisposable
{
IntPtr Data;
public void Init(int size)
{
Data = malloc((uint)size);
}
public T ReadData<T>(int RegisterID) where T : unmanaged
{
return Data.GetDataWithOffsetInBytes<T>(RegisterID * sizeof(Int64));
}
public void Dispose()
{
free(Data);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Callframe
{
public int PC;
public uint SP;
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Runtime.InteropServices;
using static SVM.Core.stdc.cstring;
namespace SVM.Core.Utils
{
public unsafe static class PointerUtils
{
public static T GetData<T>(this IntPtr ptr) where T : unmanaged
{
return Marshal.PtrToStructure<T>(ptr);
}
public static T GetDataWithOffsetInBytes<T>(this IntPtr ptr, int Offset) where T : unmanaged
{
return Marshal.PtrToStructure<T>(IntPtr.Add(ptr, Offset));
}
public static T GetDataWithOffsetInStructCount<T>(this IntPtr ptr, int Count) where T : unmanaged
{
return Marshal.PtrToStructure<T>(IntPtr.Add(ptr, Count * sizeof(T)));
}
public static void GetData<T>(this IntPtr ptr, IntPtr dest) where T : unmanaged
{
memcpy(ptr, dest, sizeof(T), 1);
}
public static void GetDataWithOffsetInBytes<T>(this IntPtr ptr, IntPtr dest, int offset) where T : unmanaged
{
memcpy(IntPtr.Add(ptr, offset), dest, sizeof(T), 1);
}
public static void GetDataWithOffsetInStructCount<T>(this IntPtr ptr, IntPtr dest, int offset) where T : unmanaged
{
memcpy(IntPtr.Add(ptr, offset * sizeof(T)), dest, sizeof(T), 1);
}
public static void GetData<T>(this IntPtr ptr, T* dest) where T : unmanaged
{
GetData<T>(ptr, (IntPtr)dest);
}
public static void GetDataWithOffsetInBytes<T>(this IntPtr ptr, T* dest, int offset) where T : unmanaged
{
GetDataWithOffsetInBytes<T>(ptr, (IntPtr)dest, offset);
}
public static void GetDataWithOffsetInStructCount<T>(this IntPtr ptr, T* dest, int offset) where T : unmanaged
{
GetDataWithOffsetInStructCount<T>(ptr, (IntPtr)dest, offset);
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace SVM.Core.stdc
{
/// <summary>
/// A simple library contains POSIX-like impl of memory functions.
/// </summary>
public static class stdlib
{
public static IntPtr malloc(uint size)
{
return Marshal.AllocHGlobal((int)size);
}
public static IntPtr realloc(IntPtr ptr, uint size)
{
return Marshal.ReAllocHGlobal(ptr, (IntPtr)size);
}
public static void free(IntPtr ptr)
{
Marshal.FreeHGlobal(ptr);
}
}
public static class cstring
{
public static void memset(IntPtr ptr, byte value, int count)
{
for (int i = 0; i < count; i++)
{
Marshal.WriteByte(ptr, value);
}
}
public unsafe static void memcpy(IntPtr src, IntPtr dst, int size, int count)
{
Buffer.MemoryCopy((void*)src, (void*)dst, size, count);
}
}
}

View File

@@ -0,0 +1,9 @@
namespace SVM.Standalone;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\SVM.Core\SVM.Core.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

48
src/SVM.sln Normal file
View File

@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Core", "SVM.Core\SVM.Core.csproj", "{5C52ADCB-596C-46F3-97E8-371F74CBFC36}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Standalone", "SVM.Standalone\SVM.Standalone.csproj", "{A2BC0739-0B24-4103-B35C-0475FEC2242C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x64.ActiveCfg = Debug|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x64.Build.0 = Debug|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x86.ActiveCfg = Debug|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x86.Build.0 = Debug|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|Any CPU.Build.0 = Release|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x64.ActiveCfg = Release|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x64.Build.0 = Release|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x86.ActiveCfg = Release|Any CPU
{5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x86.Build.0 = Release|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x64.ActiveCfg = Debug|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x64.Build.0 = Debug|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x86.ActiveCfg = Debug|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x86.Build.0 = Debug|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|Any CPU.Build.0 = Release|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x64.ActiveCfg = Release|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x64.Build.0 = Release|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x86.ActiveCfg = Release|Any CPU
{A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal