//------------------------------------------------------------------- // mndlbrot.cpp // // This is a graphics program to display the Mandelbrot set. // // References: // James Gleick, "Chaos: Making a New Science," // Penguin Press (1987), pp. 231-232. // Roger T. Stevens, "Fractal Programming in C," // M&T Books (1989), p. 255. // // programmer: ALLAN CRUSE // written on: 05 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) unsigned char *vram = (unsigned char*)VRAM_BASE_ADDRESS; const int display_mode = 0x4103, hres = 800, vres = 600; int max_iterations = 64; int max_colors = 8; float max_size = 5.0; 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 ); } 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 ); } void plot_fractal_set( float mag ) { for (int v = 0; v < vres; v++) for (int h = 0; h < hres; h++) { float a = (float)(h - hres/2); float b = (float)(vres/2 - v); a /= mag; b /= mag; int color = -2; float x = 0.0, y = 0.0; float xsquare = 0.0, ysquare = 0.0; while (( color < max_iterations ) &&( xsquare + ysquare < max_size )) { xsquare = x*x; ysquare = y*y; y *= x; y += y + b; x = xsquare - ysquare + a; ++color; } vram[ v*hres + h ] = ( color % max_colors ); } } 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 ); getchar(); float magnification = 240.0; plot_fractal_set( magnification ); getchar(); system( " mode3 3 " ); tcsetattr( 0, TCSAFLUSH, &otty ); }