Example #1
0
        public void ShiftRightArithmetic()
        {
            Memory memory  = new Memory();
            var    bitUnit = new BitUnit(memory);
            byte   value   = 0b11110000;
            byte   flags   = 0b01100000;

            // Reset carry
            bitUnit.ShiftRightArithmetic(ref value, ref flags);

            Assert.Equal(0b11111000, value);
            Assert.Equal(0x0, flags);

            // Set carry
            value = 0b00001111;
            flags = 0b01100000;

            bitUnit.ShiftRightArithmetic(ref value, ref flags);

            Assert.Equal(0b00000111, value);
            Assert.Equal(0b00010000, flags);

            //Set zero
            value = 0b00000001;
            flags = 0b01100000;

            bitUnit.ShiftRightArithmetic(ref value, ref flags);

            Assert.Equal(0x0, value);
            Assert.Equal(0b10010000, flags);
        }
Example #2
0
        public CPUTests(ITestOutputHelper testOutputHelper)
        {
            this.testOutputHelper = testOutputHelper;

            memory = new Memory(new byte[]
            {
                IMMEDIATE_BYTE,
                (IMMEDIATE_WORD >> 8) & 0xFF
            });
            display  = new BlankDisplay();
            ppu      = new PPU(memory, display);
            loadUnit = A.Fake <LoadUnit>();
            alu      = A.Fake <ALU>();
            miscUnit = A.Fake <MiscUnit>();
            jumpUnit = A.Fake <JumpUnit>();
            bitUnit  = A.Fake <BitUnit>();
            cpu      = new CPU(memory, ppu, loadUnit, alu, miscUnit, jumpUnit, bitUnit)
            {
                A  = 0x0A,
                B  = 0x0B,
                C  = 0x0C,
                D  = 0x0D,
                E  = 0x0E,
                H  = 0xAA,
                L  = 0xBB,
                PC = 0x00,
                SP = 0xFF
            };
            memory.WriteByte(0xAABB, MEM_HL_BYTE);
        }
Example #3
0
        public CPU(Memory memory, PPU ppu, LoadUnit loadUnit, ALU alu, MiscUnit miscUnit, JumpUnit jumpUnit, BitUnit bitUnit)
        {
            this.ppu = ppu;

            this.loadUnit = loadUnit;
            this.alu      = alu;
            this.miscUnit = miscUnit;
            this.jumpUnit = jumpUnit;
            this.bitUnit  = bitUnit;

            this.loadUnit.TickEvent += Tick;
            this.alu.TickEvent      += Tick;
            this.jumpUnit.TickEvent += Tick;
            this.bitUnit.TickEvent  += Tick;

            CreateLoadUnitOpCodes();
            CreateALUOpCodes();
            CreateMiscOpCodes();
            CreateJumpOpCodes();
            CreateBitUnitOpCodes();

            this.memory = memory;
            this.timer  = new Timer(memory);
            this.memory.DivResetEvent += DivResetHandler;

            interruptVector = new Dictionary <Interrupt, ushort>
            {
                { Interrupt.VBlank, 0x40 },
                { Interrupt.LCDStat, 0x48 },
                { Interrupt.Timer, 0x50 },
                { Interrupt.Serial, 0x58 },
                { Interrupt.Joypad, 0x60 }
            };
        }
Example #4
0
        public void SwapSetsZeroFlag()
        {
            var  memory   = new Memory();
            var  bitUnit  = new BitUnit(memory);
            byte flags    = 0b01110000;
            byte register = 0x00;

            bitUnit.Swap(ref register, ref flags);

            Assert.Equal(0x00, register);
            Assert.Equal(0b10000000, flags);
        }
Example #5
0
        public void SwapChangesNibbles()
        {
            var  memory   = new Memory();
            var  bitUnit  = new BitUnit(memory);
            byte flags    = 0b11110000;
            byte register = 0xAC;

            bitUnit.Swap(ref register, ref flags);

            Assert.Equal(0xCA, register);
            Assert.Equal(0x00, flags);
        }
Example #6
0
        public void SetBitTest()
        {
            var  memory  = new Memory();
            var  bitUnit = new BitUnit(memory);
            byte value   = 0x81;

            bitUnit.SetBit(ref value, 7, false);
            Assert.Equal(0x1, value);

            bitUnit.SetBit(ref value, 7, true);
            Assert.Equal(0x81, value);
        }
Example #7
0
        public void RotateALeftThroughCarry()
        {
            var  memory   = new Memory();
            var  bitUnit  = new BitUnit(memory);
            byte flags    = 0b11100000;
            byte register = 0b10010000;

            bitUnit.RotateLeftThroughCarry(ref register, ref flags, true);

            Assert.Equal(0b00100000, register);
            Assert.Equal(0b00010000, flags);
        }
Example #8
0
        public void RotateLeftThroughCarryInMemoryTest()
        {
            var  memory  = new Memory();
            var  bitUnit = new BitUnit(memory);
            byte flags   = 0b01100000;

            memory.WriteByte(0xABBA, 0b11111110);

            bitUnit.RotateLeftThroughCarry(0xAB, 0xBA, ref flags);

            Assert.Equal(0b11111100, memory.ReadByte(0xABBA));
            Assert.Equal(0b00010000, flags);
        }
Example #9
0
        public void SwapChangesNibblesInMemory()
        {
            var  memory  = new Memory();
            var  bitUnit = new BitUnit(memory);
            byte flags   = 0b11110000;

            memory.WriteByte(0xACDC, 0xAB);

            bitUnit.Swap(0xAC, 0xDC, ref flags);

            Assert.Equal(0xBA, memory.ReadByte(0xACDC));
            Assert.Equal(0x00, flags);
        }
Example #10
0
        public void ShiftRightArithmeticInMemory()
        {
            Memory memory  = new Memory();
            var    bitUnit = new BitUnit(memory);

            memory.WriteByte(0xACDC, 0b10000001);
            byte flags = 0b11100000;

            bitUnit.ShiftRightArithmetic(0xAC, 0xDC, ref flags);

            Assert.Equal(0b11000000, memory.ReadByte(0xACDC));
            Assert.Equal(0b00010000, flags);
        }
Example #11
0
        public void ShiftRightLogicInMemory()
        {
            var memory  = new Memory();
            var bitUnit = new BitUnit(memory);

            memory.WriteByte(0xABCD, 0xFF);
            byte flags = 0b11100000;

            bitUnit.ShiftRightLogic(0xAB, 0xCD, ref flags);

            Assert.Equal(0b01111111, memory.ReadByte(0xABCD));
            Assert.Equal(0b00010000, flags);
        }
Example #12
0
        public void RotateRightInMemory()
        {
            Memory memory  = new Memory();
            var    bitUnit = new BitUnit(memory);

            memory.WriteByte(0xACDC, 0b00000011);
            byte flags = 0b01100000;

            bitUnit.RotateRight(0xAC, 0xDC, ref flags);

            Assert.Equal(0b10000001, memory.ReadByte(0xACDC));
            Assert.Equal(0b00010000, flags);
        }
Example #13
0
        public void SetBitInMemory()
        {
            var memory  = new Memory();
            var bitUnit = new BitUnit(memory);

            memory.WriteByte(0xABCD, 0xFF);

            bitUnit.SetBit(0xAB, 0xCD, 0, false);
            Assert.Equal(0xFE, memory.ReadByte(0xABCD));

            memory.WriteByte(0xABCD, 0xF1);
            bitUnit.SetBit(0xAB, 0xCD, 1, true);
            Assert.Equal(0xF3, memory.ReadByte(0xABCD));
        }
Example #14
0
        public void ComplementCarryTest()
        {
            var  bitUnit = new BitUnit(new Memory());
            byte flags   = 0b00010000;

            bitUnit.ComplementCarry(ref flags);

            Assert.Equal(0b00000000, flags);

            flags = 0b10100000;

            bitUnit.ComplementCarry(ref flags);

            Assert.Equal(0b10010000, flags);
        }
Example #15
0
        public void RotateRightThroughCarry()
        {
            // No carry
            var  bitUnit = new BitUnit(new Memory());
            byte value   = 0b01111110;
            byte flags   = 0b01100000;

            bitUnit.RotateRightThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b00111111, value);
            Assert.Equal(0b00000000, flags);

            // From carry
            value = 0b01111110;
            flags = 0b01110000;

            bitUnit.RotateRightThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b10111111, value);
            Assert.Equal(0b00000000, flags);

            // To carry
            value = 0b01111101;
            flags = 0b01100000;

            bitUnit.RotateRightThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b00111110, value);
            Assert.Equal(0b00010000, flags);

            // From and to carry
            value = 0b00000001;
            flags = 0b01110000;

            bitUnit.RotateRightThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b10000000, value);
            Assert.Equal(0b00010000, flags);

            // Set zero flag
            value = 0x1;
            flags = 0b01100000;

            bitUnit.RotateRightThroughCarry(ref value, ref flags, false);

            Assert.Equal(0x0, value);
            Assert.Equal(0b10010000, flags);
        }
Example #16
0
        public void RotateLeftThroughCarryTest()
        {
            // No carry
            var  bitUnit = new BitUnit(new Memory());
            byte value   = 0b01111110;
            byte flags   = 0b01100000;

            bitUnit.RotateLeftThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b11111100, value);
            Assert.Equal(0b00000000, flags);

            // Test to carry
            value = 0b11111110;
            flags = 0b11100000;

            bitUnit.RotateLeftThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b11111100, value);
            Assert.Equal(0b00010000, flags);

            // Test from carry
            value = 0b01111110;
            flags = 0b11100000;

            bitUnit.RotateLeftThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b11111100, value);
            Assert.Equal(0b00000000, flags);

            // Test to and from carry
            value = 0b11111110;
            flags = 0b11110000;

            bitUnit.RotateLeftThroughCarry(ref value, ref flags, false);

            Assert.Equal(0b11111101, value);
            Assert.Equal(0b00010000, flags);

            // Result zero
            value = 0x0;
            flags = 0x0;

            bitUnit.RotateLeftThroughCarry(ref value, ref flags, false);

            Assert.Equal(0x0, value);
            Assert.Equal(0b10000000, flags);
        }
Example #17
0
        public void RotateLeftTest()
        {
            // No carry
            var  memory   = new Memory();
            var  bitUnit  = new BitUnit(memory);
            byte flags    = 0b11100000;
            byte register = 0b01010000;

            bitUnit.RotateLeft(ref register, ref flags, false);

            Assert.Equal(0b10100000, register);
            Assert.Equal(0b00000000, flags);

            // Set carry
            flags    = 0b11100000;
            register = 0b10010000;

            bitUnit.RotateLeft(ref register, ref flags, false);

            Assert.Equal(0b00100001, register);
            Assert.Equal(0b00010000, flags);

            // Sets zero when result is zero
            flags    = 0b11110000;
            register = 0x0;

            bitUnit.RotateLeft(ref register, ref flags, false);

            Assert.Equal(0x0, register);
            Assert.Equal(0b10000000, flags);

            // Resets zero always when forced
            flags    = 0b10010000;
            register = 0b10101111;

            bitUnit.RotateLeft(ref register, ref flags, true);

            Assert.Equal(0b01011111, register);
            Assert.Equal(0b00010000, flags);
        }
Example #18
0
        public void RotateRight()
        {
            // No carry
            var  bitUnit = new BitUnit(new Memory());
            byte value   = 0b01111110;
            byte flags   = 0b01110000;

            bitUnit.RotateRight(ref value, ref flags, false);

            Assert.Equal(0b00111111, value);
            Assert.Equal(0x0, flags);

            // Set carry
            value = 0b10000001;
            flags = 0b01100000;

            bitUnit.RotateRight(ref value, ref flags, false);

            Assert.Equal(0b11000000, value);
            Assert.Equal(0b00010000, flags);

            // Set zero
            value = 0x0;
            flags = 0b01110000;

            bitUnit.RotateRight(ref value, ref flags, false);

            Assert.Equal(0x0, value);
            Assert.Equal(0b10000000, flags);

            // Reset zero forced
            value = 0x0;
            flags = 0b11110000;

            bitUnit.RotateRight(ref value, ref flags, true);

            Assert.Equal(0x0, value);
            Assert.Equal(0b00000000, flags);
        }
Example #19
0
        public void TestBitTest()
        {
            var  bitUnit = new BitUnit(new Memory());
            byte value   = 0b01010011;

            byte flags = 0b01010000;

            bitUnit.TestBit(value, 7, ref flags);
            Assert.Equal(0b10110000, flags);

            flags = 0b10010000;
            bitUnit.TestBit(value, 6, ref flags);
            Assert.Equal(0b00110000, flags);

            flags = 0b00010000;
            bitUnit.TestBit(value, 5, ref flags);
            Assert.Equal(0b10110000, flags);

            flags = 0b10010000;
            bitUnit.TestBit(value, 4, ref flags);
            Assert.Equal(0b00110000, flags);

            flags = 0b00000000;
            bitUnit.TestBit(value, 3, ref flags);
            Assert.Equal(0b10100000, flags);

            flags = 0b00000000;
            bitUnit.TestBit(value, 2, ref flags);
            Assert.Equal(0b10100000, flags);

            flags = 0b10000000;
            bitUnit.TestBit(value, 1, ref flags);
            Assert.Equal(0b00100000, flags);

            flags = 0b10000000;
            bitUnit.TestBit(value, 0, ref flags);
            Assert.Equal(0b00100000, flags);
        }
Example #20
0
        public void ComplementTest()
        {
            var  memory  = new Memory();
            var  bitUnit = new BitUnit(memory);
            byte value   = 0xFF;
            byte flags   = 0x00;

            bitUnit.Complement(ref value, ref flags);
            Assert.Equal(0x00, value);
            Assert.Equal(0b01100000, flags);

            value = 0x00;
            flags = 0x00;
            bitUnit.Complement(ref value, ref flags);
            Assert.Equal(0xFF, value);
            Assert.Equal(0b01100000, flags);

            value = 0x0F;
            flags = 0x00;
            bitUnit.Complement(ref value, ref flags);
            Assert.Equal(0xF0, value);
            Assert.Equal(0b01100000, flags);
        }
Example #21
0
        public void ShiftRightLogic()
        {
            var  bitUnit = new BitUnit(new Memory());
            byte value   = 0x00;
            byte flags   = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x0, value);
            Assert.Equal(0b10000000, flags);

            value = 0x01;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x0, value);
            Assert.Equal(0b10010000, flags);

            value = 0x0F;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x07, value);
            Assert.Equal(0b00010000, flags);

            value = 0x10;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x08, value);
            Assert.Equal(0x0, flags);

            value = 0x1F;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x0F, value);
            Assert.Equal(0b00010000, flags);

            value = 0x7F;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x3F, value);
            Assert.Equal(0b00010000, flags);

            value = 0x80;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x40, value);
            Assert.Equal(0x0, flags);

            value = 0xF0;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x78, value);
            Assert.Equal(0x0, flags);

            value = 0xFF;
            flags = 0xF0;

            bitUnit.ShiftRightLogic(ref value, ref flags);

            Assert.Equal(0x7F, value);
            Assert.Equal(0b00010000, flags);
        }