Skip to content

Commit

Permalink
fpga: Load memory for the ref module
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokamikami committed Nov 1, 2024
1 parent 69eaa7d commit 8f1b15f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/test/csrc/common/ram.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class SimMemory {
uint64_t get_size() {
return memory_size;
}
uint64_t get_load_img_size() {
return get_img_size();
}
bool in_range_u8(uint64_t address) {
return address < memory_size;
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/csrc/fpga/fpga_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "difftest-dpic.h"
#include "difftest.h"
#include "mpool.h"
#include "ram.h"
#include "refproxy.h"
#include "xdma.h"

Expand Down Expand Up @@ -60,6 +61,8 @@ int main(int argc, char *argv[]) {
}
}
free(xdma_device);
printf("difftest releases the fpga device and exits\n");
exit(0);
}

void set_diff_ref_so(char *s) {
Expand All @@ -72,6 +75,7 @@ void set_diff_ref_so(char *s) {

void simv_init() {
xdma_device = new FpgaXdma(work_load);
init_ram(work_load, DEFAULT_EMU_RAM_SIZE);
difftest_init();
}

Expand Down
17 changes: 11 additions & 6 deletions src/test/csrc/fpga/xdma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
***************************************************************************************/
#include "xdma.h"
#include "mpool.h"
#include "ram.h"
#include <fcntl.h>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -57,18 +58,17 @@ void FpgaXdma::handle_sigint(int sig) {
}

// write xdma_bypass memory or xdma_user
int FpgaXdma::device_write(bool is_bypass, const char *workload, uint64_t addr, uint64_t value) {
void FpgaXdma::device_write(bool is_bypass, const char *workload, uint64_t addr, uint64_t value) {
uint64_t pg_size = sysconf(_SC_PAGE_SIZE);
uint64_t size = !is_bypass ? 0x1000 : 0x10000;
uint64_t aligned_size = (size + 0xffful) & ~0xffful;
uint64_t base = addr & ~0xffful;
uint32_t offset = addr & 0xfffu;
std::ifstream workload_fd;
int fd = -1;

if (base % pg_size != 0) {
printf("base must be a multiple of system page size\n");
return -1;
exit(-1);
}

if (is_bypass)
Expand All @@ -77,18 +77,23 @@ int FpgaXdma::device_write(bool is_bypass, const char *workload, uint64_t addr,
fd = open(XDMA_USER, O_RDWR | O_SYNC);
if (fd < 0) {
printf("failed to open %s\n", is_bypass ? XDMA_BYPASS : XDMA_USER);
return -1;
exit(-1);
}

void *m_ptr = mmap(nullptr, aligned_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
if (m_ptr == MAP_FAILED) {
close(fd);
printf("failed to mmap\n");
return -1;
exit(-1);
}

if (is_bypass) {
workload_fd.read(((char *)m_ptr) + offset, size);
if (simMemory->get_load_img_size() > aligned_size) {
printf("The loaded workload size exceeds the xdma bypass size");
exit(-1);
}
memcpy(static_cast<char *>(m_ptr) + offset, static_cast<const void *>(simMemory->as_ptr()),
simMemory->get_load_img_size());
} else {
((volatile uint32_t *)m_ptr)[offset >> 2] = value;
}
Expand Down
9 changes: 5 additions & 4 deletions src/test/csrc/fpga/xdma.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,23 @@ class FpgaXdma {
stop_thansmit_thread();
};

int core_reset() {
void core_reset() {
device_write(false, nullptr, 0x100000, 0x1);
device_write(false, nullptr, 0x10000, 0x8);
}

int core_restart() {
void core_restart() {
device_write(false, nullptr, 0x100000, 0);
}

int ddr_load_workload(const char *workload) {
void ddr_load_workload(const char *workload) {
core_reset();
device_write(true, workload, 0, 0);
core_restart();
}

int device_write(bool is_bypass, const char *workload, uint64_t addr, uint64_t value);
void device_write(bool is_bypass, const char *workload, uint64_t addr, uint64_t value);

// thread api
void start_transmit_thread();
void stop_thansmit_thread();
Expand Down

0 comments on commit 8f1b15f

Please sign in to comment.