From 1d54109ffe9a287f2c6dd102819c464854b87aae Mon Sep 17 00:00:00 2001 From: Manfred Steiner Date: Sun, 1 Oct 2023 13:06:22 +0200 Subject: [PATCH] v0.0.7 --- examples/simuc/src/main.cpp | 29 ++++++++++------ examples/simuc/src/simavr/simavr.cpp | 51 ++++++++++++++++++++++++---- examples/simuc/src/simavr/simavr.h | 3 +- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/examples/simuc/src/main.cpp b/examples/simuc/src/main.cpp index 345a872..19cefa8 100644 --- a/examples/simuc/src/main.cpp +++ b/examples/simuc/src/main.cpp @@ -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(¶ms); 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; + } } } diff --git a/examples/simuc/src/simavr/simavr.cpp b/examples/simuc/src/simavr/simavr.cpp index b12befe..9c974b5 100644 --- a/examples/simuc/src/simavr/simavr.cpp +++ b/examples/simuc/src/simavr/simavr.cpp @@ -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); } diff --git a/examples/simuc/src/simavr/simavr.h b/examples/simuc/src/simavr/simavr.h index 141084a..39b58c9 100644 --- a/examples/simuc/src/simavr/simavr.h +++ b/examples/simuc/src/simavr/simavr.h @@ -39,7 +39,8 @@ enum SimAvrState { StateError = 1, StateLoaded = 10, StateRunning = 11, - StateDone = 12, + StateStopped = 12, + StateDone = 13, StateCrashed = 20, StateOthers = 99, StateUnknown = 100 -- 2.39.5