X-Git-Url: http://git.subgeniuskitty.com/screensavers/.git/blobdiff_plain/b1906d46b30db40220bcee60c535bc016c1d7dae..b73247cfaa571f5f162eca6e55ee7ea803d31dd7:/hacks/NEDsim/NEDsim.c diff --git a/hacks/NEDsim/NEDsim.c b/hacks/NEDsim/NEDsim.c index e4d66e2..9b5ceef 100644 --- a/hacks/NEDsim/NEDsim.c +++ b/hacks/NEDsim/NEDsim.c @@ -3,9 +3,7 @@ #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 */ @@ -122,6 +120,11 @@ struct NEDsim { // 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; }; @@ -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 */ /* -------------------------------------------------------------------------- */ @@ -209,8 +230,9 @@ set_color(struct NEDsim * nedsim, struct color_rgb * color) XSetForeground(nedsim->dpy, nedsim->gc, temp.pixel); } -// TODO: Make this a lot faster. -// Set font size to fill 'size' cells vertically, minus space for border and padding. +// Set font size to fill 'size' cells vertically, minus space for border and +// padding. There is probably a better way to do this. The current method is +// horribly slow. Call this function as few times as possible. static void set_font_size(struct NEDsim * nedsim, int size) { @@ -220,9 +242,11 @@ set_font_size(struct NEDsim * nedsim, int size) const char * font_size_prefix = "-*-helvetica-*-r-*-*-"; const char * font_size_suffix = "-*-*-*-*-*-*-*"; - size_t buffer_size = strlen(font_size_prefix) + strlen(font_size_suffix) + 100; // '100' since nobody needs font with size in 'points' greater than 100 decimal digits long. + // In the buffer, we reserve 100 DIGITS for the font size integer. + size_t buffer_size = strlen(font_size_prefix) + strlen(font_size_suffix) + 100; char * font_full_name = malloc(buffer_size); - int font_size_in_points = 2; // Start with a 2 pt font and work our way up. + // Start with a 2pt font and work our way up. + int font_size_in_points = 2; while (1) { // Load the font. @@ -481,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, - 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 ); } @@ -714,7 +738,7 @@ draw_heap(struct NEDsim * nedsim) 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 ); @@ -742,7 +766,6 @@ NEDsim_free(Display * dpy, Window win, void * closure) free(nedsim->nedstate); } - // TODO: Replace all this with proper code to free everything related to the screensaver itself. XFreeGC(nedsim->dpy, nedsim->gc); XFreePixmap(nedsim->dpy, nedsim->panel); free(nedsim); @@ -783,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) { - // 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 { - 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. @@ -894,7 +925,8 @@ NEDsim_reshape(Display * dpy, Window win, void * closure, unsigned int w, unsign static const char * NEDsim_defaults[] = { "*delay: 250", - "*color: -1", + "*color: -1", + "*heapwindow: 0x20000000", 0 }; @@ -902,6 +934,7 @@ static XrmOptionDescRec NEDsim_options[] = { { "-delay", ".delay", XrmoptionSepArg, 0 }, { "-binary", ".binary", XrmoptionSepArg, 0 }, { "-color", ".color", XrmoptionSepArg, 0 }, + { "-heapwindow", ".heapwindow", XrmoptionSepArg, 0 }, { 0, 0, 0, 0 } };