2014-09-12 19:15:04 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2016-06-08 10:39:40 +02:00
|
|
|
#ifndef VERILATOR
|
2012-10-02 04:30:11 +02:00
|
|
|
#include "emulator.h"
|
2016-06-08 10:39:40 +02:00
|
|
|
#else
|
|
|
|
#include "verilated.h"
|
|
|
|
#if VM_TRACE
|
|
|
|
#include "verilated_vcd_c.h"
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-12-04 16:04:26 +01:00
|
|
|
#include "mm.h"
|
|
|
|
#include "mm_dramsim2.h"
|
2016-06-23 09:17:29 +02:00
|
|
|
#include <fesvr/dtm.h>
|
2016-06-08 10:39:40 +02:00
|
|
|
#include <iostream>
|
2013-01-25 08:56:45 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-03-26 01:01:13 +01:00
|
|
|
#include <unistd.h>
|
2012-10-02 04:30:11 +02:00
|
|
|
|
2015-11-03 05:10:10 +01:00
|
|
|
#define MEM_SIZE_BITS 3
|
|
|
|
#define MEM_LEN_BITS 8
|
|
|
|
#define MEM_RESP_BITS 2
|
|
|
|
|
2016-06-08 10:39:40 +02:00
|
|
|
#include "emulator_type.h"
|
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
static dtm_t* dtm;
|
2016-06-23 21:17:26 +02:00
|
|
|
static uint64_t trace_count = 0;
|
2016-06-18 06:09:08 +02:00
|
|
|
bool verbose;
|
|
|
|
|
2013-01-25 08:56:45 +01:00
|
|
|
void handle_sigterm(int sig)
|
2012-11-20 14:40:44 +01:00
|
|
|
{
|
2016-06-23 09:17:29 +02:00
|
|
|
dtm->stop();
|
2012-11-20 14:40:44 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 21:17:26 +02:00
|
|
|
double sc_time_stamp()
|
|
|
|
{
|
|
|
|
return trace_count;
|
|
|
|
}
|
|
|
|
|
2012-10-02 04:30:11 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
unsigned random_seed = (unsigned)time(NULL) ^ (unsigned)getpid();
|
2013-10-29 21:24:09 +01:00
|
|
|
uint64_t max_cycles = -1;
|
2015-09-19 03:02:03 +02:00
|
|
|
uint64_t start = 0;
|
2013-10-29 21:24:09 +01:00
|
|
|
int ret = 0;
|
2012-10-02 04:30:11 +02:00
|
|
|
const char* vcd = NULL;
|
|
|
|
const char* loadmem = NULL;
|
2013-06-13 19:53:23 +02:00
|
|
|
FILE *vcdfile = NULL;
|
2012-12-04 16:04:26 +01:00
|
|
|
bool dramsim2 = false;
|
2015-12-04 21:04:13 +01:00
|
|
|
bool print_cycles = false;
|
2015-02-13 17:56:54 +01:00
|
|
|
uint64_t memsz_mb = MEM_SIZE / (1024*1024);
|
2015-11-03 05:10:10 +01:00
|
|
|
mm_t *mm[N_MEM_CHANNELS];
|
2012-11-20 14:40:44 +01:00
|
|
|
|
2012-10-02 04:30:11 +02:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
std::string arg = argv[i];
|
2013-01-25 08:56:45 +01:00
|
|
|
if (arg.substr(0, 2) == "-v")
|
2012-10-02 04:30:11 +02:00
|
|
|
vcd = argv[i]+2;
|
2015-02-13 17:56:54 +01:00
|
|
|
else if (arg.substr(0, 9) == "+memsize=")
|
|
|
|
memsz_mb = atoll(argv[i]+9);
|
2012-10-02 04:30:11 +02:00
|
|
|
else if (arg.substr(0, 2) == "-s")
|
|
|
|
random_seed = atoi(argv[i]+2);
|
2013-01-25 08:56:45 +01:00
|
|
|
else if (arg == "+dramsim")
|
|
|
|
dramsim2 = true;
|
|
|
|
else if (arg == "+verbose")
|
2016-06-18 06:09:08 +02:00
|
|
|
verbose = true;
|
2013-01-25 08:56:45 +01:00
|
|
|
else if (arg.substr(0, 12) == "+max-cycles=")
|
|
|
|
max_cycles = atoll(argv[i]+12);
|
2012-10-02 04:30:11 +02:00
|
|
|
else if (arg.substr(0, 9) == "+loadmem=")
|
|
|
|
loadmem = argv[i]+9;
|
2015-09-19 03:02:03 +02:00
|
|
|
else if (arg.substr(0, 7) == "+start=")
|
|
|
|
start = atoll(argv[i]+7);
|
2015-12-04 21:04:13 +01:00
|
|
|
else if (arg.substr(0, 12) == "+cycle-count")
|
|
|
|
print_cycles = true;
|
2013-01-25 08:56:45 +01:00
|
|
|
}
|
2012-10-02 04:30:11 +02:00
|
|
|
|
2012-11-18 02:25:43 +01:00
|
|
|
const int disasm_len = 24;
|
2016-06-08 10:39:40 +02:00
|
|
|
|
|
|
|
#ifndef VERILATOR
|
2012-10-02 04:30:11 +02:00
|
|
|
if (vcd)
|
|
|
|
{
|
|
|
|
// Create a VCD file
|
|
|
|
vcdfile = strcmp(vcd, "-") == 0 ? stdout : fopen(vcd, "w");
|
|
|
|
assert(vcdfile);
|
|
|
|
fprintf(vcdfile, "$scope module Testbench $end\n");
|
2012-11-17 16:21:29 +01:00
|
|
|
fprintf(vcdfile, "$var reg %d NDISASM_WB wb_instruction $end\n", disasm_len*8);
|
|
|
|
fprintf(vcdfile, "$var reg 64 NCYCLE cycle $end\n");
|
2012-10-02 04:30:11 +02:00
|
|
|
fprintf(vcdfile, "$upscope $end\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// The chisel generated code
|
|
|
|
Top_t tile;
|
2015-06-26 08:17:35 +02:00
|
|
|
tile.init(random_seed);
|
2016-06-08 10:39:40 +02:00
|
|
|
#else
|
|
|
|
VTop tile;
|
|
|
|
#if VM_TRACE
|
|
|
|
VerilatedVcdC *tfp = NULL;
|
|
|
|
if (vcd) {
|
|
|
|
tfp = new VerilatedVcdC;
|
|
|
|
Verilated::traceEverOn(true); // Verilator must compute traced signals
|
|
|
|
VL_PRINTF("Enabling waves... (%s)\n", vcd);
|
|
|
|
tile.trace(tfp, 99); // Trace 99 levels of hierarchy
|
|
|
|
tfp->open(vcd); // Open the dump file
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
srand(random_seed);
|
|
|
|
|
2012-10-02 04:30:11 +02:00
|
|
|
|
2015-11-03 05:10:10 +01:00
|
|
|
uint64_t mem_width = MEM_DATA_BITS / 8;
|
2015-10-14 20:33:18 +02:00
|
|
|
|
2013-05-02 13:58:43 +02:00
|
|
|
// Instantiate and initialize main memory
|
2015-11-03 05:10:10 +01:00
|
|
|
for (int i = 0; i < N_MEM_CHANNELS; i++) {
|
|
|
|
mm[i] = dramsim2 ? (mm_t*)(new mm_dramsim2_t) : (mm_t*)(new mm_magic_t);
|
|
|
|
try {
|
2015-11-05 19:15:02 +01:00
|
|
|
mm[i]->init(memsz_mb*1024*1024 / N_MEM_CHANNELS, mem_width, CACHE_BLOCK_BYTES);
|
2015-11-03 05:10:10 +01:00
|
|
|
} catch (const std::bad_alloc& e) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to allocate %ld bytes (%ld MiB) of memory\n"
|
|
|
|
"Set smaller amount of memory using +memsize=<N> (in MiB)\n",
|
|
|
|
memsz_mb*1024*1024, memsz_mb);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2015-02-13 17:56:54 +01:00
|
|
|
}
|
2015-11-03 05:10:10 +01:00
|
|
|
|
2015-11-03 07:46:52 +01:00
|
|
|
if (loadmem) {
|
|
|
|
void *mems[N_MEM_CHANNELS];
|
|
|
|
for (int i = 0; i < N_MEM_CHANNELS; i++)
|
|
|
|
mems[i] = mm[i]->get_data();
|
2015-11-05 19:15:02 +01:00
|
|
|
load_mem(mems, loadmem, CACHE_BLOCK_BYTES, N_MEM_CHANNELS);
|
2015-11-03 07:46:52 +01:00
|
|
|
}
|
2013-05-02 13:58:43 +02:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
dtm = new dtm_t(std::vector<std::string>(argv + 1, argv + argc));
|
2013-01-25 08:56:45 +01:00
|
|
|
|
|
|
|
signal(SIGTERM, handle_sigterm);
|
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
// reset for several cycles to handle pipelined reset
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2016-06-08 10:39:40 +02:00
|
|
|
#ifndef VERILATOR
|
2012-10-02 04:30:11 +02:00
|
|
|
tile.clock_lo(LIT<1>(1));
|
|
|
|
tile.clock_hi(LIT<1>(1));
|
2016-06-08 10:39:40 +02:00
|
|
|
#else
|
|
|
|
tile.reset = 1;
|
|
|
|
tile.clk = 0;
|
|
|
|
tile.eval();
|
|
|
|
tile.clk = 1;
|
|
|
|
tile.eval();
|
2016-06-23 09:17:29 +02:00
|
|
|
tile.reset = 0;
|
2016-06-08 10:39:40 +02:00
|
|
|
#endif
|
2016-06-23 09:17:29 +02:00
|
|
|
}
|
2016-06-08 10:39:40 +02:00
|
|
|
|
|
|
|
bool_t *mem_ar_valid[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_ar_ready[N_MEM_CHANNELS];
|
|
|
|
mem_addr_t *mem_ar_bits_addr[N_MEM_CHANNELS];
|
|
|
|
mem_id_t *mem_ar_bits_id[N_MEM_CHANNELS];
|
|
|
|
mem_size_t *mem_ar_bits_size[N_MEM_CHANNELS];
|
|
|
|
mem_len_t *mem_ar_bits_len[N_MEM_CHANNELS];
|
|
|
|
|
|
|
|
bool_t *mem_aw_valid[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_aw_ready[N_MEM_CHANNELS];
|
|
|
|
mem_addr_t *mem_aw_bits_addr[N_MEM_CHANNELS];
|
|
|
|
mem_id_t *mem_aw_bits_id[N_MEM_CHANNELS];
|
|
|
|
mem_size_t *mem_aw_bits_size[N_MEM_CHANNELS];
|
|
|
|
mem_len_t *mem_aw_bits_len[N_MEM_CHANNELS];
|
|
|
|
|
|
|
|
bool_t *mem_w_valid[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_w_ready[N_MEM_CHANNELS];
|
|
|
|
mem_data_t *mem_w_bits_data[N_MEM_CHANNELS];
|
|
|
|
mem_strb_t *mem_w_bits_strb[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_w_bits_last[N_MEM_CHANNELS];
|
|
|
|
|
|
|
|
bool_t *mem_b_valid[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_b_ready[N_MEM_CHANNELS];
|
|
|
|
mem_resp_t *mem_b_bits_resp[N_MEM_CHANNELS];
|
|
|
|
mem_id_t *mem_b_bits_id[N_MEM_CHANNELS];
|
|
|
|
|
|
|
|
bool_t *mem_r_valid[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_r_ready[N_MEM_CHANNELS];
|
|
|
|
mem_resp_t *mem_r_bits_resp[N_MEM_CHANNELS];
|
|
|
|
mem_id_t *mem_r_bits_id[N_MEM_CHANNELS];
|
|
|
|
mem_data_t *mem_r_bits_data[N_MEM_CHANNELS];
|
|
|
|
bool_t *mem_r_bits_last[N_MEM_CHANNELS];
|
2015-11-03 05:10:10 +01:00
|
|
|
|
|
|
|
#include TBFRAG
|
|
|
|
|
2016-06-23 21:17:26 +02:00
|
|
|
while (!dtm->done() && trace_count < max_cycles && ret == 0)
|
2012-10-02 04:30:11 +02:00
|
|
|
{
|
2015-11-03 05:10:10 +01:00
|
|
|
for (int i = 0; i < N_MEM_CHANNELS; i++) {
|
2016-06-23 09:17:29 +02:00
|
|
|
value(mem_ar_ready[i]) = mm[i]->ar_ready();
|
|
|
|
value(mem_aw_ready[i]) = mm[i]->aw_ready();
|
|
|
|
value(mem_w_ready[i]) = mm[i]->w_ready();
|
2015-10-14 20:33:18 +02:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
value(mem_b_valid[i]) = mm[i]->b_valid();
|
|
|
|
value(mem_b_bits_resp[i]) = mm[i]->b_resp();
|
|
|
|
value(mem_b_bits_id[i]) = mm[i]->b_id();
|
2015-10-14 20:33:18 +02:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
value(mem_r_valid[i]) = mm[i]->r_valid();
|
|
|
|
value(mem_r_bits_resp[i]) = mm[i]->r_resp();
|
|
|
|
value(mem_r_bits_id[i]) = mm[i]->r_id();
|
|
|
|
value(mem_r_bits_last[i]) = mm[i]->r_last();
|
2015-11-03 05:10:10 +01:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
memcpy(values(mem_r_bits_data[i]), mm[i]->r_data(), mem_width);
|
2015-11-03 05:10:10 +01:00
|
|
|
}
|
2012-12-04 16:04:26 +01:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
value(field(io_debug_resp_ready)) = dtm->resp_ready();
|
|
|
|
value(field(io_debug_req_valid)) = dtm->req_valid();
|
|
|
|
value(field(io_debug_req_bits_addr)) = dtm->req_bits().addr;
|
|
|
|
value(field(io_debug_req_bits_op)) = dtm->req_bits().op;
|
|
|
|
value(field(io_debug_req_bits_data)) = dtm->req_bits().data;
|
|
|
|
|
2015-06-26 08:17:35 +02:00
|
|
|
try {
|
2016-06-08 10:39:40 +02:00
|
|
|
#ifndef VERILATOR
|
2015-06-26 08:17:35 +02:00
|
|
|
tile.clock_lo(LIT<1>(0));
|
2016-06-08 10:39:40 +02:00
|
|
|
#else
|
|
|
|
tile.clk = 0;
|
|
|
|
tile.eval();
|
|
|
|
// make sure we dump on cycle 0 to get dump_init
|
|
|
|
#if VM_TRACE
|
2016-06-23 21:17:26 +02:00
|
|
|
if (tfp && (trace_count == 0 || trace_count >= start))
|
|
|
|
tfp->dump(trace_count * 2);
|
2016-06-08 10:39:40 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2015-06-26 08:17:35 +02:00
|
|
|
} catch (std::runtime_error& e) {
|
2016-06-23 21:17:26 +02:00
|
|
|
max_cycles = trace_count; // terminate cleanly after this cycle
|
2015-06-26 08:17:35 +02:00
|
|
|
ret = 1;
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
2012-12-04 16:04:26 +01:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
dtm_t::resp debug_resp_bits;
|
|
|
|
debug_resp_bits.resp = value(field(io_debug_resp_bits_resp));
|
|
|
|
debug_resp_bits.data = value(field(io_debug_resp_bits_data));
|
|
|
|
|
|
|
|
dtm->tick(
|
|
|
|
value(field(io_debug_req_ready)),
|
|
|
|
value(field(io_debug_resp_valid)),
|
|
|
|
debug_resp_bits
|
|
|
|
);
|
|
|
|
|
2015-11-03 05:10:10 +01:00
|
|
|
for (int i = 0; i < N_MEM_CHANNELS; i++) {
|
|
|
|
mm[i]->tick(
|
2016-06-23 09:17:29 +02:00
|
|
|
value(mem_ar_valid[i]),
|
|
|
|
value(mem_ar_bits_addr[i]) - MEM_BASE,
|
|
|
|
value(mem_ar_bits_id[i]),
|
|
|
|
value(mem_ar_bits_size[i]),
|
|
|
|
value(mem_ar_bits_len[i]),
|
|
|
|
|
|
|
|
value(mem_aw_valid[i]),
|
|
|
|
value(mem_aw_bits_addr[i]) - MEM_BASE,
|
|
|
|
value(mem_aw_bits_id[i]),
|
|
|
|
value(mem_aw_bits_size[i]),
|
|
|
|
value(mem_aw_bits_len[i]),
|
|
|
|
|
|
|
|
value(mem_w_valid[i]),
|
|
|
|
value(mem_w_bits_strb[i]),
|
|
|
|
values(mem_w_bits_data[i]),
|
|
|
|
value(mem_w_bits_last[i]),
|
|
|
|
|
|
|
|
value(mem_r_ready[i]),
|
|
|
|
value(mem_b_ready[i])
|
2015-11-03 05:10:10 +01:00
|
|
|
);
|
|
|
|
}
|
2012-10-02 04:30:11 +02:00
|
|
|
|
2016-06-08 10:39:40 +02:00
|
|
|
#ifndef VERILATOR
|
2016-06-23 21:17:26 +02:00
|
|
|
if (verbose && trace_count >= start)
|
2014-03-05 01:38:34 +01:00
|
|
|
tile.print(stderr);
|
2013-06-13 19:53:23 +02:00
|
|
|
|
2015-09-19 03:02:03 +02:00
|
|
|
// make sure we dump on cycle 0 to get dump_init
|
2016-06-23 21:17:26 +02:00
|
|
|
if (vcd && (trace_count == 0 || trace_count >= start))
|
|
|
|
tile.dump(vcdfile, trace_count);
|
2012-10-02 04:30:11 +02:00
|
|
|
|
|
|
|
tile.clock_hi(LIT<1>(0));
|
2016-06-08 10:39:40 +02:00
|
|
|
#else
|
|
|
|
tile.clk = 1;
|
|
|
|
tile.eval();
|
|
|
|
#if VM_TRACE
|
2016-06-23 21:17:26 +02:00
|
|
|
if (tfp && (trace_count == 0 || trace_count >= start))
|
|
|
|
tfp->dump(trace_count * 2 + 1);
|
2016-06-08 10:39:40 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2012-10-02 04:30:11 +02:00
|
|
|
trace_count++;
|
|
|
|
}
|
|
|
|
|
2016-06-08 10:39:40 +02:00
|
|
|
#ifndef VERILATOR
|
|
|
|
if (vcd) fclose(vcdfile);
|
|
|
|
#else
|
|
|
|
#if VM_TRACE
|
|
|
|
if (tfp) tfp->close();
|
|
|
|
delete tfp;
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-10-02 04:30:11 +02:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
if (dtm->exit_code())
|
2012-10-02 04:30:11 +02:00
|
|
|
{
|
2016-06-23 21:17:26 +02:00
|
|
|
fprintf(stderr, "*** FAILED *** (code = %d, seed %d) after %ld cycles\n", dtm->exit_code(), random_seed, trace_count);
|
2016-06-23 09:17:29 +02:00
|
|
|
ret = dtm->exit_code();
|
2012-10-02 04:30:11 +02:00
|
|
|
}
|
2016-06-23 21:17:26 +02:00
|
|
|
else if (trace_count == max_cycles)
|
2013-10-29 21:24:09 +01:00
|
|
|
{
|
2016-06-23 21:17:26 +02:00
|
|
|
fprintf(stderr, "*** FAILED *** (timeout, seed %d) after %ld cycles\n", random_seed, trace_count);
|
2013-10-29 21:24:09 +01:00
|
|
|
ret = 2;
|
|
|
|
}
|
2016-06-18 06:09:08 +02:00
|
|
|
else if (verbose || print_cycles)
|
2015-09-19 03:02:03 +02:00
|
|
|
{
|
2016-06-23 21:17:26 +02:00
|
|
|
fprintf(stderr, "Completed after %ld cycles\n", trace_count);
|
2015-09-19 03:02:03 +02:00
|
|
|
}
|
2013-10-29 21:24:09 +01:00
|
|
|
|
2016-06-23 09:17:29 +02:00
|
|
|
delete dtm;
|
2012-10-02 04:30:11 +02:00
|
|
|
|
2013-10-29 21:24:09 +01:00
|
|
|
return ret;
|
2012-10-02 04:30:11 +02:00
|
|
|
}
|