Added COLOR() macro to NEDsim for ease of reading.
[screensavers] / hacks / NEDsim / NEDsim.c
index bee5e3b..01733f1 100644 (file)
 /* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com>                  */
 /* See LICENSE.txt file for copyright and license details.                    */
 
 /* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com>                  */
 /* See LICENSE.txt file for copyright and license details.                    */
 
-// TODO:
-//      - Write a brief description of the machine being simulated. Only one thread, reduced RAM, no meaningful console when running as screensaver, etc.
-
-// CLI Flags:
-//      -path-to-aout-binary
-//      -path-to-font-file
-//      -speed
-
-// Ideas for sample programs to include:
-//      - Build list of integers on the stack (DUP, IM_1, ADD).
-//      - Calculate prime numbers, placing them on the stack.
-//      - Lights sliding back and forth, like PDP-11 front panel with some OSes.
-
 #include "screenhack.h"
 #include "simulator.h"
 
 #include "screenhack.h"
 #include "simulator.h"
 
-/* Keep this source code C89 compliant per XScreensaver's instructions.       */
+/* -------------------------------------------------------------------------- */
+/* Front Panel Layout                                                         */
+/* -------------------------------------------------------------------------- */
+
+// All of these values are in units of 'cells', not 'pixels'. Where applicable,
+// coordinates refer to the upper-left corner of a cell. See the files
+// `layout_reference.*` for details.
+
+#define OVERALL_WIDTH_IN_CELLS      70
+#define HEADER_HEIGHT_IN_CELLS      14
+#define FOOTER_HEIGHT_IN_CELLS      2
+
+#define LOGO_X_OFFSET               2
+#define LOGO_Y_OFFSET               2
+#define LOGO_WIDTH                  20
+#define LOGO_NAME_HEIGHT            6
+#define LOGO_WEBSITE_HEIGHT         2
+
+#define HALT_X_OFFSET               26
+#define HALT_Y_OFFSET               2
+#define HALT_WIDTH                  6
+#define HALT_LIGHT_HEIGHT           6
+#define HALT_LABEL_HEIGHT           2
+
+#define WORDLINE_BITS_PER_STRIPE    4
+#define WORDLINE_WIDTH              32
+#define WORDLINE_HEIGHT             1
+
+#define PC_X_OFFSET                 36
+#define PC_Y_OFFSET                 7
+#define PC_WIDTH                    32
+#define PC_LABEL_HEIGHT             2
+#define PC_LIGHT_HEIGHT             1
+
+#define SC_X_OFFSET                 42
+#define SC_Y_OFFSET                 2
+#define SC_WIDTH                    5
+#define SC_LABEL_HEIGHT             2
+#define SC_LIGHT_HEIGHT             1
+
+#define PSW_N_X_OFFSET              51
+#define PSW_Z_X_OFFSET              58
+#define PSW_Y_OFFSET                2
+#define PSW_LABEL_WIDTH             3
+#define PSW_LABEL_HEIGHT            2
+#define PSW_LIGHT_WIDTH             1
+#define PSW_LIGHT_HEIGHT            1
+
+#define STACK_X_OFFSET              2
+#define STACK_Y_OFFSET              12
+#define STACK_WIDTH                 32
+#define STACK_LABEL_HEIGHT          2
+#define STACK_LIGHT_HEIGHT          1
+
+#define HEAP_X_OFFSET               36
+#define HEAP_Y_OFFSET               12
+#define HEAP_WIDTH                  32
+#define HEAP_LABEL_HEIGHT           2
+#define HEAP_LIGHT_HEIGHT           1
 
 /* -------------------------------------------------------------------------- */
 /* Data Structures                                                            */
 /* -------------------------------------------------------------------------- */
 
 struct NEDsim {
 
 /* -------------------------------------------------------------------------- */
 /* Data Structures                                                            */
 /* -------------------------------------------------------------------------- */
 
 struct NEDsim {
-    /* Various X resources                                                    */
+    // Various X resources
     Display * dpy;
     Window    win;
     GC        gc;
 
     Display * dpy;
     Window    win;
     GC        gc;
 
-    // TODO: Explain these
+    // Tracks the width and height (in pixels) of 'win' on 'dpy'.
     int dpy_width, dpy_height;
 
     int dpy_width, dpy_height;
 
-    // TODO: Explain that this is created during init, then lights are populated/overwritten on each frame. 
+    // To ensure the NED display is always synchronized/valid, we
+    // double-buffer. This is that buffer.
     Pixmap panel;
 
     Pixmap panel;
 
-    /* Delay (in microseconds) between clock cycles in the NED CPU.           */
-    size_t delay;
-
+    // The front panel is defined in a fixed grid of 'cells' which are then
+    // scaled and projected onto the actual screen. This variable tracks the
+    // size of one 'cell' in screen pixels.
     int cell_size;
     int cell_size;
+    // Like above, this variable tracks the scaled size (in pixels) of cell
+    // borders when projecting them on the screen.
     int border_size;
     int border_size;
+
+    // Since the panel will be smaller than the display window, these two
+    // variable track the location (in pixels) of the upper left corner of the
+    // NED panel (pretending it didn't have rounded corners) relative to the
+    // display window's origin.
     int origin_x_offset, origin_y_offset;
     int origin_x_offset, origin_y_offset;
+
+    // Delay (in microseconds) between clock cycles in the NED CPU.
+    size_t delay;
+
+    // Since the heap and stack areas are resized to fit the display, this
+    // variable tracks their height.
     int num_data_rows;
 
     int num_data_rows;
 
+    // This is an index into the color_list[] array, selecting a color scheme.
     size_t color_index;
 
     size_t color_index;
 
+    // If the display is unsuitable (e.g. too small), this boolean is 'true'.
+    // With this, we blank the display but keep the simulation itself running,
+    // allowing the ongoing simulation to display when the situation is
+    // corrected, and allowing us to avoid boring the user with unnecessary
+    // simulation restarts.
     Bool suitable_display;
 
     Bool suitable_display;
 
+    // The font size is dynamically adjusted based on display size. This
+    // variable stores the full font description, including the correct font
+    // size, all in the X11 font specifier format. For example,
+    //             -*-helvetica-*-r-*-*-72-*-*-*-*-*-*-*
+    // if the display were using a 72 point helvetica font without emphasis.
     char * current_font;
 
     char * current_font;
 
-    // TODO: Explain that this contains all the actual state of the NED machine being simulated.
+    // This struct contains all machine state for the simulated NED computer.
     struct NEDstate * nedstate;
 };
 
 struct color_rgb {
     struct NEDstate * nedstate;
 };
 
 struct color_rgb {
-    // TODO: Explain why this is an unsigned short. Copy from WolframAutomata.
+    // The type 'unsigned short' comes from the XColor struct definition.
+    // The red, green, and blue values are always in the range 0 to 65535
+    // inclusive, independent of the number of bits actually used in the
+    // display hardware. The server scales these values to the range used
+    // by the hardware. Black is represented by (0,0,0), and white is
+    // represented by (65535,65535,65535).
     unsigned short red, green, blue;
 };
 
 struct color_scheme {
     unsigned short red, green, blue;
 };
 
 struct color_scheme {
-    // TODO: Explain all this.
+    // The following entries define a full color scheme for a NED panel.
     struct color_rgb
     struct color_rgb
-        panel_bg,
-        panel_fg,
-        light_on,
-        light_off,
-        error_on,
-        error_off,
-        primary,
-        secondary,
-        tertiary,
-        border,
-        text;
+        panel_bg,  // Empty screen space not taken up by the panel
+        panel_fg,  // Panel body/faceplate
+        light_on,  // Illuminated data indicator
+        light_off, // Non-illuminated data indicator
+        error_on,  // Illuminated error indicator
+        error_off, // Non-illuminated error indicator
+        primary,   // Primary panel color (see: logo background)
+        secondary, // Secondary panel color (see: "HALT" text background)
+        tertiary,  // Tertiary panel color (see: "subgeniuskitty.com" text background)
+        border,    // Cell borders
+        text;      // Panel text
 };
 
 };
 
-//static struct color_scheme color_list[] = {
-//    // TODO: Explain all this.
-//    // TODO: Add other color schemes, like purple PDP-11/70.
-//    // TODO: http://www.chdickman.com/pdp8/DECcolors/
-//    {
-//        {63479,63479,63479}, // 092-XXXX-123
-//        { 5140, 2056, 5654}, // 092-XXXX-152
-//        {40092,11051,15677}, // 092-XXXX-139
-//        {30326,13107,12850}, // 092-XXXX-154
-//        {65535,13107,12850}, // homemade
-//        {30326,13107,12850}, // 092-XXXX-154
-//        { 4112,12850,20046}, // 092-XXXX-145
-//        {12336,29555,37008}, // 092-XXXX-151
-//        {30326,24158, 6425}, // 092-XXXX-157
-//        {63479,63479,63479}, // 092-XXXX-123
-//        {63479,63479,63479}  // 092-XXXX-123
-//    }
-//};
-
+// All included color schemes for NEDsim are defined in this array. To select a
+// particular color scheme at runtime, use the `-color N` CLI option.
 static struct color_scheme color_list[] = {
 static struct color_scheme color_list[] = {
-    // TODO: Explain all this.
-    // TODO: Add other color schemes, like purple PDP-11/70.
-    // TODO: http://www.chdickman.com/pdp8/DECcolors/
-    {
+    { // This color scheme is inspired by the KI10 version of the PDP-10.
+      // Many RGB values came from: http://www.chdickman.com/pdp8/DECcolors/
         {63479,63479,63479}, // 092-XXXX-123
         { 5140, 2056, 5654}, // 092-XXXX-152
         {63479,63479,63479}, // 092-XXXX-123
         { 5140, 2056, 5654}, // 092-XXXX-152
-        {65535,11051,15677}, // 092-XXXX-139 - edit
-        {20000,13107,12850}, // 092-XXXX-154 - edit
-        {65535,13107,12850}, // homemade
-        {20000,13107,12850}, // 092-XXXX-154 - edit
+        {65535,11051,15677}, // homemade, derived from 092-XXXX-139
+        {20000,13107,12850}, // homemade, derived from 092-XXXX-154
+        {65535,13107,12850}, // homemade, derived from 092-XXXX-154
+        {20000,13107,12850}, // homemade, derived from 092-XXXX-154
         { 4112,12850,20046}, // 092-XXXX-145
         {12336,29555,37008}, // 092-XXXX-151
         {30326,24158, 6425}, // 092-XXXX-157
         {63479,63479,63479}, // 092-XXXX-123
         {63479,63479,63479}  // 092-XXXX-123
         { 4112,12850,20046}, // 092-XXXX-145
         {12336,29555,37008}, // 092-XXXX-151
         {30326,24158, 6425}, // 092-XXXX-157
         {63479,63479,63479}, // 092-XXXX-123
         {63479,63479,63479}  // 092-XXXX-123
+    },
+    { // This color scheme is (very loosely) inspired by the first version of the PDP-11/70.
+      // Many RGB values came from: http://www.chdickman.com/pdp8/DECcolors/
+        {63479,63479,63479}, // 092-XXXX-123
+        { 5140, 2056, 5654}, // 092-XXXX-152
+        {51400,34952,26682}, // homemade, derived from 092-XXXX-136
+        {20000,13107,12850}, // homemade, derived from 092-XXXX-154
+        {65535,13107,12850}, // homemade, derived from 092-XXXX-154
+        {20000,13107,12850}, // homemade, derived from 092-XXXX-154
+        {28270, 8995,19532}, // 092-XXXX-140
+        {40092,11051,15677}, // 092-XXXX-139
+        {15000,21000, 4000}, // homemade, derived from 092-XXXX-129
+        {63479,63479,63479}, // 092-XXXX-123
+        {63479,63479,63479}  // 092-XXXX-123
     }
 };
 
     }
 };
 
@@ -116,7 +186,10 @@ static struct color_scheme color_list[] = {
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
 
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
 
-// TODO: Explain
+// Fill in boilerplate when selecting colors.
+#define COLOR(X) &color_list[nedsim->color_index].X
+
+// Set foreground color for the current graphics context.
 static void
 set_color(struct NEDsim * nedsim, struct color_rgb * color)
 {
 static void
 set_color(struct NEDsim * nedsim, struct color_rgb * color)
 {
@@ -133,9 +206,8 @@ set_color(struct NEDsim * nedsim, struct color_rgb * color)
     XSetForeground(nedsim->dpy, nedsim->gc, temp.pixel);
 }
 
     XSetForeground(nedsim->dpy, nedsim->gc, temp.pixel);
 }
 
-// TODO: Explain
 // TODO: Make this a lot faster.
 // TODO: Make this a lot faster.
-// Input: size in 'cells', and sets font to fill that size, minus border and padding room.
+// Set font size to fill 'size' cells vertically, minus space for border and padding.
 static void
 set_font_size(struct NEDsim * nedsim, int size)
 {
 static void
 set_font_size(struct NEDsim * nedsim, int size)
 {
@@ -189,8 +261,9 @@ set_font_size(struct NEDsim * nedsim, int size)
     nedsim->current_font = font_full_name;
 }
 
     nedsim->current_font = font_full_name;
 }
 
-// TODO: Explain
-// TODO: Explain that this returns result in pixels so we can track fractional cell usage.
+// Unlike most functions in this program that input/output in units of 'cells',
+// this function uses units of 'pixels' so the caller can track fractional cell
+// usage (e.g. for centering the text).
 static void
 get_text_size(struct NEDsim * nedsim, const char * text, int * x_size, int * y_size)
 {
 static void
 get_text_size(struct NEDsim * nedsim, const char * text, int * x_size, int * y_size)
 {
@@ -202,40 +275,45 @@ get_text_size(struct NEDsim * nedsim, const char * text, int * x_size, int * y_s
     *y_size = overall.ascent - overall.descent;
 }
 
     *y_size = overall.ascent - overall.descent;
 }
 
-// TODO: Explain
-// TODO: Note that this might leave the foreground color changed.
-// Argument coordinates are in 'cells', not pixels.
+// When specifying the rectangular area to draw, all coordinates and sizes are
+// in units of 'cells'.  Also, be aware that this function alters the
+// foreground color.
 static void
 draw_rect_area(struct NEDsim * nedsim, size_t x_origin, size_t y_origin, size_t x_size, size_t y_size,
                Bool bord_top, Bool bord_bottom, Bool bord_left, Bool bord_right)
 {
 static void
 draw_rect_area(struct NEDsim * nedsim, size_t x_origin, size_t y_origin, size_t x_size, size_t y_size,
                Bool bord_top, Bool bord_bottom, Bool bord_left, Bool bord_right)
 {
+    // First fill in the rectangular area...
     x_origin *= nedsim->cell_size;
     x_origin += nedsim->origin_x_offset;
     y_origin *= nedsim->cell_size;
     y_origin += nedsim->origin_y_offset;
     x_size *= nedsim->cell_size;
     y_size *= nedsim->cell_size;
     x_origin *= nedsim->cell_size;
     x_origin += nedsim->origin_x_offset;
     y_origin *= nedsim->cell_size;
     y_origin += nedsim->origin_y_offset;
     x_size *= nedsim->cell_size;
     y_size *= nedsim->cell_size;
-    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, x_origin, y_origin, x_size, y_size);
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, x_origin, y_origin, x_size, y_size);
 
 
-    set_color(nedsim, &color_list[nedsim->color_index].border);
+    // ...then give it a border, if requested.
+    set_color(nedsim, COLOR(border));
     if (bord_top) {
     if (bord_top) {
-        XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, x_origin, y_origin, x_size, nedsim->border_size);
+        XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+                x_origin, y_origin, x_size, nedsim->border_size);
     }
     if (bord_bottom) {
     }
     if (bord_bottom) {
-        XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, x_origin, (y_origin + y_size - nedsim->border_size), x_size, nedsim->border_size);
+        XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+                x_origin, (y_origin + y_size - nedsim->border_size), x_size, nedsim->border_size);
     }
     if (bord_left) {
     }
     if (bord_left) {
-        XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, x_origin, y_origin, nedsim->border_size, y_size);
+        XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+                x_origin, y_origin, nedsim->border_size, y_size);
     }
     if (bord_right) {
     }
     if (bord_right) {
-        XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, (x_origin + x_size - nedsim->border_size), y_origin, nedsim->border_size, y_size);
+        XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+                (x_origin + x_size - nedsim->border_size), y_origin, nedsim->border_size, y_size);
     }
 }
 
     }
 }
 
-// TODO: Explain
-// Arguments are in units of 'cells', not pixels.
-// Will leave foreground color changed.
-// Draws filled circle with upper left corner at x,y.
+// Draws filled circle in a square area with upper left corner at x,y.  When
+// specifying the location and size to draw, all values are in units of
+// 'cells'.  Also, be aware that this function alters the foreground color.
 static void
 draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
 {
 static void
 draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
 {
@@ -260,123 +338,164 @@ draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
     y -= (0.5 * nedsim->border_size);
 
     // Start angle 0 and ending angle 360*64 is one full circle in Xlib units.
     y -= (0.5 * nedsim->border_size);
 
     // Start angle 0 and ending angle 360*64 is one full circle in Xlib units.
-    XFillArc(nedsim->dpy, nedsim->win, nedsim->gc, x, y, diameter, diameter, 0, 360*64);
+    XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc, x, y, diameter, diameter, 0, 360*64);
 }
 
 }
 
-// TODO: Explain
+// Draws text in a square area with upper left corner at (x_origin,y_origin).
+// Requires that set_font_size() has been run at least once. All values are in
+// units of 'cells'.
 static void
 static void
-draw_panel(struct NEDsim * nedsim)
+draw_text(struct NEDsim * nedsim, const char * text, int x_origin, int y_origin, int x_size, int y_size, Bool horizontally_center)
 {
 {
-// TODO: Collect all relevant #defines somewhere.
-#define OVERALL_WIDTH_IN_CELLS 70 
-#define HEADER_HEIGHT_IN_CELLS 14
-#define FOOTER_HEIGHT_IN_CELLS 2
+    set_color(nedsim, COLOR(text));
+
+    int text_x_size, text_y_size;
+    get_text_size(nedsim, text, &text_x_size, &text_y_size);
+
+    int local_y_offset = ((y_size * nedsim->cell_size) - text_y_size) / 2;
+    int local_x_offset = 0;
+    if (horizontally_center) local_x_offset = ((x_size * nedsim->cell_size) - text_x_size) / 2;
 
 
+    XDrawString(nedsim->dpy, nedsim->panel, nedsim->gc, (x_origin * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
+            ((y_origin + y_size) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), text, strlen(text));
+}
+
+// Draws the panel itself. Not the lights/labels/etc, but the flat sheet of
+// metal that is the front panel.
+static void
+draw_panel(struct NEDsim * nedsim)
+{
     // Draw background color over entire window.
     // Draw background color over entire window.
-    set_color(nedsim, &color_list[nedsim->color_index].panel_bg);
-    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
+    set_color(nedsim, COLOR(panel_bg));
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
 
     // Draw NED panel in foreground color.
 
     // Draw NED panel in foreground color.
-    set_color(nedsim, &color_list[nedsim->color_index].panel_fg);
-    XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc,
+    set_color(nedsim, COLOR(panel_fg));
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
             nedsim->origin_x_offset,
             nedsim->origin_y_offset,
             nedsim->cell_size * OVERALL_WIDTH_IN_CELLS,
             nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS)
     );
             nedsim->origin_x_offset,
             nedsim->origin_y_offset,
             nedsim->cell_size * OVERALL_WIDTH_IN_CELLS,
             nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS)
     );
+
+    // Give the panel rounded corners by first deleting the four right-angle corners...
+    set_color(nedsim, COLOR(panel_bg));
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset,
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-1)),
+            nedsim->origin_y_offset,
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 1)),
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-1)),
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 1)),
+            nedsim->cell_size,
+            nedsim->cell_size
+    );
+    // ...and then replacing them with filled arcs, forming rounded corners.
+    set_color(nedsim, COLOR(panel_fg));
+    XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset,
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            180*64, -90*64
+    );
+    XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
+            nedsim->origin_y_offset,
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            0, 90*64
+    );
+    XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset,
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 2)),
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            180*64, 90*64
+    );
+    XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
+            nedsim->origin_x_offset + (nedsim->cell_size * (OVERALL_WIDTH_IN_CELLS-2)),
+            nedsim->origin_y_offset + (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS - 2)),
+            nedsim->cell_size * 2, nedsim->cell_size * 2,
+            0, -90*64
+    );
 }
 
 }
 
-// TODO: Explain
+// Draw the "NED" and "subgeniuskitty.com" logos on the front panel.
 static void
 draw_logo(struct NEDsim * nedsim)
 {
 static void
 draw_logo(struct NEDsim * nedsim)
 {
-#define LOGO_X_OFFSET       2
-#define LOGO_Y_OFFSET       2
-#define LOGO_WIDTH          20
-#define LOGO_NAME_HEIGHT    6
-#define LOGO_WEBSITE_HEIGHT 2
-
     // First draw the two colored boxes that comprise the logo area.
     // First draw the two colored boxes that comprise the logo area.
-    set_color(nedsim, &color_list[nedsim->color_index].primary);
+    set_color(nedsim, COLOR(primary));
     draw_rect_area(nedsim, LOGO_X_OFFSET, LOGO_Y_OFFSET, LOGO_WIDTH, LOGO_NAME_HEIGHT, True, True, False, False);
     draw_rect_area(nedsim, LOGO_X_OFFSET, LOGO_Y_OFFSET, LOGO_WIDTH, LOGO_NAME_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, False, True, False, False);
 
     // Now draw the 'NED' text in the top box.
     draw_rect_area(nedsim, LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, False, True, False, False);
 
     // Now draw the 'NED' text in the top box.
-    set_color(nedsim, &color_list[nedsim->color_index].text);
     set_font_size(nedsim, LOGO_NAME_HEIGHT);
     set_font_size(nedsim, LOGO_NAME_HEIGHT);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "NED", &text_x_size, &text_y_size);
-    int local_x_offset = ((LOGO_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((LOGO_NAME_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (LOGO_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((LOGO_Y_OFFSET+LOGO_NAME_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "NED", 3);
+    draw_text(nedsim, "NED", LOGO_X_OFFSET, LOGO_Y_OFFSET, LOGO_WIDTH, LOGO_NAME_HEIGHT, True);
 
     // And draw the 'subgeniuskitty.com' text in the bottom box.
     set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
 
     // And draw the 'subgeniuskitty.com' text in the bottom box.
     set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
-    get_text_size(nedsim, "subgeniuskitty.com", &text_x_size, &text_y_size);
-    local_x_offset = ((LOGO_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    local_y_offset = ((LOGO_WEBSITE_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (LOGO_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((LOGO_Y_OFFSET+LOGO_NAME_HEIGHT+LOGO_WEBSITE_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "subgeniuskitty.com", 18);
+    draw_text(nedsim, "subgeniuskitty.com", LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, True);
 }
 
 }
 
-// TODO: Explain
+// Draw the HALT indicator area on the front panel.
 static void
 draw_halt(struct NEDsim * nedsim)
 {
 static void
 draw_halt(struct NEDsim * nedsim)
 {
-#define HALT_X_OFFSET     26
-#define HALT_Y_OFFSET     2
-#define HALT_WIDTH        6
-#define HALT_LIGHT_HEIGHT 6
-#define HALT_LABEL_HEIGHT 2
-
     // First draw the two colored boxes that comprise the halt area.
     // First draw the two colored boxes that comprise the halt area.
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH, HALT_LIGHT_HEIGHT, True, True, False, False);
     draw_rect_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH, HALT_LIGHT_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].secondary);
+    set_color(nedsim, COLOR(secondary));
     draw_rect_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, False, True, False, False);
 
     // And finally, draw the label.
     draw_rect_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, False, True, False, False);
 
     // And finally, draw the label.
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "HALT", &text_x_size, &text_y_size);
-    int local_x_offset = ((HALT_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((HALT_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (HALT_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((HALT_Y_OFFSET+HALT_LIGHT_HEIGHT+HALT_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "HALT", 4);
+    draw_text(nedsim, "HALT", HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, True);
 }
 
 }
 
-// TODO: Explain
+// Draw the 32 lights corresponding to 'word' at coordinates ('x','y').
+// Note that this function ONLY draws the lights and is used each frame for
+// updating the panel. To draw the wordline area itself, use draw_wordline().
 static void
 draw_wordline_lights(struct NEDsim * nedsim, uint32_t word, int x, int y)
 {
 static void
 draw_wordline_lights(struct NEDsim * nedsim, uint32_t word, int x, int y)
 {
-#define WORDLINE_BITS_PER_STRIPE 4
-#define WORDLINE_WIDTH           32
-#define WORDLINE_HEIGHT          1
-
     for (int i = 0; i < WORDLINE_WIDTH; i++) {
         if (word & (1<<(WORDLINE_WIDTH-1-i))) {
     for (int i = 0; i < WORDLINE_WIDTH; i++) {
         if (word & (1<<(WORDLINE_WIDTH-1-i))) {
-            set_color(nedsim, &color_list[nedsim->color_index].light_on);
+            set_color(nedsim, COLOR(light_on));
         } else {
         } else {
-            set_color(nedsim, &color_list[nedsim->color_index].light_off);
+            set_color(nedsim, COLOR(light_off));
         }
         draw_circular_area(nedsim, x+i, y, WORDLINE_HEIGHT);
     }
 }
 
         }
         draw_circular_area(nedsim, x+i, y, WORDLINE_HEIGHT);
     }
 }
 
-// TODO: Explain
+// Draw a single 32-bit NED word line at coordinates ('x','y').
+// Note that this draws a wordline with value 0. To update with a specific
+// value, call draw_wordline_lights() after drawing the wordline area at least
+// once.
 static void
 draw_wordline(struct NEDsim * nedsim, int x, int y)
 {
     // First, draw a solid box in the primary color over the entire wordline area.
 static void
 draw_wordline(struct NEDsim * nedsim, int x, int y)
 {
     // First, draw a solid box in the primary color over the entire wordline area.
-    set_color(nedsim, &color_list[nedsim->color_index].primary);
+    set_color(nedsim, COLOR(primary));
     draw_rect_area(nedsim, x, y, WORDLINE_WIDTH, WORDLINE_HEIGHT, False, True, False, False);
 
     // Now, draw stripes in the secondary color.
     int i;
     for (i = 0; i < (WORDLINE_WIDTH/(2*WORDLINE_BITS_PER_STRIPE)); i++) {
     draw_rect_area(nedsim, x, y, WORDLINE_WIDTH, WORDLINE_HEIGHT, False, True, False, False);
 
     // Now, draw stripes in the secondary color.
     int i;
     for (i = 0; i < (WORDLINE_WIDTH/(2*WORDLINE_BITS_PER_STRIPE)); i++) {
-        set_color(nedsim, &color_list[nedsim->color_index].secondary);
+        set_color(nedsim, COLOR(secondary));
         draw_rect_area(nedsim, (x+(i*(WORDLINE_WIDTH/WORDLINE_BITS_PER_STRIPE))), y,
                 WORDLINE_BITS_PER_STRIPE, WORDLINE_HEIGHT, False, True, False, False);
     }
         draw_rect_area(nedsim, (x+(i*(WORDLINE_WIDTH/WORDLINE_BITS_PER_STRIPE))), y,
                 WORDLINE_BITS_PER_STRIPE, WORDLINE_HEIGHT, False, True, False, False);
     }
@@ -385,186 +504,117 @@ draw_wordline(struct NEDsim * nedsim, int x, int y)
     draw_wordline_lights(nedsim, 0, x, y);
 }
 
     draw_wordline_lights(nedsim, 0, x, y);
 }
 
-// TODO: Explain
+// Draw the Program Counter area (but don't populate it yet).
 static void
 draw_pc(struct NEDsim * nedsim)
 {
 static void
 draw_pc(struct NEDsim * nedsim)
 {
-// TODO: Note that all #defines use units of 'cells', not 'pixels', etc.
-#define PC_X_OFFSET     36
-#define PC_Y_OFFSET     7
-#define PC_WIDTH        32
-#define PC_LABEL_HEIGHT 2
-#define PC_LIGHT_HEIGHT 1
-
     // First draw the two colored boxes that comprise the PC area.
     // First draw the two colored boxes that comprise the PC area.
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True, True, False, False);
     draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
 
     // Now draw the label text "PC".
     draw_rect_area(nedsim, PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True, True, False, False);
     draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
 
     // Now draw the label text "PC".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "PC", &text_x_size, &text_y_size);
-    int local_x_offset = ((PC_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((PC_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (PC_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((PC_Y_OFFSET+PC_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "PC", 2);
+    draw_text(nedsim, "PC", PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True);
 }
 
 }
 
-// TODO: Explain
+// Draw the Stack Counter area (but don't populate it yet).
 static void
 draw_sc(struct NEDsim * nedsim)
 {
 static void
 draw_sc(struct NEDsim * nedsim)
 {
-#define SC_X_OFFSET     42
-#define SC_Y_OFFSET     2
-#define SC_WIDTH        5
-#define SC_LABEL_HEIGHT 2
-#define SC_LIGHT_HEIGHT 1
-
     // First draw the two colored boxes that comprise the SC area.
     // First draw the two colored boxes that comprise the SC area.
-    set_color(nedsim, &color_list[nedsim->color_index].secondary);
+    set_color(nedsim, COLOR(secondary));
     draw_rect_area(nedsim, SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True, True, False, False);
     draw_rect_area(nedsim, SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_WIDTH, SC_LIGHT_HEIGHT, False, True, False, False);
 
     // Now draw the label text "SC".
     draw_rect_area(nedsim, SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_WIDTH, SC_LIGHT_HEIGHT, False, True, False, False);
 
     // Now draw the label text "SC".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "SC", &text_x_size, &text_y_size);
-    int local_x_offset = ((SC_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((SC_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (SC_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((SC_Y_OFFSET+SC_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "SC", 2);
+    draw_text(nedsim, "SC", SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True);
 }
 
 }
 
-// TODO: Explain
+// Draw areas for the two PSW flags, 'Z'ero and 'N'egative.
 static void
 draw_psw(struct NEDsim * nedsim)
 {
 static void
 draw_psw(struct NEDsim * nedsim)
 {
-#define PSW_N_X_OFFSET   51
-#define PSW_Z_X_OFFSET   58
-#define PSW_Y_OFFSET     2
-#define PSW_LABEL_WIDTH  3
-#define PSW_LABEL_HEIGHT 2
-#define PSW_LIGHT_WIDTH  1
-#define PSW_LIGHT_HEIGHT 1
-
     // First draw the four colored boxes that comprise the two PSW areas.
     // First draw the four colored boxes that comprise the two PSW areas.
-    set_color(nedsim, &color_list[nedsim->color_index].secondary);
+    set_color(nedsim, COLOR(secondary));
     draw_rect_area(nedsim, PSW_N_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True, True, False, False);
     draw_rect_area(nedsim, PSW_N_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].secondary);
+    set_color(nedsim, COLOR(secondary));
     draw_rect_area(nedsim, PSW_Z_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True, True, False, False);
     draw_rect_area(nedsim, PSW_Z_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, (PSW_N_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT, False, True, False, False);
     draw_rect_area(nedsim, (PSW_N_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT, False, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT, False, True, False, False);
 
     draw_rect_area(nedsim, (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT, False, True, False, False);
 
-    // Now draw the label text "N".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "N", &text_x_size, &text_y_size);
-    int local_x_offset = ((PSW_LABEL_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    int local_y_offset = ((PSW_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (PSW_N_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((PSW_Y_OFFSET+PSW_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "N", 1);
-
-    // Now draw the label text "Z".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    get_text_size(nedsim, "Z", &text_x_size, &text_y_size);
-    local_x_offset = ((PSW_LABEL_WIDTH * nedsim->cell_size) - text_x_size) / 2;
-    local_y_offset = ((PSW_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, (PSW_Z_X_OFFSET * nedsim->cell_size + nedsim->origin_x_offset + local_x_offset),
-            ((PSW_Y_OFFSET+PSW_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "Z", 1);
+    // Now draw the label text.
+    draw_text(nedsim, "N", PSW_N_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True);
+    draw_text(nedsim, "Z", PSW_Z_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True);
 }
 
 }
 
-// TODO: Explain
+// Draw the stack area (but don't populate it yet).
 static void
 draw_stack(struct NEDsim * nedsim)
 {
 static void
 draw_stack(struct NEDsim * nedsim)
 {
-#define STACK_X_OFFSET     2
-#define STACK_Y_OFFSET     12
-#define STACK_WIDTH        32
-#define STACK_LABEL_HEIGHT 2
-#define STACK_LIGHT_HEIGHT 1
-
     // First draw the two colored boxes that comprise the stack area.
     // First draw the two colored boxes that comprise the stack area.
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, STACK_X_OFFSET, STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, True, True, False, False);
     for (int i = 0; i < nedsim->num_data_rows; i++) {
         draw_wordline(nedsim, STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i);
     }
 
     // Now draw the label text "Stack Size:".
     draw_rect_area(nedsim, STACK_X_OFFSET, STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, True, True, False, False);
     for (int i = 0; i < nedsim->num_data_rows; i++) {
         draw_wordline(nedsim, STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i);
     }
 
     // Now draw the label text "Stack Size:".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "Stack Size:", &text_x_size, &text_y_size);
-    int local_y_offset = ((STACK_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, ((STACK_X_OFFSET + 1) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((STACK_Y_OFFSET+STACK_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "Stack Size:", 11);
+    draw_text(nedsim, "Stack Size:", STACK_X_OFFSET+1, STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, False);
 }
 
 }
 
-// TODO: Explain
+// Draw the heap area (but don't populate it yet).
 static void
 draw_heap(struct NEDsim * nedsim)
 {
 static void
 draw_heap(struct NEDsim * nedsim)
 {
-#define HEAP_X_OFFSET     36
-#define HEAP_Y_OFFSET     12
-#define HEAP_WIDTH        32
-#define HEAP_LABEL_HEIGHT 2
-#define HEAP_LIGHT_HEIGHT 1
 // TODO: What should I do about this define? I would like to be able to specify the address so I can do things like display the code itself if nothign interesting happens in RAM.
 #define HEAP_START_ADDRESS 0x20000000
 
     // First draw the two colored boxes that comprise the heap area.
 // TODO: What should I do about this define? I would like to be able to specify the address so I can do things like display the code itself if nothign interesting happens in RAM.
 #define HEAP_START_ADDRESS 0x20000000
 
     // First draw the two colored boxes that comprise the heap area.
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, True, True, False, False);
     for (int i = 0; i < nedsim->num_data_rows; i++) {
         draw_wordline(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
     }
 
     draw_rect_area(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, True, True, False, False);
     for (int i = 0; i < nedsim->num_data_rows; i++) {
         draw_wordline(nedsim, HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
     }
 
-    // Now draw the label text "RAM Base:".
-    set_color(nedsim, &color_list[nedsim->color_index].text);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, "RAM Base:", &text_x_size, &text_y_size);
-    int local_y_offset = ((HEAP_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, ((HEAP_X_OFFSET + 1) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), "RAM Base:", 9);
+    // Now draw the text label "RAM Base:".
+    draw_text(nedsim, "RAM Base:", HEAP_X_OFFSET+1, HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, False);
 
     // Now draw the address text.
 
     // Now draw the address text.
-    set_color(nedsim, &color_list[nedsim->color_index].text);
     char address[11];
     snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
     char address[11];
     snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
-    get_text_size(nedsim, address, &text_x_size, &text_y_size);
-    local_y_offset = ((HEAP_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, ((HEAP_X_OFFSET + 1 + (HEAP_WIDTH / 2)) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), address, strlen(address));
+    draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, False);
 }
 
 }
 
-// TODO: Explain
+// After the static front panel has been drawn at least once, this function
+// updates all the dynamic parts of the panel (lights + text values) and is
+// called every frame.
 static void
 update_display(struct NEDsim * nedsim)
 {
     // Draw the halt indicator.
     if (nedsim->nedstate->halted) {
 static void
 update_display(struct NEDsim * nedsim)
 {
     // Draw the halt indicator.
     if (nedsim->nedstate->halted) {
-        set_color(nedsim, &color_list[nedsim->color_index].error_on);
+        set_color(nedsim, COLOR(error_on));
     } else {
     } else {
-        set_color(nedsim, &color_list[nedsim->color_index].error_off);
+        set_color(nedsim, COLOR(error_off));
     }
     draw_circular_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH);
 
     // Draw the PSW "N" light.
     if (nedsim->nedstate->active_thread->psw->negative) {
     }
     draw_circular_area(nedsim, HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH);
 
     // Draw the PSW "N" light.
     if (nedsim->nedstate->active_thread->psw->negative) {
-        set_color(nedsim, &color_list[nedsim->color_index].light_on);
+        set_color(nedsim, COLOR(light_on));
     } else {
     } else {
-        set_color(nedsim, &color_list[nedsim->color_index].light_off);
+        set_color(nedsim, COLOR(light_off));
     }
     draw_circular_area(nedsim, PSW_N_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT);
 
     // Draw the PSW "Z" light.
     if (nedsim->nedstate->active_thread->psw->zero) {
     }
     draw_circular_area(nedsim, PSW_N_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT);
 
     // Draw the PSW "Z" light.
     if (nedsim->nedstate->active_thread->psw->zero) {
-        set_color(nedsim, &color_list[nedsim->color_index].light_on);
+        set_color(nedsim, COLOR(light_on));
     } else {
     } else {
-        set_color(nedsim, &color_list[nedsim->color_index].light_off);
+        set_color(nedsim, COLOR(light_off));
     }
     draw_circular_area(nedsim, PSW_Z_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT);
 
     }
     draw_circular_area(nedsim, PSW_Z_X_OFFSET+1, PSW_Y_OFFSET+PSW_LABEL_HEIGHT, PSW_LIGHT_HEIGHT);
 
@@ -573,9 +623,9 @@ update_display(struct NEDsim * nedsim)
     int i;
     for (i = 0; i < SC_WIDTH; i++) {
         if ((SC_WIDTH-1-i) == nedsim->nedstate->active_thread->sc) {
     int i;
     for (i = 0; i < SC_WIDTH; i++) {
         if ((SC_WIDTH-1-i) == nedsim->nedstate->active_thread->sc) {
-            set_color(nedsim, &color_list[nedsim->color_index].light_on);
+            set_color(nedsim, COLOR(light_on));
         } else {
         } else {
-            set_color(nedsim, &color_list[nedsim->color_index].light_off);
+            set_color(nedsim, COLOR(light_off));
         }
         draw_circular_area(nedsim, SC_X_OFFSET+i, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_LIGHT_HEIGHT);
     }
         }
         draw_circular_area(nedsim, SC_X_OFFSET+i, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_LIGHT_HEIGHT);
     }
@@ -594,16 +644,11 @@ update_display(struct NEDsim * nedsim)
     }
 
     // Draw the stack size in text.
     }
 
     // Draw the stack size in text.
-    set_color(nedsim, &color_list[nedsim->color_index].tertiary);
+    set_color(nedsim, COLOR(tertiary));
     draw_rect_area(nedsim, STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET, STACK_WIDTH/2, STACK_LABEL_HEIGHT, True, True, False, False);
     draw_rect_area(nedsim, STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET, STACK_WIDTH/2, STACK_LABEL_HEIGHT, True, True, False, False);
-    set_color(nedsim, &color_list[nedsim->color_index].text);
     char stack_size[11];
     snprintf(stack_size, sizeof(stack_size), "0x%08X", nedsim->nedstate->active_thread->sp);
     char stack_size[11];
     snprintf(stack_size, sizeof(stack_size), "0x%08X", nedsim->nedstate->active_thread->sp);
-    int text_x_size, text_y_size;
-    get_text_size(nedsim, stack_size, &text_x_size, &text_y_size);
-    int local_y_offset = ((STACK_LABEL_HEIGHT * nedsim->cell_size) - text_y_size) / 2;
-    XDrawString(nedsim->dpy, nedsim->win, nedsim->gc, ((STACK_X_OFFSET + 1 + (STACK_WIDTH / 2)) * nedsim->cell_size + nedsim->origin_x_offset),
-            ((STACK_Y_OFFSET+STACK_LABEL_HEIGHT) * nedsim->cell_size + nedsim->origin_y_offset - local_y_offset), stack_size, strlen(stack_size));
+    draw_text(nedsim, stack_size, (STACK_X_OFFSET+(STACK_WIDTH/2)+1), STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, False);
 
     // Draw the heap lights.
     for (i = 0; i < nedsim->num_data_rows; i++) {
 
     // Draw the heap lights.
     for (i = 0; i < nedsim->num_data_rows; i++) {
@@ -624,18 +669,29 @@ NEDsim_event(Display * dpy, Window win, void * closure, XEvent * event)
 static void
 NEDsim_free(Display * dpy, Window win, void * closure)
 {
 static void
 NEDsim_free(Display * dpy, Window win, void * closure)
 {
-    // TODO: Replace all this with proper code to free everything.
     struct NEDsim * nedsim = closure;
     struct NEDsim * nedsim = closure;
+
+    if (nedsim->nedstate != NULL) {
+        free(nedsim->nedstate->active_thread->psw);
+        free(nedsim->nedstate->active_thread);
+        free(nedsim->nedstate->hack);
+        free(nedsim->nedstate);
+    }
+
+    // TODO: Replace all this with proper code to free everything related to the screensaver itself.
     XFreeGC(nedsim->dpy, nedsim->gc);
     XFreeGC(nedsim->dpy, nedsim->gc);
+    XFreePixmap(nedsim->dpy, nedsim->panel);
     free(nedsim);
 }
 
 static void *
 NEDsim_init(Display * dpy, Window win)
 {
     free(nedsim);
 }
 
 static void *
 NEDsim_init(Display * dpy, Window win)
 {
+    // =========================================================================
+    //  Basic Setup
+    // =========================================================================
+
     struct NEDsim * nedsim;
     struct NEDsim * nedsim;
-    XGCValues gcv;
-    XWindowAttributes xgwa;
 
     nedsim = calloc(1, sizeof(*nedsim));
     if (!nedsim) {
 
     nedsim = calloc(1, sizeof(*nedsim));
     if (!nedsim) {
@@ -646,50 +702,86 @@ NEDsim_init(Display * dpy, Window win)
     nedsim->dpy = dpy;
     nedsim->win = win;
 
     nedsim->dpy = dpy;
     nedsim->win = win;
 
+    XGCValues gcv;
+    nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv);
+
+    XWindowAttributes xgwa;
     XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
     nedsim->dpy_width = xgwa.width;
     nedsim->dpy_height = xgwa.height;
 
     XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
     nedsim->dpy_width = xgwa.width;
     nedsim->dpy_height = xgwa.height;
 
-    // TODO: Explain that this is the delay between each clock cycle of the simulated NED CPU.
+    nedsim->panel = XCreatePixmap(nedsim->dpy, nedsim->win, nedsim->dpy_width, nedsim->dpy_height, xgwa.depth);
+
+    // =========================================================================
+    //  Process User Requests
+    // =========================================================================
+
     nedsim->delay = get_integer_resource(nedsim->dpy, "delay", "Integer");
     nedsim->delay *= 1000; /* Turn milliseconds into microseconds. */
 
     nedsim->delay = get_integer_resource(nedsim->dpy, "delay", "Integer");
     nedsim->delay *= 1000; /* Turn milliseconds into microseconds. */
 
-    // TODO: Read in the a.out file. This should be done in the simulator's init function call?
-    nedsim->nedstate = init_simulator();
-
-    nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv);
+    // If the user did not supply a program for execution, randomly select one
+    // of the included programs.
+    char * input_file = get_string_resource(nedsim->dpy, "binary", "String");
+    if (input_file == NULL) {
+        // TODO: Need to include some default programs and randomly select one to load into RAM.
+        nedsim->nedstate = init_simulator("./test.out");
+    } else {
+        nedsim->nedstate = init_simulator(input_file);
+    }
 
 
-    // TODO: Do this properly.
-    nedsim->color_index = 0;
+    // If the user did not select a color scheme, select one randomly.
+    nedsim->color_index = get_integer_resource(nedsim->dpy, "color", "Integer");
+    if (nedsim->color_index == -1) {
+        nedsim->color_index = random() % sizeof(color_list)/sizeof(color_list[0]);
+    } else if (nedsim->color_index >= sizeof(color_list)/sizeof(color_list[0])) {
+        fprintf(stderr, "WARNING: Color index out of range.\n");
+        nedsim->color_index = 0;
+    }
 
 
-// TODO: Save the GIMP reference diagram somewhere, along with notes about the size/spacing, and that each cell is 10 pixels across in the reference image.
+    // =========================================================================
+    //  Scale Panel To Screen
+    // =========================================================================
 
 
-    nedsim->cell_size = nedsim->dpy_width / OVERALL_WIDTH_IN_CELLS ; // make panel as wide as it can be while keeping every cell an integer pixel size.
-    // TODO: What is my minimum cell_size? Below that, I should simply paint the window red and print an error in the console. Perform that check here, right after setting cell_size.
-    //       For now, we'll just make it 10 pixels?
+    // We desire to make the panel as wide as possible while also making the
+    // cell size an integer pixel size.
+    nedsim->cell_size = nedsim->dpy_width / OVERALL_WIDTH_IN_CELLS;
+    // Cell sizes below about 10 pixels result in unreadable fonts. For now,
+    // we'll use that as our cutoff. It also works well with a 1:10 border:cell
+    // ratio.
     if (nedsim->cell_size < 10) {
         nedsim->suitable_display = False;
         return nedsim;
     }
     if (nedsim->cell_size < 10) {
         nedsim->suitable_display = False;
         return nedsim;
     }
-    nedsim->origin_x_offset = (nedsim->dpy_width - (nedsim->cell_size * OVERALL_WIDTH_IN_CELLS)) / 2; // center panel horizontally
-
-    // Determine how many rows for the stack and heap displays. Make it the largest power of two that fits on the display.
-    int available_space_for_data_rows = nedsim->dpy_height - (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + FOOTER_HEIGHT_IN_CELLS));
+    // Center the panel horizontally.
+    nedsim->origin_x_offset = (nedsim->dpy_width -
+            (nedsim->cell_size * OVERALL_WIDTH_IN_CELLS)) / 2;
+
+    // The stack and heap displays are variable height. Size them to fit the
+    // display window.
+    int available_space_for_data_rows = nedsim->dpy_height -
+            (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + FOOTER_HEIGHT_IN_CELLS));
     for (int i = 0; ; i++) {
     for (int i = 0; ; i++) {
-        if ((nedsim->cell_size * (1 << i)) > available_space_for_data_rows) {
-            nedsim->num_data_rows = (1 << --i);
+        if ((nedsim->cell_size * i) > available_space_for_data_rows) {
+            nedsim->num_data_rows = --i;
             break;
         }
     }
             break;
         }
     }
+    // Enforce a minimum vertical size, though '4' is arbitrary.
     if (nedsim->num_data_rows < 4) {
         nedsim->suitable_display = False;
         return nedsim;
     }
     if (nedsim->num_data_rows < 4) {
         nedsim->suitable_display = False;
         return nedsim;
     }
-    nedsim->origin_y_offset = (nedsim->dpy_height - (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS))) / 2; // center panel vertically
+    // Center the panel vertically.
+    nedsim->origin_y_offset = (nedsim->dpy_height -
+            (nedsim->cell_size * (HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS))) / 2;
 
     // Scale border relative to cell_size in a 1:10 relationship.
     nedsim->border_size = nedsim->cell_size / 10;
 
 
     // Scale border relative to cell_size in a 1:10 relationship.
     nedsim->border_size = nedsim->cell_size / 10;
 
+    // =========================================================================
+    //  Draw Initial Panel
+    // =========================================================================
+
     draw_panel(nedsim);
     draw_logo(nedsim);
     draw_halt(nedsim);
     draw_panel(nedsim);
     draw_logo(nedsim);
     draw_halt(nedsim);
@@ -709,14 +801,18 @@ NEDsim_draw(Display * dpy, Window win, void * closure)
 {
     struct NEDsim * nedsim = closure;
 
 {
     struct NEDsim * nedsim = closure;
 
+    // Update the panel display buffer.
     if (nedsim->suitable_display) {
         nedsim->nedstate = run_simulator(nedsim->nedstate);
         update_display(nedsim);
     } else {
     if (nedsim->suitable_display) {
         nedsim->nedstate = run_simulator(nedsim->nedstate);
         update_display(nedsim);
     } else {
-        set_color(nedsim, &color_list[nedsim->color_index].error_on);
-        XFillRectangle(nedsim->dpy, nedsim->win, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
+        set_color(nedsim, COLOR(error_on));
+        XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height);
     }
 
     }
 
+    // Copy panel buffer to display window.
+    XCopyArea(nedsim->dpy, nedsim->panel, nedsim->win, nedsim->gc, 0, 0, nedsim->dpy_width, nedsim->dpy_height, 0, 0);
+
     return nedsim->delay;
 }
 
     return nedsim->delay;
 }
 
@@ -724,22 +820,32 @@ static void
 NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h)
 {
     struct NEDsim * nedsim = closure;
 NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsigned int h)
 {
     struct NEDsim * nedsim = closure;
+
+    // Only re-initialize the display if it changed size.
     XWindowAttributes xgwa;
     XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
     XWindowAttributes xgwa;
     XGetWindowAttributes(nedsim->dpy, nedsim->win, &xgwa);
-    /* Only restart the simulation if the window changed size.                */
     if (nedsim->dpy_width != xgwa.width || nedsim->dpy_height != xgwa.height) {
     if (nedsim->dpy_width != xgwa.width || nedsim->dpy_height != xgwa.height) {
-        NEDsim_free(dpy, win, closure);
-        closure = NEDsim_init(dpy, win);
+        // Although we are re-initializing the display, we wish to retain the
+        // in-progress NED simulation. Thus, we retain the NEDstate pointer.
+        struct NEDstate * original_nedstate = nedsim->nedstate;
+        nedsim->nedstate = NULL;
+        NEDsim_free(dpy, win, nedsim);
+        struct NEDsim * new_nedsim = NEDsim_init(dpy, win);
+        new_nedsim->nedstate = original_nedstate;
+        closure = new_nedsim;
     }
 }
 
 static const char * NEDsim_defaults[] = {
     "*delay:              250",
     }
 }
 
 static const char * NEDsim_defaults[] = {
     "*delay:              250",
+    "*color:               -1",
     0
 };
 
 static XrmOptionDescRec NEDsim_options[] = {
     { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
     0
 };
 
 static XrmOptionDescRec NEDsim_options[] = {
     { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
+    { "-binary",             ".binary",                 XrmoptionSepArg, 0      },
+    { "-color",              ".color",                  XrmoptionSepArg, 0      },
     { 0, 0, 0, 0 }
 };
 
     { 0, 0, 0, 0 }
 };