//------------------------------------------------------------------- // mmwhere.cpp // // This program shows the extent to which an application can // control the location at which our '/dev/vram' device-file // will be memory-mapped by the standard 'mmap()' function. // // NOTE: Written and tested with Linux kernel version 2.4.20. // // programmer: ALLAN CRUSE // date begun: 24 APR 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), write(), close() #include // for mmap(), munmap() char filename[] = "/dev/vram"; int main( int argc, char **argv ) { // open the device file and determine size int mm = open( filename, O_RDWR ); if ( mm < 0 ) { perror( filename ); exit(1); } int vramsize = lseek( mm, 0, SEEK_END ); printf( "\n\tvramsize=%d bytes (%d MB)\n", vramsize, vramsize >> 20 ); // ask user to type in the desired frame-buffer address unsigned long address; printf( "\nYour preferred frame-buffer address (hexadecimal): " ); scanf( "%x", &address ); printf( "\n" ); // try to map the device-memory at the requested address int prot = ( PROT_READ | PROT_WRITE ); int flags = MAP_SHARED; int offset = 0; void *aaddr = (void *)address; long *vram; vram = (long*)mmap( aaddr, vramsize, prot, flags, mm, offset ); printf( "\tAddress asked: %08X Address given: %08X \n", aaddr, vram ); // do some drawing (to verify the memory mapping) for (int i = 0; i < 1280; i++) vram[i] = 0x00FF0000; // unmap the device memory and exit munmap( vram, vramsize ); }