This is bad and you should feel bad !

master
Nabile Rahmani 2018-04-28 06:11:52 +02:00
commit 8df9d90eea
9 changed files with 295 additions and 0 deletions

40
.gitignore vendored Normal file
View File

@ -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

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "DotN64"]
path = DotN64
url = ../DotN64

1
DotN64 Submodule

@ -0,0 +1 @@
Subproject commit ecab40e39e2cc90ceb3771ca489cd362380aebe1

23
VirtualCPU.sln Normal file
View File

@ -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

33
VirtualCPU/Program.cs Normal file
View File

@ -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();
}
}
}

View File

@ -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<VR4300.Instruction>(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}");
};
}
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EEA1F343-80A6-45A5-8A97-EE7B21F28CCE}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>VirtualCPU</RootNamespace>
<AssemblyName>VirtualCPU</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Nancy">
<HintPath>..\packages\Nancy.1.4.4\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Hosting.Self">
<HintPath>..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll</HintPath>
</Reference>
<Reference Include="Mono.Posix" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="SampleModule.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotN64\DotN64\DotN64.csproj">
<Project>{3231B7B4-EFE7-469A-AD04-D75EDECE2AFE}</Project>
<Name>DotN64</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

39
VirtualCPU/index.html Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>MIIIIIIIIIIIIIIIIIIIIIIIIPS</title>
<style>
textarea {
background: black;
color: green;
font-size: 2rem;
}
</style>
</head>
<body>
<div align="center">
<h1>Have fun messing around with this state-of-the-art MIPS CPU. !!!</h1>
<hr>
<form action="" method="post">
<textarea name="data" cols="8" rows="16">
2008BABE
3C018000
AC280000</textarea>
<br>
<input type="submit" value="Run in the N I N E T I E S">
</form>
</div>
<section>
<h2>Tips</h2>
<ul>
<li><p>The bytecode gets written at the reset vector: [0xBFC00000 .. (???)]. No more than 2k instructions tho.</p></li>
<li><p>Execution time limit is 3 sec.</p></li>
<li><p>RAM is at [0x80000000 .. 0x80000100]. That's a whopping 256 bytes, just for you !!!</p></li>
<li><p>This thing is not fully implemented !!</p></li>
<br><br><br>
<li><p>That's a VR4300 !</p></li>
</ul>
</section>
</body>
</html>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nancy" version="1.4.4" targetFramework="net461" />
<package id="Nancy.Hosting.Self" version="1.4.1" targetFramework="net461" />
</packages>