1
0

include fesvr as a library; improve harnesses

This commit is contained in:
Andrew Waterman
2013-01-24 23:56:45 -08:00
parent f37b9d9a7d
commit 4077b22929
8 changed files with 132 additions and 71 deletions

View File

@ -1,61 +1,53 @@
#include "htif_phy.h"
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include "htif_emulator.h"
#include "common.h"
#include "emulator.h"
#include "mm.h"
#include "mm_dramsim2.h"
#include "Top.h" // chisel-generated code...
#include "disasm.h"
#include "Top.h" // chisel-generated code...
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static bool exit_now = false;
void handle_sigterm(int signum)
htif_emulator_t* htif;
void handle_sigterm(int sig)
{
exit_now = true;
htif->stop();
}
int main(int argc, char** argv)
{
int fromhost_fd = -1, tohost_fd = -1;
unsigned random_seed = (unsigned)time(NULL) ^ (unsigned)getpid();
uint64_t max_cycles = 0;
uint64_t trace_count = 0;
int start = 0;
bool log = false;
bool quiet = false;
const char* vcd = NULL;
const char* loadmem = NULL;
FILE *vcdfile = NULL, *logfile = stderr;
const char* failure = NULL;
disassembler disasm;
bool dramsim2 = false;
signal(SIGTERM, handle_sigterm);
std::vector<std::string> target_args;
for (int i = 1; i < argc; i++)
{
std::string arg = argv[i];
if (arg == "-l")
log = true;
else if (arg == "-q")
quiet = true;
else if (arg == "+dramsim")
dramsim2 = true;
else if (arg.substr(0, 2) == "-v")
if (arg.substr(0, 2) == "-v")
vcd = argv[i]+2;
else if (arg.substr(0, 2) == "-m")
max_cycles = atoll(argv[i]+2);
else if (arg.substr(0, 2) == "-s")
random_seed = atoi(argv[i]+2);
else if (arg.substr(0, 10) == "+fromhost=")
fromhost_fd = atoi(argv[i]+10);
else if (arg.substr(0, 8) == "+tohost=")
tohost_fd = atoi(argv[i]+8);
else if (arg == "+dramsim")
dramsim2 = true;
else if (arg == "+verbose")
log = true;
else if (arg.substr(0, 12) == "+max-cycles=")
max_cycles = atoll(argv[i]+12);
else if (arg.substr(0, 9) == "+loadmem=")
loadmem = argv[i]+9;
else if (arg.substr(0, 1) != "-" || arg.substr(0, 1) != "+")
target_args = std::vector<std::string>(argv + i, argv + argc);
else
{
fprintf(stderr, "unknown option: %s\n", argv[i]);
@ -63,8 +55,11 @@ int main(int argc, char** argv)
}
}
demand(fcntl(fromhost_fd,F_GETFD) >= 0, "fromhost file not open");
demand(fcntl(tohost_fd,F_GETFD) >= 0, "tohost file not open");
if (target_args.empty())
{
fprintf(stderr, "usage: %s [host options] <target program> [target args]\n", argv[0]);
exit(1);
}
const int disasm_len = 24;
if (vcd)
@ -89,6 +84,13 @@ int main(int argc, char** argv)
srand(random_seed);
tile.init(random_seed != 0);
// Instantiate HTIF
htif = new htif_emulator_t(target_args);
int htif_bits = tile.Top__io_host_in_bits.width();
assert(htif_bits % 8 == 0 && htif_bits <= val_n_bits());
signal(SIGTERM, handle_sigterm);
// reset for a few cycles to support pipelined reset
tile.Top__io_host_in_valid = LIT<1>(0);
tile.Top__io_host_out_ready = LIT<1>(0);
@ -99,9 +101,7 @@ int main(int argc, char** argv)
tile.clock_hi(LIT<1>(1));
}
htif_phy_t htif_phy(tile.Top__io_host_in_bits.width(), fromhost_fd, tohost_fd);
while (!exit_now)
while (!htif->done())
{
tile.Top__io_mem_req_cmd_ready = LIT<1>(mm->req_cmd_ready());
tile.Top__io_mem_req_data_ready = LIT<1>(mm->req_data_ready());
@ -109,10 +109,6 @@ int main(int argc, char** argv)
tile.Top__io_mem_resp_bits_tag = LIT<64>(mm->resp_tag());
memcpy(&tile.Top__io_mem_resp_bits_data, mm->resp_data(), tile.Top__io_mem_resp_bits_data.width()/8);
tile.Top__io_host_in_valid = LIT<1>(htif_phy.in_valid());
tile.Top__io_host_in_bits = LIT<64>(htif_phy.in_bits());
tile.Top__io_host_out_ready = LIT<1>(htif_phy.out_ready());
tile.clock_lo(LIT<1>(0));
mm->tick(
@ -127,9 +123,13 @@ int main(int argc, char** argv)
if (tile.Top__io_host_clk_edge.to_bool())
{
htif_phy.tick(tile.Top__io_host_in_ready.lo_word(),
tile.Top__io_host_out_valid.lo_word(),
tile.Top__io_host_out_bits.lo_word());
bool in_valid = tile.Top__io_host_in_ready.to_bool() &&
htif->recv_nonblocking(&tile.Top__io_host_in_bits.values[0], htif_bits/8);
tile.Top__io_host_in_valid = LIT<1>(in_valid);
tile.Top__io_host_out_ready = LIT<1>(1);
if (tile.Top__io_host_out_valid.to_bool())
htif->send(&tile.Top__io_host_out_bits.values[0], htif_bits/8);
}
@ -152,7 +152,7 @@ int main(int argc, char** argv)
wb_insn.bits = wb_reg_inst;
std::string wb_disasm = disasm.disassemble(wb_insn);
if (log || (quiet && trace_count % 10000 == 0))
if (log)
{
fprintf(logfile, "C: %10lld [%ld] pc=[%011lx] W[r%2ld=%016lx][%ld] R[r%2ld=%016lx] R[r%2ld=%016lx] inst=[%08lx] %-32s\n", \
(long long)trace_count, tile.Top_Tile_core_ctrl__wb_reg_valid.lo_word(), tile.Top_Tile_core_dpath__wb_reg_pc.lo_word(), \
@ -192,8 +192,5 @@ int main(int argc, char** argv)
return -1;
}
close(tohost_fd);
close(fromhost_fd);
return 0;
}

26
csrc/htif_emulator.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef _HTIF_EMULATOR_H
#define _HTIF_EMULATOR_H
#include <fesvr/htif_pthread.h>
class htif_emulator_t : public htif_pthread_t
{
public:
htif_emulator_t(const std::vector<std::string>& args)
: htif_pthread_t(args)
{
}
void set_clock_divisor(int divisor, int hold_cycles)
{
write_cr(-1, 63, divisor | hold_cycles << 16);
}
void start()
{
set_clock_divisor(5, 2);
htif_pthread_t::start();
}
};
#endif

View File

@ -1,10 +1,16 @@
#include "htif_phy.h"
#include "htif_emulator.h"
#include "mm.h"
#include "mm_dramsim2.h"
#include <DirectC.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include <iterator>
htif_phy_t* htif_phy = NULL;
mm_t* mm = NULL;
static htif_emulator_t* htif = NULL;
static unsigned htif_bytes;
static mm_t* mm = NULL;
extern "C" {
@ -56,9 +62,8 @@ void memory_tick(
void htif_init
(
vc_handle fromhost,
vc_handle tohost,
vc_handle width,
vc_handle argv,
vc_handle loadmem,
vc_handle dramsim
)
@ -66,17 +71,34 @@ void htif_init
mm = vc_getScalar(dramsim) ? (mm_t*)(new mm_dramsim2_t) : (mm_t*)(new mm_magic_t);
mm->init(MEM_SIZE);
vec32* fh = vc_4stVectorRef(fromhost);
vec32* th = vc_4stVectorRef(tohost);
vec32* w = vc_4stVectorRef(width);
assert(w->d <= 32 && w->d % 8 == 0); // htif_tick assumes data fits in a vec32
htif_bytes = w->d/8;
char loadmem_str[1024];
vc_VectorToString(loadmem, loadmem_str);
if (*loadmem_str)
load_mem(mm->get_data(), loadmem_str);
assert(w->d <= 32); // htif_tick assumes data fits in a vec32
htif_phy = new htif_phy_t(w->d, fh->d, th->d);
char argv_str[1024];
vc_VectorToString(argv, argv_str);
if (!*argv_str)
{
if (*loadmem_str)
strcpy(argv_str, "none");
else
{
fprintf(stderr, "Usage: ./simv [host options] +argv=\"<target program> [target args]\"\n");
exit(-1);
}
}
std::vector<std::string> args;
std::stringstream ss(argv_str);
std::istream_iterator<std::string> begin(ss), end;
std::copy(begin, end, std::back_inserter<std::vector<std::string>>(args));
htif = new htif_emulator_t(args);
}
void htif_tick
@ -86,19 +108,28 @@ void htif_tick
vc_handle htif_in_bits,
vc_handle htif_out_valid,
vc_handle htif_out_ready,
vc_handle htif_out_bits
vc_handle htif_out_bits,
vc_handle exit
)
{
vec32* ob = vc_4stVectorRef(htif_out_bits);
htif_phy->tick(vc_getScalar(htif_in_ready), vc_getScalar(htif_out_valid), ob->d);
static bool peek_in_valid;
static uint32_t peek_in_bits;
if (vc_getScalar(htif_in_ready))
peek_in_valid = htif->recv_nonblocking(&peek_in_bits, htif_bytes);
vc_putScalar(htif_in_valid, htif_phy->in_valid());
vc_putScalar(htif_out_ready, htif_phy->out_ready());
vc_putScalar(htif_out_ready, 1);
if (vc_getScalar(htif_out_valid))
{
vec32* bits = vc_4stVectorRef(htif_out_bits);
htif->send(&bits->d, htif_bytes);
}
vec32 ib;
ib.c = 0;
ib.d = htif_phy->in_bits();
vc_put4stVector(htif_in_bits, &ib);
vec32 bits = {0, 0};
bits.d = peek_in_bits;
vc_put4stVector(htif_in_bits, &bits);
vc_putScalar(htif_in_valid, peek_in_valid);
vc_putScalar(exit, htif->done() ? (htif->exit_code() << 1 | 1) : 0);
}
}