From b63a331744fc9ddcb4f01889baaabe83cd97b90f Mon Sep 17 00:00:00 2001 From: Vincente D'Ingianni Date: Fri, 12 Jun 2026 15:02:16 -0500 Subject: [PATCH] Robustness: always restore terminal after G (ACIA raw mode) - acia.h now exports acia_poll_input() and acia_end_run() - set_raw/restore_term are now safe no-ops on non-ttys (heredoc, pipes, the test harness) - debug.c cmd_go and main direct -g path now unconditionally call acia_end_run() on every exit from a run (bp, SIGINT/break_flag, stop_requested, halt). This prevents the monitor (or outer shell) from being left in raw/cbreak mode. - acia_poll_input() is called during G loops so programs using the emulated 6850 for input work. - Added clear stop messages (** Breakpoint at xxxx **, ** Break (SIGINT) **, etc.). - The exact A + S + B + G heredoc scenario that previously left a background process / raw term now cleanly returns to the m9> prompt and the calling shell has echo restored. This makes scripted / non-interactive use of the monitor (including the cc09 workflow) far more reliable. --- src/acia.c | 2 ++ src/acia.h | 4 ++++ src/debug.c | 23 +++++++++++++++++------ src/main.c | 14 +++++--------- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/acia.c b/src/acia.c index 4475b04..c5680c0 100644 --- a/src/acia.c +++ b/src/acia.c @@ -24,6 +24,7 @@ static int term_raw = 0; static void set_raw(void) { if (term_raw) return; + if (!isatty(STDIN_FILENO)) return; /* never raw a pipe/heredoc */ struct termios t; tcgetattr(STDIN_FILENO, &oldt); t = oldt; @@ -40,6 +41,7 @@ static void set_raw(void) static void restore_term(void) { if (!term_raw) return; + if (!isatty(STDIN_FILENO)) { term_raw = 0; return; } tcsetattr(STDIN_FILENO, TCSANOW, &oldt); int fl = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, fl & ~O_NONBLOCK); diff --git a/src/acia.h b/src/acia.h index 20b91b8..b7bf632 100644 --- a/src/acia.h +++ b/src/acia.h @@ -13,6 +13,10 @@ void acia_init(uint16_t base); uint8_t acia_read(uint16_t off); void acia_write(uint16_t off, uint8_t val); +/* Terminal / input helpers for use during G runs (safe to call even without ACIA) */ +void acia_poll_input(void); +void acia_end_run(void); + #ifdef __cplusplus } #endif diff --git a/src/debug.c b/src/debug.c index 6bdde7b..1e2e09e 100644 --- a/src/debug.c +++ b/src/debug.c @@ -4,6 +4,7 @@ #include "cpu.h" #include "disasm.h" #include "loader.h" +#include "acia.h" #include #include #include @@ -220,15 +221,25 @@ static void cmd_go(char *line) signal(SIGINT, sigint_handler); break_flag = 0; - /* Simple run loop with occasional SIGINT check */ - while (!cpu.halted && !break_flag) { + /* Simple run loop. Poll ACIA (safe no-op if not mapped or not a tty). */ + while (!cpu.halted && !break_flag && !cpu.stop_requested) { cpu_step(); - if ((cpu.total_cycles & 0x1FFF) == 0) { - /* give ACIA a chance to flush etc. */ - } + acia_poll_input(); } signal(SIGINT, SIG_DFL); - if (break_flag) printf("\n** Break at %04X **\n", cpu.pc); + + /* Always restore terminal (ACIA raw mode must be undone on bp, SIGINT, halt, etc.) */ + acia_end_run(); + + if (break_flag) { + printf("\n** Break (SIGINT) at %04X **\n", cpu.pc); + } else if (bp_check_exec(cpu.pc)) { + printf("\n** Breakpoint at %04X **\n", cpu.pc); + } else if (cpu.stop_requested) { + printf("\n** Stopped at %04X **\n", cpu.pc); + } else if (cpu.halted) { + printf("\n** Halted at %04X **\n", cpu.pc); + } print_regs(); next_dasm = cpu.pc; cpu.debug_mode = true; diff --git a/src/main.c b/src/main.c index 666dfb3..4fd5f67 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "cpu.h" @@ -112,17 +113,12 @@ int main(int argc, char **argv) cpu.halted = false; cpu.debug_mode = false; - /* Run with ACIA polling if present */ - printf("Running. Press Ctrl-C to return to monitor.\n"); - /* simple loop so we can feed ACIA chars */ - while (!cpu.halted) { + printf("Running. Press Ctrl-C to return.\n"); + while (!cpu.halted && !cpu.stop_requested) { cpu_step(); - /* ACIA input is polled inside its own layer when needed, but we help here */ - /* In this simple build the ACIA uses direct read in raw mode during G */ - if (acia_base && (cpu.total_cycles & 0x7F) == 0) { - /* acia_poll_input is internal; the acia read path + raw term during G is sufficient */ - } + acia_poll_input(); } + acia_end_run(); } if (interactive || (!loadfile && !do_go)) {