From 5923644e9538d96dfdcaba6f76b3f9f903da071c Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 8 Jul 2021 18:17:59 -0700 Subject: [PATCH] Added `-binary` CLI flag to NEDsim for loading a.out format programs. --- hacks/NEDsim/NEDsim.c | 17 ++++++++++------- hacks/NEDsim/simulator.c | 9 +++------ hacks/NEDsim/simulator.h | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/hacks/NEDsim/NEDsim.c b/hacks/NEDsim/NEDsim.c index 73c72f9..2d78973 100644 --- a/hacks/NEDsim/NEDsim.c +++ b/hacks/NEDsim/NEDsim.c @@ -4,11 +4,6 @@ // 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. @@ -715,8 +710,15 @@ NEDsim_init(Display * dpy, Window win) 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(); + // Load the program file specified by the user or, if none is specified, + // 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); + } nedsim->gc = XCreateGC(nedsim->dpy, nedsim->win, GCForeground, &gcv); @@ -805,6 +807,7 @@ static const char * NEDsim_defaults[] = { static XrmOptionDescRec NEDsim_options[] = { { "-delay", ".delay", XrmoptionSepArg, 0 }, + { "-binary", ".binary", XrmoptionSepArg, 0 }, { 0, 0, 0, 0 } }; diff --git a/hacks/NEDsim/simulator.c b/hacks/NEDsim/simulator.c index f5c56ee..9708486 100644 --- a/hacks/NEDsim/simulator.c +++ b/hacks/NEDsim/simulator.c @@ -379,7 +379,7 @@ parse_aout_file(FILE * input, struct exec * aout_exec, uint8_t * text_segment, } struct NEDstate * -init_simulator(void) +init_simulator(char * input_file) { struct NEDstate * state = malloc(sizeof(struct NEDstate)); state->hack = malloc(sizeof(struct NEDhack)); @@ -395,16 +395,13 @@ init_simulator(void) state->halted = false; state->hack->resume_word = false; -// TODO: This needs to be passed in as a CLI option. -#define AOUT_PATH "./test.out" - /* Load an initial image into memory. */ struct exec aout_exec; struct nlist * symbol_table; uint32_t symbol_count; FILE * input = NULL; - if ((input = fopen(AOUT_PATH, "r")) == NULL) { - fprintf(stderr, "ERROR: %s: %s\n", AOUT_PATH, strerror(errno)); + if ((input = fopen(input_file, "r")) == NULL) { + fprintf(stderr, "ERROR: %s: %s\n", input_file, strerror(errno)); state->halted = true; } parse_aout_file(input, &aout_exec, state->ram, &symbol_table, &symbol_count); diff --git a/hacks/NEDsim/simulator.h b/hacks/NEDsim/simulator.h index 971e76c..bf014eb 100644 --- a/hacks/NEDsim/simulator.h +++ b/hacks/NEDsim/simulator.h @@ -81,7 +81,7 @@ enum syllables { HALT = 0b00000000 }; -struct NEDstate * init_simulator(void); +struct NEDstate * init_simulator(char * input_file); struct NEDstate * run_simulator(struct NEDstate * state); uint32_t ram_r_word(struct NEDstate * state, uint32_t address); -- 2.20.1