Ejemplo n.º 1
0
        public void Add_20_and_10_40expected_30returned_test_fail()
        {
            MathHelper helper = new MathHelper();
            int        result = helper.Add(20, 10);

            Assert.AreEqual(40, result);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var mathLibrary = new MathHelper();

            var n1 = 15;
            var n2 = 4;

            var shuma = mathLibrary.Add(n1, n2);

            Console.WriteLine($"Shuma e numrave {n1} dhe {n2} eshte {shuma}");

            var diferenca = mathLibrary.Sub(n1, n2);

            Console.WriteLine($"Diferenca e numrave {n1} dhe {n2} eshte {diferenca}");

            var produkti = mathLibrary.Multiply(n1, n2);

            Console.WriteLine($"Produkti e numrave {n1} dhe {n2} eshte {produkti}");

            var pjestimi = mathLibrary.Devide(n1, n2);

            Console.WriteLine($"Pjestimi e numrave {n1} dhe {n2} eshte {pjestimi}");

            Console.WriteLine("Hello World!");
        }
Ejemplo n.º 3
0
        public void MathTest()
        {
            Assert.AreEqual(MathHelper.Add(1, 1), 2);
            Assert.AreEqual(MathHelper.Subtract(1, 1), 0);

            Assert.AreEqual(MathHelper.Multiply(2, 2), 4);
            Assert.AreEqual(MathHelper.Divide(10, 5), 2);
        }
Ejemplo n.º 4
0
        public void WhenBothOperandsArePositive_ReturnsCorrectResult()
        {
            var sut = new MathHelper();

            var result = sut.Add(1, 2);

            Assert.Equal(3, result);
        }
Ejemplo n.º 5
0
        public void TestAddWithVariousInputs(int op1, int op2, int expectedResult)
        {
            var sut = new MathHelper();

            var result = sut.Add(op1, op2);

            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 6
0
        public override bool Execute(Instruction instruction, InstructionPayload payload)
        {
            var rd              = payload.Rd;
            var rs1             = payload.Rs1;
            var signedImmediate = payload.SignedImmediate;
            var rs1Value        = Register.ReadUnsignedInt(rs1);

            Logger.Info("Opcode00 : rd = {rd}, rs1 = {rs1}", rd, rs1);

            IEnumerable <byte> buffer;

            byte[] result;

            var memoryAddress = MathHelper.Add(rs1Value, signedImmediate);

            switch (payload.Funct3)
            {
            case lh:
                // LH loads a 16-bit value from memory, then sign-extends to 32 - bits before storing in rd.
                buffer = Memory.GetHalfWord(memoryAddress);
                result = MathHelper.PrepareLoad(buffer.ToArray(), 4, true);
                break;

            case lhu:
                // LHU loads a 16-bit value from memory but then zero extends to 32 - bits before storing in rd.
                buffer = Memory.GetHalfWord(memoryAddress);
                result = MathHelper.PrepareLoad(buffer.ToArray(), 4, true);
                break;

            case lb:
                buffer = Memory.GetByte(memoryAddress);
                result = MathHelper.PrepareLoad(buffer.ToArray(), 4, true);
                break;

            case lbu:
                buffer = Memory.GetByte(memoryAddress);
                result = MathHelper.PrepareLoad(buffer.ToArray(), 4, true);
                break;

            case lw:
                buffer = Memory.GetWord(memoryAddress);
                result = buffer.ToArray();
                break;

            case lwu:
                buffer = Memory.GetWord(memoryAddress);
                result = buffer.ToArray();
                break;

            default:
                throw new OpCodeNotSupportedException(String.Format("OpCode = {0}, Funct3 = {1}", instruction.OpCode, payload.Funct3));
            }

            Register.WriteBlock(rd, result);

            return(true);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            MathHelper calculator = new MathHelper();
            int        r          = calculator.Add(3, 7);
            int        R          = calculator.Subtract(r, -7);

            Console.WriteLine($"3+7={0}10-7={1}"
                              + Environment.NewLine + "Press ane key...", r, R);
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        private void Jump(InstructionPayload payload)
        {
            var immediate = payload.SignedImmediate;
            var pcIndex   = Register.ProgramCounter;

            var pc = Register.ReadUnsignedLong(pcIndex);
            //var newPc = pc + immediate;
            var newPc = MathHelper.Add(pc, immediate);

            Register.WriteUnsignedLong(pcIndex, newPc);
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            MathHelper calculator = new MathHelper();
            int        r          = calculator.Add(3, 7);
            int        R          = calculator.Subtract(r, -7);

            Console.WriteLine($"3 + 7 = {r}; {r} - (-7) = {R}");

            Console.WriteLine("Press any key to exit");
            Console.ReadKey(true);
        }
Ejemplo n.º 10
0
        public void when_adding_two_number_correct_result_must_be_returned()
        {
            var n1 = 34;
            var n2 = 23;

            var mathHelper = new MathHelper();

            var shuma = mathHelper.Add(n1, n2);

            Assert.AreEqual(57, shuma);
        }
Ejemplo n.º 11
0
        public void Add_20_10_returned_30()
        {
            int a        = 20;
            int b        = 10;
            int expected = 30;

            MathHelper mathHelper = new MathHelper();
            int        result     = mathHelper.Add(a, b);

            Assert.AreEqual(result, expected);
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            decimal value1;
            decimal value2;
            decimal result = 0;
            string  operation;

            Console.WriteLine("Enter first number: ");
            value1 = Convert.ToDecimal(Console.ReadLine());
            Console.WriteLine("Enter second number: ");
            value2 = Convert.ToDecimal(Console.ReadLine());

            Console.WriteLine("Select operation: ");
            Console.WriteLine(" Addition(+)");
            Console.WriteLine(" Subtration(-)");
            Console.WriteLine(" Multiplication(*)");
            Console.WriteLine(" Division(/)");

            operation = Console.ReadLine();
            switch (operation)
            {
            case "+":
                result = MathHelper.Add(value1, value2);
                result = value1 + value2;
                break;

            case "-":
                result = MathHelper.Subtract(value1, value2);
                //result = value1 - value2;
                break;

            case "*":
                result = MathHelper.Multiplication(value1, value2);
                // result = value1 * value2;
                break;

            case "/":
                if (value2 != 0)
                {
                    result = MathHelper.Division(value1, value2);
                    //result = value1 / value2;
                }
                break;
            }
            if (value2 == 0 && operation == "/")
            {
                Console.WriteLine("Not a number");
            }
            else
            {
                Console.WriteLine("Result: {0}", result);
            }
        }
Ejemplo n.º 13
0
        public override bool Execute(Instruction instruction, InstructionPayload payload)
        {
            var rs2 = payload.Rs2;
            var rs1 = payload.Rs1;

            var rs1Value = Register.ReadUnsignedInt(rs1);

            // memory address = s1 + immediate
            var rs2Block = Register.ReadBlock(rs2);
            //var memoryAddress = Convert.ToUInt32(rs1Value + payload.SignedImmediate);
            var memoryAddress = MathHelper.Add(rs1Value, payload.SignedImmediate);

            var list = new List <byte>();

            Logger.Info("Opcode 08 : rs1 = {rs1}, rs2 = {rs2}, immediate = {imm}", rs1, rs2, payload.SignedImmediate);

            switch (payload.Funct3)
            {
            case sb:
                // sb copies only the lowest 8 Bit
                list.Add(rs2Block.First());
                break;

            case sh:
                // sh copies only the lowest 16 Bit
                list.Add(rs2Block.First());
                list.Add(rs2Block.ElementAt(1));
                break;

            case sw:
                // sw copies only the lowest 32 Bit
                list.Add(rs2Block.First());
                list.Add(rs2Block.ElementAt(1));
                list.Add(rs2Block.ElementAt(2));
                list.Add(rs2Block.ElementAt(3));
                break;

            case sd:
                list.AddRange(rs2Block);
                break;

            // Error
            default:
                throw new OpCodeNotSupportedException(String.Format("OpCode = {0}, Funct3 = {1}", instruction.OpCode, payload.Funct3));
            }


            Memory.Write(memoryAddress, list);

            return(true);
        }
Ejemplo n.º 14
0
        private ulong CalculateAddress(InstructionPayload payload)
        {
            ulong address;
            var   immediate = payload.SignedImmediate;
            int   rd        = payload.Rd;
            var   rs1       = payload.Rs1;
            var   rs1Value  = Register.ReadUnsignedLong(rs1);

            // Make the address a multiplier by 2 !
            //address = rs1Value + immediate;
            address = MathHelper.Add(rs1Value, immediate);
            var rest = address % 2;

            if (rest == 1)
            {
                address -= 1;
            }

            return(address);
        }
Ejemplo n.º 15
0
 public static int Add(int a, int b)
 {
     return(MathHelper.Add(a, b));
 }
Ejemplo n.º 16
0
 public int Add()
 {
     return(MathHelper.Add(1, 1));
 }
Ejemplo n.º 17
0
 public void Add_Test_1(int x, int y)
 {
     Assert.AreEqual(MathHelper.Add(x, y), x + y);
 }
Ejemplo n.º 18
0
    public void Test_Add_DynamicData_Method(int a, int b, int expected)
    {
        var actual = MathHelper.Add(a, b);

        Assert.AreEqual(expected, actual);
    }
Ejemplo n.º 19
0
        public override bool Execute(Instruction instruction, InstructionPayload payload)
        {
            var rs1 = payload.Rs1;
            var rs2 = payload.Rs2;
            var f3  = payload.Funct3;

            var rs1Signed   = Register.ReadSignedInt(rs1);
            var rs2Signed   = Register.ReadSignedInt(rs2);
            var rs1Unsigned = Register.ReadUnsignedInt(rs1);
            var rs2Unsigned = Register.ReadUnsignedInt(rs2);

            bool doJump = false;

            Logger.Info("Opcode 18 : rs1 = {rs1}, rs2 = {rs2} funct3 = {f3}", rs1, rs2, f3);

            switch (f3)
            {
            // beq
            case 0:
                if (rs1Signed == rs2Signed)
                {
                    doJump = true;
                }
                break;

            // bne
            case 1:
                if (rs1Signed != rs2Signed)
                {
                    doJump = true;
                }
                break;

            // blt
            case 4:
                if (rs1Signed < rs2Signed)
                {
                    doJump = true;
                }
                break;

            // bge
            case 5:
                if (rs1Signed > rs2Signed)
                {
                    doJump = true;
                }
                break;

            // bltu
            case 6:
                if (rs1Unsigned < rs2Unsigned)
                {
                    doJump = true;
                }
                break;

            // bgeu
            case 7:
                if (rs1Unsigned > rs2Unsigned)
                {
                    doJump = true;
                }
                break;

            default:
                throw new OpCodeNotSupportedException(String.Format("OpCode = {0}, Funct3 = {1}", instruction.OpCode, f3));
            }

            if (doJump)
            {
                var pcIndex = Register.ProgramCounter;
                var pc      = Register.ReadUnsignedInt(pcIndex);

                var newPc = MathHelper.Add(pc, payload.SignedImmediate);
                var rasPc = pc + 4;

                // Write it to X1 and the RAS
                Register.WriteUnsignedInt(1, rasPc);
                ras.Push(rasPc);

                Register.WriteUnsignedInt(pcIndex, newPc);
            }

            return(!doJump);
        }