private void GenerateBitmaps(BitmapMemory bmem)
        {
            int bitmapCount = (int)(bmem.Size / (bitmapWidth * bitmapHeight * bmem.WordSize));

            bitmaps = new BitmapSource[bitmapCount];

            for (int i = 0; i < bitmapCount; i++)
            {
                var pixels = new byte[256 * 3];
                for (int j = 0; j < 256; j++)
                {
                    uint pixel = bmem[(uint)((i * 256 + j) * bmem.WordSize)];
                    pixels[j * 3 + 2] = (byte)((pixel >> 8) * 16);
                    pixels[j * 3 + 1] = (byte)((pixel >> 4 & 0xf) * 16);
                    pixels[j * 3]     = (byte)((pixel & 0xf) * 16);
                }

                BitmapSource bitmap = BitmapSource.Create(bitmapWidth, bitmapHeight, 96, 96, PixelFormats.Bgr24, null, pixels,
                                                          bitmapWidth * 3);
                bitmaps[i] = bitmap;
            }
        }
        public VgaDisplay(Mips mips)
        {
            images = new Image[gridWidth * gridHeight];

            InitializeComponent();
            InitializeGrid(gridWidth, gridHeight);
            AddGridImages();
            BitmapMemory bmem = (BitmapMemory)mips.MemDict[typeof(BitmapMemory)][0];

            GenerateBitmaps(bmem);

            this.smem = (ScreenMemory)mips.MemDict[typeof(ScreenMemory)][0];

            hasSprite = mips.MemDict.ContainsKey(typeof(SpriteMemory)) &&
                        mips.MemDict.ContainsKey(typeof(SpriteBitmapMemory));

            if (hasSprite)
            {
                GenerateSpriteImage((SpriteBitmapMemory)mips.MemDict[typeof(SpriteBitmapMemory)][0]);
                spmem = (SpriteMemory)mips.MemDict[typeof(SpriteMemory)][0];
            }

            RefreshDisplay();
        }