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 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 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
            }));
        }
Exemplo n.º 5
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));
        }