Skip to content

Commit

Permalink
fpga: add independent compilation and usage support under fpga
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokamikami committed Aug 29, 2024
1 parent ee1ffa9 commit bdd099c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ include verilator.mk
include vcs.mk
include palladium.mk
include libso.mk
include fpga.mk

clean: vcs-clean pldm-clean
rm -rf $(BUILD_DIR)
Expand Down
19 changes: 19 additions & 0 deletions fpga.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

FPGA = FPGA_HOST
FPGA_TARGET = $(abspath $(BUILD_DIR)/simv)
FPGA_BUILD_DIR = $(abspath $(BUILD_DIR)/simv-compile)
FPGA_RUN_DIR = $(abspath $(BUILD_DIR)/$(notdir $(RUN_BIN)))

FPGA_CSRC_DIR = $(abspath ./src/test/csrc/fpga)
FPGA_CONFIG_DIR = $(abspath ./config)

FPGA_CXXFILES = $(SIM_CXXFILES) $(shell find $(FPGA_CSRC_DIR) -name "*.cpp")
FPGA_CXXFLAGS = $(subst \\\",\", $(SIM_CXXFLAGS)) -I$(FPGA_CSRC_DIR) -DNUM_CORES=$(NUM_CORES)
FPGA_LDFLAGS = $(SIM_LDFLAGS) -lpthread -ldl

fpga-build: fpga-clean fpga-host

fpga-host:
$(CXX) $(FPGA_CXXFLAGS) $(FPGA_CXXFILES) $^ -o $@ $(FPGA_LDFLAGS)
fpga-clean:
rm -f fpga-host
1 change: 0 additions & 1 deletion src/test/csrc/common/mpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#define NUM_BLOCKS (MEMPOOL_SIZE / MEMBLOCK_SIZE)
#define REM_NUM_BLOCKS (NUM_BLOCKS - 1)

extern bool running;
class MemoryPool {
public:
// Constructor to allocate aligned memory blocks
Expand Down
26 changes: 23 additions & 3 deletions src/test/csrc/fpga/fpga_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include "difftest.h"
#include "diffstate.h"
#include "difftest.h"
#include "mpool.h"
#include "refproxy.h"
#include "xdma.h"

#define XDMA_C2H_DEVICE "/dev/xdma0_c2h_0"
Expand All @@ -41,11 +42,13 @@ void simv_init();
void simv_step();
void cpu_endtime_check();
void set_dut_from_xdma();
void set_diff_ref_so(char *s);
void args_parsingniton(int argc, char *argv[]);

FpgaXdma *xdma_device = NULL;
FpgaXdma *xdma_device = NULL;

int main(int argc, char *argv[]) {

args_parsingniton(argc, argv);
simv_init();

while (simv_result == SIMV_RUN) {
Expand All @@ -56,6 +59,15 @@ int main(int argc, char *argv[]) {
simv_step();
cpu_endtime_check();
}
free(xdma_device);
}

void set_diff_ref_so(char *s) {
extern const char *difftest_ref_so;
printf("diff-test ref so:%s\n", s);
char *buf = (char *)malloc(256);
strcpy(buf, s);
difftest_ref_so = buf;
}

void set_dut_from_xdma() {
Expand Down Expand Up @@ -101,3 +113,11 @@ void cpu_endtime_check() {
}
}
}

void args_parsingniton(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--diff") == 0) {
set_diff_ref_so(argv[++i]);
}
}
}
35 changes: 30 additions & 5 deletions src/test/csrc/fpga/xdma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/
#include <fcntl.h>
#include <signal.h>

#include "xdma.h"
#include "mpool.h"
#include <fcntl.h>
#include <signal.h>

FpgaXdma::FpgaXdma(const char *device_name) {
signal(SIGINT, handle_sigint);
fd_c2h = open(device_name, O_RDWR);
if (fd_c2h == -1) {
printf("xdma device not find %s\n", device_name);
exit(1);
}
printf("xdma device %s\n", device_name);
set_dma_fd_block();
}

Expand All @@ -34,6 +38,7 @@ void FpgaXdma::set_dma_fd_block() {
int flags = fcntl(fd_c2h, F_GETFL, 0);
if (flags == -1) {
perror("fcntl get error");
exit(1);
return;
}
// Clear the O NONBLOCK flag and set it to blocking mode
Expand All @@ -44,6 +49,25 @@ void FpgaXdma::set_dma_fd_block() {
}
}

void FpgaXdma::start_transmit_thread() {
if (running == true)
return;
receive_thread = std::thread(&FpgaXdma::read_xdma_thread, this);
process_thread = std::thread(&FpgaXdma::write_difftest_thread, this);
running = true;
}

void FpgaXdma::stop_thansmit_thread() {
if (running == false)
return;
xdma_mempool.unlock_thread();
if (receive_thread.joinable())
receive_thread.join();
if (process_thread.joinable())
process_thread.join();
running = false;
}

void FpgaXdma::read_xdma_thread() {
while (running) {
char *memory = xdma_mempool.get_free_chunk();
Expand All @@ -65,11 +89,12 @@ void FpgaXdma::write_difftest_thread() {
diff_empile_cv.wait(lock, [this] { return !diff_packge_filled; });
memcpy(&difftest_pack[core_id], memory, sizeof(DiffTestState));
}
valid_core ++;
valid_core++;
xdma_mempool.set_free_chunk();

if (core_id == NUM_CORES) {
if (valid_core == NUM_CORES) {
diff_packge_filled = true;
valid_core = 0;
// Notify difftest to run the next check
diff_filled_cv.notify_one();
}
Expand Down
22 changes: 13 additions & 9 deletions src/test/csrc/fpga/xdma.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@
#ifndef __XDMA_H__
#define __XDMA_H__

#include "common.h"
#include "diffstate.h"
#include "mpool.h"
#include <queue>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <thread>
#include <vector>

#include "common.h"
#include "diffstate.h"
#include "mpool.h"

#define WITH_FPGA
#define HEAD_DATA_LEN 7
#define BUFSIZE 1024 * 8 * 8
#define WAIT_RECV_SLEEP 5

typedef struct FpgaPackgeHead {
DiffTestState difftestinfo;
uint8_t corid;
Expand All @@ -48,6 +44,7 @@ class FpgaXdma {

int fd_c2h;
int fd_interrupt;
bool running = false;

unsigned int recv_size = sizeof(FpgaPackgeHead);
unsigned long old_exec_instr = 0;
Expand All @@ -57,15 +54,22 @@ class FpgaXdma {
std::mutex diff_mtx;
bool diff_packge_filled = false;
FpgaXdma(const char *device_name);
~FpgaXdma() {};
~FpgaXdma() {
stop_thansmit_thread();
};

void set_dma_fd_block();

// thread api
void start_transmit_thread();
void stop_thansmit_thread();
void read_xdma_thread();
void write_difftest_thread();

private:
std::thread receive_thread;
std::thread process_thread;

static void handle_sigint(int sig);
};

Expand Down

0 comments on commit bdd099c

Please sign in to comment.