From a8b526c64573947c5f646d0ddebfe22638bca837 Mon Sep 17 00:00:00 2001 From: Creeper Lv Date: Sun, 3 Aug 2025 01:20:55 +1000 Subject: [PATCH] Working on __open. Working op `load` instruction. --- src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs | 6 ++- src/SVM.Assembler.Core/Linker.cs | 47 +++++++++++++++++++ src/SVM.Assembler/ISA.xml | 29 ++++++++++++ src/SVM.Core/Registers.cs | 6 +++ src/SVM.Core/SimpleVirtualMachine.cs | 9 ++++ 5 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs b/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs index c6fcb94..8d07f28 100644 --- a/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs +++ b/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs @@ -45,6 +45,7 @@ namespace SVM.Advanced.BSDStyleVM { config.FuncCalls.Add(1, BSDStyleFunctions0.__exit); config.FuncCalls.Add(4, BSDStyleFunctions0.__write); + config.FuncCalls.Add(5, BSDStyleFunctions0.__open); } } public static class BSDFcntl @@ -69,7 +70,7 @@ namespace SVM.Advanced.BSDStyleVM var size = machine.registers.GetData(11); var flag = machine.registers.GetData(12); var fn = Encoding.UTF8.GetString((byte*)machine.GetPointer(ptr), (int)size); - FileMode fm = default; + FileMode fm = FileMode.Create; FileAccess fa = default; if ((flag & BSDFcntl.O_WRONLY) == BSDFcntl.O_WRONLY) { @@ -80,6 +81,7 @@ namespace SVM.Advanced.BSDStyleVM var stream = File.Open(fn, fm, fa); FileDescripter fd = new FileDescripter(stream); w.FDs.Add(fdID, fd); + machine.registers.SetDataInRegister(10,fdID); } else { @@ -95,7 +97,9 @@ namespace SVM.Advanced.BSDStyleVM var size = machine.registers.GetData(12); if (w.FDs.TryGetValue(fd, out var descripter)) { + Console.OpenStandardOutput().WriteData(machine.GetPointer(ptr), size); descripter.stream.WriteData(machine.GetPointer(ptr), size); + descripter.stream.Flush(); } else Console.WriteLine($"FD:{fd} does not exist."); diff --git a/src/SVM.Assembler.Core/Linker.cs b/src/SVM.Assembler.Core/Linker.cs index 97091fd..a3c4a68 100644 --- a/src/SVM.Assembler.Core/Linker.cs +++ b/src/SVM.Assembler.Core/Linker.cs @@ -69,6 +69,32 @@ namespace SVM.Assembler.Core registerID = byte.MaxValue; return false; } + public static bool TryParseUInt8(string input, LinkingContext context, out byte value) + { + if (byte.TryParse(input, out value)) + { + return true; + } + else + { + if (context.IntermediateObject.TryGetConst(input, out var realStr)) + { + return TryParseUInt8(realStr, context, out value); + } + if (context.TryFindLabel(input, out var lblV)) + { + value = (byte)lblV; + return true; + } + if (context.TryFindData(input, out var dataV)) + { + value = (byte)dataV; + return true; + } + } + value = byte.MaxValue; + return false; + } public static bool TryParseInt32(string input, LinkingContext context, out int value) { if (int.TryParse(input, out value)) @@ -361,6 +387,27 @@ namespace SVM.Assembler.Core WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, (byte*)®isterID); } break; + case "Integer64": + { + if (!TryParseInt64(para.Content, context, out var registerID)) + { + result.AddError(new ErrorWMsg($"{para.Content} cannot be parsed to Integer64!", para)); + return result; + } + WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, (byte*)®isterID); + } + break; + case "UnsignedInteger8": + case "UInt8": + { + if (!TryParseUInt8(para.Content, context, out var registerID)) + { + result.AddError(new ErrorWMsg($"{para.Content} cannot be parsed to Unsigned Integer8!", para)); + return result; + } + WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, (byte*)®isterID); + } + break; default: break; } diff --git a/src/SVM.Assembler/ISA.xml b/src/SVM.Assembler/ISA.xml index 746d481..ee37af4 100644 --- a/src/SVM.Assembler/ISA.xml +++ b/src/SVM.Assembler/ISA.xml @@ -111,5 +111,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SVM.Core/Registers.cs b/src/SVM.Core/Registers.cs index 10bc430..e63bcc0 100644 --- a/src/SVM.Core/Registers.cs +++ b/src/SVM.Core/Registers.cs @@ -44,6 +44,12 @@ namespace SVM.Core if (offset * sizeof(ulong) + sizeof(T) > Size) return; ((T*)((byte*)Data + offset * sizeof(ulong)))[0] = d; } + public unsafe void SetDataInRegister(int offset, IntPtr ptr, int size) + { + if (offset == 0) return; + if (offset * sizeof(ulong) + size > Size) return; + Buffer.MemoryCopy((byte*)ptr, (byte*)Data + offset * sizeof(ulong), size, size); + } public unsafe void SetDataOffsetInBytes(int offset, T d) where T : unmanaged { if (offset == 0) return; diff --git a/src/SVM.Core/SimpleVirtualMachine.cs b/src/SVM.Core/SimpleVirtualMachine.cs index 0300462..f470090 100644 --- a/src/SVM.Core/SimpleVirtualMachine.cs +++ b/src/SVM.Core/SimpleVirtualMachine.cs @@ -171,6 +171,15 @@ namespace SVM.Core case PrimaryInstruction.JALF: break; case PrimaryInstruction.Load: + { + var Reg = Instruction.GetData(1); + var Length = Instruction.GetData(2); + var Target = Instruction.GetData(3); + var source = registers.GetData(Reg); + var srcPtr = GetPointer(source); + registers.SetDataInRegister(Target, srcPtr, Length); + } + break; case PrimaryInstruction.Save: break;