-\r
-/*\r
- * Define the port addresses of some VGA registers.\r
- */\r
-#define CRTC_ADDR 0x3d4 /* Base port of the CRT Controller (color) */\r
-\r
-#define SEQU_ADDR 0x3c4 /* Base port of the Sequencer */\r
-#define GRAC_ADDR 0x3ce /* Base port of the Graphics Controller */\r
-\r
-\r
-/*\r
- * Make a far pointer to the VGA graphics buffer segment. Your compiler\r
- * might not have the MK_FP macro, but you'll figure something out.\r
- */\r
-byte *vga = (byte *) MK_FP(0xA000, 0);\r
-\r
-//fontAddr = getFont();\r
-\r
-/*\r
- * width and height should specify the mode dimensions. widthBytes\r
- * specify the width of a line in addressable bytes.\r
- */\r
-unsigned width, height, widthBytes;\r
-\r
-/*\r
- * actStart specifies the start of the page being accessed by\r
- * drawing operations. visStart specifies the contents of the Screen\r
- * Start register, i.e. the start of the visible page.\r
- */\r
-unsigned actStart, visStart;\r
-\r
-/*\r
- * set320x200x256_X()\r
- * sets mode 13h, then turns it into an unchained (planar), 4-page\r
- * 320x200x256 mode.\r
- */\r
-void set320x200x256_X(void)\r
- {\r
- union REGS r;\r
-\r
- /* Set VGA BIOS mode 13h: */\r
- r.x.ax = 0x0013;\r
- int86(0x10, &r, &r);\r
-\r
- /* Turn off the Chain-4 bit (bit 3 at index 4, port 0x3c4): */\r
- outpw(SEQU_ADDR, 0x0604);\r
-\r
- /* Turn off word mode, by setting the Mode Control register\r
- of the CRT Controller (index 0x17, port 0x3d4): */\r
- outpw(CRTC_ADDR, 0xE317);\r
-\r
- /* Turn off doubleword mode, by setting the Underline Location\r
- register (index 0x14, port 0x3d4): */\r
- outpw(CRTC_ADDR, 0x0014);\r
-\r
- /* Clear entire video memory, by selecting all four planes, then\r
- writing 0 to entire segment. */\r
- outpw(SEQU_ADDR, 0x0F02);\r
- memset(vga+1, 0, 0xffff); /* stupid size_t exactly 1 too small */\r
- vga[0] = 0;\r
-\r
- /* Update the global variables to reflect dimensions of this\r
- mode. This is needed by most future drawing operations. */\r
- width = 320;\r
- height = 200;\r
-\r
- /* Each byte addresses four pixels, so the width of a scan line\r
- in *bytes* is one fourth of the number of pixels on a line. */\r
- widthBytes = width / 4;\r
-\r
- /* By default we want screen refreshing and drawing operations\r
- to be based at offset 0 in the video segment. */\r
- actStart = visStart = 0;\r
- }\r
-\r
-/*\r
- * setActiveStart() tells our graphics operations which address in video\r
- * memory should be considered the top left corner.\r
- */\r
-void setActiveStart(unsigned offset)\r
- {\r
- actStart = offset;\r
- }\r
-\r
-/*\r
- * setVisibleStart() tells the VGA from which byte to fetch the first\r
- * pixel when starting refresh at the top of the screen. This version\r
- * won't look very well in time critical situations (games for\r
- * instance) as the register outputs are not synchronized with the\r
- * screen refresh. This refresh might start when the high byte is\r
- * set, but before the low byte is set, which produces a bad flicker.\r
- */\r
-void setVisibleStart(unsigned offset)\r
- {\r
- visStart = offset;\r
- outpw(CRTC_ADDR, 0x0C); /* set high byte */\r
- outpw(CRTC_ADDR+1, visStart >> 8);\r
- outpw(CRTC_ADDR, 0x0D); /* set low byte */\r
- outpw(CRTC_ADDR+1, visStart & 0xff);\r
- }\r
-\r
-/*\r
- * setXXXPage() sets the specified page by multiplying the page number\r
- * with the size of one page at the current resolution, then handing the\r
- * resulting offset value over to the corresponding setXXXStart()\r
- * function. The first page is number 0.\r
- */\r
-void setActivePage(int page)\r
- {\r
- setActiveStart(page * widthBytes * height);\r
- }\r
-\r
-void setVisiblePage(int page)\r
- {\r
- setVisibleStart(page * widthBytes * height);\r
- }\r
-\r
-void putPixel_X(int x, int y, byte color)\r
- {\r
- /* Each address accesses four neighboring pixels, so set\r
- Write Plane Enable according to which pixel we want\r
- to modify. The plane is determined by the two least\r
- significant bits of the x-coordinate: */\r
- outp(0x3c4, 0x02);\r
- outp(0x3c5, 0x01 << (x & 3));\r
-\r
- /* The offset of the pixel into the video segment is\r
- offset = (width * y + x) / 4, and write the given\r
- color to the plane we selected above. Heed the active\r
- page start selection. */\r
- vga[(unsigned)(widthBytes * y) + (x / 4) + actStart] = color;\r
-\r
- }\r
-\r
-byte getPixel_X(int x, int y)\r
- {\r
- /* Select the plane from which we must read the pixel color: */\r
- outpw(GRAC_ADDR, 0x04);\r
- outpw(GRAC_ADDR+1, x & 3);\r
-\r
- return vga[(unsigned)(widthBytes * y) + (x / 4) + actStart];\r
-\r
- }\r
-\r
-void set320x240x256_X(void)\r
- {\r
- /* Set the unchained version of mode 13h: */\r
- set320x200x256_X();\r
-\r
- /* Modify the vertical sync polarity bits in the Misc. Output\r
- Register to achieve square aspect ratio: */\r
- outp(0x3C2, 0xE3);\r
-\r
- /* Modify the vertical timing registers to reflect the increased\r
- vertical resolution, and to center the image as good as\r
- possible: */\r
- outpw(0x3D4, 0x2C11); /* turn off write protect */\r
- outpw(0x3D4, 0x0D06); /* vertical total */\r
- outpw(0x3D4, 0x3E07); /* overflow register */\r
- outpw(0x3D4, 0xEA10); /* vertical retrace start */\r
- outpw(0x3D4, 0xAC11); /* vertical retrace end AND wr.prot */\r
- outpw(0x3D4, 0xDF12); /* vertical display enable end */\r
- outpw(0x3D4, 0xE715); /* start vertical blanking */\r
- outpw(0x3D4, 0x0616); /* end vertical blanking */\r
-\r
- /* Update mode info, so future operations are aware of the\r
- resolution */\r
- height = 240;\r
-\r
- }\r
-\r
-\r
-/*tile*/\r
-// This is Bresenham's Line Drawing Algorithm\r
-void drawline(int x1, int y1, int x2, int y2, char col)\r
-{\r
- int d, x, y, ax, ay, sx, sy, dx, dy;\r
-\r
- dx = x2-x1;\r
- ax = ABS(dx) << 1;\r
- sx = SGN(dx);\r
- dy = y2-y1;\r
- ay = ABS(dy) << 1;\r
- sy = SGN(dy);\r
-\r
- x = x1;\r
- y = y1;\r
- if( ax > ay )\r
- {\r
- d = ay - (ax >> 1);\r
- while( x != x2 )\r
- {\r
- putPixel_X( x, y, col );\r
- if( d >= 0 )\r
- {\r
- y += sy;\r
- d -= ax;\r
- }\r
- x += sx;\r
- d += ay;\r
- }\r
- }\r
- else\r
- {\r
- d = ax - (ay >> 1);\r
- while( y != y2 )\r
- {\r
- putPixel_X( x, y, col );\r
- if( d >= 0 )\r
- {\r
- x += sx;\r
- d -= ay;\r
- }\r
- y += sy;\r
- d += ax;\r
- }\r
- }\r
- return;\r
-}\r
-\r
-void drawrect(int x1, int y1, int x2, int y2, char color)\r
-{\r
- /*drawline(x1,y1,x2,y1,color);\r
- drawline(x1,y2,x2,y2,color);\r
- drawline(x1,y1,x1,y2,color);\r
- drawline(x2,y1,x2,y2+1,color);*/
- //_fmemset(vga+x1+y2, color, 16*16);
- byte far *p;
-
- p=vga+y1*width+x1; // make p point to the start of the line
- while((y2-y1)) // repeat for entire line height
- {
- _fmemset(p, color, x2-x1); // set one line
- p+=width; // move down one row
- }\r
-}\r
-\r
-\r
-/*-----------XXXX-------------*/\r
-//---------------------------------------------------\r
-//\r
-// Use the bios to get the address of the 8x8 font\r
-//\r
-// You need a font if you are going to draw text.\r
-//\r
-\r
-int far *\r
-getFont()\r
-{\r
- union REGPACK rg;\r
- int seg;\r
- int off;\r
- memset(&rg, 0, sizeof(rg));\r
-\r
- rg.w.ax = 0x1130;\r
- rg.h.bh = 0x03;\r
- intr(0x10, &rg);\r
- seg = rg.w.es;\r
- off = rg.w.bp;\r
- \r
-\r
- return (int far *)MK_FP(seg, off);\r
-}\r
-\r
-void drawChar(int x, int y, int color, byte c)\r
-{\r
- int i, j;\r
- int mask;\r
- int far *font = getFont() + (c * 8);\r
-\r
- for (i = 0; i < 8; i++)\r
- {\r
- mask = *font;\r
- for (j = 0; j < 8; j++)\r
- {\r
- if (mask & 0x80)\r
- {\r
- //pixel(x + j, y + i, color);\r
- putPixel_X(x + j, y + i, color);\r
- }\r
- mask <<= 1;\r
- }\r
- font++;\r
- }\r
-}\r
-\r
-void drawText(int x, int y, int color, byte string)\r
-{\r
- while (string)\r
- {\r
- drawChar(x, y, color, string);\r
- x += 8;\r
- string++;\r
- }\r
-}\r