From 710257cc9f5d4134fa2692653ea86a385c45abd2 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 30 Jun 2019 23:38:23 +0200
Subject: [PATCH 5/7] kernel/vga: Improve readability of ATC register handling.

---
 sys/dev/video/fb/bmp/splash_bmp.c | 11 ++++-----
 sys/dev/video/fb/vga.c            | 38 +++++++++++++++----------------
 sys/dev/video/fb/vgareg.h         | 28 +++++++++++++++++++++++
 3 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/sys/dev/video/fb/bmp/splash_bmp.c b/sys/dev/video/fb/bmp/splash_bmp.c
index f32adeaaf2..37788ddec9 100644
--- a/sys/dev/video/fb/bmp/splash_bmp.c
+++ b/sys/dev/video/fb/bmp/splash_bmp.c
@@ -591,14 +591,11 @@ bmp_Draw(video_adapter_t *adp)
     /* XXX: this is ugly, but necessary for EGA/VGA 1bpp/4bpp modes */
     if ((adp->va_type == KD_EGA) || (adp->va_type == KD_VGA)) {
 	vga_reset_flipflop();
-	outb(ATC, 0x14);
-	outb(ATC, 0);
-	for (i = 0; i < 16; ++i) {
-	    outb(ATC, i);
-	    outb(ATC, i);
-	}
+	vga_atc_set(COLOR_SELECT, 0, false);
+	for (i = INTERNAL_PALETTE0; i < ATTR_MODE_CTL; ++i)
+	    vga_atc_set(i, i, false);
 	vga_reset_flipflop();
-	outb(ATC, 0x20);			/* enable palette */
+	vga_atc_enable_palette();
 
 	vga_gc_set(ENABLE_SET_RESET, 0x0f);	/* set/reset enable */
 
diff --git a/sys/dev/video/fb/vga.c b/sys/dev/video/fb/vga.c
index 95b47a04be..dc1add0e0c 100644
--- a/sys/dev/video/fb/vga.c
+++ b/sys/dev/video/fb/vga.c
@@ -783,10 +783,9 @@ set_display_start(video_adapter_t *adp, int x, int y)
 
 	/* horizontal pel pan */
 	vga_reset_flipflop();
-	outb(ATC, 0x13 | 0x20);
-	outb(ATC, poff);
+	vga_atc_set(HORIZONTAL_PEL_PAN, poff, true);
 	vga_reset_flipflop();
-	outb(ATC, 0x20);
+	vga_atc_enable_palette();
 
 	vga_crtc_set(PRESET_ROW_SCAN, roff);
 
@@ -1136,13 +1135,13 @@ set_font_mode(video_adapter_t *adp, u_char *buf)
 	buf[3] = vga_gc_get(GRAPHICS_MODE);
 	buf[4] = vga_gc_get(MISCELLANEOUS);
 	vga_reset_flipflop();
-	outb(ATC, 0x10); buf[5] = inb(ATC + 1);
+	buf[5] = vga_atc_get(ATTR_MODE_CTL);
 
 	/* setup vga for loading fonts */
 	vga_reset_flipflop();
-	outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
+	vga_atc_set(ATTR_MODE_CTL, buf[5] & ~0x01, false);
 	vga_reset_flipflop();
-	outb(ATC, 0x20);			/* enable palette */
+	vga_atc_enable_palette();
 	vga_sequencer_set(MAP_MASK, 0x04);
 	vga_sequencer_set(MEMORY_MODE, 0x07);
 	vga_gc_set(READ_MAP_SELECT, 0x02);
@@ -1159,9 +1158,9 @@ set_normal_mode(video_adapter_t *adp, u_char *buf)
 
 	/* setup vga for normal operation mode again */
 	vga_reset_flipflop();
-	outb(ATC, 0x10); outb(ATC, buf[5]);
+	vga_atc_set(ATTR_MODE_CTL, buf[5], false);
 	vga_reset_flipflop();
-	outb(ATC, 0x20);			/* enable palette */
+	vga_atc_enable_palette();
 	vga_sequencer_set(MAP_MASK, buf[0]);
 	vga_sequencer_set(MEMORY_MODE, buf[1]);
 	vga_gc_set(READ_MAP_SELECT, buf[2]);
@@ -1367,7 +1366,7 @@ vga_load_palette(video_adapter_t *adp, const u_char *palette)
 	for (i = 0; i < 256*3; ++i)
 		outb(PALDATA, palette[i] >> 2);
 	vga_reset_flipflop();
-	outb(ATC, 0x20);		/* enable palette */
+	vga_atc_enable_palette();
 	return 0;
 }
 
@@ -1387,7 +1386,7 @@ vga_load_palette2(video_adapter_t *adp, int base, int count,
 		outb(PALDATA, b[i] >> 2);
 	}
 	vga_reset_flipflop();
-	outb(ATC, 0x20);		/* enable palette */
+	vga_atc_enable_palette();
 	return 0;
 }
 
@@ -1401,7 +1400,7 @@ vga_set_border(video_adapter_t *adp, int color)
 	prologue(adp, V_ADP_BORDER, ENODEV);
 
 	vga_reset_flipflop();
-	outb(ATC, 0x31); outb(ATC, color & 0xff);
+	vga_atc_set(OVERSCAN_COLOR, color & 0xff, true);
 
 	return 0;
 }
@@ -1442,15 +1441,15 @@ vga_save_state(video_adapter_t *adp, void *p, size_t size)
 
 	for (i = HORIZONTAL_TOTAL, j = 10; i <= LINE_COMPARE; i++) /* crtc */
 		buf[j++] = vga_crtc_get(i);
-	for (i = 0, j = 35; i < 20; i++) {	/* attribute ctrl */
+	/* attribute ctrl */
+	for (i = INTERNAL_PALETTE0, j = 35; i <= COLOR_SELECT; i++) {
 		vga_reset_flipflop();
-		outb(ATC, i);
-		buf[j++]  =  inb(ATC + 1);
+		buf[j++] = vga_atc_get(i);
 	}
 	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 */
+	vga_atc_enable_palette();
 
 	crit_exit();
 
@@ -1512,14 +1511,13 @@ vga_load_state(video_adapter_t *adp, void *p)
 	for (i = HORIZONTAL_TOTAL; i <= LINE_COMPARE; ++i) /* program crtc */
 		vga_crtc_set(i, buf[i + 10]);
 	vga_reset_flipflop();
-	for (i = 0; i < 20; ++i) {		/* program attribute ctrl */
-		outb(ATC, i);
-		outb(ATC, buf[i + 35]);
-	}
+	/* program attribute ctrl */
+	for (i = INTERNAL_PALETTE0, j = 35; i <= COLOR_SELECT; ++i)
+		vga_atc_set(i, buf[j++], false);
 	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 */
+	vga_atc_enable_palette();
 
 #if 0 /* XXX a temporary workaround for kernel panic */
 #ifndef VGA_NO_MODE_CHANGE
diff --git a/sys/dev/video/fb/vgareg.h b/sys/dev/video/fb/vgareg.h
index 416a920ea6..a1b702a599 100644
--- a/sys/dev/video/fb/vgareg.h
+++ b/sys/dev/video/fb/vgareg.h
@@ -62,6 +62,14 @@
 #define GDCIDX		(IO_VGA + 0x0E)		/* graph data controller idx */
 #define GDCREG		(IO_VGA + 0x0F)		/* graph data controller data */
 
+enum vga_atc_reg {
+	INTERNAL_PALETTE0	= 0x00,
+	ATTR_MODE_CTL		= 0x10,
+	OVERSCAN_COLOR		= 0x11,
+	HORIZONTAL_PEL_PAN	= 0x13,
+	COLOR_SELECT		= 0x14
+};
+
 enum vga_crtc_reg {
 	HORIZONTAL_TOTAL	= 0x00,
 	OVERFLOW		= 0x07,
@@ -103,6 +111,26 @@ vga_reset_flipflop(void)
 	inb(GEN_INPSTAT1);
 }
 
+static __inline uint8_t
+vga_atc_get(enum vga_atc_reg reg)
+{
+	outb(ATC, reg);
+	return inb(ATC + 1);
+}
+
+static __inline void
+vga_atc_set(enum vga_atc_reg reg, uint8_t val, bool ipas)
+{
+	outb(ATC, reg | ipas << 5);
+	outb(ATC, val);
+}
+
+static __inline void
+vga_atc_enable_palette(void)
+{
+	outb(ATC, 0x20);
+}
+
 static __inline uint8_t
 vga_crtc_get(enum vga_crtc_reg reg)
 {
-- 
2.23.0

