Skip to content

Commit

Permalink
sim: better historyfile management
Browse files Browse the repository at this point in the history
  • Loading branch information
saursin committed May 3, 2024
1 parent adea8a6 commit e7f3784
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
19 changes: 15 additions & 4 deletions sim/atomsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -125,6 +131,7 @@ int Atomsim::run()
CTRL_C_PRESSED = false;
} else if (rval == RC_EXIT) {
// Exit sim
exitcode = EXIT_SUCCESS;
break;
}
}
Expand All @@ -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;
}
33 changes: 21 additions & 12 deletions sim/atomsim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,13 @@ class Atomsim
*/
Atomsim(Atomsim_config sim_config, Backend_config bk_config);

/**
* @brief step simulation by a cycle
*/
void step();

/**
* @brief run simulation until finished
* @return int return code (non 0 if finished)
*/
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
Expand Down Expand Up @@ -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();

Expand Down
33 changes: 19 additions & 14 deletions sim/interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<std::string>&);
Expand Down Expand Up @@ -188,18 +207,6 @@ Rcode Atomsim::run_interactive_mode()
static std::string prev_cmd;
static std::vector<std::string> 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e7f3784

Please sign in to comment.