public void staticSimple() { var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, staticTest1); var translator = new vmtranslator.vmtranslator(path); var assembly = translator.TranslateToAssembly().ToList(); assembly.Add(assembler.CommandType.HALT.ToString()); System.IO.File.WriteAllLines(path, assembly); var assemblerInstance = new assembler.Assembler(path); //assemblerInstance.logger.enabled = true; var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); //simulatorInstance.logger.enabled = true; simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; simulatorInstance.runSimulation(); //simulatorInstance.printMemory(0); var sp = simulatorInstance.mainMemory[simulatorInstance.mainMemory[256] - 1]; Assert.AreEqual(1110, sp); }
public void canJumpConditionally() { var simulator = new simulator.eightChipsSimulator(16, 1000); simulator.mainMemory[0] = 6; simulator.mainMemory[1] = 20; simulator.mainMemory[2] = 3; simulator.mainMemory[3] = 100; simulator.mainMemory[4] = 13; simulator.mainMemory[5] = 25; simulator.mainMemory[6] = 15; //conditionally jump to 2 if A < B... if A < 25 keep looping simulator.mainMemory[7] = 10; simulator.mainMemory[8] = 2; simulator.mainMemory[9] = 2; simulator.mainMemory[10] = 16; simulator.mainMemory[100] = 1; simulator.runSimulation(); Assert.AreEqual(25, simulator.OutRegister); Assert.AreEqual(25, simulator.ARegister); Assert.AreEqual(25, simulator.BRegister); }
public void complexTest() { var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, complexStack); var translator = new vmtranslator.vmtranslator(path); var assembly = translator.TranslateToAssembly().ToList(); assembly.Add(assembler.CommandType.HALT.ToString()); System.IO.File.WriteAllLines(path, assembly); var assemblerInstance = new assembler.Assembler(path); var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; //setup a monitor var handle = simulatorInstance.monitor(33049 + 5); simulatorInstance.runSimulation(); var values = handle.getValues(); values.ForEach(x => Console.WriteLine(x)); var sp = simulatorInstance.mainMemory[simulatorInstance.mainMemory[256] - 1]; Assert.AreEqual(sp, -91); }
public void PushPopArgument() { var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, pushAndPop_Argument); var writer = new testVMtoASMWriter(new int[] { 2000, 2000 }); var translator = new vmtranslator.vmtranslator(path, writer); var assembly = translator.TranslateToAssembly().ToList(); //assembly.ForEach(x => Console.WriteLine(x)); assembly.Add(assembler.CommandType.HALT.ToString()); System.IO.File.WriteAllLines(path, assembly); var assemblerInstance = new assembler.Assembler(path); var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; var stackHandle = simulatorInstance.monitor(33040); var argument0 = simulatorInstance.monitor(200); var argument1 = simulatorInstance.monitor(201); //simulatorInstance.logger.enabled = true; simulatorInstance.runSimulation(); var stackValues = stackHandle.getValues(); var argument0values = argument0.getValues(); var argument1values = argument1.getValues(); //simulatorInstance.printMemory(0); stackValues.ForEach(x => Console.WriteLine(x)); Console.WriteLine("_____"); argument0values.ForEach(x => Console.WriteLine(x)); Console.WriteLine("_____"); argument1values.ForEach(x => Console.WriteLine(x)); Console.WriteLine("_____"); var sp = simulatorInstance.mainMemory[simulatorInstance.mainMemory[256] - 1]; Assert.AreEqual(10, sp); Assert.IsTrue(stackValues.SequenceEqual(new List <int>() { 0, 638, 10 })); }
public void canHalt() { var simulator = new simulator.eightChipsSimulator(16, 1000); simulator.mainMemory[0] = 16; simulator.mainMemory[1] = 6; simulator.mainMemory[2] = 21; simulator.runSimulation(); Assert.AreEqual(0, simulator.ARegister); }
public void canJump() { var simulator = new simulator.eightChipsSimulator(16, 1000); simulator.mainMemory[0] = 6; simulator.mainMemory[1] = 20; simulator.mainMemory[2] = 15; simulator.mainMemory[3] = 7; simulator.mainMemory[4] = 6; simulator.mainMemory[5] = 2; simulator.mainMemory[6] = 16; simulator.runSimulation(); Assert.AreEqual(0, simulator.OutRegister); Assert.AreEqual(20, simulator.ARegister); }
public void popLocalSimple() { var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, popLocalSimpleProgram); var translator = new vmtranslator.vmtranslator(path); var assembly = translator.TranslateToAssembly().ToList(); assembly.Add(assembler.CommandType.HALT.ToString()); System.IO.File.WriteAllLines(path, assembly); assembly.ForEach(x => Console.WriteLine(x)); var assemblerInstance = new assembler.Assembler(path); var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; //setup monitors: var stackHandle = simulatorInstance.monitor(33040 + 5); simulatorInstance.logger.enabled = true; simulatorInstance.runSimulation(); var stackValues = stackHandle.getValues(); simulatorInstance.printMemory(0); stackValues.ForEach(x => Console.WriteLine(x)); Console.WriteLine("_____"); Assert.IsTrue(stackValues.SequenceEqual(new List <int>() { 0, 725, 2, })); }
public void multiVMFilesTest() { var directory = Path.Combine(Path.GetTempPath(), "TEMPVMFILES"); System.IO.Directory.CreateDirectory(directory); System.IO.Directory.Delete(directory, true); System.IO.Directory.CreateDirectory(directory); var path1 = Path.Combine(directory, "sys.vm"); var path2 = Path.Combine(directory, "main.vm"); var outputAssemblyPath = Path.Combine(directory, "out.asm"); System.IO.File.WriteAllText(path1, sysvm); System.IO.File.WriteAllText(path2, mainvm); var translator = new vmtranslator.vmtranslator(directory); //translator.logger.enabled = true; var assembly = translator.TranslateToAssembly().ToList(); assembly.Add(assembler.CommandType.HALT.ToString()); assembly.Select <string, string>((x, i) => { Console.WriteLine($"{i + (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart}:{x}"); return(x); }).ToList(); System.IO.File.WriteAllLines(outputAssemblyPath, assembly); var assemblerInstance = new assembler.Assembler(outputAssemblyPath); var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); // simulatorInstance.logger.enabled = true; simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; assemblerInstance.symbolTable.ToList().ForEach(kv => Console.WriteLine($"{kv.Key} : {kv.Value}")); simulatorInstance.runSimulation(); //simulatorInstance.printMemory(0); //need to check what the SP points to Assert.AreEqual(13, simulatorInstance.mainMemory[simulatorInstance.mainMemory[256] - 1]); }
public void NOTTest() { var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, NOTtestprogram1); var translator = new vmtranslator.vmtranslator(path); var assembly = translator.TranslateToAssembly().ToList(); assembly.Add(assembler.CommandType.HALT.ToString()); System.IO.File.WriteAllLines(path, assembly); var assemblerInstance = new assembler.Assembler(path); var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); simulatorInstance.logger.enabled = true; simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; //setup a monitor var handle = simulatorInstance.monitor(33040 + 5); simulatorInstance.runSimulation(); var values = handle.getValues(); values.ForEach(x => Console.WriteLine(x)); //1 bitwise negated in 2's complement is -2 Assert.IsTrue(values.SequenceEqual(new List <int>() { 0, 725, -2 })); }
public void fibSeries() { var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, fibTest); var testWriter = new testVMtoASMWriter(new int[2] { 7, 3000 }); var translator = new vmtranslator.vmtranslator(path, testWriter); var assembly = translator.TranslateToAssembly().ToList(); assembly.Add(assembler.CommandType.HALT.ToString()); System.IO.File.WriteAllLines(path, assembly); var assemblerInstance = new assembler.Assembler(path); var assembledResult = assemblerInstance.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); var simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); simulatorInstance.logger.enabled = true; simulatorInstance.setUserCode(binaryProgram.ToArray()); simulatorInstance.ProgramCounter = (int)MemoryMap[MemoryMapKeys.user_code].AbsoluteStart; simulatorInstance.mainMemory[200] = 7; simulatorInstance.mainMemory[201] = 3000; simulatorInstance.runSimulation(); Assert.AreEqual(0, simulatorInstance.mainMemory[3000]); Assert.AreEqual(1, simulatorInstance.mainMemory[3001]); Assert.AreEqual(1, simulatorInstance.mainMemory[3002]); Assert.AreEqual(2, simulatorInstance.mainMemory[3003]); Assert.AreEqual(3, simulatorInstance.mainMemory[3004]); Assert.AreEqual(5, simulatorInstance.mainMemory[3005]); Assert.AreEqual(8, simulatorInstance.mainMemory[3006]); }
static void Main(string[] args) { //AppDomain.CurrentDomain.ProcessExit+= (s,e)=>{} var path = Path.GetTempFileName(); System.IO.File.WriteAllText(path, testVGAOutputProgram); var assemblerInst = new assembler.Assembler(path); var assembledResult = assemblerInst.ConvertToBinary(); var binaryProgram = assembledResult.Select(x => Convert.ToInt32(x, 16)); //lets convert our final assembled program back to assembly instructions so we can view it. //dissasembly) var assembler2 = new assembler.Assembler(path); expandedCode = assembler2.ExpandMacros().ToArray(); simulatorInstance = new simulator.eightChipsSimulator(16, (int)Math.Pow(2, 16)); simulatorInstance.setUserCode(binaryProgram.ToArray()); //TODO use cancellation token here. simulationThread = Task.Run(() => { simulatorInstance.ProgramCounter = (int)assembler.Assembler.MemoryMap[assembler.Assembler.MemoryMapKeys.user_code].AbsoluteStart; simulatorInstance.runSimulation(); }); // Create window, GraphicsDevice, and all resources necessary for the demo. VeldridStartup.CreateWindowAndGraphicsDevice( new WindowCreateInfo(50, 50, 1280, 720, WindowState.Normal, "8 chips simulator 2"), new GraphicsDeviceOptions(true, null, true), out mainWindow, out gd); mainWindow.Resized += () => { gd.MainSwapchain.Resize((uint)mainWindow.Width, (uint)mainWindow.Height); controller.WindowResized(mainWindow.Width, mainWindow.Height); }; commandList = gd.ResourceFactory.CreateCommandList(); controller = new Veldrid.ImGuiRenderer(gd, gd.MainSwapchain.Framebuffer.OutputDescription, mainWindow.Width, mainWindow.Height); var data = simulatorInstance.mainMemory.Select(x => convertShortFormatToFullColor(Convert.ToInt32(x).ToBinary())).ToArray(); //try creating an texture and binding it to an image which imgui will draw... //we'll need to modify this image every frame potentially... var texture = gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled)); CPUframeBufferTextureId = controller.GetOrCreateImGuiBinding(gd.ResourceFactory, texture); gd.UpdateTexture(texture, data, 0, 0, 0, width, height, 1, 0, 0); textureMap.Add(CPUframeBufferTextureId, texture); /* * var state = new object(); * var timer = new System.Threading.Timer((o) => * { * int[] newdata = simulatorInstance.mainMemory.Select(x => convertShortFormatToFullColor(x)).ToArray(); * var currenttexture = textureMap[CPUframeBufferTextureId]; * gd.UpdateTexture(currenttexture, newdata, 0, 0, 0, 256, 256, 1, 0, 0); * }, state, 1000, 150); * */ // Main application loop while (mainWindow.Exists) { InputSnapshot snapshot = mainWindow.PumpEvents(); if (!mainWindow.Exists) { break; } controller.Update(1f / 60f, snapshot); // Feed the input events to our ImGui controller, which passes them through to ImGui. SubmitUI(); commandList.Begin(); commandList.SetFramebuffer(gd.MainSwapchain.Framebuffer); commandList.ClearColorTarget(0, new RgbaFloat(.1f, .1f, .1f, .2f)); controller.Render(gd, commandList); commandList.End(); gd.SubmitCommands(commandList); gd.SwapBuffers(gd.MainSwapchain); } // Clean up Veldrid resources gd.WaitForIdle(); controller.Dispose(); commandList.Dispose(); gd.Dispose(); }
private static void SubmitUI() { // Demo code adapted from the official Dear ImGui demo program: // https://github.com/ocornut/imgui/blob/master/examples/example_win32_directx11/main.cpp#L172 // 1. Show a simple window. // Tip: if we don't call ImGui.BeginWindow()/ImGui.EndWindow() the widgets automatically appears in a window called "Debug". ImGui.Begin("8Chips Simulator"); { ImGui.StyleColorsDark(); ImGui.PushStyleColor(ImGuiCol.Border, new Vector4(.2f, 1f, .4f, 1f)); ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(.5f, 1f, .7f, 1f)); ImGui.SetWindowFontScale(1.5f); ImGui.Text("Hello, world!"); // Display some text (you can use a format string too) ImGui.SameLine(0, -1); float framerate = ImGui.GetIO().Framerate; ImGui.NewLine(); ImGui.Text($"Total CPU Instruction Count {simulatorInstance.TotalInstructionCount}"); ImGui.Begin("Registers: "); ImGui.SetWindowFontScale(1.5f); simulatorInstance.Registers.ToList().ForEach(x => { ImGui.Text($"{x.Key} : {x.Value}"); }); ImGui.End(); if (ImGui.Checkbox("Halt CPU", ref halted)) { simulatorInstance.HALT = halted; if (!halted) { Task.Run(() => { simulatorInstance.runSimulation(); }); } } if (ImGui.SliderInt("CPU speed", ref cpuSpeed, 1, 100000)) { simulatorInstance.instructionBundleSize = cpuSpeed; } ImGui.Begin("Assembly"); ImGui.Text("Expanded Assembly"); ImGui.ListBox("", ref currentProgramLine, expandedCode, expandedCode.Length, expandedCode.Length); //TODO I think this is going to be offset incorrectly based on how many labels were removed during expansion... currentProgramLine = simulatorInstance.ProgramCounter - assembler.Assembler.MemoryMap[assembler.Assembler.MemoryMapKeys.user_code].AbsoluteStart; int[] data = simulatorInstance.mainMemory.Select(x => convertShortFormatToFullColor(Convert.ToInt32(x).ToBinary())).ToArray(); var texture = textureMap[CPUframeBufferTextureId]; gd.UpdateTexture(texture, data, 0, 0, 0, width, height, 1, 0, 0); ImGui.Begin("FrameBuffer_Window"); ImGui.Text("FrameBuffer"); ImGui.Image(CPUframeBufferTextureId, new Vector2(width * 2, height * 2)); ImGui.End(); } ImGuiIOPtr io = ImGui.GetIO(); }