#define RED 0x08 #define GREEN 0x04 #define BLUE 0x02 #define HI_INTENSITY 0x01 /* demo program to draw solid boxes with different colors on screen */ Main( argc, argv) int argc; char *argv[]; { int offset; char c; int Set_16_colors(); Cputs("Graphics Solution 640 x 200 with 16 colors demo program\r\n"); Cputs("Press any key to continue \r\n"); Getch(c); /*====> This is the routine used to program the Graphics Solution */ /* to 16 colors either in color or emulation mode */ if( Set_16_colors() > 0 ) /* Draw the solid boxes */ /* this part is demo only */ for( offset = 0; offset < 600; offset += 20) { Fill_box( 21, 21 + offset, 30, 40 + offset, 0x00); Fill_box( 31, 21 + offset, 40, 40 + offset, 0x02); Fill_box( 41, 21 + offset, 50, 40 + offset, 0x04); Fill_box( 51, 21 + offset, 60, 40 + offset, 0x06); Fill_box( 61, 21 + offset, 70, 40 + offset, 0x08); Fill_box( 71, 21 + offset, 80, 40 + offset, 0x0a); Fill_box( 81, 21 + offset, 90, 40 + offset, 0x0c); Fill_box( 91, 21 + offset, 100, 40 + offset, 0x0e); Fill_box( 101, 21 + offset, 110, 40 + offset, 0x01); Fill_box( 111, 21 + offset, 120, 40 + offset, 0x03); Fill_box( 121, 21 + offset, 130, 40 + offset, 0x05); Fill_box( 131, 21 + offset, 140, 40 + offset, 0x07); Fill_box( 141, 21 + offset, 150, 40 + offset, 0x09); Fill_box( 151, 21 + offset, 160, 40 + offset, 0x0b); Fill_box( 161, 21 + offset, 170, 40 + offset, 0x0d); Fill_box( 171, 21 + offset, 180, 40 + offset, 0x0f); } else Cputs("Invalid mode setting\r\n"); /* Wait for keyboard input before clear screen */ Getch(); /* Switch back to text mode */ Text_mode(); } Set_16_colors() { /* return 1 if set to emulation mode */ /* return 2 if set to color mode */ /* return -1 if nothing is set because in monochrome mode or */ /* cannot determine which mode is in */ /* the CRT controller is programmed accordingly */ int Terminal_type(); int t_type; t_type = Terminal_type(); if ( t_type == -1 ) return(-1); else { if ( t_type == 1 ) { Emul_640x200_16_colors(); return(1); } else { if ( t_type == 2 ) { Color_640x200_16_colors(); return(2); } else return(-1); } } } Terminal_type() { /* return 1 if set to emulation mode */ /* return 2 if set to color mode */ /* return -1 if nothing is set because in monochrome mode or */ /* cannot determine which mode is in */ int Get_word(), Iabs(); int Inp(); unsigned a; long t1, t2; int count; int vr1; static t_type = -1; if( t_type > 0 ) return( t_type); if ( Get_word( 0x40, 0x63 ) != 0x03d4 ) return( t_type); Time_1A( &a, &t1); for( t2 = t1; t2 == t1 ; Time_1A( &a, &t2)); count = 0; vr1 = Inp( 0x03da) & 0x08; for( t1 = t2; (t2 - t1 ) <= 18; Time_1A( &a, &t2)) { for( ; vr1 != 0; vr1 = Inp( 0x03da) & 0x0008); for( ; vr1 == 0; vr1 = Inp( 0x03da) & 0x0008); ++count; } if( Iabs( count - 50) <= 5 ) t_type = 1; if( Iabs( count - 60) <= 5 ) t_type = 2; return(t_type); } Emul_640x200_16_colors() { static int parms[16] = { 0x61, 0x50, 0x52, 0x08, 0x32, 0x06, 0x32, 0x32, 0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00 }; int i; /* Set the Graphics Solution to 640 x 200 with 16 colors in Emulation Mode */ Outp( 0x03d8, 0x02); /* Set extended graphics registers */ Outp( 0x03dd, 0x80); Outp( 0x03df, 0xa2); Outp( 0x03d8, 0x02); /* Program 6845 crt controlller */ for( i =0; i < 16; ++i) { Outp( 0x03d4, i); Outp( 0x03d5, parms[i]); } Outp( 0x03d8, 0x0a); Outp( 0x03d9, 0x30); Outp( 0x03df, 0xa0); Outp( 0x03dd, 0x80); /* Clear video memory */ Fill_ram( 0xb000, 0, 0x4000, 0); Fill_ram( 0xb800, 0, 0x4000, 0); } Color_640x200_16_colors() { static int parms[16] = { 0x70, 0x50, 0x58, 0x0a, 0x40, 0x06, 0x32, 0x38, 0x02, 0x03, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00 }; int i; /* Set the Graphics Solution to 640 x 200 with 16 colors in Color Mode */ Outp( 0x03d8, 0x02); /* Set extended graphics registers */ Outp( 0x03dd, 0x80); Outp( 0x03d8, 0x02); /* Program 6845 crt controlller */ for( i =0; i < 16; ++i) { Outp( 0x03d4, i); Outp( 0x03d5, parms[i]); } Outp( 0x03d8, 0x0a); Outp( 0x03d9, 0x30); Outp( 0x03dd, 0x80); /* Clear video memory */ Fill_ram( 0xb000, 0, 0x4000, 0); Fill_ram( 0xb800, 0, 0x4000, 0); } Text_mode() { static int parms[16] = { 0x71, 0x50, 0x5a, 0x0a, 0x1f, 0x06, 0x19, 0x1c, 0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00 }; int i; /* Set the Graphics Solution to 80 x 25 Text Mode */ Outp( 0x03d8, 0x02); /* Set extended graphics registers */ Outp( 0x03dd, 0x00); Outp( 0x03d8, 0x25); /* Program 6845 crt controlller */ for( i =0; i < 16; ++i) { Outp( 0x03d4, i); Outp( 0x03d5, parms[i]); } Outp( 0x03d8, 0x2d); Outp( 0x03d9, 0x30); Outp( 0x03dd, 0x00); /* Clear video memory */ Fill_ram( 0xb800, 0, 0x2000, ' ' + 0x0700); } Fill_box(from_row,from_col,to_row,to_col,color) int from_row, from_col, to_row, to_col; unsigned color; { int row ,col; /* Routine to draw a solid box */ for( row = from_row; row <= to_row; ++row) for( col = from_col; col <= to_col; ++col) Putpixel(row,col,color); } Mem_loc( g_row, g_col, offset_loc, shr, mask) int g_row, g_col; unsigned *offset_loc, *shr; unsigned *mask; { static unsigned mmmmmm = 0xc000; /* Convert a ( x, y ) coordinate into a offset and mask into the video memory */ *offset_loc = ( g_row >> 2 ) * 160 + ( g_row & 0x03 ) * 0x2000 + ( ( g_col * 2 ) >> 3 ); *shr = ( g_col * 2 ) & 0x0007; *mask = mmmmmm >> *shr; } Putpixel( g_row, g_col, color) int g_row, g_col; unsigned color; { int offset, shr; unsigned valuex, value, mask; unsigned Get_bb(); /* calculate the offset of the ( x, y) in the video memory */ Mem_loc( g_row, g_col, &offset, &shr, &mask); color = color << 12; /* Update the pixel value in plane 0 */ valuex = Get_bb( 0xb000, offset); value = ( valuex & (~mask) ) | ( ( color >> shr ) & mask); Str_bb( 0xb000, offset, value); /* Update the pixel value in plane 1 */ valuex = Get_bb( 0xb800, offset); value = ( valuex & (~mask) ) | ( ( ( color << 2 ) >> shr ) & mask); Str_bb( 0xb800, offset, value); } Iabs( i1) int i1; { if ( i1 > 0 ) return( i1); return( -i1); }