2024-04-09 09:03:21 +00:00
|
|
|
#include "VFlow___024root.h"
|
2024-07-09 12:42:01 +00:00
|
|
|
#include "components.hpp"
|
2024-03-29 02:35:49 +00:00
|
|
|
#include <VFlow.h>
|
2024-07-09 12:42:01 +00:00
|
|
|
#include <array>
|
2024-04-12 01:35:41 +00:00
|
|
|
#include <config.hpp>
|
2024-04-04 16:16:58 +00:00
|
|
|
#include <cstdint>
|
2024-04-09 09:03:21 +00:00
|
|
|
#include <cstdlib>
|
2024-07-09 12:42:01 +00:00
|
|
|
#include <devices.hpp>
|
2024-04-12 01:35:41 +00:00
|
|
|
#include <disasm.hpp>
|
2024-04-10 12:12:41 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
2024-07-09 12:42:01 +00:00
|
|
|
#include <memory>
|
2024-04-09 09:03:21 +00:00
|
|
|
#include <sdb.hpp>
|
2024-04-10 12:12:41 +00:00
|
|
|
#include <trm_difftest.hpp>
|
|
|
|
#include <trm_interface.hpp>
|
2024-04-09 09:03:21 +00:00
|
|
|
#include <types.h>
|
2024-04-12 01:35:41 +00:00
|
|
|
#include <vl_wrapper.hpp>
|
|
|
|
#include <vpi_user.h>
|
|
|
|
#include <vpi_wrapper.hpp>
|
2024-03-13 06:53:31 +00:00
|
|
|
|
2024-04-03 14:39:33 +00:00
|
|
|
using VlModule = VlModuleInterfaceCommon<VFlow>;
|
|
|
|
using Registers = _RegistersVPI<uint32_t, 32>;
|
2024-03-29 02:35:49 +00:00
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
// SDB::SDB<NPC::npc_interface> sdb_dut;
|
|
|
|
using CPUState = CPUStateBase<uint32_t, 32>;
|
2024-04-12 01:35:41 +00:00
|
|
|
bool g_skip_memcheck = false;
|
2024-04-10 12:12:41 +00:00
|
|
|
CPUState npc_cpu;
|
2024-04-12 01:35:41 +00:00
|
|
|
VlModule *top;
|
|
|
|
Registers *regs;
|
|
|
|
vpiHandle pc = nullptr;
|
|
|
|
|
2024-07-09 12:42:01 +00:00
|
|
|
const size_t PMEM_START = 0x80000000;
|
|
|
|
const size_t PMEM_END = 0x87ffffff;
|
|
|
|
|
2024-04-02 08:15:16 +00:00
|
|
|
extern "C" {
|
2024-07-09 12:42:01 +00:00
|
|
|
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
|
2024-04-03 14:39:33 +00:00
|
|
|
void *pmem_get() {
|
2024-07-09 12:42:01 +00:00
|
|
|
static Devices::DeviceMap devices{
|
|
|
|
new Devices::Serial(0x10000000, 0x1000),
|
|
|
|
new Devices::RTC(0x10001000, 0x1000),
|
|
|
|
};
|
|
|
|
static auto pmem = new MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>(
|
|
|
|
std::make_unique<Memory<128 * 1024>>(
|
|
|
|
config.memory_file, config.memory_file_binary, PMEM_START, PMEM_END),
|
|
|
|
std::make_unique<Devices::DeviceMap>(devices), config.mtrace_ranges);
|
2024-04-03 14:39:33 +00:00
|
|
|
return pmem;
|
|
|
|
}
|
2024-04-02 08:15:16 +00:00
|
|
|
|
2024-04-03 14:39:33 +00:00
|
|
|
int pmem_read(int raddr) {
|
|
|
|
void *pmem = pmem_get();
|
2024-07-09 12:42:01 +00:00
|
|
|
auto mem = static_cast<MMap *>(pmem);
|
2024-04-04 16:16:58 +00:00
|
|
|
// TODO: Do memory difftest at memory read and write to diagnose at a finer
|
|
|
|
// granularity
|
2024-04-12 01:35:41 +00:00
|
|
|
if (config.do_mtrace)
|
|
|
|
mem->trace(raddr, true, regs->get_pc());
|
2024-07-09 12:42:01 +00:00
|
|
|
if (g_skip_memcheck)
|
|
|
|
return mem->read(PMEM_START);
|
2024-04-03 14:39:33 +00:00
|
|
|
return mem->read(raddr);
|
|
|
|
}
|
2024-04-02 06:30:14 +00:00
|
|
|
|
2024-04-03 14:39:33 +00:00
|
|
|
void pmem_write(int waddr, int wdata, char wmask) {
|
|
|
|
void *pmem = pmem_get();
|
2024-07-09 12:42:01 +00:00
|
|
|
auto mem = static_cast<MMap *>(pmem);
|
2024-04-12 01:35:41 +00:00
|
|
|
if (config.do_mtrace)
|
|
|
|
mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata);
|
2024-04-03 14:39:33 +00:00
|
|
|
return mem->write((std::size_t)waddr, wdata, wmask);
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 02:35:49 +00:00
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
namespace NPC {
|
|
|
|
void npc_memcpy(paddr_t addr, void *buf, size_t sz, bool direction) {
|
|
|
|
if (direction == TRM_FROM_MACHINE) {
|
2024-07-09 12:42:01 +00:00
|
|
|
static_cast<MMap *>(pmem_get())->copy_to(addr, (uint8_t *)buf, sz);
|
2024-04-10 12:12:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void npc_regcpy(void *p, bool direction) {
|
2024-04-04 16:16:58 +00:00
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
if (direction == TRM_FROM_MACHINE) {
|
2024-04-04 16:16:58 +00:00
|
|
|
((CPUState *)p)->pc = regs->get_pc();
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
((CPUState *)p)->reg[i] = (*regs)[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
void npc_exec(uint64_t n) {
|
2024-04-04 16:16:58 +00:00
|
|
|
while (n--) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (top->is_posedge()) {
|
|
|
|
// Posedge
|
|
|
|
regs->update();
|
|
|
|
}
|
|
|
|
top->eval();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 09:03:21 +00:00
|
|
|
|
2024-04-12 01:35:41 +00:00
|
|
|
void npc_atexit(void) {
|
|
|
|
delete top;
|
|
|
|
delete regs;
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
void npc_init(int port) {
|
2024-04-12 01:35:41 +00:00
|
|
|
top = new VlModule{config.wavefile};
|
2024-04-04 16:16:58 +00:00
|
|
|
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
|
2024-04-12 01:35:41 +00:00
|
|
|
atexit(npc_atexit);
|
2024-04-04 16:16:58 +00:00
|
|
|
top->reset_eval(10);
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:12:41 +00:00
|
|
|
class DutTrmInterface : public TrmInterface {
|
|
|
|
public:
|
|
|
|
DutTrmInterface(memcpy_t f_memcpy, regcpy_t f_regcpy, exec_t f_exec,
|
|
|
|
init_t f_init, void *cpu_state)
|
|
|
|
: TrmInterface{f_memcpy, f_regcpy, f_exec, f_init, cpu_state} {}
|
|
|
|
word_t at(std::string name) const override {
|
|
|
|
return ((CPUState *)cpu_state)->at(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
word_t at(paddr_t addr) const override {
|
|
|
|
word_t buf;
|
|
|
|
this->memcpy(addr, &buf, sizeof(word_t), TRM_FROM_MACHINE);
|
|
|
|
return buf;
|
|
|
|
}
|
2024-07-09 12:42:01 +00:00
|
|
|
void print(std::ostream &os) const override {
|
|
|
|
this->regcpy(cpu_state, TRM_FROM_MACHINE);
|
|
|
|
os << *(CPUState *)cpu_state << std::endl;
|
|
|
|
}
|
2024-04-10 12:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DutTrmInterface npc_interface =
|
|
|
|
DutTrmInterface{&npc_memcpy, &npc_regcpy, &npc_exec, &npc_init, &npc_cpu};
|
|
|
|
}; // namespace NPC
|
2024-04-09 09:03:21 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
word_t reg_str2val(const char *name, bool *success) {
|
2024-04-10 12:12:41 +00:00
|
|
|
return npc_cpu.reg_str2val(name, success);
|
2024-04-09 09:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 02:35:49 +00:00
|
|
|
int main(int argc, char **argv, char **env) {
|
2024-04-03 14:39:33 +00:00
|
|
|
config.cli_parse(argc, argv);
|
2024-03-29 02:35:49 +00:00
|
|
|
|
2024-07-09 12:42:01 +00:00
|
|
|
if (config.lib_ref.empty()) {
|
|
|
|
if (config.interactive) {
|
|
|
|
SDB::SDB sdb_npc{NPC::npc_interface};
|
|
|
|
sdb_npc.main_loop();
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
NPC::npc_interface.init(0);
|
|
|
|
while (true) {
|
|
|
|
word_t inst = NPC::npc_interface.at(regs->get_pc());
|
|
|
|
if (inst == 1048691) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NPC::npc_interface.exec(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-12 01:35:41 +00:00
|
|
|
|
2024-07-09 12:42:01 +00:00
|
|
|
/* -- Difftest -- */
|
|
|
|
std::filesystem::path ref{config.lib_ref};
|
|
|
|
RefTrmInterface ref_interface{ref};
|
|
|
|
DifftestTrmInterface diff_interface{
|
|
|
|
NPC::npc_interface, ref_interface,
|
|
|
|
static_cast<MMap *>(pmem_get())->get_pmem(), 128 * 1024};
|
|
|
|
if (config.interactive) {
|
|
|
|
SDB::SDB sdb_diff{diff_interface};
|
2024-04-12 01:35:41 +00:00
|
|
|
sdb_diff.main_loop();
|
2024-07-09 12:42:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
diff_interface.init(0);
|
|
|
|
diff_interface.exec(-1);
|
|
|
|
} catch (TrmRuntimeException &e) {
|
|
|
|
switch (e.error_code()) {
|
|
|
|
case TrmRuntimeException::EBREAK:
|
|
|
|
return 0;
|
|
|
|
case TrmRuntimeException::DIFFTEST_FAILED:
|
|
|
|
std::cout << "Difftest Failed" << std::endl;
|
|
|
|
diff_interface.print(std::cout);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
std::cerr << "Unknown error happened" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2024-04-12 01:35:41 +00:00
|
|
|
}
|
2024-04-04 16:16:58 +00:00
|
|
|
|
2024-04-02 06:30:14 +00:00
|
|
|
return 0;
|
2024-03-13 06:53:31 +00:00
|
|
|
}
|