Skip to content

Commit

Permalink
sim: refactor & add features
Browse files Browse the repository at this point in the history
- add breakpoints support
- refactor simulation loop, use proper return codes
- cleanup backends
- add new commands: break & info
  • Loading branch information
saursin committed Oct 23, 2023
1 parent 35234f2 commit e52ea79
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 392 deletions.
115 changes: 96 additions & 19 deletions sim/atomsim.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#include "atomsim.hpp"
#include "simstate.hpp"
#include <iostream>
#include "util.hpp"

#include TARGET_HEADER
#define RV_INSTR_EBREAK 0x100073

Atomsim::Atomsim(Atomsim_config sim_config, Backend_config bk_config):
sim_config_(sim_config),
backend_(this, bk_config) // create backend
{
// get input file disassembly
disassembly_ = getDisassembly(sim_config_.ifile);

// clear breakpoints
for(int i=0; i<NUM_MAX_BREAKPOINTS; i++) {
breakpoints_[i].active = false;
}

// Open trace if specified at CLI
if (sim_config_.trace_flag)
Expand All @@ -31,33 +38,103 @@ void Atomsim::step()


int Atomsim::run()
{
{
// tick backend and update backend status, until ctrl+c is.
try
{
bkend_running_ = true; // assume this; since simulation is just started
// assume this; since simulation is just started
bkend_running_ = true;

// Should we be in debug mode at start?
in_debug_mode_ = sim_config_.debug_flag;

pending_steps = 0;

// *** Simulation Loop ***
Rcode rval = RC_NONE;
while (bkend_running_)
{
if(sim_config_.debug_flag || CTRL_C_PRESSED)
{
// we might enter here using ctrl+c also therefore need to set this
sim_config_.debug_flag = true;

// enter interactive mode
int rval = run_interactive_mode();

if (rval == ATOMSIM_RCODE_EXIT)
{
// back to run mode
sim_config_.debug_flag = false;
// Refresh state
backend_.refresh_state();

int breakpoint_hit = -1;

// Evaluate breakpoints (early)
for(int i=0; i<NUM_MAX_BREAKPOINTS; i++) {
if(breakpoints_[i].active && breakpoints_[i].addr == simstate_.state_.pc_e){
breakpoint_hit = i;
break;
}
}

// Display debug screen
// if we are in debug mode OR we'll be in debug mode due to CTRL_C OR breakpoint occured
if(in_debug_mode_ || CTRL_C_PRESSED || breakpoint_hit != -1) {
display_dbg_screen();
}

if(breakpoint_hit != -1) {
// Make sure we enter debug mode after breakpoint hit
printf("Breakpoint %d hit %s0x%08x%s\n", breakpoint_hit, ansicode(FG_BLUE), simstate_.state_.pc_e, ansicode(FG_RESET));
in_debug_mode_ = true;
pending_steps = 0;
}

// check ebreak
if(simstate_.state_.ins_e == RV_INSTR_EBREAK) {
printf("EBreak hit at %s0x%08x%s\n", ansicode(FG_BLUE), simstate_.state_.pc_e, ansicode(FG_RESET));

if(sim_config_.dump_on_ebreak_flag) // For SCAR
simstate_.dump_simstate(sim_config_.dump_file);

// ebreak hit while debug mode was enabled through cli
if(sim_config_.debug_flag) {
// back to interactive mode
in_debug_mode_ = true;
pending_steps = 0;
} else {
printf("Exiting..\n");
return 0;
}
}

// 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;
}

// Enter interactive mode if we aren's stepping and we are already in debug mode or run mode
// was interrupted by user (CTRL_C)
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;
pending_steps = 0;

// run interactive mode
rval = run_interactive_mode();

// if we return, that's either for exiting simulation, single/multi steping, or
// switching to run mode
if(rval == RC_STEP){
// Number of steps to be taken is set in pending_steps variable
// We just pass here
} else if (rval == RC_RUN) {
/* code */
in_debug_mode_ = false;
CTRL_C_PRESSED = false;
} else if (rval == RC_EXIT) {
// Exit sim
break;
}
else if (rval == ATOMSIM_RCODE_EXIT_SIM)
return 0; // exit sim
}
else
{
step();

// Step
this->step();

// Decrement pending_steps if we are in Step mode
if(rval == RC_STEP && pending_steps > 0) {
pending_steps--;
}
}
}
Expand Down
55 changes: 35 additions & 20 deletions sim/atomsim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include TARGET_HEADER
#include "simstate.hpp"

#define ATOMSIM_RCODE_OK 0
#define ATOMSIM_RCODE_EXIT 1
#define ATOMSIM_RCODE_EXIT_SIM 2
enum Rcode{
RC_NONE, RC_OK, RC_STEP, RC_RUN, RC_EXIT
};

#define NUM_MAX_BREAKPOINTS 16

// forward declarations
// class Backend_atomsim;
Expand Down Expand Up @@ -42,6 +44,10 @@ struct Atomsim_config
bool print_info_topdown = true;
};

struct Breakpoint_t {
bool active = false;
uint32_t addr = 0;
};

class Atomsim
{
Expand Down Expand Up @@ -70,7 +76,7 @@ class Atomsim
*
* @returns int return code [if 2: exit sim]
*/
int run_interactive_mode();
Rcode run_interactive_mode();


private:
Expand Down Expand Up @@ -99,6 +105,8 @@ class Atomsim
*/
bool bkend_running_ = false;

bool in_debug_mode_ = false;

/**
* @brief Disassembly of input file
*/
Expand All @@ -117,32 +125,39 @@ class Atomsim

friend class Backend_atomsim;

Breakpoint_t breakpoints_[NUM_MAX_BREAKPOINTS];

// used to provide cycles for step command
long long pending_steps = 0;

// display debug screen
void display_dbg_screen();

// interactive Commands

// General Commands
int cmd_help(const std::vector<std::string>&);
int cmd_quit(const std::vector<std::string>&);
int cmd_verbose(const std::vector<std::string>&);
int cmd_trace(const std::vector<std::string>&);
Rcode cmd_help(const std::vector<std::string>&);
Rcode cmd_quit(const std::vector<std::string>&);
Rcode cmd_verbose(const std::vector<std::string>&);
Rcode cmd_trace(const std::vector<std::string>&);

// Control Commands
int cmd_reset(const std::vector<std::string>&);
int cmd_step(const std::vector<std::string>&);
int cmd_run(const std::vector<std::string>&);
int cmd_rst(const std::vector<std::string>&);
int cmd_while(const std::vector<std::string>&);
Rcode cmd_reset(const std::vector<std::string>&);
Rcode cmd_step(const std::vector<std::string>&);
Rcode cmd_run(const std::vector<std::string>&);
Rcode cmd_rst(const std::vector<std::string>&);
Rcode cmd_while(const std::vector<std::string>&);
Rcode cmd_break(const std::vector<std::string>&);
Rcode cmd_info(const std::vector<std::string>&);

// Query Commands
int cmd_reg(const std::vector<std::string>&);
int cmd_dereference_reg(const std::vector<std::string>&);
int cmd_pc(const std::vector<std::string>&);
int cmd_str(const std::vector<std::string>&);
int cmd_mem(const std::vector<std::string>&);
int cmd_dumpmem(const std::vector<std::string>&);
int cmd_load(const std::vector<std::string>&);
Rcode cmd_reg(const std::vector<std::string>&);
Rcode cmd_dereference_reg(const std::vector<std::string>&);
Rcode cmd_pc(const std::vector<std::string>&);
Rcode cmd_str(const std::vector<std::string>&);
Rcode cmd_mem(const std::vector<std::string>&);
Rcode cmd_dumpmem(const std::vector<std::string>&);
Rcode cmd_load(const std::vector<std::string>&);

};

Expand Down
Loading

0 comments on commit e52ea79

Please sign in to comment.