From 78b65086b9e1bcb23863206afb4d709d7dde5181 Mon Sep 17 00:00:00 2001
From: ga <ga@oldell.fish>
Date: Fri, 13 May 2022 21:46:32 +0100
Subject: [PATCH] Speed execution with GDB by running a burst of instructions
 before checking for input from GDB.  Discussed in #489.

---
 simavr/sim/sim_gdb.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/simavr/sim/sim_gdb.c b/simavr/sim/sim_gdb.c
index ec950e6..cd2196e 100644
--- a/simavr/sim/sim_gdb.c
+++ b/simavr/sim/sim_gdb.c
@@ -47,12 +47,17 @@ typedef struct {
 	} points[WATCH_LIMIT];
 } avr_gdb_watchpoints_t;
 
+/* How many AVR instructions to execute before looking for gdb input. */
+
+#define GDB_BURST 256
+
 typedef struct avr_gdb_t {
 	avr_t * avr;
-	int	listen;	// listen socket
-	int	s;	// current gdb connection
+	int burst_count;	// Current instruction burst size
+	int	listen;			// listen socket
+	int	s;				// current gdb connection
 
-	avr_gdb_watchpoints_t breakpoints;
+    avr_gdb_watchpoints_t breakpoints;
 	avr_gdb_watchpoints_t watchpoints;
 
 	// These are used by gdb's "info io_registers" command.
@@ -789,6 +794,7 @@ gdb_network_handler(
 	int max;
 	FD_ZERO(&read_set);
 
+	g->burst_count = 0; // Reset burst count
 	if (g->s != -1) {
 		FD_SET(g->s, &read_set);
 		max = g->s + 1;
@@ -934,8 +940,12 @@ avr_gdb_processor(
 	} else if (avr->state == cpu_StepDone) {
 		gdb_send_quick_status(g, 0);
 		avr->state = cpu_Stopped;
+	} else {
+		/* Look for gdb input every GDB_BURST instructions. */
+
+		if (sleep == 0 && g->burst_count++ < GDB_BURST)
+			return 0;
 	}
-	// this also sleeps for a bit
 	return gdb_network_handler(g, sleep);
 }
 
-- 
2.39.5