//---------------------------------------------------------------- // newgprog.cpp // // This program creates the skeleton-code for a new graphics // application program, relying on our 'int86.cpp' routines. // // usage: $ newgprog // // programmer: ALLAN CRUSE // written on: 25 OCT 2005 //---------------------------------------------------------------- #include // for fprintf(), fopen(), etc #include // for strncpy(), strncat() #include // for time(), localtime() char authorname[] = "-your name-"; char monthlist[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"; int main( int argc, char *argv[] ) { // check for program-name as command-line argument if ( argc == 1 ) { fprintf( stderr, "Must specify program-name\n" ); return -1; } // prepare program name char appname[33] = ""; strncpy( appname, argv[1], 28 ); // prepare code-file name char srcname[33] = ""; strncpy( srcname, argv[1], 28 ); strtok( srcname, ". \t\n" ); strncat( srcname, ".cpp", 4 ); // announce this program's purpose printf( "\nCreating skeleton for program " ); printf( "named \'%s\' \n", srcname ); // insure source-file doesn't already exist FILE *fp = fopen( srcname, "rb" ); if ( fp != NULL ) { fclose( fp ); fprintf( stderr, "File \'%s\' already exists\n", srcname ); return -1; } // create the new source-file fp = fopen( srcname, "wb" ); if ( fp == NULL ) { fprintf( stderr, "Cannot create source-file\n" ); return -1; } // obtain today's date (in DD MMM YYYY format) time_t now = time( (time_t *)NULL ); struct tm *t = localtime( &now ); char month[4] = ""; strncpy( month, monthlist+3*t->tm_mon, 3 ); month[3] = '\0'; char when[16] = ""; sprintf( when, "%02d %3s %04d", t->tm_mday, month, 1900+t->tm_year ); char border[68] = ""; memset( border, '-', 67 ); fprintf( fp, "//%s\n", border ); fprintf( fp, "//\t%s\n", srcname ); fprintf( fp, "//\n" ); fprintf( fp, "//\t compile using: $ g++ %s ", srcname ); fprintf( fp, "int86.cpp -o %s \n", appname ); fprintf( fp, "//\n" ); fprintf( fp, "//\tprogrammer: %s\n", authorname ); fprintf( fp, "//\tdate begun: %s\n", when ); fprintf( fp, "//%s\n", border ); fprintf( fp, "\n#include " ); fprintf( fp, "\t// for printf(), perror() " ); fprintf( fp, "\n#include " ); fprintf( fp, "\t// for open() " ); fprintf( fp, "\n#include " ); fprintf( fp, "\t// for exit() " ); fprintf( fp, "\n#include " ); fprintf( fp, "\t// for read(), write(), close() " ); fprintf( fp, "\n#include " ); fprintf( fp, "\t// for mmap() " ); fprintf( fp, "\n#include " ); fprintf( fp, "\t// for inb(), outb(), etc." ); fprintf( fp, "\n#include \"int86.h\"" ); fprintf( fp, "\t// for init8086(), int86()" ); fprintf( fp, "\n" ); fprintf( fp, "\nunsigned char *vram = (unsigned char *)0xA0000000; " ); fprintf( fp, "\nint vesa_mode = 0x4105, hres = 1024, vres = 768;" ); fprintf( fp, "\nstruct vm86plus_struct\tvm; " ); fprintf( fp, "\n" ); fprintf( fp, "\nint main( int argc, char **argv )" ); fprintf( fp, "\n{" ); fprintf( fp, "\n\t// memory-map the display-memory into user-space" ); fprintf( fp, "\n\tint\tvd = open( \"/dev/vram\", O_RDWR ); " ); fprintf( fp, "\n\tif ( vd < 0 ) { perror( \"/dev/vram\" ); " ); fprintf( fp, "exit(1); }" ); fprintf( fp, "\n\tint\tsize = lseek( vd, 0, SEEK_END ); " ); fprintf( fp, "\n\tint\tprot = PROT_READ | PROT_WRITE; " ); fprintf( fp, "\n\tint\tflag = MAP_FIXED | MAP_SHARED; " ); fprintf( fp, "\n\tif ( mmap( (void*)vram, size, prot, flag, vd, 0 )" ); fprintf( fp, " == MAP_FAILED )" ); fprintf( fp, "\n\t\t{ perror( \"mmap\" ); exit(1); } " ); fprintf( fp, "\n\t" ); fprintf( fp, "\n\t// enter graphics mode " ); fprintf( fp, "\n\tinit8086(); " ); fprintf( fp, "\n\tvm.regs.eax = 0x4F02;\t" ); fprintf( fp, "\n\tvm.regs.ebx = vesa_mode;\t" ); fprintf( fp, "\n\tint86( 0x10, vm );\t" ); fprintf( fp, "\n\t" ); fprintf( fp, "\n\t// draw screen border " ); fprintf( fp, "\n\tint\tx = 0, y = 0, color = 15; " ); fprintf( fp, "\n\tdo { vram[ y * hres + x ] = color; ++x; } " ); fprintf( fp, "while ( x < hres-1 ); " ); fprintf( fp, "\n\tdo { vram[ y * hres + x ] = color; ++y; } " ); fprintf( fp, "while ( y < vres-1 ); " ); fprintf( fp, "\n\tdo { vram[ y * hres + x ] = color; --x; } " ); fprintf( fp, "while ( x > 0 ); " ); fprintf( fp, "\n\tdo { vram[ y * hres + x ] = color; --y; } " ); fprintf( fp, "while ( y > 0 ); " ); fprintf( fp, "\n\t" ); fprintf( fp, "\n\t" ); fprintf( fp, "\n\tgetchar();" ); fprintf( fp, "\n\t" ); fprintf( fp, "\n\t// leave graphics mode " ); fprintf( fp, "\n\tvm.regs.eax = 0x0003;\t" ); fprintf( fp, "\n\tint86( 0x10, vm );\t" ); fprintf( fp, "\n\tprintf( \"\\n\" );" ); fprintf( fp, "\n}" ); fprintf( fp, "\n" ); printf( "\n" ); }