simple.c revision 53013
128328Ssos/*-
228328Ssos * Copyright (c) 1991-1997 S�ren Schmidt
328328Ssos * All rights reserved.
428328Ssos *
528328Ssos * Redistribution and use in source and binary forms, with or without
628328Ssos * modification, are permitted provided that the following conditions
728328Ssos * are met:
828328Ssos * 1. Redistributions of source code must retain the above copyright
928328Ssos *    notice, this list of conditions and the following disclaimer
1028328Ssos *    in this position and unchanged.
1128328Ssos * 2. Redistributions in binary form must reproduce the above copyright
1228328Ssos *    notice, this list of conditions and the following disclaimer in the
1328328Ssos *    documentation and/or other materials provided with the distribution.
1428328Ssos * 3. The name of the author may not be used to endorse or promote products
1528328Ssos *    derived from this software withough specific prior written permission
1628328Ssos *
1728328Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1828328Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1928328Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2028328Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2128328Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2228328Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2328328Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2428328Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2528328Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2628328Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2728328Ssos *
2850476Speter * $FreeBSD: head/lib/libvgl/simple.c 53013 1999-11-08 11:37:46Z yokota $
2928328Ssos */
3028328Ssos
3128328Ssos#include <signal.h>
3228328Ssos#include <machine/console.h>
3328328Ssos#include "vgl.h"
3428328Ssos
3528328Ssosstatic byte VGLSavePaletteRed[256];
3628328Ssosstatic byte VGLSavePaletteGreen[256];
3728328Ssosstatic byte VGLSavePaletteBlue[256];
3828328Ssos
3928328Ssos#define ABS(a)		(((a)<0) ? -(a) : (a))
4028328Ssos#define SGN(a)		(((a)<0) ? -1 : 1)
4153013Syokota#define min(x, y)	(((x) < (y)) ? (x) : (y))
4253013Syokota#define max(x, y)	(((x) > (y)) ? (x) : (y))
4328328Ssos
4428328Ssosvoid
4528328SsosVGLSetXY(VGLBitmap *object, int x, int y, byte color)
4628328Ssos{
4753013Syokota  int offset;
4853013Syokota
4928328Ssos  VGLCheckSwitch();
5053013Syokota  if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
5128328Ssos    if (!VGLMouseFreeze(x, y, 1, 1, color)) {
5228328Ssos      switch (object->Type) {
5328328Ssos      case MEMBUF:
5428328Ssos      case VIDBUF8:
5553013Syokota        object->Bitmap[y*object->VXsize+x]=(color);
5628328Ssos        break;
5753013Syokota      case VIDBUF8S:
5853013Syokota	object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=(color);
5953013Syokota	break;
6028328Ssos      case VIDBUF8X:
6128328Ssos        outb(0x3c4, 0x02);
6228328Ssos        outb(0x3c5, 0x01 << (x&0x3));
6353013Syokota	object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = (color);
6428328Ssos	break;
6553013Syokota      case VIDBUF4S:
6653013Syokota	offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
6753013Syokota	goto set_planar;
6828328Ssos      case VIDBUF4:
6953013Syokota	offset = y*VGLAdpInfo.va_line_width + x/8;
7053013Syokotaset_planar:
7153013Syokota        outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
7253013Syokota        outb(0x3ce, 0x00); outb(0x3cf, color & 0x0f);	/* set/reset */
7353013Syokota        outb(0x3ce, 0x01); outb(0x3cf, 0x0f);		/* set/reset enable */
7453013Syokota        outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8));	/* bit mask */
7553013Syokota	object->Bitmap[offset] |= color;
7628328Ssos      }
7728328Ssos    }
7828328Ssos    VGLMouseUnFreeze();
7928328Ssos  }
8028328Ssos}
8128328Ssos
8228328Ssosbyte
8328328SsosVGLGetXY(VGLBitmap *object, int x, int y)
8428328Ssos{
8553013Syokota  int offset;
8653013Syokota#if 0
8753013Syokota  int i;
8853013Syokota  byte color;
8953013Syokota  byte mask;
9053013Syokota#endif
9153013Syokota
9228328Ssos  VGLCheckSwitch();
9353013Syokota  if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
9453013Syokota    return 0;
9528328Ssos  switch (object->Type) {
9628328Ssos    case MEMBUF:
9728328Ssos    case VIDBUF8:
9853013Syokota      return object->Bitmap[((y*object->VXsize)+x)];
9953013Syokota    case VIDBUF8S:
10053013Syokota      return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
10128328Ssos    case VIDBUF8X:
10228328Ssos      outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
10353013Syokota      return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
10453013Syokota    case VIDBUF4S:
10553013Syokota      offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
10653013Syokota      goto get_planar;
10728328Ssos    case VIDBUF4:
10853013Syokota      offset = y*VGLAdpInfo.va_line_width + x/8;
10953013Syokotaget_planar:
11053013Syokota#if 1
11153013Syokota      return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0;	/* XXX */
11253013Syokota#else
11353013Syokota      color = 0;
11453013Syokota      mask = 0x80 >> (x%8);
11553013Syokota      for (i = 0; i < VGLModeInfo.vi_planes; i++) {
11653013Syokota	outb(0x3ce, 0x04); outb(0x3cf, i);
11753013Syokota	color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
11853013Syokota      }
11953013Syokota      return color;
12053013Syokota#endif
12128328Ssos  }
12228328Ssos  return 0;
12328328Ssos}
12428328Ssos
12528328Ssosvoid
12628328SsosVGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
12728328Ssos{
12828328Ssos  int d, x, y, ax, ay, sx, sy, dx, dy;
12928328Ssos
13028328Ssos  dx = x2-x1; ax = ABS(dx)<<1; sx = SGN(dx); x = x1;
13128328Ssos  dy = y2-y1; ay = ABS(dy)<<1; sy = SGN(dy); y = y1;
13228328Ssos
13328328Ssos  if (ax>ay) {					/* x dominant */
13428328Ssos    d = ay-(ax>>1);
13528328Ssos    for (;;) {
13628328Ssos      VGLSetXY(object, x, y, color);
13728328Ssos      if (x==x2)
13828328Ssos	break;
13928328Ssos      if (d>=0) {
14028328Ssos	y += sy; d -= ax;
14128328Ssos      }
14228328Ssos      x += sx; d += ay;
14328328Ssos    }
14428328Ssos  }
14528328Ssos  else {					/* y dominant */
14628328Ssos    d = ax-(ay>>1);
14728328Ssos    for (;;) {
14828328Ssos      VGLSetXY(object, x, y, color);
14928328Ssos      if (y==y2)
15028328Ssos	break;
15128328Ssos      if (d>=0) {
15228328Ssos	x += sx; d -= ay;
15328328Ssos      }
15428328Ssos      y += sy; d += ax;
15528328Ssos    }
15628328Ssos  }
15728328Ssos}
15828328Ssos
15928328Ssosvoid
16028328SsosVGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
16128328Ssos{
16228328Ssos  VGLLine(object, x1, y1, x2, y1, color);
16328328Ssos  VGLLine(object, x2, y1, x2, y2, color);
16428328Ssos  VGLLine(object, x2, y2, x1, y2, color);
16528328Ssos  VGLLine(object, x1, y2, x1, y1, color);
16628328Ssos}
16728328Ssos
16828328Ssosvoid
16928328SsosVGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
17028328Ssos{
17128328Ssos  int y;
17228328Ssos
17328328Ssos  for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
17428328Ssos}
17528328Ssos
17628328Ssosvoid
17728328Ssosinline set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, byte color)
17828328Ssos{
17928328Ssos  if (x!=0) {
18028328Ssos    VGLSetXY(object, xc+x, yc+y, color);
18128328Ssos    VGLSetXY(object, xc-x, yc+y, color);
18228328Ssos    if (y!=0) {
18328328Ssos      VGLSetXY(object, xc+x, yc-y, color);
18428328Ssos      VGLSetXY(object, xc-x, yc-y, color);
18528328Ssos    }
18628328Ssos  }
18728328Ssos  else {
18828328Ssos    VGLSetXY(object, xc, yc+y, color);
18928328Ssos    if (y!=0)
19028328Ssos      VGLSetXY(object, xc, yc-y, color);
19128328Ssos  }
19228328Ssos}
19328328Ssos
19428328Ssosvoid
19528328SsosVGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, byte color)
19628328Ssos{
19728328Ssos  int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
19828328Ssos  int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
19928328Ssos
20028328Ssos  while (dx<dy) {
20128328Ssos    set4pixels(object, x, y, xc, yc, color);
20228328Ssos    if (d>0) {
20328328Ssos      y--; dy-=asq2; d-=dy;
20428328Ssos    }
20528328Ssos    x++; dx+=bsq2; d+=bsq+dx;
20628328Ssos  }
20728328Ssos  d+=(3*(asq-bsq)/2-(dx+dy))/2;
20828328Ssos  while (y>=0) {
20928328Ssos    set4pixels(object, x, y, xc, yc, color);
21028328Ssos    if (d<0) {
21128328Ssos      x++; dx+=bsq2; d+=dx;
21228328Ssos    }
21328328Ssos    y--; dy-=asq2; d+=asq-dy;
21428328Ssos  }
21528328Ssos}
21628328Ssos
21728328Ssosvoid
21828328Ssosinline set2lines(VGLBitmap *object, int x, int y, int xc, int yc, byte color)
21928328Ssos{
22028328Ssos  if (x!=0) {
22128328Ssos    VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
22228328Ssos    if (y!=0)
22328328Ssos      VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
22428328Ssos  }
22528328Ssos  else {
22628328Ssos    VGLLine(object, xc, yc+y, xc, yc-y, color);
22728328Ssos  }
22828328Ssos}
22928328Ssos
23028328Ssosvoid
23128328SsosVGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, byte color)
23228328Ssos{
23328328Ssos  int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
23428328Ssos  int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
23528328Ssos
23628328Ssos  while (dx<dy) {
23728328Ssos    set2lines(object, x, y, xc, yc, color);
23828328Ssos    if (d>0) {
23928328Ssos      y--; dy-=asq2; d-=dy;
24028328Ssos    }
24128328Ssos    x++; dx+=bsq2; d+=bsq+dx;
24228328Ssos  }
24328328Ssos  d+=(3*(asq-bsq)/2-(dx+dy))/2;
24428328Ssos  while (y>=0) {
24528328Ssos    set2lines(object, x, y, xc, yc, color);
24628328Ssos    if (d<0) {
24728328Ssos      x++; dx+=bsq2; d+=dx;
24828328Ssos    }
24928328Ssos    y--; dy-=asq2; d+=asq-dy;
25028328Ssos  }
25128328Ssos}
25228328Ssos
25328328Ssosvoid
25428328SsosVGLClear(VGLBitmap *object, byte color)
25528328Ssos{
25653013Syokota  int offset;
25753013Syokota  int len;
25853013Syokota
25928328Ssos  VGLCheckSwitch();
26028328Ssos  VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color);
26128328Ssos  switch (object->Type) {
26228328Ssos  case MEMBUF:
26328328Ssos  case VIDBUF8:
26453013Syokota    memset(object->Bitmap, color, object->VXsize*object->VYsize);
26528328Ssos    break;
26653013Syokota
26753013Syokota  case VIDBUF8S:
26853013Syokota    for (offset = 0; offset < object->VXsize*object->VYsize; ) {
26953013Syokota      VGLSetSegment(offset);
27053013Syokota      len = min(object->VXsize*object->VYsize - offset,
27153013Syokota		VGLAdpInfo.va_window_size);
27253013Syokota      memset(object->Bitmap, color, len);
27353013Syokota      offset += len;
27453013Syokota    }
27553013Syokota    break;
27653013Syokota
27728328Ssos  case VIDBUF8X:
27828328Ssos    /* XXX works only for Xsize % 4 = 0 */
27953013Syokota    outb(0x3c6, 0xff);
28028328Ssos    outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
28153013Syokota    memset(object->Bitmap, color, VGLAdpInfo.va_line_width*object->VYsize);
28228328Ssos    break;
28328328Ssos
28428328Ssos  case VIDBUF4:
28553013Syokota  case VIDBUF4S:
28628328Ssos    /* XXX works only for Xsize % 8 = 0 */
28753013Syokota    outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
28853013Syokota    outb(0x3ce, 0x05); outb(0x3cf, 0x02);		/* mode 2 */
28953013Syokota    outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
29053013Syokota    outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
29153013Syokota    for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
29253013Syokota      VGLSetSegment(offset);
29353013Syokota      len = min(object->VXsize*object->VYsize - offset,
29453013Syokota		VGLAdpInfo.va_window_size);
29553013Syokota      memset(object->Bitmap, color, len);
29653013Syokota      offset += len;
29753013Syokota    }
29853013Syokota    outb(0x3ce, 0x05); outb(0x3cf, 0x00);
29928328Ssos    break;
30028328Ssos  }
30128328Ssos  VGLMouseUnFreeze();
30228328Ssos}
30328328Ssos
30428328Ssosvoid
30528328SsosVGLRestorePalette()
30628328Ssos{
30728328Ssos  int i;
30828328Ssos
30928328Ssos  outb(0x3C6, 0xFF);
31028328Ssos  inb(0x3DA);
31128328Ssos  outb(0x3C8, 0x00);
31228328Ssos  for (i=0; i<256; i++) {
31328328Ssos    outb(0x3C9, VGLSavePaletteRed[i]);
31428328Ssos    inb(0x84);
31528328Ssos    outb(0x3C9, VGLSavePaletteGreen[i]);
31628328Ssos    inb(0x84);
31728328Ssos    outb(0x3C9, VGLSavePaletteBlue[i]);
31828328Ssos    inb(0x84);
31928328Ssos  }
32028328Ssos  inb(0x3DA);
32128328Ssos  outb(0x3C0, 0x20);
32228328Ssos}
32328328Ssos
32428328Ssosvoid
32528328SsosVGLSavePalette()
32628328Ssos{
32728328Ssos  int i;
32828328Ssos
32928328Ssos  outb(0x3C6, 0xFF);
33028328Ssos  inb(0x3DA);
33128328Ssos  outb(0x3C7, 0x00);
33228328Ssos  for (i=0; i<256; i++) {
33328328Ssos    VGLSavePaletteRed[i] = inb(0x3C9);
33428328Ssos    inb(0x84);
33528328Ssos    VGLSavePaletteGreen[i] = inb(0x3C9);
33628328Ssos    inb(0x84);
33728328Ssos    VGLSavePaletteBlue[i] = inb(0x3C9);
33828328Ssos    inb(0x84);
33928328Ssos  }
34028328Ssos  inb(0x3DA);
34128328Ssos  outb(0x3C0, 0x20);
34228328Ssos}
34328328Ssos
34428328Ssosvoid
34528328SsosVGLSetPalette(byte *red, byte *green, byte *blue)
34628328Ssos{
34728328Ssos  int i;
34828328Ssos
34928328Ssos  for (i=0; i<256; i++) {
35028328Ssos    VGLSavePaletteRed[i] = red[i];
35128328Ssos    VGLSavePaletteGreen[i] = green[i];
35228328Ssos    VGLSavePaletteBlue[i] = blue[i];
35328328Ssos  }
35428328Ssos  VGLCheckSwitch();
35528328Ssos  outb(0x3C6, 0xFF);
35628328Ssos  inb(0x3DA);
35728328Ssos  outb(0x3C8, 0x00);
35828328Ssos  for (i=0; i<256; i++) {
35928328Ssos    outb(0x3C9, VGLSavePaletteRed[i]);
36028328Ssos    inb(0x84);
36128328Ssos    outb(0x3C9, VGLSavePaletteGreen[i]);
36228328Ssos    inb(0x84);
36328328Ssos    outb(0x3C9, VGLSavePaletteBlue[i]);
36428328Ssos    inb(0x84);
36528328Ssos  }
36628328Ssos  inb(0x3DA);
36728328Ssos  outb(0x3C0, 0x20);
36828328Ssos}
36928328Ssos
37028328Ssosvoid
37128328SsosVGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
37228328Ssos{
37328328Ssos  VGLSavePaletteRed[color] = red;
37428328Ssos  VGLSavePaletteGreen[color] = green;
37528328Ssos  VGLSavePaletteBlue[color] = blue;
37628328Ssos  VGLCheckSwitch();
37728328Ssos  outb(0x3C6, 0xFF);
37828328Ssos  inb(0x3DA);
37928328Ssos  outb(0x3C8, color);
38028328Ssos  outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
38128328Ssos  inb(0x3DA);
38228328Ssos  outb(0x3C0, 0x20);
38328328Ssos}
38428328Ssos
38528328Ssosvoid
38628328SsosVGLSetBorder(byte color)
38728328Ssos{
38828328Ssos  VGLCheckSwitch();
38928328Ssos  inb(0x3DA);
39028328Ssos  outb(0x3C0,0x11); outb(0x3C0, color);
39128328Ssos  inb(0x3DA);
39228328Ssos  outb(0x3C0, 0x20);
39328328Ssos}
39428328Ssos
39528328Ssosvoid
39628328SsosVGLBlankDisplay(int blank)
39728328Ssos{
39828328Ssos  byte val;
39928328Ssos
40028328Ssos  VGLCheckSwitch();
40128328Ssos  outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
40228328Ssos  outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
40328328Ssos}
404