refactor RTL sim, added DRAM stalls support

This commit is contained in:
Blaise Tine
2020-03-30 04:13:19 -04:00
parent 638625184f
commit f6eb5dfbae
3 changed files with 183 additions and 235 deletions

View File

@@ -2,166 +2,129 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
unsigned long time_stamp = 0;
double sc_time_stamp() {
return time_stamp / 1000.0;
}
Simulator::Simulator(RAM *ram) Simulator::Simulator(RAM *ram)
: start_pc(0), curr_cycle(0), stop(true), unit_test(true), stats_static_inst(0), stats_dynamic_inst(-1), : total_cycles_(0)
stats_total_cycles(0), stats_fwd_stalls(0), stats_branch_stalls(0), , dram_stalled_(false)
debug_state(0), ibus_state(0), dbus_state(0), debug_return(0), , I_dram_stalled_(false) {
debug_wait_num(0), debug_inst_num(0), debug_end_wait(0), debug_debugAddr(0) { ram_ = ram;
this->ram = ram;
#ifdef USE_MULTICORE #ifdef USE_MULTICORE
this->vortex = new VVortex_SOC(); vortex_ = new VVortex_SOC();
#else #else
this->vortex = new VVortex(); vortex_ = new VVortex();
#endif #endif
#ifdef VCD_OUTPUT #ifdef VCD_OUTPUT
Verilated::traceEverOn(true); Verilated::traceEverOn(true);
this->m_trace = new VerilatedVcdC; trace_ = new VerilatedVcdC;
this->vortex->trace(m_trace, 99); vortex_->trace(trace_, 99);
this->m_trace->open("trace.vcd"); trace_->open("trace.vcd");
#endif #endif
this->results.open("../results.txt");
} }
Simulator::~Simulator() { Simulator::~Simulator() {
#ifdef VCD_OUTPUT #ifdef VCD_OUTPUT
m_trace->close(); trace_->close();
#endif #endif
this->results.close(); delete vortex_;
delete this->vortex;
} }
void Simulator::print_stats(bool cycle_test) { void Simulator::print_stats(std::ostream& out) {
if (cycle_test) { out << std::left;
this->results << std::left; out << std::setw(24) << "# of total cycles:" << std::dec << total_cycles_ << std::endl;
// this->results << "# Static Instructions:\t" << std::dec << this->stats_static_inst << std::endl;
this->results << std::setw(24) << "# Dynamic Instructions:" << std::dec << this->stats_dynamic_inst << std::endl;
this->results << std::setw(24) << "# of total cycles:" << std::dec << this->stats_total_cycles << std::endl;
this->results << std::setw(24) << "# of forwarding stalls:" << std::dec << this->stats_fwd_stalls << std::endl;
this->results << std::setw(24) << "# of branch stalls:" << std::dec << this->stats_branch_stalls << std::endl;
this->results << std::setw(24) << "# CPI:" << std::dec << (double)this->stats_total_cycles / (double)this->stats_dynamic_inst << std::endl;
this->results << std::setw(24) << "# time to simulate: " << std::dec << this->stats_sim_time << " milliseconds" << std::endl;
} else {
this->results << std::left;
this->results << std::setw(24) << "# of total cycles:" << std::dec << this->stats_total_cycles << std::endl;
this->results << std::setw(24) << "# time to simulate: " << std::dec << this->stats_sim_time << " milliseconds" << std::endl;
}
uint32_t status;
ram->getWord(0, &status);
if (this->unit_test) {
if (status == 1) {
this->results << std::setw(24) << "# GRADE:"
<< "PASSING\n";
} else {
this->results << std::setw(24) << "# GRADE:"
<< "Failed on test: " << status << "\n";
}
} else {
this->results << std::setw(24) << "# GRADE:"
<< "N/A [NOT A UNIT TEST]\n";
}
this->stats_static_inst = 0;
this->stats_dynamic_inst = -1;
this->stats_total_cycles = 0;
this->stats_fwd_stalls = 0;
this->stats_branch_stalls = 0;
} }
#ifndef USE_MULTICORE #ifndef USE_MULTICORE
bool Simulator::ibus_driver() { void Simulator::ibus_driver() {
// Iterate through each element, and get pop index // Iterate through each element, and get pop index
int dequeue_index = -1; int dequeue_index = -1;
bool dequeue_valid = false; bool dequeue_valid = false;
for (int i = 0; i < this->I_dram_req_vec.size(); i++) { for (int i = 0; i < I_dram_req_vec_.size(); i++) {
if (this->I_dram_req_vec[i].cycles_left > 0) { if (I_dram_req_vec_[i].cycles_left > 0) {
this->I_dram_req_vec[i].cycles_left -= 1; I_dram_req_vec_[i].cycles_left -= 1;
} }
if ((this->I_dram_req_vec[i].cycles_left == 0) && (!dequeue_valid)) { if ((I_dram_req_vec_[i].cycles_left == 0) && (!dequeue_valid)) {
dequeue_index = i; dequeue_index = i;
dequeue_valid = true; dequeue_valid = true;
} }
} }
if (vortex->I_dram_req) { if (vortex_->I_dram_req && !I_dram_stalled_) {
// std::cout << "Icache Dram Request received!\n"; // std::cout << "Icache Dram Request received!\n";
if (vortex->I_dram_req_read) { if (vortex_->I_dram_req_read) {
// std::cout << "Icache Dram Request is read!\n"; // std::cout << "Icache Dram Request is read!\n";
// Need to add an element // Need to add an element
dram_req_t dram_req; dram_req_t dram_req;
dram_req.cycles_left = vortex->I_dram_expected_lat; dram_req.cycles_left = DRAM_LATENCY;
dram_req.data_length = vortex->I_dram_req_size / 4; dram_req.data_length = vortex_->I_dram_req_size / 4;
dram_req.base_addr = vortex->I_dram_req_addr; dram_req.base_addr = vortex_->I_dram_req_addr;
dram_req.data = (unsigned *)malloc(dram_req.data_length * sizeof(unsigned)); dram_req.data = (unsigned *)malloc(dram_req.data_length * sizeof(unsigned));
for (int i = 0; i < dram_req.data_length; i++) { for (int i = 0; i < dram_req.data_length; i++) {
unsigned curr_addr = dram_req.base_addr + (i * 4); unsigned curr_addr = dram_req.base_addr + (i * 4);
unsigned data_rd; unsigned data_rd;
ram->getWord(curr_addr, &data_rd); ram_->getWord(curr_addr, &data_rd);
dram_req.data[i] = data_rd; dram_req.data[i] = data_rd;
} }
// std::cout << "Fill Req -> Addr: " << std::hex << dram_req.base_addr << std::dec << "\n"; // std::cout << "Fill Req -> Addr: " << std::hex << dram_req.base_addr << std::dec << "\n";
this->I_dram_req_vec.push_back(dram_req); I_dram_req_vec_.push_back(dram_req);
} }
if (vortex->I_dram_req_write) { if (vortex_->I_dram_req_write) {
unsigned base_addr = vortex->I_dram_req_addr; unsigned base_addr = vortex_->I_dram_req_addr;
unsigned data_length = vortex->I_dram_req_size / 4; unsigned data_length = vortex_->I_dram_req_size / 4;
for (int i = 0; i < data_length; i++) { for (int i = 0; i < data_length; i++) {
unsigned curr_addr = base_addr + (i * 4); unsigned curr_addr = base_addr + (i * 4);
unsigned data_wr = vortex->I_dram_req_data[i]; unsigned data_wr = vortex_->I_dram_req_data[i];
ram->writeWord(curr_addr, &data_wr); ram_->writeWord(curr_addr, &data_wr);
} }
} }
} }
if (vortex->I_dram_fill_accept && dequeue_valid) { if (vortex_->I_dram_fill_accept && dequeue_valid) {
// std::cout << "Icache Dram Response Sending...!\n"; // std::cout << "Icache Dram Response Sending...!\n";
vortex->I_dram_fill_rsp = 1; vortex_->I_dram_fill_rsp = 1;
vortex->I_dram_fill_rsp_addr = this->I_dram_req_vec[dequeue_index].base_addr; vortex_->I_dram_fill_rsp_addr = I_dram_req_vec_[dequeue_index].base_addr;
// std::cout << "Fill Rsp -> Addr: " << std::hex << (this->I_dram_req_vec[dequeue_index].base_addr) << std::dec << "\n"; // std::cout << "Fill Rsp -> Addr: " << std::hex << (I_dram_req_vec_[dequeue_index].base_addr) << std::dec << "\n";
for (int i = 0; i < this->I_dram_req_vec[dequeue_index].data_length; i++) { for (int i = 0; i < I_dram_req_vec_[dequeue_index].data_length; i++) {
vortex->I_dram_fill_rsp_data[i] = this->I_dram_req_vec[dequeue_index].data[i]; vortex_->I_dram_fill_rsp_data[i] = I_dram_req_vec_[dequeue_index].data[i];
} }
free(this->I_dram_req_vec[dequeue_index].data); free(I_dram_req_vec_[dequeue_index].data);
this->I_dram_req_vec.erase(this->I_dram_req_vec.begin() + dequeue_index); I_dram_req_vec_.erase(I_dram_req_vec_.begin() + dequeue_index);
} else { } else {
vortex->I_dram_fill_rsp = 0; vortex_->I_dram_fill_rsp = 0;
vortex->I_dram_fill_rsp_addr = 0; vortex_->I_dram_fill_rsp_addr = 0;
} }
return false; #ifdef ENABLE_DRAM_STALLS
I_dram_stalled_ = false;
if (0 == (total_cycles_ % DRAM_STALLS_MODULO)) {
I_dram_stalled_ = true;
} else
if (I_dram_req_vec_.size() >= DRAM_RQ_SIZE) {
I_dram_stalled_ = true;
}
#endif
} }
#endif #endif
bool Simulator::dbus_driver() { void Simulator::dbus_driver() {
// Iterate through each element, and get pop index // Iterate through each element, and get pop index
int dequeue_index = -1; int dequeue_index = -1;
bool dequeue_valid = false; bool dequeue_valid = false;
for (int i = 0; i < this->dram_req_vec.size(); i++) { for (int i = 0; i < dram_req_vec_.size(); i++) {
if (this->dram_req_vec[i].cycles_left > 0) { if (dram_req_vec_[i].cycles_left > 0) {
this->dram_req_vec[i].cycles_left -= 1; dram_req_vec_[i].cycles_left -= 1;
} }
if ((this->dram_req_vec[i].cycles_left == 0) && (!dequeue_valid)) { if ((dram_req_vec_[i].cycles_left == 0) && (!dequeue_valid)) {
dequeue_index = i; dequeue_index = i;
dequeue_valid = true; dequeue_valid = true;
} }
@@ -169,113 +132,117 @@ bool Simulator::dbus_driver() {
#ifdef USE_MULTICORE #ifdef USE_MULTICORE
if (vortex->out_dram_req) { if (vortex_->out_dram_req && !dram_stalled_) {
if (vortex->out_dram_req_read) { if (vortex_->out_dram_req_read) {
// Need to add an element // Need to add an element
dram_req_t dram_req; dram_req_t dram_req;
dram_req.cycles_left = vortex->out_dram_expected_lat; dram_req.cycles_left = DRAM_LATENCY;
dram_req.data_length = vortex->out_dram_req_size / 4; dram_req.data_length = vortex_->out_dram_req_size / 4;
dram_req.base_addr = vortex->out_dram_req_addr; dram_req.base_addr = vortex_->out_dram_req_addr;
dram_req.data = (unsigned *)malloc(dram_req.data_length * sizeof(unsigned)); dram_req.data = (unsigned *)malloc(dram_req.data_length * sizeof(unsigned));
for (int i = 0; i < dram_req.data_length; i++) { for (int i = 0; i < dram_req.data_length; i++) {
unsigned curr_addr = dram_req.base_addr + (i * 4); unsigned curr_addr = dram_req.base_addr + (i * 4);
unsigned data_rd; unsigned data_rd;
ram->getWord(curr_addr, &data_rd); ram_->getWord(curr_addr, &data_rd);
dram_req.data[i] = data_rd; dram_req.data[i] = data_rd;
} }
// std::cout << "Fill Req -> Addr: " << std::hex << dram_req.base_addr << std::dec << "\n"; dram_req_vec_.push_back(dram_req);
this->dram_req_vec.push_back(dram_req);
} }
if (vortex->out_dram_req_write) { if (vortex_->out_dram_req_write) {
unsigned base_addr = vortex->out_dram_req_addr; unsigned base_addr = vortex_->out_dram_req_addr;
unsigned data_length = vortex->out_dram_req_size / 4; unsigned data_length = vortex_->out_dram_req_size / 4;
for (int i = 0; i < data_length; i++) { for (int i = 0; i < data_length; i++) {
unsigned curr_addr = base_addr + (i * 4); unsigned curr_addr = base_addr + (i * 4);
unsigned data_wr = vortex->out_dram_req_data[i]; unsigned data_wr = vortex_->out_dram_req_data[i];
ram->writeWord(curr_addr, &data_wr); ram_->writeWord(curr_addr, &data_wr);
} }
} }
} }
if (vortex->out_dram_fill_accept && dequeue_valid) { if (vortex_->out_dram_fill_accept && dequeue_valid) {
vortex->out_dram_fill_rsp = 1; vortex_->out_dram_fill_rsp = 1;
vortex->out_dram_fill_rsp_addr = this->dram_req_vec[dequeue_index].base_addr; vortex_->out_dram_fill_rsp_addr = dram_req_vec_[dequeue_index].base_addr;
// std::cout << "Fill Rsp -> Addr: " << std::hex << (this->dram_req_vec[dequeue_index].base_addr) << std::dec << "\n";
for (int i = 0; i < this->dram_req_vec[dequeue_index].data_length; i++) { for (int i = 0; i < dram_req_vec_[dequeue_index].data_length; i++) {
vortex->out_dram_fill_rsp_data[i] = this->dram_req_vec[dequeue_index].data[i]; vortex_->out_dram_fill_rsp_data[i] = dram_req_vec_[dequeue_index].data[i];
} }
free(this->dram_req_vec[dequeue_index].data); free(dram_req_vec_[dequeue_index].data);
this->dram_req_vec.erase(this->dram_req_vec.begin() + dequeue_index); dram_req_vec_.erase(dram_req_vec_.begin() + dequeue_index);
} else { } else {
vortex->out_dram_fill_rsp = 0; vortex_->out_dram_fill_rsp = 0;
vortex->out_dram_fill_rsp_addr = 0; vortex_->out_dram_fill_rsp_addr = 0;
} }
#else #else
if (vortex->dram_req) { if (vortex_->dram_req && !dram_stalled_) {
if (vortex->dram_req_read) { if (vortex_->dram_req_read) {
// Need to add an element // Need to add an element
dram_req_t dram_req; dram_req_t dram_req;
dram_req.cycles_left = vortex->dram_expected_lat; dram_req.cycles_left = DRAM_LATENCY;
dram_req.data_length = vortex->dram_req_size / 4; dram_req.data_length = vortex_->dram_req_size / 4;
dram_req.base_addr = vortex->dram_req_addr; dram_req.base_addr = vortex_->dram_req_addr;
dram_req.data = (unsigned *)malloc(dram_req.data_length * sizeof(unsigned)); dram_req.data = (unsigned *)malloc(dram_req.data_length * sizeof(unsigned));
for (int i = 0; i < dram_req.data_length; i++) { for (int i = 0; i < dram_req.data_length; i++) {
unsigned curr_addr = dram_req.base_addr + (i * 4); unsigned curr_addr = dram_req.base_addr + (i * 4);
unsigned data_rd; unsigned data_rd;
ram->getWord(curr_addr, &data_rd); ram_->getWord(curr_addr, &data_rd);
dram_req.data[i] = data_rd; dram_req.data[i] = data_rd;
} }
// std::cout << "Fill Req -> Addr: " << std::hex << dram_req.base_addr << std::dec << "\n"; dram_req_vec_.push_back(dram_req);
this->dram_req_vec.push_back(dram_req);
} }
if (vortex->dram_req_write) { if (vortex_->dram_req_write) {
unsigned base_addr = vortex->dram_req_addr; unsigned base_addr = vortex_->dram_req_addr;
unsigned data_length = vortex->dram_req_size / 4; unsigned data_length = vortex_->dram_req_size / 4;
for (int i = 0; i < data_length; i++) { for (int i = 0; i < data_length; i++) {
unsigned curr_addr = base_addr + (i * 4); unsigned curr_addr = base_addr + (i * 4);
unsigned data_wr = vortex->dram_req_data[i]; unsigned data_wr = vortex_->dram_req_data[i];
ram->writeWord(curr_addr, &data_wr); ram_->writeWord(curr_addr, &data_wr);
} }
} }
} }
if (vortex->dram_fill_accept && dequeue_valid) { if (vortex_->dram_fill_accept && dequeue_valid) {
vortex->dram_fill_rsp = 1; vortex_->dram_fill_rsp = 1;
vortex->dram_fill_rsp_addr = this->dram_req_vec[dequeue_index].base_addr; vortex_->dram_fill_rsp_addr = dram_req_vec_[dequeue_index].base_addr;
// std::cout << "Fill Rsp -> Addr: " << std::hex << (this->dram_req_vec[dequeue_index].base_addr) << std::dec << "\n";
for (int i = 0; i < this->dram_req_vec[dequeue_index].data_length; i++) { for (int i = 0; i < dram_req_vec_[dequeue_index].data_length; i++) {
vortex->dram_fill_rsp_data[i] = this->dram_req_vec[dequeue_index].data[i]; vortex_->dram_fill_rsp_data[i] = dram_req_vec_[dequeue_index].data[i];
} }
free(this->dram_req_vec[dequeue_index].data); free(dram_req_vec_[dequeue_index].data);
this->dram_req_vec.erase(this->dram_req_vec.begin() + dequeue_index); dram_req_vec_.erase(dram_req_vec_.begin() + dequeue_index);
} else { } else {
vortex->dram_fill_rsp = 0; vortex_->dram_fill_rsp = 0;
vortex->dram_fill_rsp_addr = 0; vortex_->dram_fill_rsp_addr = 0;
} }
#endif #endif
return false; #ifdef ENABLE_DRAM_STALLS
dram_stalled_ = false;
if (0 == (total_cycles_ % DRAM_STALLS_MODULO)) {
dram_stalled_ = true;
} else
if (dram_req_vec_.size() >= DRAM_RQ_SIZE) {
dram_stalled_ = true;
}
#endif
} }
void Simulator::io_handler() { void Simulator::io_handler() {
#ifdef USE_MULTICORE #ifdef USE_MULTICORE
bool io_valid = false; bool io_valid = false;
for (int c = 0; c < vortex->number_cores; c++) { for (int c = 0; c < vortex_->number_cores; c++) {
if (vortex->io_valid[c]) { if (vortex_->io_valid[c]) {
uint32_t data_write = (uint32_t)vortex->io_data[c]; uint32_t data_write = (uint32_t)vortex_->io_data[c];
char c = (char)data_write; char c = (char)data_write;
std::cerr << c; std::cerr << c;
io_valid = true; io_valid = true;
@@ -285,8 +252,8 @@ void Simulator::io_handler() {
std::cout << std::flush; std::cout << std::flush;
} }
#else #else
if (vortex->io_valid) { if (vortex_->io_valid) {
uint32_t data_write = (uint32_t)vortex->io_data; uint32_t data_write = (uint32_t)vortex_->io_data;
char c = (char)data_write; char c = (char)data_write;
std::cerr << c; std::cerr << c;
std::cout << std::flush; std::cout << std::flush;
@@ -295,21 +262,21 @@ void Simulator::io_handler() {
} }
void Simulator::reset() { void Simulator::reset() {
vortex->reset = 1; vortex_->reset = 1;
this->step(); this->step();
vortex->reset = 0; vortex_->reset = 0;
} }
void Simulator::step() { void Simulator::step() {
vortex->clk = 0; vortex_->clk = 0;
vortex->eval(); vortex_->eval();
#ifdef VCD_OUTPUT #ifdef VCD_OUTPUT
m_trace->dump(2 * this->stats_total_cycles + 0); trace_->dump(2 * total_cycles_ + 0);
#endif #endif
vortex->clk = 1; vortex_->clk = 1;
vortex->eval(); vortex_->eval();
#ifndef USE_MULTICORE #ifndef USE_MULTICORE
ibus_driver(); ibus_driver();
@@ -319,11 +286,10 @@ void Simulator::step() {
io_handler(); io_handler();
#ifdef VCD_OUTPUT #ifdef VCD_OUTPUT
m_trace->dump(2 * this->stats_total_cycles + 1); trace_->dump(2 * total_cycles_ + 1);
#endif #endif
++time_stamp; ++total_cycles_;
++stats_total_cycles;
} }
void Simulator::wait(uint32_t cycles) { void Simulator::wait(uint32_t cycles) {
@@ -333,7 +299,7 @@ void Simulator::wait(uint32_t cycles) {
} }
bool Simulator::is_busy() { bool Simulator::is_busy() {
return (0 == vortex->out_ebreak); return (0 == vortex_->out_ebreak);
} }
void Simulator::send_snoops(uint32_t mem_addr, uint32_t size) { void Simulator::send_snoops(uint32_t mem_addr, uint32_t size) {
@@ -343,34 +309,34 @@ void Simulator::send_snoops(uint32_t mem_addr, uint32_t size) {
#ifdef USE_MULTICORE #ifdef USE_MULTICORE
// submit snoop requests for the needed blocks // submit snoop requests for the needed blocks
vortex->llc_snp_req_addr = aligned_addr_start; vortex_->llc_snp_req_addr = aligned_addr_start;
vortex->llc_snp_req = false; vortex_->llc_snp_req = false;
for (;;) { for (;;) {
this->step(); this->step();
if (vortex->llc_snp_req) { if (vortex_->llc_snp_req) {
vortex->llc_snp_req = false; vortex_->llc_snp_req = false;
if (vortex->llc_snp_req_addr >= aligned_addr_end) if (vortex_->llc_snp_req_addr >= aligned_addr_end)
break; break;
vortex->llc_snp_req_addr += GLOBAL_BLOCK_SIZE_BYTES; vortex_->llc_snp_req_addr += GLOBAL_BLOCK_SIZE_BYTES;
} }
if (!vortex->llc_snp_req_delay) { if (!vortex_->llc_snp_req_delay) {
vortex->llc_snp_req = true; vortex_->llc_snp_req = true;
} }
} }
#else #else
// submit snoop requests for the needed blocks // submit snoop requests for the needed blocks
vortex->snp_req_addr = aligned_addr_start; vortex_->snp_req_addr = aligned_addr_start;
vortex->snp_req = false; vortex_->snp_req = false;
for (;;) { for (;;) {
this->step(); this->step();
if (vortex->snp_req) { if (vortex_->snp_req) {
vortex->snp_req = false; vortex_->snp_req = false;
if (vortex->snp_req_addr >= aligned_addr_end) if (vortex_->snp_req_addr >= aligned_addr_end)
break; break;
vortex->snp_req_addr += GLOBAL_BLOCK_SIZE_BYTES; vortex_->snp_req_addr += GLOBAL_BLOCK_SIZE_BYTES;
} }
if (!vortex->snp_req_delay) { if (!vortex_->snp_req_delay) {
vortex->snp_req = true; vortex_->snp_req = true;
} }
} }
#endif #endif
@@ -385,8 +351,8 @@ void Simulator::flush_caches(uint32_t mem_addr, uint32_t size) {
this->send_snoops(mem_addr, size); this->send_snoops(mem_addr, size);
#endif #endif
// wait 300 cycles to ensure that the request has committed // wait some cycles to ensure that the request has committed
this->wait(300); this->wait(PIPELINE_FLUSH_LATENCY);
} }
bool Simulator::run() { bool Simulator::run() {
@@ -394,22 +360,18 @@ bool Simulator::run() {
this->reset(); this->reset();
// execute program // execute program
while (!vortex->out_ebreak) { while (!vortex_->out_ebreak) {
this->step(); this->step();
} }
// wait 5 cycles to flush the pipeline // wait 5 cycles to flush the pipeline
this->wait(5); this->wait(5);
std::cerr << "New Total Cycles: " << (this->stats_total_cycles) << "\n";
this->print_stats();
#ifdef USE_MULTICORE #ifdef USE_MULTICORE
int status = 0; int status = 0;
#else #else
// check riscv-tests PASSED/FAILED status // check riscv-tests PASSED/FAILED status
int status = (unsigned int) vortex->Vortex->vx_back_end->VX_wb->last_data_wb & 0xf; int status = (int)vortex_->Vortex->vx_back_end->VX_wb->last_data_wb & 0xf;
#endif #endif
return (status == 1); return (status == 1);

View File

@@ -15,9 +15,15 @@
#include "VX_define.h" #include "VX_define.h"
#include "ram.h" #include "ram.h"
#include <fstream> #include <ostream>
#include <vector> #include <vector>
//#define ENABLE_DRAM_STALLS
#define DRAM_LATENCY 200
#define DRAM_RQ_SIZE 16
#define DRAM_STALLS_MODULO 16
#define PIPELINE_FLUSH_LATENCY 300
typedef struct { typedef struct {
int cycles_left; int cycles_left;
int data_length; int data_length;
@@ -36,53 +42,31 @@ public:
void step(); void step();
void flush_caches(uint32_t mem_addr, uint32_t size); void flush_caches(uint32_t mem_addr, uint32_t size);
bool run(); bool run();
void print_stats(std::ostream& out);
protected: private:
void print_stats(bool cycle_test = true);
#ifndef USE_MULTICORE #ifndef USE_MULTICORE
bool ibus_driver(); void ibus_driver();
#endif #endif
bool dbus_driver(); void dbus_driver();
void io_handler(); void io_handler();
void send_snoops(uint32_t mem_addr, uint32_t size); void send_snoops(uint32_t mem_addr, uint32_t size);
void wait(uint32_t cycles); void wait(uint32_t cycles);
RAM *ram; uint64_t total_cycles_;
bool dram_stalled_;
unsigned start_pc; bool I_dram_stalled_;
bool refill_d; std::vector<dram_req_t> dram_req_vec_;
unsigned refill_addr_d; std::vector<dram_req_t> I_dram_req_vec_;
bool refill_i; RAM *ram_;
unsigned refill_addr_i;
long int curr_cycle;
bool stop;
bool unit_test;
std::ofstream results;
int stats_static_inst;
int stats_dynamic_inst;
int stats_total_cycles;
int stats_fwd_stalls;
int stats_branch_stalls;
int debug_state;
int ibus_state;
int dbus_state;
int debug_return;
int debug_wait_num;
int debug_inst_num;
int debug_end_wait;
int debug_debugAddr;
double stats_sim_time;
std::vector<dram_req_t> dram_req_vec;
std::vector<dram_req_t> I_dram_req_vec;
#ifdef USE_MULTICORE #ifdef USE_MULTICORE
VVortex_SOC *vortex; VVortex_SOC *vortex_;
#else #else
VVortex *vortex; VVortex *vortex_;
#endif #endif
#ifdef VCD_OUTPUT #ifdef VCD_OUTPUT
VerilatedVcdC *m_trace; VerilatedVcdC *trace_;
#endif #endif
}; };

View File

@@ -1,5 +1,7 @@
#include "simulator.h" #include "simulator.h"
#include <iostream> #include <iostream>
#include <fstream>
#include <iomanip>
#define NUM_TESTS 46 #define NUM_TESTS 46
@@ -10,9 +12,9 @@ int main(int argc, char **argv)
Verilated::commandArgs(argc, argv); Verilated::commandArgs(argc, argv);
// #define ALL_TESTS //#define ALL_TESTS
#ifdef ALL_TESTS #ifdef ALL_TESTS
bool passed = true; bool passed = true;
std::string tests[NUM_TESTS] = { std::string tests[NUM_TESTS] = {
"../../emulator/riscv_tests/rv32ui-p-add.hex", "../../emulator/riscv_tests/rv32ui-p-add.hex",
@@ -63,31 +65,31 @@ int main(int argc, char **argv)
"../../emulator/riscv_tests/rv32um-p-remu.hex" "../../emulator/riscv_tests/rv32um-p-remu.hex"
}; };
for (std::string s : tests) { for (std::string s : tests) {
std::cerr << DEFAULT << "\n---------------------------------------\n"; std::cerr << DEFAULT << "\n---------------------------------------\n";
std::cerr << s << std::endl; std::cerr << s << std::endl;
RAM ram; RAM ram;
loadHexImpl(s.c_str(), &ram); loadHexImpl(s.c_str(), &ram);
Simulator v(&ram); Simulator simulator(&ram);
bool curr = v.simulate(); bool curr = simulator.run();
if ( curr) std::cerr << GREEN << "Test Passed: " << s << std::endl; if (curr) std::cerr << GREEN << "Test Passed: " << s << std::endl;
if (!curr) std::cerr << RED << "Test Failed: " << s << std::endl; if (!curr) std::cerr << RED << "Test Failed: " << s << std::endl;
std::cerr << DEFAULT; std::cerr << DEFAULT;
passed = passed && curr; passed = passed && curr;
} }
std::cerr << DEFAULT << "\n***************************************\n"; std::cerr << DEFAULT << "\n***************************************\n";
if( passed) std::cerr << DEFAULT << "PASSED ALL TESTS\n"; if (passed) std::cerr << DEFAULT << "PASSED ALL TESTS\n";
if(!passed) std::cerr << DEFAULT << "Failed one or more tests\n"; if(!passed) std::cerr << DEFAULT << "Failed one or more tests\n";
return !passed; return !passed;
#else #else
char testing[] = "../../runtime/mains/simple/vx_simple_main.hex"; char testing[] = "../../runtime/mains/simple/vx_simple_main.hex";
//char testing[] = "../../emulator/riscv_tests/rv32ui-p-lw.hex"; //char testing[] = "../../emulator/riscv_tests/rv32ui-p-lw.hex";
@@ -109,7 +111,7 @@ int main(int argc, char **argv)
Simulator simulator(&ram); Simulator simulator(&ram);
bool curr = simulator.run(); bool curr = simulator.run();
if ( curr) std::cerr << GREEN << "Test Passed: " << testing << std::endl; if (curr) std::cerr << GREEN << "Test Passed: " << testing << std::endl;
if (!curr) std::cerr << RED << "Test Failed: " << testing << std::endl; if (!curr) std::cerr << RED << "Test Failed: " << testing << std::endl;
return !curr; return !curr;