diff --git a/sim/atomsim.cpp b/sim/atomsim.cpp index 0aa114a..2445c37 100755 --- a/sim/atomsim.cpp +++ b/sim/atomsim.cpp @@ -39,6 +39,8 @@ void Atomsim::step() int Atomsim::run() { + int exitcode=EXIT_SUCCESS; + // tick backend and update backend status, until ctrl+c is. try { @@ -49,9 +51,10 @@ int Atomsim::run() in_debug_mode_ = sim_config_.debug_flag; pending_steps = 0; + + init_interactive_mode(); // *** Simulation Loop *** - Rcode rval = RC_NONE; while (bkend_running_) { // Refresh state @@ -94,18 +97,21 @@ int Atomsim::run() pending_steps = 0; } else { printf("Exiting..\n"); - return 0; + exitcode = EXIT_SUCCESS; + break; } } // check sim iterations if(simstate_.state_.tickcount_total > sim_config_.maxitr) { throwError("SIM0", "Simulation iterations exceeded maxitr("+std::to_string(sim_config_.maxitr)+")\n"); - return 1; + exitcode = EXIT_FAILURE; + break; } // Enter interactive mode if we aren's stepping and we are already in debug mode or run mode // was interrupted by user (CTRL_C) + Rcode rval; if((pending_steps == 0) && (in_debug_mode_ || CTRL_C_PRESSED)) { // explictly set: since we can also enter if CTRL_C_PRESSED in_debug_mode_ = true; @@ -125,6 +131,7 @@ int Atomsim::run() CTRL_C_PRESSED = false; } else if (rval == RC_EXIT) { // Exit sim + exitcode = EXIT_SUCCESS; break; } } @@ -143,5 +150,9 @@ int Atomsim::run() std::cerr << "Runtime exception: " << e.what() << std::endl; return 1; } - return 0; + + // Exiting sim + deinit_interactive_mode(); + + return exitcode; } diff --git a/sim/atomsim.hpp b/sim/atomsim.hpp index 7e4ce3c..9640d73 100755 --- a/sim/atomsim.hpp +++ b/sim/atomsim.hpp @@ -60,10 +60,6 @@ class Atomsim */ Atomsim(Atomsim_config sim_config, Backend_config bk_config); - /** - * @brief step simulation by a cycle - */ - void step(); /** * @brief run simulation until finished @@ -71,14 +67,6 @@ class Atomsim */ int run(); - /** - * @brief enter interactive mode - * - * @returns int return code [if 2: exit sim] - */ - Rcode run_interactive_mode(); - - private: /** * @brief config struct object for sim @@ -130,6 +118,27 @@ class Atomsim // used to provide cycles for step command long long pending_steps = 0; + /** + * @brief step simulation by a cycle + */ + void step(); + + /** + * @brief initialize interactive mode + */ + void init_interactive_mode(); + + /** + * @brief Deinitialize interactive mode + */ + void deinit_interactive_mode(); + + /** + * @brief enter interactive mode + */ + Rcode run_interactive_mode(); + + // display debug screen void display_dbg_screen(); diff --git a/sim/interactive.cpp b/sim/interactive.cpp index 03bb7e5..cf1550b 100755 --- a/sim/interactive.cpp +++ b/sim/interactive.cpp @@ -18,6 +18,7 @@ #define DEFAULT_TRACEFILE_PATH "trace.vcd" #define ATOMSIM_PROMPT "atomsim> " #define ATOMSIM_HISTORY_FILE ".atomsim_history" +#define ATOMSIM_HISTORY_LENGTH 1000 void Atomsim::display_dbg_screen() @@ -157,6 +158,24 @@ void _hexdump(const unsigned char *buf, size_t bufsz, uint32_t base_addr, bool e } } +void Atomsim::init_interactive_mode(){ + // init + using_history(); + + // Set history list length + stifle_history(ATOMSIM_HISTORY_LENGTH); + + // read history file + read_history(ATOMSIM_HISTORY_FILE); +} + + +void Atomsim::deinit_interactive_mode(){ + // write history file + write_history(ATOMSIM_HISTORY_FILE); +} + + Rcode Atomsim::run_interactive_mode() { typedef Rcode (Atomsim::*interactive_func)(const std::vector&); @@ -188,18 +207,6 @@ Rcode Atomsim::run_interactive_mode() static std::string prev_cmd; static std::vector prev_args; - // create history file if it doen't exist - FILE *history_file = fopen(ATOMSIM_HISTORY_FILE, "a"); - if (history_file != NULL) { - fclose(history_file); - } else { - std::cerr << "Error creating history file.\n"; - return RC_EXIT; - } - - // read history file - read_history(ATOMSIM_HISTORY_FILE); - while(!backend_.done()) { // get input @@ -231,8 +238,6 @@ Rcode Atomsim::run_interactive_mode() // Add input to history if it's != previous input if(cmd != prev_cmd){ add_history(input.c_str()); - // save to history file - write_history(ATOMSIM_HISTORY_FILE); } // prev <= current