+void
+parse_aout_file(FILE * input, struct exec * aout_exec, uint8_t * text_segment,
+ struct nlist ** symbol_table, uint32_t * symbol_count)
+{
+ uint32_t read_count = 0;
+
+ /* Read in and check the a.out header. */
+ for (uint32_t i=0; i<8; i++) {
+ switch (i) {
+ case 0: read_count = fread(&(aout_exec->a_midmag), 4, 1, input); break;
+ case 1: read_count = fread(&(aout_exec->a_text), 4, 1, input); break;
+ case 2: read_count = fread(&(aout_exec->a_data), 4, 1, input); break;
+ case 3: read_count = fread(&(aout_exec->a_bss), 4, 1, input); break;
+ case 4: read_count = fread(&(aout_exec->a_syms), 4, 1, input); break;
+ case 5: read_count = fread(&(aout_exec->a_entry), 4, 1, input); break;
+ case 6: read_count = fread(&(aout_exec->a_trsize), 4, 1, input); break;
+ case 7: read_count = fread(&(aout_exec->a_drsize), 4, 1, input); break;
+ }
+ if (read_count != 1) {
+ fprintf(stderr, "ERROR: Invalid a.out header.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (N_BADMAG(*aout_exec)) {
+ fprintf(stderr, "ERROR: Invalid magic number in a.out header.\n");
+ exit(EXIT_FAILURE);
+ } else if (N_GETMID(*aout_exec) != MID_NED) {
+ fprintf(stderr, "ERROR: Executable not intended for NED Machine ID.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Read in the text segment. */
+ uint32_t text_segment_size = (N_DATOFF(*aout_exec) - N_TXTOFF(*aout_exec));
+ read_count = fread(text_segment, 1, text_segment_size, input);
+ if (read_count != text_segment_size) {
+ fprintf(stderr, "ERROR: Failed to read entire text segment.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Correct the byte order. */
+ for (uint32_t i=0; i < (text_segment_size / 4); i++) {
+ uint8_t temp_word[4];
+ for (uint8_t j=0; j<4; j++) temp_word[j] = text_segment[((i*4)+j)];
+ for (uint8_t j=0; j<4; j++) text_segment[((i*4)+j)] = temp_word[(3-j)];
+ }
+
+ /* Read in the symbol table. */
+ *symbol_count = ((N_STROFF(*aout_exec) - N_SYMOFF(*aout_exec)) / 20); /* 20 bytes per symbol. */
+ *symbol_table = malloc((*symbol_count) * sizeof(struct nlist));
+ for (uint32_t i=0; i < *symbol_count; i++) {
+ for (uint32_t j=0; j<5; j++) {
+ switch (j) {
+ case 0: read_count = fread(&((*symbol_table)[i].n_un.n_strx), 4, 1, input); break;
+ case 1: read_count = fread(&((*symbol_table)[i].n_type), 4, 1, input); break;
+ case 2: read_count = fread(&((*symbol_table)[i].n_other), 4, 1, input); break;
+ case 3: read_count = fread(&((*symbol_table)[i].n_desc), 4, 1, input); break;
+ case 4: read_count = fread(&((*symbol_table)[i].n_value), 4, 1, input); break;
+ }
+ if (read_count != 1) {
+ fprintf(stderr, "ERROR: Unable to read entire symbol table.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
+
+ /* Read in the string table and update the symbol table entries with pointers to new strings. */
+ uint32_t string_table_size;
+ read_count = fread(&string_table_size, 4, 1, input);
+ if (read_count != 1) {
+ fprintf(stderr, "ERROR: Failed to read string table size.\n");
+ exit(EXIT_FAILURE);
+ }
+ for (uint32_t i=0; i < *symbol_count; i++) {
+ uint32_t len = 0;
+ if (i < ((*symbol_count)-1)) {
+ len = ((*symbol_table)[i+1].n_un.n_strx - (*symbol_table)[i].n_un.n_strx);
+ } else {
+ len = (string_table_size - (*symbol_table)[i].n_un.n_strx);
+ }
+ (*symbol_table)[i].n_un.n_name = malloc(len);
+ read_count = fread((*symbol_table)[i].n_un.n_name, 1, len, input);
+ if (read_count != len) {
+ fprintf(stderr, "ERROR: Failed to read a string from the string table.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+}
+
+void
+send_front_panel_update(struct NEDstate * state, int sockfd, uint32_t ram_address,
+ const struct sockaddr *to, socklen_t tolen)
+{
+ /*
+ * Datagram format (all entries are 4 bytes):
+ * Sequence number (See RFC1982)
+ * Currently executing word
+ * PC
+ * SC
+ * PSW
+ * SP
+ * Top 16 stack entries
+ * RAM start address
+ * Top 16 RAM words from start address
+ */
+ uint32_t snapshot[39];
+
+ static uint32_t seq_num;
+ seq_num == 0xffffffff ? seq_num = 0 : seq_num++;
+ snapshot[0] = seq_num;
+
+ snapshot[1] = ram_r_word(state, (state->active_thread->pc - 4));
+ snapshot[2] = state->active_thread->pc;
+ snapshot[3] = (uint32_t) state->active_thread->sc;
+ snapshot[4] = generate_binary_psw(state);
+ snapshot[5] = state->active_thread->sp;
+
+ int64_t sp = state->active_thread->sp;
+ for (int i=0; i<16; i++) {
+ if ((sp-i) < 0) {
+ snapshot[i+6] = 0;
+ } else {
+ snapshot[i+6] = state->active_thread->stack[sp-i];
+ }
+ }
+
+ snapshot[22] = ram_address;
+ for (int i=0; i<16; i++) {
+ snapshot[i+23] = ram_r_word(state,(ram_address + (i * 4)));
+ }
+
+ int numbytes;
+ if ((numbytes = sendto(sockfd, snapshot, 156, 0, to, tolen)) == -1) {
+ fprintf(stderr, "WARNING: Unable to send machine snapshot to front panel socket.\n");
+ }
+}
+