Fixed bug. The SC now correctly displays the currently executing syllable.
[screensavers] / hacks / NEDsim / simulator.c
index 73623dc..7f5f135 100644 (file)
@@ -7,21 +7,10 @@
 
 #include <stdio.h>
 #include <stdint.h>
 
 #include <stdio.h>
 #include <stdint.h>
-#include <inttypes.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <stdbool.h>
-#include <unistd.h>
-#include <fcntl.h>
 #include <string.h>
 #include <errno.h>
 #include <string.h>
 #include <errno.h>
-#include <time.h>
-#include <termios.h>
-#include <signal.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
 
 #include "a.out.h"
 #include "simulator.h"
 
 #include "a.out.h"
 #include "simulator.h"
@@ -419,8 +408,10 @@ run_simulator(struct NEDstate * state)
     uint32_t iw;
     if (state->hack->resume_word) {
         iw = state->hack->iw;
     uint32_t iw;
     if (state->hack->resume_word) {
         iw = state->hack->iw;
+        state->active_thread->sc++;
     } else {
         iw = fetch_instruction_word(state);
     } else {
         iw = fetch_instruction_word(state);
+        state->active_thread->sc = 0;
     }
 
     /* Decode instruction word format and execute. */
     }
 
     /* Decode instruction word format and execute. */
@@ -428,18 +419,15 @@ run_simulator(struct NEDstate * state)
         stack_push(state->active_thread, (iw << 1));
     } else if ((iw & (0b11 << 30)) == 0) { /* Instruction word is type C. */
         uint8_t syllable = extract_syllable_from_word(iw, state->active_thread->sc);
         stack_push(state->active_thread, (iw << 1));
     } else if ((iw & (0b11 << 30)) == 0) { /* Instruction word is type C. */
         uint8_t syllable = extract_syllable_from_word(iw, state->active_thread->sc);
-        state->active_thread->sc++;
         /* The following variable allows us to catch JMP/BRZ instructions that */
         /* jump to a new PC and need the SC reset.                             */
         uint32_t pre_execution_pc = state->active_thread->pc;
         execute_syllable(state, syllable);
         if (state->active_thread->pc != pre_execution_pc) {
             /* Jumped to a new address, so prepare to execute a new instruction word. */
         /* The following variable allows us to catch JMP/BRZ instructions that */
         /* jump to a new PC and need the SC reset.                             */
         uint32_t pre_execution_pc = state->active_thread->pc;
         execute_syllable(state, syllable);
         if (state->active_thread->pc != pre_execution_pc) {
             /* Jumped to a new address, so prepare to execute a new instruction word. */
-            state->active_thread->sc = 0;
             state->hack->resume_word = false;
             state->hack->resume_word = false;
-        } else if (state->active_thread->sc >= SPW) {
+        } else if (state->active_thread->sc >= SPW-1) {
             /* Just executed the last syllable in this word, follow the PC to the next word. */
             /* Just executed the last syllable in this word, follow the PC to the next word. */
-            state->active_thread->sc = 0;
             state->hack->resume_word = false;
         } else {
             /* More syllables remain to be executed in this instruction word. */
             state->hack->resume_word = false;
         } else {
             /* More syllables remain to be executed in this instruction word. */