From 08187df80bc1032d738887abb743185a1bff727a Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 6 Sep 2016 21:29:23 +0200
Subject: [PATCH 4/7] kernel/vga: Improve readability of GC register handling.

---
 sys/dev/misc/syscons/scvgarndr.c  | 78 ++++++++++++++++---------------
 sys/dev/video/fb/bmp/splash_bmp.c |  6 +--
 sys/dev/video/fb/vga.c            | 44 ++++++++---------
 sys/dev/video/fb/vgareg.h         | 25 ++++++++++
 4 files changed, 88 insertions(+), 65 deletions(-)

diff --git a/sys/dev/misc/syscons/scvgarndr.c b/sys/dev/misc/syscons/scvgarndr.c
index f6dfa316a9..6e869fefab 100644
--- a/sys/dev/misc/syscons/scvgarndr.c
+++ b/sys/dev/misc/syscons/scvgarndr.c
@@ -560,11 +560,11 @@ vga_pxlborder_planar(scr_stat *scp, int color)
 
 	(*vidsw[scp->sc->adapter]->set_border)(scp->sc->adp, color);
 
-	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
-	outw(GDCIDX, 0x0003);		/* data rotate/function select */
-	outw(GDCIDX, 0x0f01);		/* set/reset enable */
-	outw(GDCIDX, 0xff08);		/* bit mask */
-	outw(GDCIDX, (color << 8) | 0x00);	/* set/reset */
+	vga_gc_set(GRAPHICS_MODE, 0x00);	/* read mode 0, write mode 0 */
+	vga_gc_set(DATA_ROTATE, 0x00);		/* data rotate/function select */
+	vga_gc_set(ENABLE_SET_RESET, 0x0f);	/* set/reset enable */
+	vga_gc_set(BIT_MASK, 0xff);		/* bit mask */
+	vga_gc_set(SET_RESET, color);		/* set/reset */
 	line_width = scp->sc->adp->va_line_width;
 	p = scp->sc->adp->va_window;
 	if (scp->yoff > 0)
@@ -582,8 +582,8 @@ vga_pxlborder_planar(scr_stat *scp, int color)
 			bzero_io((void *)(p + line_width*(y + i)
 				     + scp->xoff + scp->xsize), x);
 	}
-	outw(GDCIDX, 0x0000);		/* set/reset */
-	outw(GDCIDX, 0x0001);		/* set/reset enable */
+	vga_gc_set(SET_RESET, 0x00);		/* set/reset */
+	vga_gc_set(ENABLE_SET_RESET, 0x00);	/* set/reset enable */
 	lwkt_reltoken(&vga_token);
 }
 
@@ -719,10 +719,10 @@ vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
 
 	line_width = scp->sc->adp->va_line_width;
 
-	outw(GDCIDX, 0x0305);		/* read mode 0, write mode 3 */
-	outw(GDCIDX, 0x0003);		/* data rotate/function select */
-	outw(GDCIDX, 0x0f01);		/* set/reset enable */
-	outw(GDCIDX, 0xff08);		/* bit mask */
+	vga_gc_set(GRAPHICS_MODE, 0x03);	/* read mode 0, write mode 3 */
+	vga_gc_set(DATA_ROTATE, 0x00);		/* data rotate/function select */
+	vga_gc_set(ENABLE_SET_RESET, 0x0f);	/* set/reset enable */
+	vga_gc_set(BIT_MASK, 0xff);		/* bit mask */
 	bg = -1;
 	if (from + count > scp->xsize*scp->ysize)
 		count = scp->xsize*scp->ysize - from;
@@ -738,14 +738,16 @@ vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
 		/* set background color in EGA/VGA latch */
 		if (bg != col2) {
 			bg = col2;
-			outw(GDCIDX, 0x0005);	/* read mode 0, write mode 0 */
-			outw(GDCIDX, bg | 0x00); /* set/reset */
+			/* read mode 0, write mode 0 */
+			vga_gc_set(GRAPHICS_MODE, 0x00);
+			vga_gc_set(SET_RESET, bg >> 8);
 			writeb(d, 0);
 			c = readb(d);		/* set bg color in the latch */
-			outw(GDCIDX, 0x0305);	/* read mode 0, write mode 3 */
+			/* read mode 0, write mode 3 */
+			vga_gc_set(GRAPHICS_MODE, 0x03);
 		}
 		/* foreground color */
-		outw(GDCIDX, col1 | 0x00);	/* set/reset */
+		vga_gc_set(SET_RESET, col1 >> 8);	/* set/reset */
 		e = d;
 		f = &(scp->font[sc_vtb_getc(&scp->vtb, i)*scp->font_height]);
 		for (j = 0; j < scp->font_height; ++j, ++f) {
@@ -757,9 +759,9 @@ vga_vgadraw_planar(scr_stat *scp, int from, int count, int flip)
 			d += scp->xoff*2 
 				 + (scp->font_height - 1)*line_width;
 	}
-	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
-	outw(GDCIDX, 0x0000);		/* set/reset */
-	outw(GDCIDX, 0x0001);		/* set/reset enable */
+	vga_gc_set(GRAPHICS_MODE, 0x00);	/* read mode 0, write mode 0 */
+	vga_gc_set(SET_RESET, 0x00);		/* set/reset */
+	vga_gc_set(ENABLE_SET_RESET, 0x00);	/* set/reset enable */
 }
 
 static void 
@@ -890,36 +892,36 @@ draw_pxlcursor_planar(scr_stat *scp, int at, int on, int flip)
 	d = VIDEO_MEMORY_POS(scp, at, 1) +
 	    (scp->font_height - scp->cursor_base - 1) * line_width;
 
-	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
-	outw(GDCIDX, 0x0003);		/* data rotate/function select */
-	outw(GDCIDX, 0x0f01);		/* set/reset enable */
+	vga_gc_set(GRAPHICS_MODE, 0x00);	/* read mode 0, write mode 0 */
+	vga_gc_set(DATA_ROTATE, 0x00);		/* data rotate/function select */
+	vga_gc_set(ENABLE_SET_RESET, 0x0f);	/* set/reset enable */
 	/* set background color in EGA/VGA latch */
 	a = sc_vtb_geta(&scp->vtb, at);
 	if (flip)
 		col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
 	else
 		col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
-	outw(GDCIDX, col | 0x00);	/* set/reset */
-	outw(GDCIDX, 0xff08);		/* bit mask */
+	vga_gc_set(SET_RESET, col >> 8);	/* set/reset */
+	vga_gc_set(BIT_MASK, 0xff);		/* bit mask */
 	writeb(d, 0);
-	c = readb(d);			/* set bg color in the latch */
+	c = readb(d);				/* set bg color in the latch */
 	/* foreground color */
 	if (flip)
 		col = (on) ? (a & 0x0f00) : ((a & 0xf000) >> 4);
 	else
 		col = (on) ? ((a & 0xf000) >> 4) : (a & 0x0f00);
-	outw(GDCIDX, col | 0x00);	/* set/reset */
+	vga_gc_set(SET_RESET, col >> 8);	/* set/reset */
 	f = &(scp->font[sc_vtb_getc(&scp->vtb, at)*scp->font_height
 		+ scp->font_height - scp->cursor_base - 1]);
 	height = imin(scp->cursor_height, scp->font_height);
 	for (i = 0; i < height; ++i, --f) {
-		outw(GDCIDX, (*f << 8) | 0x08);	/* bit mask */
+		vga_gc_set(BIT_MASK, *f);	/* bit mask */
 	       	writeb(d, 0);
 		d -= line_width;
 	}
-	outw(GDCIDX, 0x0000);		/* set/reset */
-	outw(GDCIDX, 0x0001);		/* set/reset enable */
-	outw(GDCIDX, 0xff08);		/* bit mask */
+	vga_gc_set(SET_RESET, 0x00);		/* set/reset */
+	vga_gc_set(ENABLE_SET_RESET, 0x00);	/* set/reset enable */
+	vga_gc_set(BIT_MASK, 0xff);		/* bit mask */
 }
 
 static int pxlblinkrate = 0;
@@ -1133,12 +1135,12 @@ draw_pxlmouse_planar(scr_stat *scp, int x, int y)
 	xoff = (x - scp->xoff*8)%8;
 	ymax = imin(y + 16, scp->font_height * (scp->yoff + scp->ysize));
 
-	outw(GDCIDX, 0x0805);		/* read mode 1, write mode 0 */
-	outw(GDCIDX, 0x0001);		/* set/reset enable */
-	outw(GDCIDX, 0x0002);		/* color compare */
-	outw(GDCIDX, 0x0007);		/* color don't care */
-	outw(GDCIDX, 0xff08);		/* bit mask */
-	outw(GDCIDX, 0x0803);		/* data rotate/function select (and) */
+	vga_gc_set(GRAPHICS_MODE, 0x08);	/* read mode 1, write mode 0 */
+	vga_gc_set(ENABLE_SET_RESET, 0x00);	/* set/reset enable */
+	vga_gc_set(COLOR_COMPARE, 0x00);	/* color compare */
+	vga_gc_set(COLOR_DONT_CARE, 0x00);	/* color don't care */
+	vga_gc_set(BIT_MASK, 0xff);		/* bit mask */
+	vga_gc_set(DATA_ROTATE, 0x08);		/* data rotate/function select (and) */
 	p = scp->sc->adp->va_window + line_width*y + x/8;
 	if (x < 8 * (scp->xoff + scp->xsize) - 8) {
 		for (i = y, j = 0; i < ymax; ++i, ++j) {
@@ -1155,7 +1157,7 @@ draw_pxlmouse_planar(scr_stat *scp, int x, int y)
 			p += line_width;
 		}
 	}
-	outw(GDCIDX, 0x1003);		/* data rotate/function select (or) */
+	vga_gc_set(DATA_ROTATE, 0x10);		/* data rotate/function select (or) */
 	p = scp->sc->adp->va_window + line_width*y + x/8;
 	if (x < 8 * (scp->xoff + scp->xsize) - 8) {
 		for (i = y, j = 0; i < ymax; ++i, ++j) {
@@ -1171,8 +1173,8 @@ draw_pxlmouse_planar(scr_stat *scp, int x, int y)
 			p += line_width;
 		}
 	}
-	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
-	outw(GDCIDX, 0x0003);		/* data rotate/function select */
+	vga_gc_set(GRAPHICS_MODE, 0x00);	/* read mode 0, write mode 0 */
+	vga_gc_set(DATA_ROTATE, 0x00);		/* data rotate/function select */
 }
 
 static void
diff --git a/sys/dev/video/fb/bmp/splash_bmp.c b/sys/dev/video/fb/bmp/splash_bmp.c
index 112232bd03..f32adeaaf2 100644
--- a/sys/dev/video/fb/bmp/splash_bmp.c
+++ b/sys/dev/video/fb/bmp/splash_bmp.c
@@ -279,8 +279,8 @@ bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
 	}
 	sofs %= info->adp->va_window_size;
 	bofs = x & 0x7;				/* offset within byte */
-	outw(GDCIDX, (0x8000 >> bofs) | 0x08);	/* bit mask */
-	outw(GDCIDX, (val << 8) | 0x00);	/* set/reset */
+	vga_gc_set(BIT_MASK, 0x80 >> bofs);	/* bit mask */
+	vga_gc_set(SET_RESET, val);		/* set/reset */
 	*(info->vidmem + sofs) ^= 0xff;		/* read-modify-write */
 	break;
 
@@ -600,7 +600,7 @@ bmp_Draw(video_adapter_t *adp)
 	vga_reset_flipflop();
 	outb(ATC, 0x20);			/* enable palette */
 
-	outw(GDCIDX, 0x0f01);			/* set/reset enable */
+	vga_gc_set(ENABLE_SET_RESET, 0x0f);	/* set/reset enable */
 
 	if (bmp_info.sdepth == 1)
 	    vga_sequencer_set(MAP_MASK, 0x01);	/* unmask plane #0 */
diff --git a/sys/dev/video/fb/vga.c b/sys/dev/video/fb/vga.c
index bd86b5b852..95b47a04be 100644
--- a/sys/dev/video/fb/vga.c
+++ b/sys/dev/video/fb/vga.c
@@ -1132,9 +1132,9 @@ set_font_mode(video_adapter_t *adp, u_char *buf)
 	/* save register values */
 	buf[0] = vga_sequencer_get(MAP_MASK);
 	buf[1] = vga_sequencer_get(MEMORY_MODE);
-	outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
-	outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
-	outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
+	buf[2] = vga_gc_get(READ_MAP_SELECT);
+	buf[3] = vga_gc_get(GRAPHICS_MODE);
+	buf[4] = vga_gc_get(MISCELLANEOUS);
 	vga_reset_flipflop();
 	outb(ATC, 0x10); buf[5] = inb(ATC + 1);
 
@@ -1145,9 +1145,9 @@ set_font_mode(video_adapter_t *adp, u_char *buf)
 	outb(ATC, 0x20);			/* enable palette */
 	vga_sequencer_set(MAP_MASK, 0x04);
 	vga_sequencer_set(MEMORY_MODE, 0x07);
-	outw(GDCIDX, 0x0204);
-	outw(GDCIDX, 0x0005);
-	outw(GDCIDX, 0x0406);			/* addr = a0000, 64kb */
+	vga_gc_set(READ_MAP_SELECT, 0x02);
+	vga_gc_set(GRAPHICS_MODE, 0x00);
+	vga_gc_set(MISCELLANEOUS, 0x04);	/* addr = a0000, 64kb */
 
 	crit_exit();
 }
@@ -1164,9 +1164,9 @@ set_normal_mode(video_adapter_t *adp, u_char *buf)
 	outb(ATC, 0x20);			/* enable palette */
 	vga_sequencer_set(MAP_MASK, buf[0]);
 	vga_sequencer_set(MEMORY_MODE, buf[1]);
-	outw(GDCIDX, 0x0004 | (buf[2] << 8));
-	outw(GDCIDX, 0x0005 | (buf[3] << 8));
-	outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
+	vga_gc_set(READ_MAP_SELECT, buf[2]);
+	vga_gc_set(GRAPHICS_MODE, buf[3]);
+	vga_gc_set(MISCELLANEOUS, (buf[4] & 0x03) | 0x0c);
 
 	crit_exit();
 }
@@ -1447,10 +1447,8 @@ vga_save_state(video_adapter_t *adp, void *p, size_t size)
 		outb(ATC, i);
 		buf[j++]  =  inb(ATC + 1);
 	}
-	for (i = 0, j = 55; i < 9; i++) {	/* graph data ctrl */
-		outb(GDCIDX, i);
-		buf[j++]  =  inb(GDCREG);
-	}
+	for (i = SET_RESET, j = 55; i <= BIT_MASK; i++) /* graph data ctrl */
+		buf[j++] = vga_gc_get(i);
 	vga_reset_flipflop();
 	outb(ATC, 0x20);			/* enable palette */
 
@@ -1518,10 +1516,8 @@ vga_load_state(video_adapter_t *adp, void *p)
 		outb(ATC, i);
 		outb(ATC, buf[i + 35]);
 	}
-	for (i = 0; i < 9; ++i) {		/* program graph data ctrl */
-		outb(GDCIDX, i);
-		outb(GDCREG, buf[i + 55]);
-	}
+	for (i = SET_RESET, j = 55; i <= BIT_MASK; i++) /* graph data ctrl */
+		vga_gc_set(i, buf[j++]);
 	vga_reset_flipflop();
 	outb(ATC, 0x20);			/* enable palette */
 
@@ -1701,11 +1697,11 @@ planar_fill(video_adapter_t *adp, int val)
 	int l;
 
 	lwkt_gettoken(&vga_token);
-	outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
-	outw(GDCIDX, 0x0003);		/* data rotate/function select */
-	outw(GDCIDX, 0x0f01);		/* set/reset enable */
-	outw(GDCIDX, 0xff08);		/* bit mask */
-	outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
+	vga_gc_set(GRAPHICS_MODE, 0x00);	/* read mode 0, write mode 0 */
+	vga_gc_set(DATA_ROTATE, 0x00);		/* data rotate/function select */
+	vga_gc_set(ENABLE_SET_RESET, 0x0f);	/* set/reset enable */
+	vga_gc_set(BIT_MASK, 0xff);		/* bit mask */
+	vga_gc_set(SET_RESET, val);		/* set/reset */
 	at = 0;
 	length = adp->va_line_width*adp->va_info.vi_height;
 	while (length > 0) {
@@ -1715,8 +1711,8 @@ planar_fill(video_adapter_t *adp, int val)
 		length -= l;
 		at += l;
 	}
-	outw(GDCIDX, 0x0000);		/* set/reset */
-	outw(GDCIDX, 0x0001);		/* set/reset enable */
+	vga_gc_set(SET_RESET, 0x00);		/* set/reset */
+	vga_gc_set(ENABLE_SET_RESET, 0x00);	/* set/reset enable */
 	lwkt_reltoken(&vga_token);
 }
 
diff --git a/sys/dev/video/fb/vgareg.h b/sys/dev/video/fb/vgareg.h
index 6e47a49dce..416a920ea6 100644
--- a/sys/dev/video/fb/vgareg.h
+++ b/sys/dev/video/fb/vgareg.h
@@ -77,6 +77,18 @@ enum vga_crtc_reg {
 	LINE_COMPARE		= 0x18
 };
 
+enum vga_gc_reg {
+	SET_RESET		= 0x00,
+	ENABLE_SET_RESET	= 0x01,
+	COLOR_COMPARE		= 0x02,
+	DATA_ROTATE		= 0x03,
+	READ_MAP_SELECT		= 0x04,
+	GRAPHICS_MODE		= 0x05,
+	MISCELLANEOUS		= 0x06,
+	COLOR_DONT_CARE		= 0x07,
+	BIT_MASK		= 0x08
+};
+
 enum vga_sequencer_reg {
 	RESET			= 0x00,
 	CLOCKING_MODE		= 0x01,
@@ -123,6 +135,19 @@ vga_crtc_or(enum vga_crtc_reg reg, uint8_t val)
 	vga_crtc_set(reg, oldval | val);
 }
 
+static __inline uint8_t
+vga_gc_get(enum vga_gc_reg reg)
+{
+	outb(GDCIDX, reg);
+	return inb(GDCREG);
+}
+
+static __inline void
+vga_gc_set(enum vga_gc_reg reg, uint8_t val)
+{
+	outw(GDCIDX, reg | (val << 8));
+}
+
 static __inline uint8_t
 vga_sequencer_get(enum vga_sequencer_reg reg)
 {
-- 
2.23.0

