Strange Graphics problem !
Posted: Wed Apr 15, 2009 9:50 am
I am running this code :
It runs perfectly fine in Bochs and gives me a nice black screen. But when I run it on Virtualbox its giving a blurred garbage colored screen, similar to then one that appears when text mode doesnt properly switch to Graphics Mode. I am required to run this thing on Virtualbox, as the real machine I need to run my future code is configured(Hardware) according to VirtualBox configurations.
Can someone give me a direction on whats going wrong in the initializations that I am doing for the VGA Registers ?
Thanks
Code: Select all
#include <kernel.h>
#define VGA_AC_INDEX 0x3C0
#define VGA_AC_WRITE 0x3C0
#define VGA_AC_READ 0x3C1
#define VGA_MISC_WRITE 0x3C2
#define VGA_SEQ_INDEX 0x3C4
#define VGA_SEQ_DATA 0x3C5
#define VGA_DAC_READ_INDEX 0x3C7
#define VGA_DAC_WRITE_INDEX 0x3C8
#define VGA_DAC_DATA 0x3C9
#define VGA_MISC_READ 0x3CC
#define VGA_GC_INDEX 0x3CE
#define VGA_GC_DATA 0x3CF
#define VGA_CRTC_INDEX 0x3D4 /* 0x3B4 */
#define VGA_CRTC_DATA 0x3D5 /* 0x3B5 */
#define VGA_INSTAT_READ 0x3DA
#define VGA_NUM_SEQ_REGS 5
#define VGA_NUM_CRTC_REGS 25
#define VGA_NUM_GC_REGS 9
#define VGA_NUM_AC_REGS 21
#define VGA_NUM_REGS (1+VGA_NUM_SEQ_REGS+VGA_NUM_CRTC_REGS+VGA_NUM_GC_REGS+VGA_NUM_AC_REGS)
//the vga identifiers
unsigned int VGA_width;
unsigned int VGA_height;
unsigned int VGA_bpp;
unsigned char *VGA_address;
void VGA_init(int width, int height, int bpp);
void write_registers(unsigned char *regs);
void vga_mode()
{
VGA_init(640,480,16);
}
/*
* Create Register Array
*/
unsigned char g_640x480x16[] =
{
/* MISC */
0xE3,
/* SEQ */
0x03, 0x01, 0x0F, 0x00, 0x02,
/* CRTC */
0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0x0B, 0x3E,
0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xEA, 0x0C, 0xDF, 0x28, 0x00, 0xE7, 0x04, 0xE3,
0xFF,
/* GC -> 4th is Function Set and 8th Bit Set
6th is Mode Select default Read0 write2*/
0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x05, 0x0F,
0xFF,
/* AC */
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x2D,
0x01, 0x0F, 0x0F, 0xC0, 0x00
};
void write_registers(unsigned char *regs){
unsigned i;
/* write MISCELLANEOUS reg */
outportb(VGA_MISC_WRITE, *regs);
regs++;
/* write SEQUENCER regs */
for(i = 0; i < VGA_NUM_SEQ_REGS; i++)
{
outportb(VGA_SEQ_INDEX, i);
outportb(VGA_SEQ_DATA, *regs);
regs++;
}
/* unlock CRTC registers */
outportb(VGA_CRTC_INDEX, 0x03);
outportb(VGA_CRTC_DATA, inportb(VGA_CRTC_DATA) | 0x80);
outportb(VGA_CRTC_INDEX, 0x11);
outportb(VGA_CRTC_DATA, inportb(VGA_CRTC_DATA) & ~0x80);
/* make sure they remain unlocked */
regs[0x03] |= 0x80;
regs[0x11] &= ~0x80;
/* write CRTC regs */
for(i = 0; i < VGA_NUM_CRTC_REGS; i++)
{
outportb(VGA_CRTC_INDEX, i);
outportb(VGA_CRTC_DATA, *regs);
regs++;
}
/* write GRAPHICS CONTROLLER regs */
for(i = 0; i < VGA_NUM_GC_REGS; i++)
{
outportb(VGA_GC_INDEX, i);
outportb(VGA_GC_DATA, *regs);
regs++;
}
/* write ATTRIBUTE CONTROLLER regs */
for(i = 0; i < VGA_NUM_AC_REGS; i++)
{
(void)inportb(VGA_INSTAT_READ);
outportb(VGA_AC_INDEX, i);
outportb(VGA_AC_WRITE, *regs);
regs++;
}
/* lock 16-color palette and unblank display */
(void)inportb(VGA_INSTAT_READ);
outportb(VGA_AC_INDEX, 0x20);
}
/*
* Clears the VGA screen
*/
void VGA_clear_screen(){
unsigned int x=0;
unsigned int y=0;
for(y=0; y<VGA_height; y++){
for(x=0; x<VGA_width; x++){
// Address 0xA0000 is (0,0)
VGA_address[(VGA_width/8)*y+(int)(x/8)]=0x00;
}
}
}
void VGA_init(int width, int height, int bpp){
//setup the vga struct
VGA_width=(unsigned int)width;
VGA_height=(unsigned int)height;
VGA_bpp=bpp; // Bit per pixel
VGA_address=0xA0000;
write_registers(g_640x480x16);
//clears the screen
VGA_clear_screen();
}
Can someone give me a direction on whats going wrong in the initializations that I am doing for the VGA Registers ?
Thanks