Example #1
0
 public Interpreter(CPU cpu, Memory memory, Random random, IOutput output)
 {
     _cpu = cpu;
     _memory = memory;
     _random = random;
     _output = output;
 }
        public void TestComparison()
        {
            // Arrange
            var cpu = new CPU();

            cpu.C = 0;
            cpu.R[2] = 100;
            cpu.R[3] = 200;

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 100,
                FirstRegister = 2,
                MemoryAddress = 0,
                OpCode = OpCodes.Compare,
                SecondRegister = 3,
                ThirdRegister = 0
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            // Assert
            Assert.AreEqual(-1, cpu.C);
        }
        public void TestHalt()
        {
            // Arrange
            var cpu = new CPU();

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 100,
                FirstRegister = 0,
                MemoryAddress = 0,
                OpCode = OpCodes.Halt,
                SecondRegister = 0,
                ThirdRegister = 0
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            // Assert
            // Expected exception specified in method attribute
        }
        public void TestJumpIfGreater()
        {
            // Arrange
            var cpu = new CPU();

            cpu.C = 1;
            cpu.PC = 0;

            unchecked
            {
                cpu.R[0] = (int) 0x98513691;
            }

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 100,
                FirstRegister = 0,
                MemoryAddress = 0,
                OpCode = OpCodes.JumpIfGreater,
                SecondRegister = 0,
                ThirdRegister = 0
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            // Assert
            Assert.AreEqual(0x98513691, cpu.PC);
        }
 public VirtualMachine(CPU cpu, Memory memory, Random random, IOutput output)
 {
     _cpu = cpu;
     _memory = memory;
     _interpreter = new Interpreter(_cpu, _memory, random, output);
 }
        public void TestStoreByMem()
        {
            // Arrange
            var cpu = new CPU();

            cpu.R[0] = 0x0A0B0C0D;

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 100,
                FirstRegister = 0,
                MemoryAddress = 0x87654320,
                OpCode = OpCodes.StoreByMem,
                SecondRegister = 1,
                ThirdRegister = 2
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            var valueInMemory = memory.ReadWord(0x87654320);

            // Assert
            Assert.AreEqual(0x0A0B0C0D, valueInMemory);
        }
        public void TestRandomizedAdd()
        {
            // Arrange
            var cpu = new CPU();

            cpu.R[0] = 0;
            cpu.R[1] = 200;
            cpu.R[2] = 300;

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 100,
                FirstRegister = 0,
                MemoryAddress = 0,
                OpCode = OpCodes.RandomizedAdd,
                SecondRegister = 1,
                ThirdRegister = 2
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            // Assert
            Assert.That(cpu.R[0], Is.EqualTo(500).Within(1).Percent);
        }
        public void TestMultiplication()
        {
            // Arrange
            var cpu = new CPU();

            cpu.R[0] = 0;
            cpu.R[1] = 200;
            cpu.R[2] = 300;

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 100,
                FirstRegister = 0,
                MemoryAddress = 0,
                OpCode = OpCodes.Multiply,
                SecondRegister = 1,
                ThirdRegister = 2
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            // Assert
            Assert.AreEqual(60000, cpu.R[0]);
        }
        public void TestLogicalOrWithZeroProbability()
        {
            // Arrange
            var cpu = new CPU();

            cpu.R[10] = 0;
            cpu.R[11] = 0x02040608;
            cpu.R[12] = 0x10305070;

            var random = new Random();

            var memory = new Memory(random);

            var consoleOutput = new ConsoleOutput();

            var interpreter = new Interpreter(cpu, memory, random, consoleOutput);

            var instruction = new Instruction
            {
                ExecutionPossibility = 0,
                FirstRegister = 10,
                MemoryAddress = 0,
                OpCode = OpCodes.Or,
                SecondRegister = 11,
                ThirdRegister = 12
            };

            // Act
            interpreter.ExecuteInstruction(instruction);

            // Assert
            Assert.AreEqual(0, cpu.R[10]);
        }
Example #10
0
 private void ShowRegisters(CPU cpu)
 {
     textBox2.Text = "0x" + cpu.R[0].ToString("X8");
     textBox3.Text = "0x" + cpu.R[1].ToString("X8");
     textBox4.Text = "0x" + cpu.R[2].ToString("X8");
     textBox5.Text = "0x" + cpu.R[3].ToString("X8");
     textBox6.Text = "0x" + cpu.R[4].ToString("X8");
     textBox7.Text = "0x" + cpu.R[5].ToString("X8");
     textBox8.Text = "0x" + cpu.R[6].ToString("X8");
     textBox9.Text = "0x" + cpu.R[7].ToString("X8");
     textBox10.Text = "0x" + cpu.R[8].ToString("X8");
     textBox11.Text = "0x" + cpu.R[9].ToString("X8");
     textBox12.Text = "0x" + cpu.R[10].ToString("X8");
     textBox13.Text = "0x" + cpu.R[11].ToString("X8");
     textBox14.Text = "0x" + cpu.R[12].ToString("X8");
     textBox15.Text = "0x" + cpu.R[13].ToString("X8");
     textBox16.Text = "0x" + cpu.R[14].ToString("X8");
     textBox17.Text = "0x" + cpu.R[15].ToString("X8");
     textBox19.Text = "0x" + cpu.PC.ToString("X8");
     textBox20.Text = _cpu.C.ToString();
 }
Example #11
0
        private void NextStep(VirtualMachine vm, CPU cpu, Memory memory)
        {
            vm.NextStep();

            ShowRegisters(cpu);
        }
Example #12
0
        private void NewVirtualMachine()
        {
            _cpu = new CPU();
            var random = new Random();
            _memory = new Memory(random);

            var guiOutput = new GuiOutput(textBox18);

            _vm = new VirtualMachine(_cpu, _memory, random, guiOutput);

            _vm.LoadFromFile(textBox1.Text);

            _fileLoaded = true;

            dataGridView1.DataSource = new BindingSource(_memory._dataStore, null);

            dataGridView1.Columns[0].HeaderText = Resources.Address;
            dataGridView1.Columns[1].HeaderText = Resources.Content;

            textBox18.Text = "";

            try
            {
                ShowRegisters(_cpu);

                if (!checkBox1.Checked)
                {
                    while (true)
                    {
                        NextStep(_vm, _cpu, _memory);
                    }
                }
            }
            catch (VmHaltException)
            {
                MessageBox.Show(Resources.Virtual_machine_halted_execution);
            }
        }