public ControlUnitOutput Do(Byte2 data, bool clock)
        {
            var decodedInstruction = InstructionDecoder.Decode(data);

            var selectedSourceMemory =
                Select16.Do(decodedInstruction.SourceMemory,
                            _memoryOutput.Ram,
                            _memoryOutput.A);

            _aluOutput =
                ArithmeticLogicUnit.Do(decodedInstruction.Operation,
                                       _memoryOutput.D,
                                       selectedSourceMemory);

            _selectedBasedOnComputationInstruction =
                Select16.Do(decodedInstruction.ComputationInstruction,
                            _aluOutput,
                            decodedInstruction.w);

            _memoryOutput = _memory.Do(decodedInstruction.Destination,
                                       _selectedBasedOnComputationInstruction,
                                       clock);

            var isCondition =
                ArithmeticLogicUnit.Evaluate(decodedInstruction.Condition,
                                             _aluOutput);

            return(new ControlUnitOutput(isCondition, _memoryOutput.A));
        }
        public void ValidateSerialNumber(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12, int i13, int i14, int expectedW, int expectedX, int expectedY, int expectedZ)
        {
            var sut = new ArithmeticLogicUnit(REAL_INSTRUCTIONS);

            sut.Input(i1);
            sut.Input(i2);
            sut.Input(i3);
            sut.Input(i4);
            sut.Input(i5);
            sut.Input(i6);
            sut.Input(i7);
            sut.Input(i8);
            sut.Input(i9);
            sut.Input(i10);
            sut.Input(i11);
            sut.Input(i12);
            sut.Input(i13);
            sut.Input(i14);
            sut.Run();

            Assert.Equal(expectedW, sut.W);
            Assert.Equal(expectedX, sut.X);
            Assert.Equal(expectedY, sut.Y);
            Assert.Equal(expectedZ, sut.Z);
        }
        public void AddZregisterToItselfCorrectly()
        {
            var sut = new ArithmeticLogicUnit("inp z\nadd z z");

            sut.Input(12);
            sut.Run();
            Assert.Equal(24, sut.Z);
        }
        public void MoveInputValueToRegisterW()
        {
            var sut = new ArithmeticLogicUnit("inp w");

            sut.Input(7);
            sut.Run();
            Assert.Equal(7, sut.W);
        }
        public void MoveInputValueToRegisterX()
        {
            var sut = new ArithmeticLogicUnit("inp x");

            sut.Input(5);
            sut.Run();
            Assert.Equal(5, sut.X);
        }
        public void MoveInputValueToRegisterY()
        {
            var sut = new ArithmeticLogicUnit("inp y");

            sut.Input(9);
            sut.Run();
            Assert.Equal(9, sut.Y);
        }
        public void MoveInputValueToRegisterZ()
        {
            var sut = new ArithmeticLogicUnit("inp z");

            sut.Input(3);
            sut.Run();
            Assert.Equal(3, sut.Z);
        }
        public void ReturnOne_WhenDividingZbyItself()
        {
            var sut = new ArithmeticLogicUnit("inp z\ndiv z z");

            sut.Input(12);
            sut.Run();
            Assert.Equal(1, sut.Z);
        }
        public void DivideRegisterYcorrectly(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(12);
            sut.Input(6);
            sut.Run();
            Assert.Equal(2, sut.Y);
        }
        public void VerifyWhetherAvalueIsTripleTheOtherWithRegisterW(string instructions, int firstInput, int secondInput, int expectedResult)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(firstInput);
            sut.Input(secondInput);
            sut.Run();
            Assert.Equal(expectedResult, sut.W);
        }
        public void AddRegisterZcorrectly(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(5);
            sut.Input(6);
            sut.Run();
            Assert.Equal(11, sut.Z);
        }
        public void LeaveRemainderInRegisterZcorrectly(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(12);
            sut.Input(7);
            sut.Run();
            Assert.Equal(5, sut.Z);
        }
        public void SetYto0_WhenValuesAreNotEqual(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(6);
            sut.Input(5);
            sut.Run();
            Assert.Equal(0, sut.Y);
        }
        public void MultiplyRegisterZcorrectly(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(3);
            sut.Input(3);
            sut.Run();
            Assert.Equal(9, sut.Z);
        }
        public void NegateInputNumberStoredInX(int input, int expectedX)
        {
            var sut = new ArithmeticLogicUnit(@"inp x
mul x -1");

            sut.Input(input);
            sut.Run();
            Assert.Equal(expectedX, sut.X);
        }
        public void SetXto1_WhenValuesAreEqual(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(6);
            sut.Input(6);
            sut.Run();
            Assert.Equal(1, sut.X);
        }
        public void ALUMultiply_Multiplies32BitIntegers(Int32 first, Int32 second, Int32 expected)
        {
            // Arrange
            var alu = new ArithmeticLogicUnit();

            // Act
            Int32 result = alu.Multiply(first, second);

            // Assert
            result.Should().Be(expected);
        }
        public void LeaveZeroAsRemainder_WhenSameRegisterIsUsedForMod(string instructions)
        {
            var sut = new ArithmeticLogicUnit(instructions);

            sut.Input(6);
            sut.Run();
            Assert.Equal(0, sut.W);
            Assert.Equal(0, sut.X);
            Assert.Equal(0, sut.Y);
            Assert.Equal(0, sut.Z);
        }
        public void RunFirstSectionOfMonadCode(int inputValue, int expectedW, int expectedX, int expectedY, int expectedZ)
        {
            var sut = new ArithmeticLogicUnit(ONE_LOOP_INSTRUCTION);

            sut.Input(inputValue);
            sut.Run();
            Assert.Equal(expectedW, sut.W);
            Assert.Equal(expectedX, sut.X);
            Assert.Equal(expectedY, sut.Y);
            Assert.Equal(expectedZ, sut.Z);
        }
Exemple #20
0
        public void Bitwise_complement_y_should_return_negative_y_minus_1()
        {
            // Arrange
            var opcode = Opcodes.ComplementY();

            // Act
            var output = ArithmeticLogicUnit.Do(opcode, new Byte2(27), new Byte2(42));

            // Assert
            output.ToInt16().Should().Be(-43);
        }
Exemple #21
0
        public void XorY_should_return_bitwise_or()
        {
            // Arrange
            var opcodeX = Opcodes.XorY();

            // Act
            var output = ArithmeticLogicUnit.Do(opcodeX, new Byte2("1000000000000001"), new Byte2("0100000000000011"));

            // Assert
            output.ToString().Should().Be("1100000000000011");
        }
Exemple #22
0
        public void Add_should_return_x_plus_y()
        {
            // Arrange
            var opcode = Opcodes.Add();

            // Act
            var output = ArithmeticLogicUnit.Do(opcode, new Byte2(27), new Byte2(45));

            // Assert
            output.ToInt16().Should().Be(72);
        }
Exemple #23
0
        public void X_should_return_X()
        {
            // Arrange
            var opcodeX = Opcodes.X();

            // Act
            var output = ArithmeticLogicUnit.Do(opcodeX, new Byte2(42), new Byte2(127));

            // Assert
            output.ToInt16().Should().Be(42);
        }
        [InlineData(9, 9, 9, 1, 21, 645)]   // 99 z = 645
        public void RunSecondSectionOfMonadCode(int firstInput, int secondInput, int expectedW, int expectedX, int expectedY, int expectedZ)
        {
            var sut = new ArithmeticLogicUnit(TWO_LOOP_INSTRUCTIONS);

            sut.Input(firstInput);
            sut.Input(secondInput);
            sut.Run();
            Assert.Equal(expectedW, sut.W);
            Assert.Equal(expectedX, sut.X);
            Assert.Equal(expectedY, sut.Y);
            Assert.Equal(expectedZ, sut.Z);
        }
        public void Test6()
        {
            // Arrange
            var byte2X    = new Byte2(1);
            var condition = new Condition(true, false, false);

            // Act
            var output = ArithmeticLogicUnit.Evaluate(condition, byte2X);

            // Assert
            output.Should().BeFalse();
        }
Exemple #26
0
        public void Test6()
        {
            // Arrange
            var byte2X    = new Byte2(27);
            var byte2Y    = new Byte2(-16);
            var operation = new InstructionDecoder.Operation(false, false, true, true, false, true);

            // Act
            var output = ArithmeticLogicUnit.Do(operation, byte2X, byte2Y);

            // Assert
            output.ToUInt16().Should().Be(65508);
        }
Exemple #27
0
        public void TestModuloNonZeroRemainder()
        {
            int                 inputA   = 13;
            int                 inputB   = 5;
            const int           EXPECTED = 3;
            ArithmeticLogicUnit alu      = new ArithmeticLogicUnit()
            {
                InputA = inputA,
                InputB = inputB
            };

            alu.Modulo();
            Assert.AreEqual(EXPECTED, alu.Output);
        }
Exemple #28
0
        public void TestSubtractNegative()
        {
            int                 inputA   = -3;
            int                 inputB   = -2;
            const int           EXPECTED = -1;
            ArithmeticLogicUnit alu      = new ArithmeticLogicUnit()
            {
                InputA = inputA,
                InputB = inputB
            };

            alu.Subtract();
            Assert.AreEqual(EXPECTED, alu.Output);
        }
Exemple #29
0
        public void TestDivisionNegative()
        {
            int                 inputA   = -10;
            int                 inputB   = -5;
            const int           EXPECTED = 2;
            ArithmeticLogicUnit alu      = new ArithmeticLogicUnit()
            {
                InputA = inputA,
                InputB = inputB
            };

            alu.Divide();
            Assert.AreEqual(EXPECTED, alu.Output);
        }
Exemple #30
0
        public void TestMultplicationMixed()
        {
            int                 inputA   = -2;
            int                 inputB   = 3;
            const int           EXPECTED = -6;
            ArithmeticLogicUnit alu      = new ArithmeticLogicUnit()
            {
                InputA = inputA,
                InputB = inputB
            };

            alu.Multiply();
            Assert.AreEqual(EXPECTED, alu.Output);
        }
Exemple #31
0
        public void TestSubtractPositive()
        {
            int                 inputA   = 10;
            int                 inputB   = 5;
            const int           EXPECTED = 5;
            ArithmeticLogicUnit alu      = new ArithmeticLogicUnit()
            {
                InputA = inputA,
                InputB = inputB
            };

            alu.Subtract();
            Assert.AreEqual(EXPECTED, alu.Output);
        }