예제 #1
0
        private void instruction_0xD_handler(CPUInstruction instruction, CPU cpu, RAM ram, Screen screen, Keyboard keyboard)
        {
            // Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
            byte[] sprites = ram.Read(cpu.I, (uint)cpu.I + instruction.N - 1);
            uint   x       = cpu.VX[instruction.X];
            uint   y       = cpu.VX[instruction.Y];

            cpu.VX[0xF] = screen.XorPixel(sprites, x, y) ? (byte)1 : (byte)0;
        }
예제 #2
0
        private void instruction_0xF_handler(CPUInstruction instruction, CPU cpu, RAM ram, Screen screen, Keyboard keyboard)
        {
            // Set Vx = delay timer value.
            if (instruction.KK == 0x07)
            {
                cpu.VX[instruction.X] = cpu.DelayTimer;
                return;
            }

            // Wait for a key press, store the value of the key in Vx.
            // I THINK I'VE FOUND A F*****G ERROR!!!!
            if (instruction.KK == 0x0A)
            {
                /*
                 * bool pressedKey = false;
                 * while (!pressedKey)
                 * {
                 *  for (byte key = 0; key <= 0xF; ++key)
                 *  {
                 *      pressedKey = keyboard.IsKeyPressed(key);
                 *      if (pressedKey)
                 *      {
                 *          cpu.VX[instruction.X] = key;
                 *          break;
                 *      }
                 *  }
                 * }*/
                bool pressedKey = false;
                for (byte key = 0; key <= 0xF; ++key)
                {
                    pressedKey = keyboard.IsKeyPressed(key);
                    if (pressedKey)
                    {
                        cpu.VX[instruction.X] = key;
                        break;
                    }
                }

                if (!pressedKey)
                {
                    cpu.PC -= 2;
                }
                return;
            }

            // Set delay timer = Vx.
            if (instruction.KK == 0x15)
            {
                cpu.DelayTimer = cpu.VX[instruction.X];
                return;
            }

            // Set sound timer = Vx.
            if (instruction.KK == 0x18)
            {
                cpu.SoundTimer = cpu.VX[instruction.X];
                return;
            }

            // Set I = I + Vx.
            if (instruction.KK == 0x1E)
            {
                cpu.I += cpu.VX[instruction.X];
                return;
            }

            // Set I = location of sprite for digit Vx.
            if (instruction.KK == 0x29)
            {
                cpu.I = (ushort)(RAM.SPRITES_OFFSET + cpu.VX[instruction.X] * RAM.SPRITE_SIZE);
                return;
            }

            // Store BCD representation of Vx in memory locations I, I+1, and I+2.
            if (instruction.KK == 0x33)
            {
                byte number        = cpu.VX[instruction.X];
                byte hundredsDigit = (byte)(Math.Floor((double)number / 100) % 10);
                byte tensDigit     = (byte)(Math.Floor((double)number / 10) % 10);
                byte onesDigit     = (byte)(number % 10);

                byte[] data = { hundredsDigit, tensDigit, onesDigit };
                ram.LoadToMemory(data, cpu.I);

                return;
            }

            // Store registers V0 through Vx in memory starting at location I.
            if (instruction.KK == 0x55)
            {
                byte[] data = new byte[instruction.X + 1];
                for (uint i = 0; i <= instruction.X; ++i)
                {
                    data[i] = cpu.VX[i];
                }
                ram.LoadToMemory(data, cpu.I);
                return;
            }

            // Read registers V0 through Vx from memory starting at location I.
            if (instruction.KK == 0x65)
            {
                byte[] data = ram.Read(cpu.I, (uint)cpu.I + instruction.X);

                for (uint i = 0; i < data.Length; ++i)
                {
                    cpu.VX[i] = data[i];
                }

                return;
            }
        }