From: ga <ga@oldell.fish>
Date: Wed, 16 Mar 2022 19:20:43 +0000 (+0000)
Subject: Fix three problems with gdb support:
X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=4eb0e42896fde15b3f6e6f34a2d3dce3c025217a;p=sx%2Fsimavr.git

Fix three problems with gdb support:
1. Do not report a TRAP signal on every stop, as it confuses gdb.
   Initial symptom was failure to step into an ISR.
2. AVR was running after reset command.
3. Excessive processor activity (buzzing) while AVR stopped.
---

diff --git a/simavr/sim/sim_avr.c b/simavr/sim/sim_avr.c
index 6e43bf9..8729040 100644
--- a/simavr/sim/sim_avr.c
+++ b/simavr/sim/sim_avr.c
@@ -286,7 +286,7 @@ void
 avr_callback_run_gdb(
 		avr_t * avr)
 {
-	avr_gdb_processor(avr, avr->state == cpu_Stopped);
+	avr_gdb_processor(avr, avr->state == cpu_Stopped ? 50000 : 0);
 
 	if (avr->state == cpu_Stopped)
 		return ;
@@ -331,7 +331,6 @@ avr_callback_run_gdb(
 	// if we were stepping, use this state to inform remote gdb
 	if (step)
 		avr->state = cpu_StepDone;
-
 }
 
 /*
diff --git a/simavr/sim/sim_gdb.c b/simavr/sim/sim_gdb.c
index 702a2cd..d68aea2 100644
--- a/simavr/sim/sim_gdb.c
+++ b/simavr/sim/sim_gdb.c
@@ -210,9 +210,10 @@ gdb_send_quick_status(
 	READ_SREG_INTO(g->avr, sreg);
 
 	sprintf(cmd, "T%02x20:%02x;21:%02x%02x;22:%02x%02x%02x00;",
-		signal ? signal : 5, sreg,
+		signal, sreg,
 		g->avr->data[R_SPL], g->avr->data[R_SPH],
-		g->avr->pc & 0xff, (g->avr->pc>>8)&0xff, (g->avr->pc>>16)&0xff);
+		g->avr->pc & 0xff, (g->avr->pc >> 8) & 0xff,
+		(g->avr->pc >> 16) & 0xff);
 	gdb_send_reply(g, cmd);
 }
 
@@ -314,8 +315,8 @@ handle_monitor(avr_t * avr, avr_gdb_t * g, char * cmd)
 			++ip;
 
 		if (strncmp(ip, "reset", 5) == 0) {
-			avr->state = cpu_StepDone;
 			avr_reset(avr);
+			avr->state = cpu_Stopped;
 			ip += 5;
 		} else if (strncmp(ip, "halt", 4) == 0) {
 			avr->state = cpu_Stopped;
@@ -668,8 +669,8 @@ gdb_handle_command(
 			avr->state = cpu_Step;
 		}	break;
 		case 'r': {	// deprecated, suggested for AVRStudio compatibility
-			avr->state = cpu_StepDone;
 			avr_reset(avr);
+			avr->state = cpu_Stopped;
 		}	break;
 		case 'Z': 	// set clear break/watchpoint
 		case 'z': {
@@ -792,7 +793,8 @@ gdb_network_handler(
 		// control C -- lets send the guy a nice status packet
 		if (*src == 3) {
 			src++;
-			g->avr->state = cpu_StepDone;
+			gdb_send_quick_status(g, 2); // SIGINT
+			g->avr->state = cpu_Stopped;
 			printf("GDB hit control-c\n");
 		}
 		if (*src  == '$') {
@@ -862,7 +864,7 @@ avr_gdb_processor(
 	if (avr->state == cpu_Running &&
 			gdb_watch_find(&g->breakpoints, avr->pc) != -1) {
 		DBG(printf("avr_gdb_processor hit breakpoint at %08x\n", avr->pc);)
-		gdb_send_quick_status(g, 0);
+		gdb_send_quick_status(g, 5);
 		avr->state = cpu_Stopped;
 	} else if (avr->state == cpu_StepDone) {
 		gdb_send_quick_status(g, 0);