public void SqureNumberShouldOutputCorrectValues()
 {
     var cpu = new Cpu32(4);
     var motherboardMock = new Mock<IMotherboard>();
     motherboardMock.Setup(x => x.LoadRamValue()).Returns(123);
     cpu.AttachTo(motherboardMock.Object);
     cpu.SquareNumber();
     motherboardMock.Verify(x => x.DrawOnVideoCard(
         It.Is<string>(param => param.Contains("15129"))));
 }
 public void SquareNumberShouldDrawErrorMessageWhenValueIsLessThanZero()
 {
     var cpu = new Cpu32(4);
     var motherboardMock = new Mock<IMotherboard>();
     motherboardMock.Setup(x => x.LoadRamValue()).Returns(-1);
     cpu.AttachTo(motherboardMock.Object);
     cpu.SquareNumber();
     motherboardMock.Verify(x => x.DrawOnVideoCard(
         It.Is<string>(param => param == Cpu.NumberTooLowMessage)));
 }
        public void SquareNumberInCpu32ShouldDrawErrorMessageWhenValueIsGreaterThanMaxValue()
        {
            var cpu             = new Cpu32(4);
            var motherboardMock = new Mock <IMotherboard>();

            motherboardMock.Setup(x => x.LoadRamValue()).Returns(Cpu32.MaxValue + 1);
            cpu.AttachTo(motherboardMock.Object);
            cpu.SquareNumber();
            motherboardMock.Verify(x => x.DrawOnVideoCard(
                                       It.Is <string>(param => param == Cpu.NumberTooHighMessage)));
        }
        public void SqureNumberShouldOutputCorrectValues()
        {
            var cpu             = new Cpu32(4);
            var motherboardMock = new Mock <IMotherboard>();

            motherboardMock.Setup(x => x.LoadRamValue()).Returns(123);
            cpu.AttachTo(motherboardMock.Object);
            cpu.SquareNumber();
            motherboardMock.Verify(x => x.DrawOnVideoCard(
                                       It.Is <string>(param => param.Contains("15129"))));
        }
Пример #5
0
        public void SquareNumber()
        {
            // Arrange
            Ram        ram       = new Ram(400);
            byte       cores     = 4;
            IVideoCard videoCard = new ColorfullVideoCard();

            ICpu cpu = new Cpu32(cores, ram, videoCard);

            cpu.SquareNumber();
        }
Пример #6
0
        public void TestGenerateRandom_ShouldCallSaveToRam(int lower, int higher)
        {
            var mockedMotherboard = new Mock<IMotherboard>();

            var cpu = new Cpu32(8);

            cpu.AttachToMotherboard(mockedMotherboard.Object);

            cpu.GenerateRandom(lower, higher);

            mockedMotherboard.Verify(x => x.SaveRamValue(It.IsAny<int>()), Times.Once);
        }
Пример #7
0
        public void TestGenerateRandom_ShouldCallSaveToRam(int lower, int higher)
        {
            var mockedMotherboard = new Mock <IMotherboard>();

            var cpu = new Cpu32(8);

            cpu.AttachToMotherboard(mockedMotherboard.Object);

            cpu.GenerateRandom(lower, higher);

            mockedMotherboard.Verify(x => x.SaveRamValue(It.IsAny <int>()), Times.Once);
        }
Пример #8
0
        public void TestSquareNumber_PassLowerNumber_ShouldReturnCorrectString(int number)
        {
            var mockedMotherboard = new Mock<IMotherboard>();

            var cpu = new Cpu32(8);

            cpu.AttachToMotherboard(mockedMotherboard.Object);

            mockedMotherboard.Setup(x => x.LoadRamValue()).Returns(number);

            cpu.SquareNumber();

            mockedMotherboard.Verify(x => x.DrawOnVideoCard(GlobalConstants.NumberTooLowMessage), Times.Once);
        }
Пример #9
0
        public void TestSquareNumber_PassHigherNumber_ShouldReturnCorrectString(int number)
        {
            var mockedMotherboard = new Mock <IMotherboard>();

            var cpu = new Cpu32(8);

            cpu.AttachToMotherboard(mockedMotherboard.Object);

            mockedMotherboard.Setup(x => x.LoadRamValue()).Returns(number);

            cpu.SquareNumber();

            mockedMotherboard.Verify(x => x.DrawOnVideoCard(GlobalConstants.NumberTooHighMessage), Times.Once);
        }
Пример #10
0
        public void RandShouldNotProduceNumberGreaterThanMaxValue()
        {
            var cpu             = new Cpu32(2);
            var motherboardMock = new Mock <IMotherboard>();

            cpu.AttachTo(motherboardMock.Object);
            var globalMax = int.MinValue;

            motherboardMock.Setup(x => x.SaveRamValue(It.IsAny <int>())).Callback <int>(param => globalMax = Math.Max(globalMax, param));
            for (int i = 0; i < 10000; i++)
            {
                cpu.Rand(1, 10);
            }

            Assert.AreEqual(10, globalMax);
        }
Пример #11
0
        public void TestSquareNumber_PassCorrectNumber_ShouldReturnCorrectString(int number)
        {
            var mockedMotherboard = new Mock<IMotherboard>();

            var cpu = new Cpu32(8);

            cpu.AttachToMotherboard(mockedMotherboard.Object);

            mockedMotherboard.Setup(x => x.LoadRamValue()).Returns(number);

            var expectedString = $"Square of {number} is {number * number}.";

            cpu.SquareNumber();

            mockedMotherboard.Verify(x => x.DrawOnVideoCard(expectedString), Times.Once);
        }
Пример #12
0
        public void TestSquareNumber_PassCorrectNumber_ShouldReturnCorrectString(int number)
        {
            var mockedMotherboard = new Mock <IMotherboard>();

            var cpu = new Cpu32(8);

            cpu.AttachToMotherboard(mockedMotherboard.Object);

            mockedMotherboard.Setup(x => x.LoadRamValue()).Returns(number);

            var expectedString = $"Square of {number} is {number * number}.";

            cpu.SquareNumber();

            mockedMotherboard.Verify(x => x.DrawOnVideoCard(expectedString), Times.Once);
        }
Пример #13
0
        public void RandShouldNotProduceNumberLessThanMinValue()
        {
            var cpu = new Cpu32(2);
            var motherboardMock = new Mock<IMotherboard>();
            cpu.AttachTo(motherboardMock.Object);
            var globalMin = int.MaxValue;
            motherboardMock.Setup(
                x => x.SaveRamValue(It.IsAny<int>()))
                .Callback<int>(param => globalMin = Math.Min(globalMin, param));
            for (int i = 0; i < 10000; i++)
            {
                cpu.Rand(1, 10);
            }

            Assert.AreEqual(1, globalMin);
        }
Пример #14
0
        public void RandShouldAlwaysReturnRandomNumbers()
        {
            var hashSet = new HashSet<int>();
            var cpu = new Cpu32(2);
            var motherboardMock = new Mock<IMotherboard>();
            cpu.AttachTo(motherboardMock.Object);
            motherboardMock.Setup(
                x => x.SaveRamValue(It.IsAny<int>()))
                .Callback<int>(param => hashSet.Add(param));
            for (int i = 0; i < 10000; i++)
            {
                cpu.Rand(1, 10);
            }

            Assert.AreEqual(10, hashSet.Count);
        }
Пример #15
0
        public void RandShouldAlwaysReturnRandomNumbers()
        {
            var hashSet = new HashSet <int>();

            var cpu             = new Cpu32(2);
            var motherboardMock = new Mock <IMotherboard>();

            cpu.AttachTo(motherboardMock.Object);

            motherboardMock.Setup(x => x.SaveRamValue(It.IsAny <int>())).Callback <int>(param => hashSet.Add(param));
            for (int i = 0; i < 10000; i++)
            {
                cpu.Rand(1, 10);
            }

            Assert.AreEqual(10, hashSet.Count);
        }
Пример #16
0
        public void Run(IEnumerable <byte> program)
        {
            // Init the core
            Memory.Write(BaseAddres, program);
            Register.WriteUnsignedInt(Register.ProgramCounter, BaseAddres);
            var decoder     = new InstructionDecoder(EndianCoding);
            var typeDecoder = new TypeDecoder();

            // Create a simple bootstrap CPU
            var cpu = new Cpu32();

            cpu.AssignMemory(Memory);
            cpu.AssignRegister(Register);
            cpu.AssignEEI(Environment);
            cpu.AssignRasStack(RasStack);
            cpu.AssignCrs(CsrRegister);
            cpu.Init();

            // Fetch the first instruction and run the loop
            var pc = Register.ReadUnsignedInt(Register.ProgramCounter);

            // Get the first 2 Bytes from the Base Address aka PC
            var instructionCoding = Memory.GetHalfWord(pc);

            while (ContinueIfValid(instructionCoding))
            {
                // Loop for the commands
                var instruction = decoder.Decode(instructionCoding);
                InstructionsProcessed.Add(instruction);

                // If the decoder cannot decode the parameter pattern, throw an exception
                if (instruction.Type == InstructionType.Unknown)
                {
                    string unknownOpCodeErrorMessage = String.Format("Error:  OpCode = {0}", instruction.OpCode);
                    throw new OpCodeNotSupportedException(unknownOpCodeErrorMessage)
                          {
                              Coding = instructionCoding,
                              Type   = instruction.Type,
                              OpCode = instruction.OpCode
                          };
                }

                // Reload the other bytes if required!

                if (instruction.InstructionLength == 4)
                {
                    // Read the complete 32 Bit instruction set for the decoding
                    var inst32Coding = Memory.GetWord(pc);
                    var payload      = typeDecoder.DecodeType(instruction, inst32Coding);

                    if (payload == null)
                    {
                        throw new RiscVSimException("No Payload available!");
                    }

                    // For UnitTesting:  Add the result to the list
                    InstructionPayloads.Add(payload);


                    // Execute the command
                    cpu.Execute(instruction, payload);
                }


                // Done. Next run.
                pc = Register.ReadUnsignedInt(Register.ProgramCounter);
                instructionCoding = Memory.GetHalfWord(pc);
            }
        }