Cleaned up all the helper and drawing function areas in NEDsim.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 11 Jul 2021 01:38:05 +0000 (18:38 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 11 Jul 2021 01:38:05 +0000 (18:38 -0700)
hacks/NEDsim/NEDsim.c

index 7192cce..e4d66e2 100644 (file)
@@ -4,6 +4,9 @@
 #include "screenhack.h"
 #include "simulator.h"
 
 #include "screenhack.h"
 #include "simulator.h"
 
+// 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
+
 /* -------------------------------------------------------------------------- */
 /* Front Panel Layout                                                         */
 /* -------------------------------------------------------------------------- */
 /* -------------------------------------------------------------------------- */
 /* Front Panel Layout                                                         */
 /* -------------------------------------------------------------------------- */
@@ -276,8 +279,7 @@ get_text_size(struct NEDsim * nedsim, const char * text, int * x_size, int * y_s
 }
 
 // When specifying the rectangular area to draw, all coordinates and sizes are
 }
 
 // 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.
+// in units of 'cells'.
 static void
 draw_rect_area(struct NEDsim * nedsim, struct color_rgb * color,
                size_t x_origin, size_t y_origin, size_t x_size, size_t y_size,
 static void
 draw_rect_area(struct NEDsim * nedsim, struct color_rgb * color,
                size_t x_origin, size_t y_origin, size_t x_size, size_t y_size,
@@ -291,31 +293,41 @@ draw_rect_area(struct NEDsim * nedsim, struct color_rgb * color,
     x_size *= nedsim->cell_size;
     y_size *= nedsim->cell_size;
     set_color(nedsim, color);
     x_size *= nedsim->cell_size;
     y_size *= nedsim->cell_size;
     set_color(nedsim, color);
-    XFillRectangle(nedsim->dpy, nedsim->panel, 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
+    );
 
     // ...then give it a border, if requested.
     set_color(nedsim, COLOR(border));
     if (bord_top) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
 
     // ...then give it a border, if requested.
     set_color(nedsim, COLOR(border));
     if (bord_top) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
-                x_origin, y_origin, x_size, nedsim->border_size);
+                x_origin, y_origin,
+                x_size, nedsim->border_size
+        );
     }
     if (bord_bottom) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
     }
     if (bord_bottom) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
-                x_origin, (y_origin + y_size - nedsim->border_size), x_size, nedsim->border_size);
+                x_origin, (y_origin + y_size - nedsim->border_size),
+                x_size, nedsim->border_size
+        );
     }
     if (bord_left) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
     }
     if (bord_left) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
-                x_origin, y_origin, nedsim->border_size, y_size);
+                x_origin, y_origin,
+                nedsim->border_size, y_size
+        );
     }
     if (bord_right) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
     }
     if (bord_right) {
         XFillRectangle(nedsim->dpy, nedsim->panel, nedsim->gc,
-                (x_origin + x_size - nedsim->border_size), y_origin, nedsim->border_size, y_size);
+                (x_origin + x_size - nedsim->border_size), y_origin,
+                nedsim->border_size, y_size
+        );
     }
 }
 
 // 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
     }
 }
 
 // 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.
+// 'cells'.
 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)
 {
@@ -347,7 +359,8 @@ draw_circular_area(struct NEDsim * nedsim, size_t x, size_t y, double diameter)
 // Requires that set_font_size() has been run at least once. All values are in
 // units of 'cells'.
 static void
 // Requires that set_font_size() has been run at least once. All values are in
 // units of 'cells'.
 static void
-draw_text(struct NEDsim * nedsim, const char * text, int x_origin, int y_origin, int x_size, int y_size, Bool horizontally_center)
+draw_text(struct NEDsim * nedsim, const char * text,
+        int x_origin, int y_origin, int x_size, int y_size, Bool horizontally_center)
 {
     set_color(nedsim, COLOR(text));
 
 {
     set_color(nedsim, COLOR(text));
 
@@ -358,55 +371,168 @@ draw_text(struct NEDsim * nedsim, const char * text, int x_origin, int y_origin,
     int local_x_offset = 0;
     if (horizontally_center) local_x_offset = ((x_size * nedsim->cell_size) - text_x_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));
+    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)
+    );
+}
+
+/* -------------------------------------------------------------------------- */
+/* Dynamic Panel Functions                                                    */
+/* -------------------------------------------------------------------------- */
+
+// 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)
+{
+    for (int i = 0; i < WORDLINE_WIDTH; i++) {
+        if (word & (1<<(WORDLINE_WIDTH-1-i))) {
+            set_color(nedsim, COLOR(light_on));
+        } else {
+            set_color(nedsim, COLOR(light_off));
+        }
+        draw_circular_area(nedsim, x+i, y, WORDLINE_HEIGHT);
+    }
+}
+
+// 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) {
+        set_color(nedsim, COLOR(error_on));
+    } else {
+        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) {
+        set_color(nedsim, COLOR(light_on));
+    } else {
+        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) {
+        set_color(nedsim, COLOR(light_on));
+    } else {
+        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 the SC.
+    for (int i = 0; i < SC_WIDTH; i++) {
+        if ((SC_WIDTH-1-i) == nedsim->nedstate->active_thread->sc) {
+            set_color(nedsim, COLOR(light_on));
+        } else {
+            set_color(nedsim, COLOR(light_off));
+        }
+        draw_circular_area(nedsim,
+                SC_X_OFFSET+i, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_LIGHT_HEIGHT
+        );
+    }
+
+    // Draw the PC.
+    draw_wordline_lights(nedsim, nedsim->nedstate->active_thread->pc,
+            PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT
+    );
+
+    // Draw the stack lights.
+    int64_t top_of_stack = ((int64_t)nedsim->nedstate->active_thread->sp) - 1;
+    for (int i = 0; i < nedsim->num_data_rows; i++) {
+        if ((top_of_stack-i) >= 0) {
+            draw_wordline_lights(nedsim,
+                    nedsim->nedstate->active_thread->stack[top_of_stack-i],
+                    STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i
+            );
+        } else {
+            draw_wordline_lights(nedsim, 0,
+                    STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i
+            );
+        }
+    }
+
+    // Draw the stack size in text.
+    draw_rect_area(nedsim, COLOR(tertiary),
+            STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET,
+            STACK_WIDTH/2, STACK_LABEL_HEIGHT,
+            True, True, False, False
+    );
+    char stack_size[11];
+    snprintf(stack_size, sizeof(stack_size),"0x%08X", nedsim->nedstate->active_thread->sp);
+    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 (int i = 0; i < nedsim->num_data_rows; i++) {
+        draw_wordline_lights(nedsim,
+                ram_r_word(nedsim->nedstate, HEAP_START_ADDRESS+(i*BPW)),
+                HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i
+        );
+    }
 }
 
 }
 
+/* -------------------------------------------------------------------------- */
+/* Static Panel Functions                                                     */
+/* -------------------------------------------------------------------------- */
+
 // 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)
 {
 // 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)
 {
+    size_t overall_height_in_cells = HEADER_HEIGHT_IN_CELLS + nedsim->num_data_rows + FOOTER_HEIGHT_IN_CELLS;
+
     // Draw background color over entire window.
     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 background color over entire window.
     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.
-    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)
+    draw_rect_area(nedsim, COLOR(panel_fg),
+            0, 0,
+            OVERALL_WIDTH_IN_CELLS, overall_height_in_cells,
+            False, False, False, False
     );
 
     // Give the panel rounded corners by first deleting the four right-angle corners...
     );
 
     // 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
+    draw_rect_area(nedsim, COLOR(panel_bg),
+            0, 0,
+            1, 1,
+            False, False, False, False
     );
     );
-    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
+    draw_rect_area(nedsim, COLOR(panel_bg),
+            OVERALL_WIDTH_IN_CELLS-1, 0,
+            1, 1,
+            False, False, False, False
     );
     );
-    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
+    draw_rect_area(nedsim, COLOR(panel_bg),
+            0, overall_height_in_cells-1,
+            1, 1,
+            False, False, False, False
     );
     );
-    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
+    draw_rect_area(nedsim, COLOR(panel_bg),
+            OVERALL_WIDTH_IN_CELLS-1, overall_height_in_cells-1,
+            1, 1,
+            False, False, False, False
     );
     // ...and then replacing them with filled arcs, forming rounded corners.
     );
     // ...and then replacing them with filled arcs, forming rounded corners.
+    // Note that we use XFillArc() instead of draw_circular_area() since we do
+    // not desire any padding.
     set_color(nedsim, COLOR(panel_fg));
     XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
             nedsim->origin_x_offset,
     set_color(nedsim, COLOR(panel_fg));
     XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
             nedsim->origin_x_offset,
@@ -422,219 +548,176 @@ draw_panel(struct NEDsim * nedsim)
     );
     XFillArc(nedsim->dpy, nedsim->panel, nedsim->gc,
             nedsim->origin_x_offset,
     );
     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->origin_y_offset + (nedsim->cell_size * (overall_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->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->origin_y_offset + (nedsim->cell_size * (overall_height_in_cells-2)),
             nedsim->cell_size * 2, nedsim->cell_size * 2,
             0, -90*64
     );
 }
 
             nedsim->cell_size * 2, nedsim->cell_size * 2,
             0, -90*64
     );
 }
 
-// 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)
 {
-    // First draw the two colored boxes that comprise the logo area.
-    draw_rect_area(nedsim, COLOR(primary), LOGO_X_OFFSET, LOGO_Y_OFFSET, LOGO_WIDTH, LOGO_NAME_HEIGHT, True, True, False, False);
-    draw_rect_area(nedsim, COLOR(tertiary), 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, COLOR(primary),
+            LOGO_X_OFFSET, LOGO_Y_OFFSET,
+            LOGO_WIDTH, LOGO_NAME_HEIGHT,
+            True, True, False, False
+    );
+    draw_rect_area(nedsim, COLOR(tertiary),
+            LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT,
+            LOGO_WIDTH, LOGO_WEBSITE_HEIGHT,
+            False, True, False, False
+    );
     set_font_size(nedsim, LOGO_NAME_HEIGHT);
     set_font_size(nedsim, LOGO_NAME_HEIGHT);
-    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.
+    draw_text(nedsim, "NED", LOGO_X_OFFSET, LOGO_Y_OFFSET,
+            LOGO_WIDTH, LOGO_NAME_HEIGHT, True
+    );
     set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
     set_font_size(nedsim, LOGO_WEBSITE_HEIGHT);
-    draw_text(nedsim, "subgeniuskitty.com", LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT, LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, True);
+    draw_text(nedsim, "subgeniuskitty.com", LOGO_X_OFFSET, LOGO_Y_OFFSET+LOGO_NAME_HEIGHT,
+            LOGO_WIDTH, LOGO_WEBSITE_HEIGHT, True
+    );
 }
 
 }
 
-// Draw the HALT indicator area on the front panel.
 static void
 draw_halt(struct NEDsim * nedsim)
 {
 static void
 draw_halt(struct NEDsim * nedsim)
 {
-    // First draw the two colored boxes that comprise the halt area.
-    draw_rect_area(nedsim, COLOR(tertiary), HALT_X_OFFSET, HALT_Y_OFFSET, HALT_WIDTH, HALT_LIGHT_HEIGHT, True, True, False, False);
-    draw_rect_area(nedsim, COLOR(secondary), HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, False, True, False, False);
-
-    // And finally, draw the label.
-    draw_text(nedsim, "HALT", HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT, HALT_WIDTH, HALT_LABEL_HEIGHT, True);
-}
-
-// 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)
-{
-    for (int i = 0; i < WORDLINE_WIDTH; i++) {
-        if (word & (1<<(WORDLINE_WIDTH-1-i))) {
-            set_color(nedsim, COLOR(light_on));
-        } else {
-            set_color(nedsim, COLOR(light_off));
-        }
-        draw_circular_area(nedsim, x+i, y, WORDLINE_HEIGHT);
-    }
+    draw_rect_area(nedsim, COLOR(tertiary),
+            HALT_X_OFFSET, HALT_Y_OFFSET,
+            HALT_WIDTH, HALT_LIGHT_HEIGHT,
+            True, True, False, False
+    );
+    draw_rect_area(nedsim, COLOR(secondary),
+            HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT,
+            HALT_WIDTH, HALT_LABEL_HEIGHT,
+            False, True, False, False
+    );
+    draw_text(nedsim, "HALT", HALT_X_OFFSET, HALT_Y_OFFSET+HALT_LIGHT_HEIGHT,
+            HALT_WIDTH, HALT_LABEL_HEIGHT, True
+    );
 }
 
 }
 
-// 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.
+// Draw a single 32-bit NED word line at coordinates ('x','y').  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.
-    draw_rect_area(nedsim, COLOR(primary), x, y, WORDLINE_WIDTH, WORDLINE_HEIGHT, False, True, False, False);
+    draw_rect_area(nedsim, COLOR(primary),
+            x, y,
+            WORDLINE_WIDTH, WORDLINE_HEIGHT,
+            False, True, False, False
+    );
 
     // Now, draw stripes in the secondary color.
     for (int i = 0; i < (WORDLINE_WIDTH/(2*WORDLINE_BITS_PER_STRIPE)); i++) {
 
     // Now, draw stripes in the secondary color.
     for (int i = 0; i < (WORDLINE_WIDTH/(2*WORDLINE_BITS_PER_STRIPE)); i++) {
-        draw_rect_area(nedsim, COLOR(secondary), (x+(i*(WORDLINE_WIDTH/WORDLINE_BITS_PER_STRIPE))), y,
-                WORDLINE_BITS_PER_STRIPE, WORDLINE_HEIGHT, False, True, False, False);
+        draw_rect_area(nedsim, COLOR(secondary),
+                (x+(i*(WORDLINE_WIDTH/WORDLINE_BITS_PER_STRIPE))), y,
+                WORDLINE_BITS_PER_STRIPE, WORDLINE_HEIGHT,
+                False, True, False, False
+        );
     }
 }
 
     }
 }
 
-// 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)
 {
-    // First draw the two colored boxes that comprise the PC area.
-    draw_rect_area(nedsim, COLOR(tertiary), PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True, True, False, False);
+    draw_rect_area(nedsim, COLOR(tertiary),
+            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);
     draw_wordline(nedsim, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
-
-    // Now draw the label text "PC".
     draw_text(nedsim, "PC", PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True);
 }
 
     draw_text(nedsim, "PC", PC_X_OFFSET, PC_Y_OFFSET, PC_WIDTH, PC_LABEL_HEIGHT, True);
 }
 
-// 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)
 {
-    // First draw the two colored boxes that comprise the SC area.
-    draw_rect_area(nedsim, COLOR(secondary), SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True, True, False, False);
-    draw_rect_area(nedsim, COLOR(tertiary), 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, COLOR(secondary),
+            SC_X_OFFSET, SC_Y_OFFSET,
+            SC_WIDTH, SC_LABEL_HEIGHT,
+            True, True, False, False
+    );
+    draw_rect_area(nedsim, COLOR(tertiary),
+            SC_X_OFFSET, SC_Y_OFFSET+SC_LABEL_HEIGHT,
+            SC_WIDTH, SC_LIGHT_HEIGHT,
+            False, True, False, False
+    );
     draw_text(nedsim, "SC", SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True);
 }
 
     draw_text(nedsim, "SC", SC_X_OFFSET, SC_Y_OFFSET, SC_WIDTH, SC_LABEL_HEIGHT, True);
 }
 
-// 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)
 {
-    // First draw the four colored boxes that comprise the two PSW areas.
-    draw_rect_area(nedsim, COLOR(secondary), PSW_N_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True, True, False, False);
-    draw_rect_area(nedsim, COLOR(secondary), PSW_Z_X_OFFSET, PSW_Y_OFFSET, PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT, True, True, False, False);
-    draw_rect_area(nedsim, COLOR(tertiary), (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, COLOR(tertiary), (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.
-    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);
+    draw_rect_area(nedsim, COLOR(secondary),
+            PSW_N_X_OFFSET, PSW_Y_OFFSET,
+            PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT,
+            True, True, False, False
+    );
+    draw_rect_area(nedsim, COLOR(secondary),
+            PSW_Z_X_OFFSET, PSW_Y_OFFSET,
+            PSW_LABEL_WIDTH, PSW_LABEL_HEIGHT,
+            True, True, False, False
+    );
+    draw_rect_area(nedsim, COLOR(tertiary),
+            (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, COLOR(tertiary),
+            (PSW_Z_X_OFFSET + 1), PSW_Y_OFFSET+PSW_LABEL_HEIGHT,
+            PSW_LIGHT_WIDTH, PSW_LIGHT_HEIGHT,
+            False, True, False, False
+    );
+    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
+    );
 }
 
 }
 
-// Draw the stack area (but don't populate it yet).
 static void
 draw_stack(struct NEDsim * nedsim)
 {
 static void
 draw_stack(struct NEDsim * nedsim)
 {
-    // First draw the two colored boxes that comprise the stack area.
-    draw_rect_area(nedsim, COLOR(tertiary), STACK_X_OFFSET, STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, True, True, False, False);
+    draw_rect_area(nedsim, COLOR(tertiary),
+            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);
     }
     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_text(nedsim, "Stack Size:", STACK_X_OFFSET+1, STACK_Y_OFFSET, STACK_WIDTH, STACK_LABEL_HEIGHT, False);
+    draw_text(nedsim, "Stack Size:", STACK_X_OFFSET+1, STACK_Y_OFFSET,
+            STACK_WIDTH, STACK_LABEL_HEIGHT, False
+    );
 }
 
 }
 
-// Draw the heap area (but don't populate it yet).
 static void
 draw_heap(struct NEDsim * nedsim)
 {
 static void
 draw_heap(struct NEDsim * nedsim)
 {
-// 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.
-    draw_rect_area(nedsim, COLOR(tertiary), HEAP_X_OFFSET, HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, True, True, False, False);
+    draw_rect_area(nedsim, COLOR(tertiary),
+            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);
     }
     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 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.
+    draw_text(nedsim, "RAM Base:", HEAP_X_OFFSET+1, HEAP_Y_OFFSET,
+            HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
+    );
     char address[11];
     snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
     char address[11];
     snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
-    draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET, HEAP_WIDTH, HEAP_LABEL_HEIGHT, False);
-}
-
-// 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) {
-        set_color(nedsim, COLOR(error_on));
-    } else {
-        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) {
-        set_color(nedsim, COLOR(light_on));
-    } else {
-        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) {
-        set_color(nedsim, COLOR(light_on));
-    } else {
-        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 the SC.
-    for (int i = 0; i < SC_WIDTH; i++) {
-        if ((SC_WIDTH-1-i) == nedsim->nedstate->active_thread->sc) {
-            set_color(nedsim, COLOR(light_on));
-        } else {
-            set_color(nedsim, COLOR(light_off));
-        }
-        draw_circular_area(nedsim, SC_X_OFFSET+i, SC_Y_OFFSET+SC_LABEL_HEIGHT, SC_LIGHT_HEIGHT);
-    }
-
-    // Draw the PC.
-    draw_wordline_lights(nedsim, nedsim->nedstate->active_thread->pc, PC_X_OFFSET, PC_Y_OFFSET+PC_LABEL_HEIGHT);
-
-    // Draw the stack lights.
-    int64_t top_of_stack = ((int64_t)nedsim->nedstate->active_thread->sp) - 1;
-    for (int i = 0; i < nedsim->num_data_rows; i++) {
-        if ((top_of_stack-i) >= 0) {
-            draw_wordline_lights(nedsim, nedsim->nedstate->active_thread->stack[top_of_stack-i], STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i);
-        } else {
-            draw_wordline_lights(nedsim, 0, STACK_X_OFFSET, STACK_Y_OFFSET+STACK_LABEL_HEIGHT+i);
-        }
-    }
-
-    // Draw the stack size in text.
-    draw_rect_area(nedsim, COLOR(tertiary), STACK_X_OFFSET+(STACK_WIDTH/2), STACK_Y_OFFSET, STACK_WIDTH/2, STACK_LABEL_HEIGHT, True, True, False, False);
-    char stack_size[11];
-    snprintf(stack_size, sizeof(stack_size), "0x%08X", nedsim->nedstate->active_thread->sp);
-    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 (int i = 0; i < nedsim->num_data_rows; i++) {
-        draw_wordline_lights(nedsim, ram_r_word(nedsim->nedstate, HEAP_START_ADDRESS+(i*BPW)), HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i);
-    }
+    draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET,
+            HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
+    );
 }
 
 /* -------------------------------------------------------------------------- */
 }
 
 /* -------------------------------------------------------------------------- */
@@ -668,9 +751,7 @@ NEDsim_free(Display * dpy, Window win, void * closure)
 static void *
 NEDsim_init(Display * dpy, Window win)
 {
 static void *
 NEDsim_init(Display * dpy, Window win)
 {
-    // =========================================================================
-    //  Basic Setup
-    // =========================================================================
+    // Basic Setup ---------------------------------------------------------------------------------
 
     struct NEDsim * nedsim;
 
 
     struct NEDsim * nedsim;
 
@@ -693,9 +774,7 @@ NEDsim_init(Display * dpy, Window win)
 
     nedsim->panel = XCreatePixmap(nedsim->dpy, nedsim->win, nedsim->dpy_width, nedsim->dpy_height, xgwa.depth);
 
 
     nedsim->panel = XCreatePixmap(nedsim->dpy, nedsim->win, nedsim->dpy_width, nedsim->dpy_height, xgwa.depth);
 
-    // =========================================================================
-    //  Process User Requests
-    // =========================================================================
+    // 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. */
@@ -719,9 +798,7 @@ NEDsim_init(Display * dpy, Window win)
         nedsim->color_index = 0;
     }
 
         nedsim->color_index = 0;
     }
 
-    // =========================================================================
-    //  Scale Panel To Screen
-    // =========================================================================
+    // Scale Panel To Screen -----------------------------------------------------------------------
 
     // We desire to make the panel as wide as possible while also making the
     // cell size an integer pixel size.
 
     // We desire to make the panel as wide as possible while also making the
     // cell size an integer pixel size.
@@ -759,9 +836,7 @@ NEDsim_init(Display * dpy, Window win)
     // 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 Initial Panel --------------------------------------------------------------------------
 
     draw_panel(nedsim);
     draw_logo(nedsim);
 
     draw_panel(nedsim);
     draw_logo(nedsim);