about summary refs log tree commit diff stats
path: root/tools/vga_palette.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-03-03 22:09:50 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-03-03 22:21:03 -0800
commit71e4f3812982dba2efb471283d310224e8db363e (patch)
treeea111a1acb8b8845dbda39c0e1b4bac1d198143b /tools/vga_palette.c
parentc6b928be29ac8cdb4e4d6e1eaa20420ff03e5a4c (diff)
downloadmu-71e4f3812982dba2efb471283d310224e8db363e.tar.gz
7842 - new directory organization
Baremetal is now the default build target and therefore has its sources
at the top-level. Baremetal programs build using the phase-2 Mu toolchain
that requires a Linux kernel. This phase-2 codebase which used to be at
the top-level is now under the linux/ directory. Finally, the phase-2 toolchain,
while self-hosting, has a way to bootstrap from a C implementation, which
is now stored in linux/bootstrap. The bootstrap C implementation uses some
literate programming tools that are now in linux/bootstrap/tools.

So the whole thing has gotten inverted. Each directory should build one
artifact and include the main sources (along with standard library). Tools
used for building it are relegated to sub-directories, even though those
tools are often useful in their own right, and have had lots of interesting
programs written using them.

A couple of things have gotten dropped in this process:
  - I had old ways to run on just a Linux kernel, or with a Soso kernel.
    No more.
  - I had some old tooling for running a single test at the cursor. I haven't
    used that lately. Maybe I'll bring it back one day.

The reorg isn't done yet. Still to do:
  - redo documentation everywhere. All the README files, all other markdown,
    particularly vocabulary.md.
  - clean up how-to-run comments at the start of programs everywhere
  - rethink what to do with the html/ directory. Do we even want to keep
    supporting it?

In spite of these shortcomings, all the scripts at the top-level, linux/
and linux/bootstrap are working. The names of the scripts also feel reasonable.
This is a good milestone to take stock at.
Diffstat (limited to 'tools/vga_palette.c')
-rw-r--r--tools/vga_palette.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/tools/vga_palette.c b/tools/vga_palette.c
new file mode 100644
index 00000000..9f4161ce
--- /dev/null
+++ b/tools/vga_palette.c
@@ -0,0 +1,179 @@
+/* Visualize the standard VGA palette.
+ * Based on https://github.com/canidlogic/vgapal (MIT License) */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+char* bar(int n) {
+  char* result = calloc(1024, 32);
+  int i;
+  strcat(result, "<div style='float:left; width:40em'>");
+  for (i = 0; i < n; ++i)
+//?     result[i] = '.';
+    strcat(result, "&#9632;");  /* black square */
+//?   for (; i < 64; ++i)
+//?     result[i] = ' ';
+//?   result[64] = '\0';
+  strcat(result, "&nbsp;</div>");  /* make sure the div occupies space */
+  return result;
+}
+
+/* convert 6-bit color to 8-bit color */
+int levelUp(int n) {
+  assert(n < 64);
+  /* duplicate two most significant bits in two least significant bits */
+  return (n<<2) | (n>>4);
+}
+
+void addColor(int r, int g, int b) {
+  static int i = 0;
+//?   printf("%02x %02x %02x\n", r, g, b);
+//?   printf("%3d: %2d %2d %2d %s %s %s\n", i, r, g, b, bar(r), bar(g), bar(b));
+  printf("<div style='clear:both; white-space:pre; color:#%02x%02x%02x'><div style='float:left; margin-right:1em'>%03d: %02d %02d %02d</div> %s %s %s</div>\n", levelUp(r), levelUp(g), levelUp(b), i, r, g, b, bar(r), bar(g), bar(b));
+  ++i;
+}
+
+void addGrayColor(int v) {
+  addColor(v, v, v);
+}
+
+void add16Color(int lo, int melo, int mehi, int hi) {
+  int r, g, b, i, h, l;
+
+  for (i = 0; i < 8; i++) {
+    r = g = b = lo;
+    if (i & 4) r = mehi;
+    if (i & 2) g = (i==6)?melo:mehi;  /* exception: color 6 is brown rather than dark yellow */
+    if (i & 1) b = mehi;
+    addColor(r, g, b);
+  }
+
+  for (i = 0; i < 8; i++) {
+    r = g = b = melo;
+    if (i & 4) r = hi;
+    if (i & 2) g = hi;
+    if (i & 1) b = hi;
+    addColor(r, g, b);
+  }
+}
+
+/* Add four colors to the palette corresponding to a "run" within an RGB cycle.
+ *
+ * start - high and low starting states for each channel at the start of the run
+ * ch - the channel to change from high to low or low to high */
+void addRun(int start, int ch, int lo, int melo, int me, int mehi, int hi) {
+  int r, g, b, i, up;
+
+  /* Check parameters */
+  if (start < 0 || start > 7)
+    abort();
+  if (ch != 1 && ch != 2 && ch != 4)
+    abort();
+
+  /* Get the starting RGB color and add it */
+  r = lo;
+  g = lo;
+  b = lo;
+  if ((start & 4) == 4)
+    r = hi;
+  if ((start & 2) == 2)
+    g = hi;
+  if ((start & 1) == 1)
+    b = hi;
+  addColor(r, g, b);
+
+  /* If selected channel starts high, we're going down; otherwise we're going up */
+  up = (start & ch) != ch;
+
+  /* Add remaining three colors of the run */
+  switch (ch) {
+  case 4:  r = up?melo:mehi; break;
+  case 2:  g = up?melo:mehi; break;
+  case 1:  b = up?melo:mehi; break;
+  }
+  addColor(r, g, b);
+
+  switch (ch) {
+  case 4:  r = me; break;
+  case 2:  g = me; break;
+  case 1:  b = me; break;
+  }
+  addColor(r, g, b);
+
+  switch (ch) {
+  case 4:  r = up?mehi:melo; break;
+  case 2:  g = up?mehi:melo; break;
+  case 1:  b = up?mehi:melo; break;
+  }
+  addColor(r, g, b);
+}
+
+/* A cycle consists of six 4-color runs, each of which transitions from
+ * one hue to another until arriving back at starting position. */
+void addCycle(int lo, int melo, int me, int mehi, int hi) {
+  int hue = 1;  /* blue */
+  addRun(hue, 4, lo, melo, me, mehi, hi);
+  hue ^= 4;
+  assert(hue == 5);
+  addRun(hue, 1, lo, melo, me, mehi, hi);
+  hue ^= 1;
+  assert(hue == 4);
+  addRun(hue, 2, lo, melo, me, mehi, hi);
+  hue ^= 2;
+  assert(hue == 6);
+  addRun(hue, 4, lo, melo, me, mehi, hi);
+  hue ^= 4;
+  assert(hue == 2);
+  addRun(hue, 1, lo, melo, me, mehi, hi);
+  hue ^= 1;
+  assert(hue == 3);
+  addRun(hue, 2, lo, melo, me, mehi, hi);
+}
+
+int main(void) {
+  int i;
+
+  /* 16-color palette */
+  add16Color(0, 21, 42, 63);
+
+  /* 16 shades of gray */
+  addGrayColor( 0);
+  addGrayColor( 5);
+  addGrayColor( 8);
+  addGrayColor(11);
+  addGrayColor(14);
+  addGrayColor(17);
+  addGrayColor(20);
+  addGrayColor(24);
+  addGrayColor(28);
+  addGrayColor(32);
+  addGrayColor(36);
+  addGrayColor(40);
+  addGrayColor(45);
+  addGrayColor(50);
+  addGrayColor(56);
+  addGrayColor(63);
+
+  /* Nine RGB cycles organized in three groups of three cycles,
+   * The groups represent high/medium/low value,
+   * and the cycles within the groups represent high/medium/low saturation. */
+  addCycle( 0, 16, 31, 47, 63);
+  addCycle(31, 39, 47, 55, 63);
+  addCycle(45, 49, 54, 58, 63);
+
+  addCycle( 0,  7, 14, 21, 28);
+  addCycle(14, 17, 21, 24, 28);
+  addCycle(20, 22, 24, 26, 28);
+
+  addCycle( 0,  4,  8, 12, 16);
+  addCycle( 8, 10, 12, 14, 16);
+  addCycle(11, 12, 13, 15, 16);
+
+  /* final eight palette entries are full black */
+  for (i = 0; i < 8; ++i)
+    addGrayColor(0);
+
+  return 0;
+}