Skip to content

Commit

Permalink
Do not exit on reset in interactive mode (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose authored Sep 3, 2024
1 parent ea35026 commit 147631c
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions cli/src/interactive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub(crate) fn run_interactive(
rl.set_helper(Some(h));

let mut last_command = None;
let mut halted = false;

loop {
let readline = rl.readline(">> ")?;
Expand All @@ -280,17 +281,20 @@ pub(crate) fn run_interactive(

debug!("Executing command: {:?}", command);

match &command {
Command::Exit => break,
Command::Step { number } => {
// TODO: recover from errors
match (&command, halted) {
(Command::Exit, _) => break,
(Command::Step { number }, false) => {
session.reset_list();

for _ in 0..*number {
computer.step()?;
if let Err(e) = computer.step() {
warn!(error = &e as &dyn std::error::Error, "Halted");
halted = true;
break;
}
}

session.reset_list();
}
Command::Registers { register } => {
(Command::Registers { register }, _) => {
if let Some(reg) = register {
match reg {
Reg::SR => {
Expand All @@ -305,7 +309,8 @@ pub(crate) fn run_interactive(
info!("Registers: {}", computer.registers);
}
}
Command::Memory { address, number } => {

(Command::Memory { address, number }, _) => {
let address = address.clone().evaluate(computer, &session.labels)?;

if number.is_positive() {
Expand All @@ -323,41 +328,43 @@ pub(crate) fn run_interactive(
}
}

Command::Interrupt => {
(Command::Interrupt, false) => {
computer.recover_from_exception(&Exception::HardwareInterrupt)?;
session.reset_list();
}

Command::List { number } => {
(Command::List { number }, _) => {
let addr = session.offset_list(computer, *number);
for i in 0..*number {
let addr = addr + i;
session.display_instruction(computer, addr);
}
}

Command::Break { address } => {
(Command::Break { address }, false) => {
let address = address.clone().evaluate(computer, &session.labels)?;
session.add_breakpoint(address);
}

Command::Unbreak { address } => {
(Command::Unbreak { address }, false) => {
let address = address.clone().evaluate(computer, &session.labels)?;
session.remove_breakpoint(address);
}

Command::Continue => {
loop {
// TODO: recover from error
computer.step()?;
if session.has_breakpoint(computer.registers.pc) {
break;
}
(Command::Continue, false) => loop {
if let Err(e) = computer.step() {
warn!(error = &e as &dyn std::error::Error, "Halted");
halted = true;
break;
}
info!(address = computer.registers.pc, "Stopped at a breakpoint");
}

Command::Info { sub } => match sub {
if session.has_breakpoint(computer.registers.pc) {
info!(address = computer.registers.pc, "Stopped at a breakpoint");
break;
}
},

(Command::Info { sub }, _) => match sub {
Some(InfoCommand::Breakpoints) => {
session.display_breakpoints(computer);
}
Expand All @@ -375,6 +382,11 @@ pub(crate) fn run_interactive(
Session::display_cycles(computer);
}
},

(_, true) => {
// Computer is halted but the user asked to continue, we just warn
warn!("Computer is halted. Use \"exit\" to quit");
}
};

last_command = Some(command);
Expand Down

0 comments on commit 147631c

Please sign in to comment.