commit 8df9d90eea7ca889011b7ca5b3bbbe4b3ad89200 Author: Nabile Rahmani Date: Sat Apr 28 06:11:52 2018 +0200 This is bad and you should feel bad ! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e82d27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Autosave files +*~ + +# build +[Oo]bj/ +[Bb]in/ +packages/ +TestResults/ + +# globs +Makefile.in +*.DS_Store +*.sln.cache +*.suo +*.cache +*.pidb +*.userprefs +*.usertasks +config.log +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.user +*.tar.gz +tarballs/ +test-results/ +Thumbs.db + +# Mac bundle stuff +*.dmg +*.app + +# resharper +*_Resharper.* +*.Resharper + +# dotCover +*.dotCover diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f01dfef --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "DotN64"] + path = DotN64 + url = ../DotN64 diff --git a/DotN64 b/DotN64 new file mode 160000 index 0000000..ecab40e --- /dev/null +++ b/DotN64 @@ -0,0 +1 @@ +Subproject commit ecab40e39e2cc90ceb3771ca489cd362380aebe1 diff --git a/VirtualCPU.sln b/VirtualCPU.sln new file mode 100644 index 0000000..515a008 --- /dev/null +++ b/VirtualCPU.sln @@ -0,0 +1,23 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtualCPU", "VirtualCPU\VirtualCPU.csproj", "{EEA1F343-80A6-45A5-8A97-EE7B21F28CCE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotN64", "DotN64\DotN64\DotN64.csproj", "{3231B7B4-EFE7-469A-AD04-D75EDECE2AFE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EEA1F343-80A6-45A5-8A97-EE7B21F28CCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EEA1F343-80A6-45A5-8A97-EE7B21F28CCE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EEA1F343-80A6-45A5-8A97-EE7B21F28CCE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EEA1F343-80A6-45A5-8A97-EE7B21F28CCE}.Release|Any CPU.Build.0 = Release|Any CPU + {3231B7B4-EFE7-469A-AD04-D75EDECE2AFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3231B7B4-EFE7-469A-AD04-D75EDECE2AFE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3231B7B4-EFE7-469A-AD04-D75EDECE2AFE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3231B7B4-EFE7-469A-AD04-D75EDECE2AFE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/VirtualCPU/Program.cs b/VirtualCPU/Program.cs new file mode 100644 index 0000000..6085c8d --- /dev/null +++ b/VirtualCPU/Program.cs @@ -0,0 +1,33 @@ +using System; +using Mono.Unix; +using Mono.Unix.Native; +using Nancy.Hosting.Self; + +namespace VirtualCPU +{ + static class Program + { + static void Main(string[] args) + { + var address = new Uri(Environment.GetEnvironmentVariable("HOST_ADDRESS")); + var host = new NancyHost(address); + + host.Start(); + + if (Type.GetType("Mono.Runtime") != null) + { + UnixSignal.WaitAny(new[] + { + new UnixSignal(Signum.SIGINT), + new UnixSignal(Signum.SIGTERM), + new UnixSignal(Signum.SIGQUIT), + new UnixSignal(Signum.SIGHUP) + }); + } + else + Console.ReadLine(); + + host.Stop(); + } + } +} diff --git a/VirtualCPU/SampleModule.cs b/VirtualCPU/SampleModule.cs new file mode 100644 index 0000000..ad1ce37 --- /dev/null +++ b/VirtualCPU/SampleModule.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Text; +using DotN64; +using DotN64.CPU; +using DotN64.Extensions; +using DotN64.Helpers; +using Nancy; +using Nancy.Responses; + +namespace VirtualCPU +{ + public class SampleModule : NancyModule + { + private readonly TimeSpan executionTimeLimit = TimeSpan.FromSeconds(3.0); + + public SampleModule() + { + Get["/"] = _ => View["index.html"]; + Post["/"] = r => + { + var data = ((string)Request.Form["data"]).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(" ", string.Empty); + var instructionCount = Math.Min(data.Length / 8, 2048); + var instructions = new List(instructionCount); + var ram = new byte[0x0100]; + + for (int i = 0; i < instructionCount; i++) + { + if (uint.TryParse(new string(data.Skip(i * 8).Take(8).ToArray()), NumberStyles.HexNumber, null, out var instruction)) + instructions.Add(instruction); + else + return "BAD BAD BAD instruction"; + } + + var stop = false; + var maps = new[] + { + new MappingEntry(0x1FC00000, 0xC0000000) + { + Read = a => + { + var index = (int)(a / 4); + + if (index >= instructions.Count) + stop = true; + + return index < instructions.Count ? instructions[index] : 0; + } + }, + new MappingEntry(0x00000000, (uint)ram.Length) + { + Read = a => BitConverter.ToUInt32(ram, (int)a), + Write = (a, v) => BitHelper.Write(ram, (int)a, v) + } + }; + var cpu = new VR4300 + { + ReadSysAD = maps.ReadWord, + WriteSysAD = maps.WriteWord + }; + var timer = new Stopwatch(); + + cpu.Reset(); + + try + { + timer.Start(); + + while (!stop && timer.Elapsed < executionTimeLimit) + { + cpu.Cycle(); + } + + timer.Stop(); + } + catch (Exception e) + { + return $"NOT OK !!! PLEASE UNDERSTAND {e.Message}"; + } + + var memory = new StringBuilder(); + + memory.AppendLine("Memory be like:"); + memory.AppendLine(); + + for (int i = 0; i < ram.Length; i += sizeof(uint)) + { + memory.AppendLine($"{i:X2}: 0x{BitConverter.ToUInt32(ram, i):X8}"); + } + + return new TextResponse("OK !!!\n" + memory + $"\nRuntime like {timer.Elapsed}"); + }; + } + } +} diff --git a/VirtualCPU/VirtualCPU.csproj b/VirtualCPU/VirtualCPU.csproj new file mode 100644 index 0000000..3c42793 --- /dev/null +++ b/VirtualCPU/VirtualCPU.csproj @@ -0,0 +1,53 @@ + + + + Debug + AnyCPU + {EEA1F343-80A6-45A5-8A97-EE7B21F28CCE} + Exe + VirtualCPU + VirtualCPU + v4.6.1 + + + full + true + false + bin\Debug + DEBUG; + 4 + + + true + bin\Release + 4 + + + + + ..\packages\Nancy.1.4.4\lib\net40\Nancy.dll + + + ..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll + + + + + + + + + + + + PreserveNewest + + + + + {3231B7B4-EFE7-469A-AD04-D75EDECE2AFE} + DotN64 + + + + \ No newline at end of file diff --git a/VirtualCPU/index.html b/VirtualCPU/index.html new file mode 100644 index 0000000..eac8a6a --- /dev/null +++ b/VirtualCPU/index.html @@ -0,0 +1,39 @@ + + + + + MIIIIIIIIIIIIIIIIIIIIIIIIPS + + + +
+

Have fun messing around with this state-of-the-art MIPS CPU. !!!

+
+
+ +
+ +
+
+
+

Tips

+
    +
  • The bytecode gets written at the reset vector: [0xBFC00000 .. (???)]. No more than 2k instructions tho.

  • +
  • Execution time limit is 3 sec.

  • +
  • RAM is at [0x80000000 .. 0x80000100]. That's a whopping 256 bytes, just for you !!!

  • +
  • This thing is not fully implemented !!

  • +


    +
  • That's a VR4300 !

  • +
+
+ + diff --git a/VirtualCPU/packages.config b/VirtualCPU/packages.config new file mode 100644 index 0000000..5e4eec5 --- /dev/null +++ b/VirtualCPU/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file