Commit 1d54109ffe9a287f2c6dd102819c464854b87aae
receivedSun, 1. Oct 2023, 13:06:26 (by user sx)
Sun, 1 Oct 2023 11:06:26 +0000 (13:06 +0200)
authorManfred Steiner <sx@htl-kaindorf.at>
Sun, 1 Oct 2023 11:06:22 +0000 (13:06 +0200)
committerManfred Steiner <sx@htl-kaindorf.at>
Sun, 1 Oct 2023 11:06:22 +0000 (13:06 +0200)
3 files changed:
examples/simuc/src/main.cpp
examples/simuc/src/simavr/simavr.cpp
examples/simuc/src/simavr/simavr.h

index 345a87214d7ec595fa2bd51b002681b29e679313..19cefa85650fc79d4c791be7cf4876e63de77113 100644 (file)
@@ -8,7 +8,7 @@
 #include "simavr/simavr.h"
 
 void printVersion () {
-    printf("simuc V0.0.6 (%s,%s)\n", __DATE__, __TIME__);
+    printf("simuc V0.0.7 (%s,%s)\n", __DATE__, __TIME__);
 }
 
 void printHelp () {
@@ -199,6 +199,7 @@ int main (int argc, char **argv) {
         return 1;
     }
 
+    printVersion();
     printf("----------------------------------------------------------------------\n");
     init(&params);
     if (errno == 1) {
@@ -263,18 +264,24 @@ int main (int argc, char **argv) {
                         }
                     }
                 }
-                printf("foundCnt=%d  foundIndex=%d  command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : "");
+                // printf("foundCnt=%d  foundIndex=%d  command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : "");
                 if (foundCnt == 1 || length == 0) {
                     EnumSimAvrCommand cmd = (EnumSimAvrCommand)(foundCnt > 0 ? foundIndex + 2 : 1);
-                    setCommand(cmd, NULL);
-                    while (1) {
-                        struct SimAvrEvent ev = waitForEvent();
-                        if (ev.event == EventShutdown || (cmd == CommandQuit && ev.event == EventCommandExecuted)) {
-                            quit = 1;
-                            break;
-                        }
-                        if (ev.event == EventCommandExecuted) {
-                            break;
+                    struct SimAvrStatus status = getStatus();
+                    if (cmd == CommandStatus && status.state == StateStopped) { 
+                        // ignore status command, because status was already printed
+                        // only line feed visible on screen
+                    } else {
+                        setCommand(cmd, NULL);
+                        while (1) {
+                            struct SimAvrEvent ev = waitForEvent();
+                            if (ev.event == EventShutdown || (cmd == CommandQuit && ev.event == EventCommandExecuted)) {
+                                quit = 1;
+                                break;
+                            }
+                            if (ev.event == EventCommandExecuted) {
+                                break;
+                            }
                         }
                     }
 
index b12befe4c97e069260ce2372e55523e28f26a506..9c974b5b4660d1cba382aeab9c36beda1954caaa 100644 (file)
@@ -653,6 +653,7 @@ void SimAvr::avrRun () {
                                        switch (avr->state) {
                                                case cpu_Done:    status.state = StateDone; break;
                                                case cpu_Running: status.state = StateRunning; break;
+                                               case cpu_Stopped: status.state = StateStopped; break;
                                                case cpu_Crashed: status.state = StateCrashed; break;
                                                default: status.state = StateOthers; break;
                                        }
@@ -739,6 +740,8 @@ void SimAvr::avrRun () {
                                cnt = 0;
                        }
 
+                       EnumSimAvrCommand nextCommand = command;
+                       bool updateState = false;
                        if (command != ReadyForNewCommand) {
                                switch (command) {
                                        case CommandStatus: {
@@ -746,18 +749,23 @@ void SimAvr::avrRun () {
                                                char s[80];
                                                sprintfLedStatus(s, sizeof(s), false);
                                                printf("   %s\n", s);
+                                               nextCommand = ReadyForNewCommand;
                                                break;
                                        }
                                        case CommandBreak: {
                                                if (avr->state == cpu_Running) {
                                                        avr->state = cpu_Stopped;
+                                                       updateState = true;
                                                }
+                                               nextCommand = ReadyForNewCommand;
                                                break;
                                        }
                                        case CommandContinue: {
                                                if (avr->state == cpu_Stopped) {
                                                        avr->state = cpu_Running;
+                                                       updateState = true;
                                                }
+                                               nextCommand = ReadyForNewCommand;
                                                break;
                                        }
                                        case CommandStack: {
@@ -781,28 +789,59 @@ void SimAvr::avrRun () {
                                                        }
                                                }
                                                printf("\n");
+                                               nextCommand = ReadyForNewCommand;
                                                break;
                                        }
 
                                        case CommandReset: {
                                                reset(external);
+                                               if (avr->state == cpu_Running) {
+                                                       avr->state = cpu_Stopped;
+                                                       updateState = true;
+                                               }
                                                printf("\n\n----------------------------------------------------------------\n");
                                                printf("RESET done (registers set to init value, SRAM remains unchanged)\n");
-                                               command = CommandBreak;
-                                               continue;
+                                               nextCommand = CommandBreak;
+                                               break;
                                        }
 
                                        case CommandPower: {
                                                reset(powerOn);
+                                               if (avr->state == cpu_Running) {
+                                                       avr->state = cpu_Stopped;
+                                                       updateState = true;
+                                               }
                                                printf("\n\n-----------------------------------------------\n");
                                                printf("Power-On done (SRAM cleared and power-on reset)\n");
-                                               command = CommandBreak;
-                                               continue;
+                                               nextCommand = CommandBreak;
+                                               break;
                                        }
 
-                                       default: break;
+                                       default: {
+                                               nextCommand = ReadyForNewCommand;
+                                               break;
+                                       }
+                               }
+                               command = nextCommand;
+                               if (updateState) {
+                                       if (pthread_mutex_lock(&lock)) {
+                                               throw std::logic_error(error(AT, "avrRun() fails caused by mutex error (2)"));
+                                       }
+                                       try {
+                                               switch (avr->state) {
+                                                       case cpu_Done:    status.state = StateDone; break;
+                                                       case cpu_Running: status.state = StateRunning; break;
+                                                       case cpu_Stopped: status.state = StateStopped; break;
+                                                       case cpu_Crashed: status.state = StateCrashed; break;
+                                                       default: status.state = StateOthers; break;
+                                               }
+                                               pthread_mutex_unlock(&lock);
+                                       }
+                                       catch (std::exception& e) {
+                                               pthread_mutex_unlock(&lock);
+                                               status.state = StateError;
+                                       }
                                }
-                               command = ReadyForNewCommand;
                                addEvent(EventCommandExecuted);
                        }
 
index 141084a7c02a09d760a447d5a1f6f2dff34167eb..39b58c94087561ee1ab883e1865248ba5341e303 100644 (file)
@@ -39,7 +39,8 @@ enum SimAvrState {
        StateError = 1,
        StateLoaded = 10,
        StateRunning = 11,
-       StateDone = 12,
+       StateStopped = 12,
+       StateDone = 13,
        StateCrashed = 20,
        StateOthers = 99,
        StateUnknown = 100