From 2f2552a7994b4fe58f477549f64ec04c96763624 Mon Sep 17 00:00:00 2001 From: Manfred Steiner Date: Fri, 19 Jul 2024 18:27:51 +0200 Subject: [PATCH] start behavior improved, avr-gdb connect detection --- examples/simuc/.vscode/launch.json | 3 +- examples/simuc/src/main.cpp | 162 +++++++++++---------------- examples/simuc/src/simavr/simavr.cpp | 31 +++++ examples/simuc/src/simavr/simavr.h | 2 + 4 files changed, 102 insertions(+), 96 deletions(-) diff --git a/examples/simuc/.vscode/launch.json b/examples/simuc/.vscode/launch.json index 447fd73..b6d3ec2 100644 --- a/examples/simuc/.vscode/launch.json +++ b/examples/simuc/.vscode/launch.json @@ -9,7 +9,8 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/dist/simuc", - "args": [ "--board", "sure", "/work/4ahme/sx-la1/g2/turnus3/2_timer/dist/atmega16.elf"], + //"args": [ "--board", "sure", "/work/4ahme/sx-la1/g2/turnus3/2_timer/dist/atmega16.elf"], + "args": [ "--pc", "0xe000", "--port", "1234", "--board", "nano-644", "/home/steiner/Private/work/nano-644/software/gdb-stub/dist/gdb-stub_atmega644p.elf" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], diff --git a/examples/simuc/src/main.cpp b/examples/simuc/src/main.cpp index cf4abba..f287e24 100644 --- a/examples/simuc/src/main.cpp +++ b/examples/simuc/src/main.cpp @@ -32,12 +32,56 @@ void printHelp () { } void printAvailableCommands (struct StartParameters ¶ms) { - printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n"); + printf("----------------------------------------------------------------------\n"); + printf("available commands:\n"); + printf(" h (help)\n"); + printf(" b (break), n (stepi), c (continue), s (stack), r (reset), p (power), q (quit)\n"); if (params.board == BoardNano644) { printf(" 1 (SW2 not pressed), 2 (SW2 pressed)\n"); } else if (params.board == BoardSure) { printf(" 1 (SW1 not pressed), 2 (SW1 pressed), 3 (SW2 not pressed), 4 (SW2 pressed)\n"); } + printf("----------------------------------------------------------------------\n"); +} + +EnumSimAvrCommand parseSimAvrCommand (struct StartParameters& params, char c) { + switch (c) { + case 'h': return CommandHelp; + case 'b': return CommandBreak; + case 'n': return CommandStepi; + case 'c': return CommandContinue; + case 's': return CommandStack; + case 'r': return CommandReset; + case 'p': return CommandPower; + case 'q': return CommandPower; + case '1': { + switch (params.board) { + case BoardSure: return CommandSW1Released; + case BoardNano644: return CommandSW2Released; + default: break; + } + } + case '2': { + switch (params.board) { + case BoardSure: return CommandSW1Pressed; + case BoardNano644: return CommandSW2Pressed; + default: break; + } + } + case '3': { + switch (params.board) { + case BoardSure: return CommandSW2Released; + default: break; + } + } + case '4': { + switch (params.board) { + case BoardSure: return CommandSW2Pressed; + default: break; + } + } + default: return ReadyForNewCommand; + } } struct SimUcInit { @@ -224,113 +268,48 @@ int main (int argc, char **argv) { return 1; } printf("----------------------------------------------------------------------\n"); - printf("available commands:\n"); + printf("init done\n"); printAvailableCommands(params); + printf("start simulator with command b (break)\n"); + printf("ready for avr-gdb -> (gdb) target remote :%d\n", params.gdbPort); printf("----------------------------------------------------------------------\n"); - printf("init done - press key to start\n"); - char c = getchar(); - if (c != '\n') { - getchar(); // remove line feed from stdin - } - printf("----------------------------------------------------------------------\n"); - - switch (c) { - case 'b': start(CommandBreak, NULL); break; - case 's': start(CommandStack, NULL); break; - case 'r': start(CommandReset, NULL); break; - case 'p': start(CommandPower, NULL); break; - case 'q': return 0; - case '1': case '2': { - if (params.board == BoardNano644) { - break; - } - } - default: start(ReadyForNewCommand, NULL); break; - } - - // int cnt = 0; + EnumSimAvrCommand cmd = CommandBreak; + start(CommandBreak, NULL); + EnumSimAvrCommand lastCmd = cmd; + char *line = NULL; size_t size = 0; int quit = 0; while (!quit) { - // struct SimAvrEvent ev = waitForEvent(); - // printf("%10.03lf: event %s (%d) received \r", ev.cycle / 20E6, eventText((EnumSimEvent)ev.event), ev.event); - // fflush(stdout); - // if (++cnt == 10000) { - // stop(); - // shutdown(); - // break; - // } - if (getline(&line, &size, stdin) > 0) { try { - EnumSimAvrCommand simCmd = ReadyForNewCommand; - - switch (params.board) { - case BoardNano644: { - if (strcmp(line, "1\n") == 0) { - simCmd = CommandSW2Released; - } else if (strcmp(line, "2\n") == 0) { - simCmd = CommandSW2Pressed; - } - break; - } - case BoardSure: { - if (strcmp(line, "1\n") == 0) { - simCmd = CommandSW1Released; - } else if (strcmp(line, "2\n") == 0) { - simCmd = CommandSW1Pressed; - } else if (strcmp(line, "3\n") == 0) { - simCmd = CommandSW2Released; - } else if (strcmp(line, "4\n") == 0) { - simCmd = CommandSW2Pressed; - } - break; - } - default: break; - } - if (simCmd != ReadyForNewCommand) { - const char *commands[] = { "quit", "break", "continue", "stack", "reset", "power" }; - int foundIndex = -1; - int foundCnt = 0; - size_t length = strlen(line) - 1; - if (length > 0 && size >= (length + 1)) { - line[length] = 0; - for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) { - const char *cmd = commands[i]; - size_t max = strlen(cmd); - bool ok = true; - for (size_t j = 0; j < length; j++) { - if (j >= max || line[j] != cmd[j]) { - ok = false; - break; - } - } - if (ok) { - foundCnt++; - foundIndex = i; - } - } + EnumSimAvrCommand cmd = ReadyForNewCommand; + if (strnlen(line, size) == 1) { // Enter pressed + switch (lastCmd) { + case CommandStepi: cmd = CommandStepi; break; + default: cmd = CommandStatus; } - - // printf("foundCnt=%d foundIndex=%d command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : ""); - if (foundCnt == 1 || length == 0) { - simCmd = (EnumSimAvrCommand)(foundCnt > 0 ? foundIndex + 2 : 1); + } else if (strnlen(line, size) == 2) { + cmd = parseSimAvrCommand(params, line[0]); + lastCmd = cmd; + if (cmd == CommandHelp) { + printAvailableCommands(params); + continue; } } - if (simCmd != ReadyForNewCommand) { + if (cmd != ReadyForNewCommand) { struct SimAvrStatus status = getStatus(); - if (simCmd == CommandStatus && status.state == StateStopped) { + if (cmd == CommandStatus && status.state == StateStopped) { // ignore status command, because status was already printed // only line feed visible on screen } else { - setCommand(simCmd, NULL); + setCommand(cmd, NULL); while (1) { struct SimAvrEvent ev = waitForEvent(); - if (ev.event == EventShutdown || (simCmd == CommandQuit && ev.event == EventCommandExecuted)) { + if (ev.event == EventShutdown || (cmd == CommandQuit && ev.event == EventCommandExecuted)) { quit = 1; break; } @@ -353,13 +332,6 @@ int main (int argc, char **argv) { } } - // while (1) { - // struct SimAvrEvent ev = waitForEvent(); - // printf("event %s (%d) received\n", eventText((EnumSimEvent)ev.event), ev.event); - // if (ev.event == EventShutdown) { - // break; - // } - // } if (simUcInit != NULL) { for (int i = 0; i < simUcInit->argc; i++) { diff --git a/examples/simuc/src/simavr/simavr.cpp b/examples/simuc/src/simavr/simavr.cpp index c678f48..71d4996 100644 --- a/examples/simuc/src/simavr/simavr.cpp +++ b/examples/simuc/src/simavr/simavr.cpp @@ -19,6 +19,13 @@ #include #include +typedef struct { + avr_t * avr; + int burst_count; // Current instruction burst size + int listen; // listen socket + int s; // current gdb connection +} Gdb; + SimAvr::SimAvr () { memset(&status, 0, sizeof(status)); @@ -792,6 +799,22 @@ void SimAvr::avrRun () { lastAvrState = avr->state; } + if (avr->gdb != NULL) { + static int session = -1; + void *p = (void *)avr->gdb; + Gdb *pg = (Gdb *)p; + if (pg->s != session) { + if (pg->s == -1) { + printf("\navr-gdb disconnected%s\n\n", avr->state == cpu_Stopped ? "" : " (CPU stopped)"); + + } else { + printf("\navr-gdb connected%s\n\n", avr->state == cpu_Stopped ? "" : " (CPU stopped)"); + } + session = pg->s; + avr->state = cpu_Stopped; + } + } + if (cnt <= 0) { // usleep(10000); if (pthread_mutex_lock(&lock)) { @@ -911,6 +934,14 @@ void SimAvr::avrRun () { nextCommand = ReadyForNewCommand; break; } + case CommandStepi: { + if (avr->state != cpu_Step) { + avr->state = cpu_Step; + updateState = true; + } + nextCommand = ReadyForNewCommand; + break; + } case CommandContinue: { if (avr->state == cpu_Stopped) { avr->state = cpu_Running; diff --git a/examples/simuc/src/simavr/simavr.h b/examples/simuc/src/simavr/simavr.h index d4ee0d0..60c3998 100644 --- a/examples/simuc/src/simavr/simavr.h +++ b/examples/simuc/src/simavr/simavr.h @@ -58,9 +58,11 @@ typedef enum { typedef enum { ReadyForNewCommand = 0, + CommandHelp, CommandStatus, CommandQuit, CommandBreak, + CommandStepi, CommandContinue, CommandStack, CommandReset, -- 2.39.5