Added ability to directly embed NED programs in NEDsim binary for runtime execution.
[screensavers] / hacks / NEDsim / NEDsim.c
index 15a25a8..9b5ceef 100644 (file)
@@ -3,9 +3,7 @@
 
 #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
+#include "ned_programs/embedded_ned_program_declarations.h"
 
 /* -------------------------------------------------------------------------- */
 /* Front Panel Layout                                                         */
 
 /* -------------------------------------------------------------------------- */
 /* Front Panel Layout                                                         */
@@ -122,6 +120,11 @@ struct NEDsim {
     // if the display were using a 72 point helvetica font without emphasis.
     char * current_font;
 
     // if the display were using a 72 point helvetica font without emphasis.
     char * current_font;
 
+    // Contains the address of the top of the heap window. In total, the heap
+    // window will display from `heap_window_offset` to
+    // `heap_window_offset+num_data_rows`.
+    uint32_t heap_window_offset;
+
     // This struct contains all machine state for the simulated NED computer.
     struct NEDstate * nedstate;
 };
     // This struct contains all machine state for the simulated NED computer.
     struct NEDstate * nedstate;
 };
@@ -185,6 +188,24 @@ static struct color_scheme color_list[] = {
     }
 };
 
     }
 };
 
+// Each instance of this struct will describe a single NED1 program embedded in
+// NEDsim as a binary blob. See `ned_programs/README.md` for details.
+struct ned_program {
+    const uint8_t * binary_blob; // Pointer to raw NED1 machine code
+    const size_t * blob_size;    // Size of raw NED1 machine code
+    uint32_t heap_window_offset; // Location of window into NED system RAM
+};
+
+// All NED1 programs embedded in NEDsim should be manually added to this array.
+// See `ned_programs/README.md` for details.
+static struct ned_program ned_programs[] = {
+    {
+        integer_stack,
+        &integer_stack_size,
+        0x20000000
+    }
+};
+
 /* -------------------------------------------------------------------------- */
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
 /* -------------------------------------------------------------------------- */
 /* Helper Functions                                                           */
 /* -------------------------------------------------------------------------- */
@@ -484,7 +505,7 @@ update_display(struct NEDsim * nedsim)
     // Draw the heap lights.
     for (int i = 0; i < nedsim->num_data_rows; i++) {
         draw_wordline_lights(nedsim,
     // 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)),
+                ram_r_word(nedsim->nedstate, nedsim->heap_window_offset+(i*BPW)),
                 HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i
         );
     }
                 HEAP_X_OFFSET, HEAP_Y_OFFSET+HEAP_LABEL_HEIGHT+i
         );
     }
@@ -717,7 +738,7 @@ draw_heap(struct NEDsim * nedsim)
             HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
     );
     char address[11];
             HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
     );
     char address[11];
-    snprintf(address, sizeof(address), "0x%08X", HEAP_START_ADDRESS);
+    snprintf(address, sizeof(address), "0x%08X", nedsim->heap_window_offset);
     draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET,
             HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
     );
     draw_text(nedsim, address, (HEAP_X_OFFSET+(HEAP_WIDTH/2)+1), HEAP_Y_OFFSET,
             HEAP_WIDTH, HEAP_LABEL_HEIGHT, False
     );
@@ -785,10 +806,18 @@ NEDsim_init(Display * dpy, Window win)
     // of the included programs.
     char * input_file = get_string_resource(nedsim->dpy, "binary", "String");
     if (input_file == NULL) {
     // 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");
+        size_t id = random() % sizeof(ned_programs)/sizeof(ned_programs[0]);
+        nedsim->nedstate = init_simulator(NULL, ned_programs[id].binary_blob, ned_programs[id].blob_size);
+        nedsim->heap_window_offset = ned_programs[id].heap_window_offset;
     } else {
     } else {
-        nedsim->nedstate = init_simulator(input_file);
+        nedsim->nedstate = init_simulator(input_file, NULL, 0);
+        // Note: Since we're accepting this as a runtime CLI option, we really
+        // should verify that `heap_window_offset+num_data_rows` doesn't exceed
+        // the limits of NED's simulated RAM. However, since the RAM size is a
+        // #define located in the simulator itself, and since I mostly plan to
+        // use this screensaver with built-in programs, for now we'll just let
+        // it segfault if the user requests a window outside simulated RAM.
+        nedsim->heap_window_offset = get_integer_resource(nedsim->dpy, "heapwindow", "Integer");
     }
 
     // If the user did not select a color scheme, select one randomly.
     }
 
     // If the user did not select a color scheme, select one randomly.
@@ -896,7 +925,8 @@ NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsign
 
 static const char * NEDsim_defaults[] = {
     "*delay:              250",
 
 static const char * NEDsim_defaults[] = {
     "*delay:              250",
-    "*color:               -1",
+    "*color:              -1",
+    "*heapwindow:         0x20000000",
     0
 };
 
     0
 };
 
@@ -904,6 +934,7 @@ static XrmOptionDescRec NEDsim_options[] = {
     { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
     { "-binary",             ".binary",                 XrmoptionSepArg, 0      },
     { "-color",              ".color",                  XrmoptionSepArg, 0      },
     { "-delay",              ".delay",                  XrmoptionSepArg, 0      },
     { "-binary",             ".binary",                 XrmoptionSepArg, 0      },
     { "-color",              ".color",                  XrmoptionSepArg, 0      },
+    { "-heapwindow",         ".heapwindow",             XrmoptionSepArg, 0      },
     { 0, 0, 0, 0 }
 };
 
     { 0, 0, 0, 0 }
 };