public void DeserializeComponentState(BinaryReader reader)
        {
            _addressHighByte = reader.ReadInt32();
            _addressLowByte = reader.ReadInt32();
            _addressWriteMode = (PortAddressWriteMode)reader.ReadInt32();
            _currentPaletteAddress = reader.ReadInt32();
            _currentScanline = reader.ReadInt32();
            _currentVRamAddress = reader.ReadInt32();
            _statusByte = reader.ReadByte();
            _writeMode = (VramWriteMode)reader.ReadInt32();
            _palette = reader.ReadBytes(_palette.Length);

            for (Int32 i = 0; i < _transformedPalette.Length; i++)
            {
                _transformedPalette[i] = reader.ReadUInt16();
            }

            _vram = reader.ReadBytes(_vram.Length);
        }
        public Byte ReadVRamPort()
        {
            _addressWriteMode = PortAddressWriteMode.LowByte;
            if (_writeMode == VramWriteMode.Vram)
                return _vram[_currentVRamAddress++];
            else if (_writeMode == VramWriteMode.Palette)
                return _palette[_currentPaletteAddress++];

            throw new ApplicationException();
        }
        public void WriteVRamPort(Byte value)
        {
            _addressWriteMode = PortAddressWriteMode.LowByte;

            if (_writeMode == VramWriteMode.Vram)
            {
                _vram[_currentVRamAddress++] = value;
                if (_currentVRamAddress > 0x3FFF)
                    _currentVRamAddress = 0x3FFF;
            }
            else if (_writeMode == VramWriteMode.Palette)
            {
                WritePaletteEntry(value);
            }
        }
        public void WriteAddressPort(Byte value)
        {
            if (_addressWriteMode == PortAddressWriteMode.LowByte)
            {
                _addressLowByte = value;
                _addressWriteMode = PortAddressWriteMode.HighByte;
                return;
            }

            _addressHighByte = value;
            _addressWriteMode = PortAddressWriteMode.LowByte;
            var writeType = _addressHighByte >> 6;
            switch (writeType)
            {                
                case 0:
                case 1:
                    _currentPaletteAddress = 0;
                    _writeMode = VramWriteMode.Vram;
                    _currentVRamAddress = ((_addressHighByte & 0x3F) << 8) | _addressLowByte;
                    break;
                case 2:
                    WriteToRegister(_addressHighByte & 0x0F, _addressLowByte);
                    break;
                case 3:
                    // Write to palette.
                    _currentVRamAddress = 0;
                    _writeMode = VramWriteMode.Palette;
                    _currentPaletteAddress = _addressLowByte & 0x1F;
                    break;

                default:
                    throw new ApplicationException();
            }
        }