Commit 2f2552a7994b4fe58f477549f64ec04c96763624
receivedFri, 19. Jul 2024, 18:28:03 (by user sx)
Fri, 19 Jul 2024 16:28:03 +0000 (18:28 +0200)
authorManfred Steiner <sx@htl-kaindorf.at>
Fri, 19 Jul 2024 16:27:51 +0000 (18:27 +0200)
committerManfred Steiner <sx@htl-kaindorf.at>
Fri, 19 Jul 2024 16:27:51 +0000 (18:27 +0200)
4 files changed:
examples/simuc/.vscode/launch.json
examples/simuc/src/main.cpp
examples/simuc/src/simavr/simavr.cpp
examples/simuc/src/simavr/simavr.h

index 447fd73745476b1560881330a3367cbe573e110d..b6d3ec25fc10ae88749dfadfc5d486e75e435afb 100644 (file)
@@ -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": [],
index cf4abba0b210ad5af3c14e9fc934d237b80e9805..f287e242e150f2e4d9e383ca8a620f30770860bb 100644 (file)
@@ -32,12 +32,56 @@ void printHelp () {
 }
 
 void printAvailableCommands (struct StartParameters &params) {
-    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++) {
index c678f48c0b52a99a40c1ade97c946dad48ef561f..71d4996b0b82de6fbd7f321e106c9f78427ef22a 100644 (file)
 #include <simavr/avr_uart.h>
 #include <simavr/avr_ioport.h>
 
+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;
index d4ee0d0386b2baefdf0a0150914087a4236ae945..60c3998df6b08c7e3f087bf927d4abb4f2f853aa 100644 (file)
@@ -58,9 +58,11 @@ typedef enum {
 
 typedef enum {
        ReadyForNewCommand = 0,
+       CommandHelp,
        CommandStatus,
        CommandQuit,
        CommandBreak,
+       CommandStepi,
        CommandContinue,
        CommandStack,
        CommandReset,