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 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);
        }
Esempio n. 3
0
        public void assembleMacroAndSymbolsAndDefine()
        {
            var path = Path.GetTempFileName();

            System.IO.File.WriteAllText(path, defineProgram);
            var assembler       = new assembler.Assembler(path);
            var assembledResult = assembler.ConvertToBinary();

            CollectionAssert.AreEqual(assembledResult, new List <string> {
                "0x0006", //loadAimmediate
                "0x0001", //1
                "0x0005", //Store A
                "0x0110", //next location in variable space
                "0x0006", //loadAImmediate
                "0x0000", //0
                "0x0003", //add
                "0x0110", // refer to increment symbol
                "0x0002", // outA
                "0x000D", //loadBImmediate
                "0x0010", //16
                "0x000F", //update flags for jump
                "0x000A", //jump if less (A<B)
                "0x0216", //ADD_1 label location in hex
                "0x0007", //JUMP
                "0x0214", //START label location})
            }.Select(x => x.ToLower()));
        }
        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
            }));
        }
Esempio n. 5
0
        public void assemblerShouldConvertDirectMemoryAddressesCorrectly()
        {
            var path = Path.GetTempFileName();

            System.IO.File.WriteAllText(path, testVGAOutputProgram);
            var assembler       = new assembler.Assembler(path);
            var assembledResult = assembler.ConvertToBinary();

            CollectionAssert.AreEqual(assembledResult, new List <string> {
                "0x0006", //loadAimmediate
                "0x0001", //1
                "0x0005", //Store A
                "0x0110", //next location in variable space,
                "0x0006", //loadAImmediate
                "0x9C40", //40000
                "0x0003", //add
                "0x0110", //increment memory location,
                "0x0002", // outA
                "0x0005", //Store A,
                "0x0111", // pixel index next var space
                "0x0015", //load A from pointer
                "0x0111", // pixel index next var space
                "0x000D", //loadBImmediate
                "0x0000", //0
                "0x000F", //update flags for jump
                "0x0009", //jump if equal
                "0x0228", //address 24 color white
                "0x0006", //loadAImmediate
                "0x0000", //0
                "0x0014", //store a at pointer,
                "0x0111", // pixel index next var space
                "0x0007", //JUMP
                "0x022c", //done check (28)
                "0x0006", //loadAImmediate
                "0xFDE8", //65000
                "0x0014", //store a at pointer,
                "0x0111", // pixel index next var space,
                "0x000D", //loadbimm
                "0xF230", //62000,
                "0x000F", //update flags for jump
                "0x000A", //jump if less (A<B)
                "0x0216", //add 1 (6)
                "0x0007", //JUMP
                "0x0214"  // START (4)
            }.Select(x => x.ToLower()));
        }
Esempio n. 6
0
        public void symbolOffsets()
        {
            var path = Path.GetTempFileName();

            System.IO.File.WriteAllText(path, testProgramWithSymbol_Offset);
            var assembler       = new assembler.Assembler(path);
            var assembledResult = assembler.ConvertToBinary();

            CollectionAssert.AreEqual(assembledResult, new List <string> {
                "0x0006", //loadAimmediate
                "0x0001", //1
                "0x0005", //Store A
                "0x0110", //next location in variable space
                "0x0003",
                "0x0112"  //symbol + 2
            }.Select(x => x.ToLower()));
        }
        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
            }));
        }
Esempio n. 10
0
        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]);
        }
        public void pointerSimple()
        {
            var path = Path.GetTempFileName();

            System.IO.File.WriteAllText(path, pointerTest1);

            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;

            var pt1Monitor = new MonitorHandle <int>(3032, simulatorInstance.mainMemory);
            var pt2Monitor = new MonitorHandle <int>(3046, simulatorInstance.mainMemory);
            var thismon    = new MonitorHandle <int>(259, simulatorInstance.mainMemory);
            var thatmon    = new MonitorHandle <int>(260, simulatorInstance.mainMemory);

            simulatorInstance.runSimulation();
            //simulatorInstance.printMemory(0);

            var sp = simulatorInstance.mainMemory[simulatorInstance.mainMemory[256] - 1];

            //simulatorInstance.printMemory(0);
            Assert.AreEqual(6084, sp);

            var pt1values  = pt1Monitor.getValues();
            var pt2values  = pt2Monitor.getValues();
            var thisvalues = thismon.getValues();
            var thatvalues = thatmon.getValues();

            new List <List <int> >()
            {
                pt1values, pt2values, thisvalues, thatvalues
            }.ForEach(x =>
            {
                x.ForEach(y => Console.WriteLine(y));
                Console.WriteLine("---------------");
            });

            //pointers are set correctly
            //TODO I am not sure 100% sure these 27x values are correct.
            Assert.IsTrue(new int[] { 0, 272, 3030, 272, 272 }.SequenceEqual(thisvalues));
            Assert.IsTrue(new int[] { 0, 273, 3040, 273, 273 }.SequenceEqual(thatvalues));
            //Assert.AreEqual(3030, simulatorInstance.mainMemory[259]);
            //Assert.AreEqual(3040, simulatorInstance.mainMemory[260]);


            //values at pointers are correct.
            //TODO not sure how to check this or what the correct value should be because we set the
            //pointers back when returning - we may want to use monitors for these mem locations instead.
            Assert.IsTrue(new int[] { 0, 32 }.SequenceEqual(pt1values));
            Assert.IsTrue(new int[] { 0, 46 }.SequenceEqual(pt2values));
        }
        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();
        }