//------------------------------------------------------------------- // studydac.cpp // // This testbed lets us see the effect of modifying the values // that get programmed into the 256 DAC color-table registers. // // programmer: ALLAN CRUSE // written on: 12 NOV 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for close() #include // for tcgetattr(), tcsetattr() #include // for mmap() #include // for iopl() #define VRAM_BASE_ADDRESS 0xA0000000 #define VGA_MEMORY_LENGTH ( 4 << 20) typedef struct { unsigned char r, g, b; } rgb_t; rgb_t color_table[ 256 ] = {0}; unsigned char *vram = (unsigned char*)VRAM_BASE_ADDRESS; const int display_mode = 0x4101, hres = 640, vres = 480; void draw_rectangle( int x, int y, int h, int v, int color ) { int minx = x, miny = y, maxx = x+h-1, maxy = y+v-1; 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 ); } void draw_color_grid( void ) { int wide = hres/16, high = vres/16; for (int i = 0; i < 256; i++) { int r = i / 16, k = i % 16; unsigned char *where = &vram[ r*high*hres + k*wide ]; for (r = 5; r < high-5; r++) for (k = 5; k < wide-5; k++) where[ r*hres + k ] = i; } } void set_color_registers( rgb_t *table ) { for (int i = 0; i < 256; i++) { outb( i, 0x3C8 ); outb( table[ i ].r, 0x3C9 ); outb( table[ i ].g, 0x3C9 ); outb( table[ i ].b, 0x3C9 ); } } int map_system_memory( int base, int size ) { int fd = open( "/dev/vram", O_RDWR ); if ( fd < 0 ) { perror( "/dev/vram" ); return -1; } int prot = PROT_READ | PROT_WRITE; int flag = MAP_FIXED | MAP_SHARED; void *mm = (void*)base; if ( mmap( mm, size, prot, flag, fd, 0 ) == MAP_FAILED ) { perror( "mmap" ); return -1; } close( fd ); } int main( int argc, char **argv ) { if ( iopl( 3 ) ) { perror( "iopl" ); exit(1); } system( "/sbin/insmod vram.o" ); map_system_memory( VRAM_BASE_ADDRESS, VGA_MEMORY_LENGTH ); struct termios otty; tcgetattr( 0, &otty ); struct termios tty = otty; tty.c_lflag &= ~( ICANON | ECHO | ISIG ); tty.c_cc[ VMIN ] = 1; tty.c_cc[ VTIME ] = 0; tcsetattr( 0, TCSAFLUSH, &tty ); char command[ 40 ]; sprintf( command, " clear ; mode3 %d ", display_mode ); system( command ); int color = 15; draw_rectangle( 0, 0, hres, vres, color ); draw_color_grid(); getchar(); // design a new array of color values int b = 0, g = 64, r = 128, w = 192; for (int i = 0; i < 64; i++) { color_table[ r + i ].r = i; color_table[ g + i ].g = i; color_table[ b + i ].b = i; color_table[ w + i ].r = i; color_table[ w + i ].g = i; color_table[ w + i ].b = i; } set_color_registers( color_table ); getchar(); system( " mode3 3 " ); tcsetattr( 0, TCSAFLUSH, &otty ); }