From ae1006f3ea810193b43e57305e65890639af5e66 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Tue, 23 Mar 2010 14:37:53 +1300 Subject: [PATCH] gdb: can now interrupt execution on both sides --- bsl.c | 4 +-- device.h | 9 ++++++- fet.c | 10 ++++---- gdb.c | 9 ++++--- sim.c | 74 +++++++++++++++++++++++++++++++++----------------------- 5 files changed, 65 insertions(+), 41 deletions(-) diff --git a/bsl.c b/bsl.c index ed14157..48719bd 100644 --- a/bsl.c +++ b/bsl.c @@ -202,9 +202,9 @@ static int bsl_control(device_ctl_t type) return -1; } -static int bsl_wait(int blocking) +static device_status_t bsl_wait(int blocking) { - return 0; + return DEVICE_STATUS_HALTED; } static int bsl_breakpoint(u_int16_t addr) diff --git a/device.h b/device.h index c7e84b4..7f77bab 100644 --- a/device.h +++ b/device.h @@ -33,10 +33,17 @@ typedef enum { DEVICE_CTL_ERASE } device_ctl_t; +typedef enum { + DEVICE_STATUS_HALTED, + DEVICE_STATUS_RUNNING, + DEVICE_STATUS_INTR, + DEVICE_STATUS_ERROR +} device_status_t; + struct device { void (*close)(void); int (*control)(device_ctl_t action); - int (*wait)(int blocking); + device_status_t (*wait)(int blocking); int (*breakpoint)(u_int16_t addr); int (*getregs)(u_int16_t *regs); int (*setregs)(const u_int16_t *regs); diff --git a/fet.c b/fet.c index 4b3415c..a8dc6ee 100644 --- a/fet.c +++ b/fet.c @@ -602,23 +602,23 @@ static int do_erase(void) return 0; } -static int fet_wait(int blocking) +static device_status_t fet_wait(int blocking) { do { /* Without this delay, breakpoints can get lost. */ if (usleep(500000) < 0) - break; + return DEVICE_STATUS_INTR; if (xfer(C_STATE, NULL, 0, 1, 0) < 0) { fprintf(stderr, "fet: polling failed\n"); - return -1; + return DEVICE_STATUS_ERROR; } if (!(fet_reply.argv[0] & FET_POLL_RUNNING)) - return 0; + return DEVICE_STATUS_HALTED; } while (blocking); - return 1; + return DEVICE_STATUS_RUNNING; } static int fet_control(device_ctl_t action) diff --git a/gdb.c b/gdb.c index 3990072..755a0e8 100644 --- a/gdb.c +++ b/gdb.c @@ -388,18 +388,21 @@ static int run(char *buf) } for (;;) { - int status = gdb_device->wait(0); + device_status_t status = gdb_device->wait(0); - if (status < 0) { + if (status == DEVICE_STATUS_ERROR) { gdb_send("E00"); return run_final_status(); } - if (!status) { + if (status == DEVICE_STATUS_HALTED) { printf("Target halted\n"); goto out; } + if (status == DEVICE_STATUS_INTR) + goto out; + while (gdb_peek()) { int c = gdb_getc(); diff --git a/sim.c b/sim.c index 0706832..6dc1a18 100644 --- a/sim.c +++ b/sim.c @@ -511,52 +511,66 @@ static int sim_control(device_ctl_t action) case DEVICE_CTL_RUN_BP: run_mode = RUN_TO_BREAKPOINT; + ctrlc_reset(); return 0; case DEVICE_CTL_RUN: run_mode = RUN_FREE; + ctrlc_reset(); return 0; } return -1; } -static int sim_wait(int blocking) +static int run_burst(void) { - int i = 100000; + int i = 1000000; - if (run_mode != RUN_HALTED) { - ctrlc_reset(); - - while (i) { - if (run_mode == RUN_TO_BREAKPOINT && - sim_regs[MSP430_REG_PC] == run_breakpoint) { - run_mode = RUN_HALTED; - return 0; - } - - if (sim_regs[MSP430_REG_SR] & MSP430_SR_CPUOFF) { - run_mode = RUN_HALTED; - printf("CPU disabled\n"); - return 0; - } - - if (ctrlc_check()) - break; - - if (step_cpu() < 0) { - run_mode = RUN_HALTED; - return -1; - } - - if (!blocking) - i--; + while (i--) { + if (run_mode == RUN_TO_BREAKPOINT && + sim_regs[MSP430_REG_PC] == run_breakpoint) { + printf("Breakpoint reached\n"); + run_mode = RUN_HALTED; + return 0; } - return 1; + if (sim_regs[MSP430_REG_SR] & MSP430_SR_CPUOFF) { + run_mode = RUN_HALTED; + printf("CPU disabled\n"); + return 0; + } + + if (step_cpu() < 0) { + run_mode = RUN_HALTED; + return -1; + } } - return 0; + return 1; +} + +static device_status_t sim_wait(int blocking) +{ + if (run_mode != RUN_HALTED) { + do { + int ret = run_burst(); + + if (ret < 0) + return DEVICE_STATUS_ERROR; + if (!ret) + return DEVICE_STATUS_HALTED; + + if (ctrlc_check()) { + ctrlc_reset(); + return DEVICE_STATUS_INTR; + } + } while (blocking); + + return DEVICE_STATUS_RUNNING; + } + + return DEVICE_STATUS_HALTED; } static int sim_breakpoint(u_int16_t addr)