ysyx-workbench/npc/csrc/Flow/main.cpp

181 lines
4.8 KiB
C++
Raw Normal View History

2024-07-10 12:27:09 +00:00
extern "C" {
#include <gdbstub.h>
}
2024-07-18 11:16:15 +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-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>
#include <memory>
2024-04-09 09:03:21 +00:00
#include <types.h>
2024-07-10 12:27:09 +00:00
#include <vector>
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
using Registers = _RegistersVPI<uint32_t, 32>;
2024-07-10 12:27:09 +00:00
using VlModule = VlModuleInterfaceCommon<VFlow, Registers>;
2024-03-29 02:35:49 +00:00
// SDB::SDB<NPC::npc_interface> sdb_dut;
2024-04-12 01:35:41 +00:00
bool g_skip_memcheck = false;
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-07-10 12:27:09 +00:00
struct DbgState {
2024-07-18 11:16:15 +00:00
std::vector<Breakpoint> *bp;
2024-07-10 12:27:09 +00:00
};
2024-04-02 08:15:16 +00:00
extern "C" {
2024-07-10 12:27:09 +00:00
/* === Memory Access === */
2024-07-09 12:42:01 +00:00
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
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>>(
2024-07-18 11:16:15 +00:00
"/nix/store/"
"nv2c00y24qwz1jihfbaip9n1lskbzyb3-am-kernel-riscv32-none-elf-2024-07-"
"10/share/am-kernels/add.bin",
config.memory_file_binary, PMEM_START, PMEM_END),
2024-07-09 12:42:01 +00:00
std::make_unique<Devices::DeviceMap>(devices), config.mtrace_ranges);
return pmem;
}
2024-04-02 08:15:16 +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);
return mem->read(raddr);
}
2024-04-02 06:30:14 +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);
return mem->write((std::size_t)waddr, wdata, wmask);
}
2024-03-29 02:35:49 +00:00
2024-07-10 12:27:09 +00:00
/* === For gdbstub === */
2024-07-10 12:27:09 +00:00
int npc_read_mem(void *args, size_t addr, size_t len, void *val) {
void *pmem = pmem_get();
auto mmap = static_cast<MMap *>(pmem);
mmap->copy_to(addr, (uint8_t *)val, len);
return 0;
}
2024-04-04 16:16:58 +00:00
2024-07-10 12:27:09 +00:00
int npc_write_mem(void *args, size_t addr, size_t len, void *val) {
void *pmem = pmem_get();
auto mmap = static_cast<MMap *>(pmem);
mmap->copy_from(addr, (uint8_t *)val, len);
return 0;
2024-04-04 16:16:58 +00:00
}
2024-07-10 12:27:09 +00:00
int npc_read_reg(void *args, int regno, size_t *value) {
if (regno == 32)
*value = regs->get_pc();
else
*value = (*regs)[regno];
return 0;
2024-04-04 16:16:58 +00:00
}
2024-04-09 09:03:21 +00:00
2024-07-10 12:27:09 +00:00
int npc_write_reg(void *args, int regno, size_t value) { return 1; }
void npc_cont(void *args, gdb_action_t *res) {
DbgState *dbg = (DbgState *)args;
2024-07-18 11:16:15 +00:00
*res = top->eval(*dbg->bp);
2024-04-12 01:35:41 +00:00
}
2024-07-10 12:27:09 +00:00
void npc_stepi(void *args, gdb_action_t *res) {
DbgState *dbg = (DbgState *)args;
2024-07-18 11:16:15 +00:00
*res = top->eval(*dbg->bp);
2024-04-04 16:16:58 +00:00
}
2024-07-10 12:27:09 +00:00
bool npc_set_bp(void *args, size_t addr, bp_type_t type) {
DbgState *dbg = (DbgState *)args;
2024-07-18 11:16:15 +00:00
for (const auto &bp : *dbg->bp) {
2024-07-10 12:27:09 +00:00
if (bp.addr == addr && bp.type == type) {
return true;
}
}
2024-07-18 11:16:15 +00:00
dbg->bp->push_back({.addr = addr, .type = type});
2024-07-10 12:27:09 +00:00
return true;
}
2024-07-10 12:27:09 +00:00
bool npc_del_bp(void *args, size_t addr, bp_type_t type) {
DbgState *dbg = (DbgState *)args;
2024-07-18 11:16:15 +00:00
for (auto it = dbg->bp->begin(); it != dbg->bp->end(); it++) {
2024-07-10 12:27:09 +00:00
if (it->addr == addr && it->type == type) {
2024-07-18 11:16:15 +00:00
std::swap(*it, *dbg->bp->rbegin());
dbg->bp->pop_back();
2024-07-10 12:27:09 +00:00
return true;
}
2024-07-09 12:42:01 +00:00
}
2024-07-10 12:27:09 +00:00
return false;
2024-04-09 09:03:21 +00:00
}
2024-07-10 12:27:09 +00:00
2024-07-18 11:16:15 +00:00
void npc_on_interrupt(void *args) { ; }
void npc_init(void *args) {
DbgState *dbg = (DbgState *)args;
dbg->bp = new std::vector<Breakpoint>;
top = new VlModule;
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
top->setup(config.wavefile, regs);
top->reset_eval(10);
}
2024-07-10 12:27:09 +00:00
static gdbstub_t gdbstub_priv;
arch_info_t isa_arch_info = {
2024-07-18 11:16:15 +00:00
.target_desc = strdup(TARGET_RV32), .reg_num = 32, .reg_byte = 4};
size_t argsize = sizeof(DbgState);
} // extern "C"
2024-07-10 12:27:09 +00:00
int gdbstub_loop() {
2024-07-18 11:16:15 +00:00
DbgState dbg;
target_ops npc_gdbstub_ops = {.cont = npc_cont,
.stepi = npc_stepi,
.read_reg = npc_read_reg,
.write_reg = npc_write_reg,
.read_mem = npc_read_mem,
.write_mem = npc_write_mem,
.set_bp = npc_set_bp,
.del_bp = npc_del_bp,
.on_interrupt = NULL};
2024-07-10 12:27:09 +00:00
if (!gdbstub_init(&gdbstub_priv, &npc_gdbstub_ops, (arch_info_t)isa_arch_info,
2024-07-18 11:16:15 +00:00
strdup("/tmp/gdbstub-npc.sock"))) {
2024-07-10 12:27:09 +00:00
return EINVAL;
}
2024-07-18 11:16:15 +00:00
npc_init(&dbg);
2024-07-10 12:27:09 +00:00
bool success = gdbstub_run(&gdbstub_priv, &dbg);
2024-07-18 11:16:15 +00:00
// gdbstub_close(&gdbstub_priv);
2024-07-10 12:27:09 +00:00
return !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) {
config.cli_parse(argc, argv);
2024-03-29 02:35:49 +00:00
2024-07-10 12:27:09 +00:00
return gdbstub_loop();
2024-03-13 06:53:31 +00:00
}