diff --git a/SVM/Instructions/CALL.cs b/SVM/Instructions/CALL.cs index af0dd03..e0965da 100644 --- a/SVM/Instructions/CALL.cs +++ b/SVM/Instructions/CALL.cs @@ -44,7 +44,8 @@ namespace SVM.Instructions Debug.Assert(vm.SP < VM.STACKDEPTH); vm.STACK[vm.SP++] = vm.PC; - vm.PC = (ushort)((vars[0] << 8) + vars[1]); + var jmp = (ushort)((vars[0] << 8) + vars[1]); + vm.PC = jmp; } public override string ToASM(byte[] vars) diff --git a/SVM/Instructions/MUL.cs b/SVM/Instructions/MUL.cs index a6f18e5..23a00a7 100644 --- a/SVM/Instructions/MUL.cs +++ b/SVM/Instructions/MUL.cs @@ -6,7 +6,7 @@ namespace SVM.Instructions { class MUL : ADD { - public override string ASM => "SUB"; + public override string ASM => "MUL"; public override byte OP => 0x24; diff --git a/SVM/Instructions/RET.cs b/SVM/Instructions/RET.cs index a632f02..f87116d 100644 --- a/SVM/Instructions/RET.cs +++ b/SVM/Instructions/RET.cs @@ -19,10 +19,11 @@ namespace SVM.Instructions public override void Exec(VM vm, byte[] vars) { Debug.Assert(vars.Length == 0); - Debug.Assert(vm.SP >= 0); + Debug.Assert(vm.SP > 0); + vm.SP--; vm.PC = vm.STACK[vm.SP]; - vm.STACK[vm.SP--] = 0; + vm.STACK[vm.SP] = 0; } public override string ToASM(byte[] vars) diff --git a/SVM/PGM/CALL.txt b/SVM/PGM/CALL.txt new file mode 100644 index 0000000..89d79c2 --- /dev/null +++ b/SVM/PGM/CALL.txt @@ -0,0 +1,29 @@ +ORIGIN 0 +JMP :PGM + +:WRITESTR +SETI A +:WRITESTR_LOOP +LOADI A +JZ A :WRITESTR_RET +SAVE A P 0 +INCI +JMP :WRITESTR_LOOP +:WRITESTR_RET +RET + +:WRITENL +LOAD A L 13 +SAVE A P 0 +LOAD A L 10 +SAVE A P 0 +RET + +:PGM +LOAD A L 0x100 +CALL :WRITESTR +CALL :WRITENL +HALT + +MEMORY +0x100 "CALL/RET Test" \ No newline at end of file diff --git a/SVM/PGM/A.txt b/SVM/PGM/HELLO.txt similarity index 100% rename from SVM/PGM/A.txt rename to SVM/PGM/HELLO.txt diff --git a/SVM/Program.cs b/SVM/Program.cs index 00a412b..fcd80d5 100644 --- a/SVM/Program.cs +++ b/SVM/Program.cs @@ -7,8 +7,18 @@ namespace SVM { static void Main(string[] args) { + if (args.Length == 0) + { + Console.WriteLine("Usage: dotnet SVN.dll [Filename]"); + return; + } var file = Path.Combine(Environment.CurrentDirectory, args[0]); + if (!File.Exists(file)) + { + Console.WriteLine("Error: File Not Found"); + } + var content = File.ReadAllText(file); Console.WriteLine("Assembling {0}", file); @@ -16,7 +26,8 @@ namespace SVM var asm = new Assembler(); var mem = asm.Compile(content); - + Console.WriteLine("Compiled to {0} bytes", mem.Length); + var vm = new VM(); vm.CycleDelay = 25; vm.Load(mem, 0); diff --git a/SVM/SVM.csproj b/SVM/SVM.csproj index 96924e4..998612e 100644 --- a/SVM/SVM.csproj +++ b/SVM/SVM.csproj @@ -3,14 +3,20 @@ Exe netcoreapp2.0 + + - + + - + + PreserveNewest + + PreserveNewest diff --git a/SVM/VM.cs b/SVM/VM.cs index 93e1d07..5e82eee 100644 --- a/SVM/VM.cs +++ b/SVM/VM.cs @@ -69,14 +69,13 @@ namespace SVM { if (RUN) { - //Get op var pc = PC; var instr = instructions[MEM[PC++]]; byte[] decoded = instr.Decode(this); - Console.WriteLine("A{0} B{1} C{2} D{3}", R[0], R[1], R[2], R[3]); - Console.WriteLine("0x{0:X4} {1}", pc, instr.ToASM(decoded)); + //Console.WriteLine("A{0} B{1} C{2} D{3}", R[0], R[1], R[2], R[3]); + //Console.WriteLine("0x{0:X4} {1}", pc, instr.ToASM(decoded)); instr.Exec(this, decoded); - Console.ReadKey(true); + //Console.ReadKey(true); } } }