ysyx-workbench/npc/include/vpi_wrapper.hpp
xinyangli f5335c21f1
All checks were successful
Build npc tests / npc-build (flow-simlib) (push) Successful in 2m17s
Build abstract machine with nix / build-packages (abstract-machine) (pull_request) Successful in 9s
Build abstract machine with nix / build-packages (nemu) (pull_request) Successful in 9s
Build abstract machine with nix / build-packages (nemu-lib) (pull_request) Successful in 9s
Build abstract machine with nix / build-packages (rv32Cross.abstract-machine) (pull_request) Successful in 8s
Build npc tests / npc-build (flow) (push) Successful in 3m2s
npc,fix: bugs of new arch in cpu-tests
2024-09-06 15:10:35 +08:00

50 lines
1.4 KiB
C++

#ifndef _NPC_VPI_WRAPPER_H_
#define _NPC_VPI_WRAPPER_H_
#include <components.hpp>
#include <spdlog/logger.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <vpi_user.h>
template <typename T, std::size_t nr>
class _RegistersVPI : public _RegistersBase<T, nr> {
std::array<vpiHandle, nr> reg_handles;
vpiHandle pc_handle;
T vpi_get(vpiHandle vh) const {
s_vpi_value v;
v.format = vpiIntVal;
vpi_get_value(vh, &v);
return v.value.integer;
}
T fetch_pc(void) const { return vpi_get(pc_handle); }
T fetch_reg(std::size_t id) const { return vpi_get(reg_handles[id]); }
public:
_RegistersVPI<T, nr>(const std::string regs_prefix,
const std::string pcname) {
init_handlers(regs_prefix, pcname);
}
private:
static vpiHandle get_handle(const std::string name) {
vpiHandle hdl = vpi_handle_by_name((PLI_BYTE8 *)name.c_str(), nullptr);
if (hdl == nullptr) {
SPDLOG_ERROR("VPI Handle {} not found", name);
exit(EXIT_FAILURE);
} else {
SPDLOG_INFO("Found VPI handle {} at {}", name, (void *)hdl);
}
return hdl;
}
void init_handlers(const std::string regs_prefix, const std::string pcname) {
pc_handle = get_handle(pcname);
for (int i = 0; i < nr; i++) {
std::string regname = regs_prefix + std::to_string(i);
reg_handles[i] = get_handle(regname);
}
}
};
#endif