npc: wip, cannot build
This commit is contained in:
parent
29a9850210
commit
3acab0a751
19 changed files with 404 additions and 128 deletions
|
@ -1 +1,2 @@
|
|||
add_subdirectory(${TOPMODULE})
|
||||
add_subdirectory(devices)
|
||||
add_subdirectory(${TOPMODULE})
|
||||
|
|
|
@ -1,19 +1,29 @@
|
|||
include(ChiselBuild)
|
||||
add_executable(V${TOPMODULE} config.cpp main.cpp)
|
||||
target_link_libraries(V${TOPMODULE} PRIVATE sdb)
|
||||
target_link_libraries(V${TOPMODULE} PRIVATE sdb devices)
|
||||
target_include_directories(V${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
verilate(V${TOPMODULE} TRACE THREADS
|
||||
TOP_MODULE ${TOPMODULE}
|
||||
PREFIX V${TOPMODULE}
|
||||
SOURCES ${CHISEL_OUTPUT_TOPMODULE} ${CHISEL_OUTPUT_VERILATOR_CONF}
|
||||
INCLUDE_DIRS ${CHISEL_OUTPUT_DIR}
|
||||
verilate(
|
||||
V${TOPMODULE}
|
||||
TRACE
|
||||
THREADS
|
||||
TOP_MODULE
|
||||
${TOPMODULE}
|
||||
PREFIX
|
||||
V${TOPMODULE}
|
||||
SOURCES
|
||||
${CHISEL_OUTPUT_TOPMODULE}
|
||||
${CHISEL_OUTPUT_VERILATOR_CONF}
|
||||
INCLUDE_DIRS
|
||||
${CHISEL_OUTPUT_DIR}
|
||||
VERILATOR_ARGS
|
||||
"--vpi" # Enable VPI
|
||||
"-Wno-UNOPTFLAT"
|
||||
)
|
||||
"--vpi" # Enable VPI
|
||||
"-Wno-UNOPTFLAT")
|
||||
|
||||
add_test(
|
||||
NAME V${TOPMODULE}
|
||||
COMMAND V${TOPMODULE}
|
||||
--no-bin -m ${PROJECT_SOURCE_DIR}/resource/addi.txt
|
||||
--diff-lib ${DIFFTEST_LIB})
|
||||
foreach(DIFFTEST_BINARY_FILE IN LISTS DIFFTEST_BINARY_FILES)
|
||||
get_filename_component(FILENAME ${DIFFTEST_BINARY_FILE} NAME_WE)
|
||||
add_test(NAME V${TOPMODULE}.${FILENAME}
|
||||
COMMAND V${TOPMODULE} -m ${DIFFTEST_BINARY_FILE} --diff-lib
|
||||
${DIFFTEST_LIB})
|
||||
unset(FILENAME)
|
||||
endforeach()
|
||||
|
|
|
@ -8,7 +8,7 @@ void Config::cli_parse(int argc, char **argv) {
|
|||
app.add_flag("!--no-bin", memory_file_binary,
|
||||
"Memory file is in text format");
|
||||
app.add_option("--wav", wavefile, "output .vcd file path");
|
||||
app.add_option("-t", max_sim_time, "Max simulation timestep");
|
||||
app.add_flag("-i", interactive, "Launch sdb for interactive session");
|
||||
app.add_option("--diff-lib", lib_ref,
|
||||
"Dynamic library file of difftest reference")
|
||||
->check(CLI::ExistingFile);
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
#include "VFlow___024root.h"
|
||||
#include "components.hpp"
|
||||
#include <VFlow.h>
|
||||
#include <array>
|
||||
#include <config.hpp>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <devices.hpp>
|
||||
#include <disasm.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <sdb.hpp>
|
||||
#include <trm_difftest.hpp>
|
||||
#include <trm_interface.hpp>
|
||||
|
@ -25,27 +29,38 @@ VlModule *top;
|
|||
Registers *regs;
|
||||
vpiHandle pc = nullptr;
|
||||
|
||||
const size_t PMEM_START = 0x80000000;
|
||||
const size_t PMEM_END = 0x87ffffff;
|
||||
|
||||
extern "C" {
|
||||
using MMap = MemoryMap<Memory<128 * 1024>, Devices::DeviceMap>;
|
||||
void *pmem_get() {
|
||||
static auto pmem =
|
||||
new Memory<int, 128 * 1024>(config.memory_file, config.memory_file_binary,
|
||||
std::move(config.mtrace_ranges));
|
||||
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);
|
||||
return pmem;
|
||||
}
|
||||
|
||||
int pmem_read(int raddr) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<Memory<int, 128 * 1024> *>(pmem);
|
||||
auto mem = static_cast<MMap *>(pmem);
|
||||
// TODO: Do memory difftest at memory read and write to diagnose at a finer
|
||||
// granularity
|
||||
if (config.do_mtrace)
|
||||
mem->trace(raddr, true, regs->get_pc());
|
||||
if (g_skip_memcheck)
|
||||
return mem->read(PMEM_START);
|
||||
return mem->read(raddr);
|
||||
}
|
||||
|
||||
void pmem_write(int waddr, int wdata, char wmask) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<Memory<int, 128 * 1024> *>(pmem);
|
||||
auto mem = static_cast<MMap *>(pmem);
|
||||
if (config.do_mtrace)
|
||||
mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata);
|
||||
return mem->write((std::size_t)waddr, wdata, wmask);
|
||||
|
@ -55,10 +70,7 @@ void pmem_write(int waddr, int wdata, char wmask) {
|
|||
namespace NPC {
|
||||
void npc_memcpy(paddr_t addr, void *buf, size_t sz, bool direction) {
|
||||
if (direction == TRM_FROM_MACHINE) {
|
||||
memcpy(
|
||||
buf,
|
||||
static_cast<Memory<int, 128 * 1024> *>(pmem_get())->guest_to_host(addr),
|
||||
sz);
|
||||
static_cast<MMap *>(pmem_get())->copy_to(addr, (uint8_t *)buf, sz);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -110,7 +122,10 @@ public:
|
|||
this->memcpy(addr, &buf, sizeof(word_t), TRM_FROM_MACHINE);
|
||||
return buf;
|
||||
}
|
||||
void print(std::ostream &os) const override {}
|
||||
void print(std::ostream &os) const override {
|
||||
this->regcpy(cpu_state, TRM_FROM_MACHINE);
|
||||
os << *(CPUState *)cpu_state << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
DutTrmInterface npc_interface =
|
||||
|
@ -126,18 +141,50 @@ word_t reg_str2val(const char *name, bool *success) {
|
|||
int main(int argc, char **argv, char **env) {
|
||||
config.cli_parse(argc, argv);
|
||||
|
||||
if (config.max_sim_time > 1) {
|
||||
NPC::npc_interface.exec(config.max_sim_time / 2);
|
||||
} else {
|
||||
/* -- Difftest -- */
|
||||
std::filesystem::path ref{config.lib_ref};
|
||||
RefTrmInterface ref_interface{ref};
|
||||
DifftestTrmInterface diff_interface{NPC::npc_interface, ref_interface,
|
||||
pmem_get(), 128 * 1024};
|
||||
SDB::SDB sdb_diff{diff_interface};
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int t = 8;
|
||||
/* -- 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};
|
||||
sdb_diff.main_loop();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
3
npc/csrc/devices/CMakeLists.txt
Normal file
3
npc/csrc/devices/CMakeLists.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
add_library(devices serial.cpp rtc.cpp)
|
||||
|
||||
target_include_directories(devices PUBLIC include)
|
85
npc/csrc/devices/include/devices.hpp
Normal file
85
npc/csrc/devices/include/devices.hpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
namespace Devices {
|
||||
class Device {
|
||||
public:
|
||||
uint8_t *p_buf;
|
||||
uint64_t addr;
|
||||
size_t len;
|
||||
Device(uint64_t addr, size_t len, uint8_t buf[])
|
||||
: addr(addr), len(len), p_buf(buf) {}
|
||||
virtual ~Device(){};
|
||||
virtual void io_handler(uint32_t offset, size_t len, bool is_write) = 0;
|
||||
void transfer(uint8_t *src, size_t len, bool is_write) {
|
||||
if (is_write) {
|
||||
memmove(p_buf, src, len);
|
||||
} else {
|
||||
memmove(src, p_buf, len);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
class Serial : public Device {
|
||||
uint8_t buf[1];
|
||||
|
||||
public:
|
||||
Serial(uint64_t addr, size_t len);
|
||||
~Serial() override{};
|
||||
void io_handler(uint32_t offset, size_t len, bool is_write) override;
|
||||
// void transfer(uint8_t *src, size_t len, bool is_write) override;
|
||||
};
|
||||
|
||||
class RTC : public Device {
|
||||
uint8_t buf[8];
|
||||
uint64_t boot_time;
|
||||
uint64_t get_time_internal();
|
||||
uint64_t get_time();
|
||||
|
||||
public:
|
||||
RTC(uint64_t addr, size_t len);
|
||||
~RTC() override{};
|
||||
void io_handler(uint32_t offset, size_t len, bool is_write) override;
|
||||
// void transfer(uint8_t *src, size_t len, bool is_write) override;
|
||||
};
|
||||
|
||||
class DeviceMap {
|
||||
std::map<uint64_t, Device *> addr_to_device;
|
||||
|
||||
public:
|
||||
DeviceMap(std::initializer_list<Device *> devices) {
|
||||
for (auto device : devices) {
|
||||
addr_to_device.insert(std::make_pair(device->addr, device));
|
||||
}
|
||||
}
|
||||
bool handle(uint64_t addr, uint8_t *data, size_t len, bool is_write) {
|
||||
auto it = addr_to_device.upper_bound(addr);
|
||||
if (it == addr_to_device.begin() ||
|
||||
(--it)->second->addr + it->second->len <= addr) {
|
||||
std::cerr << "Use of a unintialized device at memory addr: 0x" << std::hex
|
||||
<< addr << std::dec << std::endl;
|
||||
return false;
|
||||
}
|
||||
auto &device = it->second;
|
||||
uint32_t offset = addr - device->addr;
|
||||
if (is_write) {
|
||||
device->transfer(data, len, is_write);
|
||||
device->io_handler(offset, len, is_write);
|
||||
} else {
|
||||
device->io_handler(offset, len, is_write);
|
||||
device->transfer(data, len, is_write);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
} // namespace Devices
|
49
npc/csrc/devices/rtc.cpp
Normal file
49
npc/csrc/devices/rtc.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <cstdint>
|
||||
#include <devices.hpp>
|
||||
|
||||
namespace Devices {
|
||||
uint64_t RTC::get_time_internal() {
|
||||
#if defined(CONFIG_TARGET_AM)
|
||||
uint64_t us = io_read(AM_TIMER_UPTIME).us;
|
||||
#elif defined(CONFIG_TIMER_GETTIMEOFDAY)
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
uint64_t us = now.tv_sec * 1000000 + now.tv_usec;
|
||||
#else
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
|
||||
uint64_t us = now.tv_sec * 1000000 + now.tv_nsec / 1000;
|
||||
#endif
|
||||
return us;
|
||||
}
|
||||
|
||||
uint64_t RTC::get_time() {
|
||||
if (boot_time == 0)
|
||||
boot_time = get_time_internal();
|
||||
uint64_t now = get_time_internal();
|
||||
return now - boot_time;
|
||||
}
|
||||
|
||||
RTC::RTC(uint64_t addr, size_t len) : Device(addr, len, buf) {
|
||||
*(uint64_t *)buf = 0;
|
||||
};
|
||||
|
||||
void RTC::io_handler(uint32_t offset, size_t len, bool is_write) {
|
||||
assert(offset == 0 || offset == 4);
|
||||
if (!is_write && offset == 4) {
|
||||
uint64_t us = get_time();
|
||||
buf[0] = (uint32_t)us;
|
||||
buf[1] = us >> 32;
|
||||
}
|
||||
}
|
||||
|
||||
// void RTC::transfer(uint8_t *src, size_t len, bool is_write) {
|
||||
// if (is_write) {
|
||||
// for (size_t i = 0; i < len; i++)
|
||||
// buf[i] = src[i];
|
||||
// } else {
|
||||
// for (size_t i = 0; i < len; i++)
|
||||
// src[i] = buf[i];
|
||||
// }
|
||||
// }
|
||||
} // namespace Devices
|
22
npc/csrc/devices/serial.cpp
Normal file
22
npc/csrc/devices/serial.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <devices.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace Devices {
|
||||
Serial::Serial(uint64_t addr, size_t len) : Device(addr, len, buf) {
|
||||
buf[0] = 0;
|
||||
};
|
||||
void Serial::io_handler(uint32_t offset, size_t len, bool is_write) {
|
||||
if (is_write) {
|
||||
std::cout << (char)buf[0];
|
||||
}
|
||||
}
|
||||
// void Serial::transfer(uint8_t *src, size_t len, bool is_write) {
|
||||
// if (is_write) {
|
||||
// for (size_t i = 0; i < len; i++)
|
||||
// buf[i] = src[i];
|
||||
// } else {
|
||||
// for (size_t i = 0; i < len; i++)
|
||||
// src[i] = buf[i];
|
||||
// }
|
||||
// }
|
||||
} // namespace Devices
|
Loading…
Add table
Add a link
Reference in a new issue