//------------------------------------------------------------------- // brickpat.cpp // // This demo shows how a graphics application draws patterns. // // programmer: ALLAN CRUSE // written on: 13 SEP 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for close() #include // for mman() #define VRAM_BASE_ADDRESS 0x000A0000 #define VGA_WINDOW_LENGTH (64 << 10) unsigned char *vram = (unsigned char *)VRAM_BASE_ADDRESS; int mode = 19, hres = 320, vres = 200; char brickpat[ 8 ] = { 0xFF, 0x80, 0x80, 0x80, 0xFF, 0x08, 0x08, 0x08 }; void fill_rect( int x, int y, int h, int v, int fg, int bg, char *pat ); int map_system_memory( int base, int size ); int main( int argc, char **argv ) { // map the VRAM into application's virtual address-space map_system_memory( VRAM_BASE_ADDRESS, VGA_WINDOW_LENGTH ); // enter graphics mode char command[ 40 ]; sprintf( command, "clear ; mode3 %d ", mode ); system( command ); // draw screen border int minx = 0, maxx = hres-1, miny = 0, maxy = vres-1; int x = 0, y = 0, color = 15; do { vram[ y*hres + x ] = color; ++x; } while ( x < maxx ); do { vram[ y*hres + x ] = color; ++y; } while ( y < maxy ); do { vram[ y*hres + x ] = color; --x; } while ( x > minx ); do { vram[ y*hres + x ] = color; --y; } while ( y > miny ); getchar(); // fill the screen area with our 2-color brick pattern int red = 4, white = 15; fill_rect( 1, 1, hres-2, vres-2, white, red, brickpat ); getchar(); // restore text mode system( "mode3" ); } void fill_rect( int x, int y, int h, int v, int fg, int bg, char *pat ) { int minx = x, maxx = x+h-1, miny = y, maxy = y+v-1; for (y = miny; y <= maxy; y++) for (x = minx; x <= maxx; x++) { int r = y % 8; int k = x % 8; int color = ( pat[ r ] & (0x80>>k) ) ? fg : bg; vram[ y*hres + x ] = color; } } int map_system_memory( int base, int size ) { int prot = PROT_READ | PROT_WRITE; int flag = MAP_FIXED | MAP_SHARED; void *mm = (void*)base; int fd = open( "/dev/mem", O_RDWR ); if ( fd < 0 ) { perror( "/dev/mem" ); exit(1); } if ( mmap( mm, size, prot, flag, fd, base ) == MAP_FAILED ) { perror( "mmap" ); exit(1); } close( fd ); return 0; }