Skip to content

Commit

Permalink
difftest: move declaration of extern func to header (#490)
Browse files Browse the repository at this point in the history
Previous we declare and implement extern func in cpp file directly,
and call by import DPIC in verilog or another extern declaration
in other cpp files.

However, when supporting gsim, too many extern declaration and calls
in different files make it hard to link and locate, causing undefined
reference error.

This change move declaration of extern func to header, add we will
include header while generating extern-module func.
  • Loading branch information
klin02 authored and Tang-Haojin committed Nov 25, 2024
1 parent 85823eb commit 48e9a5c
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/test/csrc/common/SimJTAG.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// See LICENSE.SiFive for license details.

#include "SimJTAG.h"
#include "common.h"
#include "remote_bitbang.h"
#include <cstdlib>
Expand All @@ -11,12 +12,12 @@ remote_bitbang_t *jtag;
bool enable_simjtag = false;
uint16_t remote_jtag_port = 23334;

extern "C" void jtag_init() {
void jtag_init() {
jtag = new remote_bitbang_t(remote_jtag_port);
}

extern "C" int jtag_tick(unsigned char *jtag_TCK, unsigned char *jtag_TMS, unsigned char *jtag_TDI,
unsigned char *jtag_TRSTn, unsigned char jtag_TDO) {
int jtag_tick(unsigned char *jtag_TCK, unsigned char *jtag_TMS, unsigned char *jtag_TDI, unsigned char *jtag_TRSTn,
unsigned char jtag_TDO) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_jtag_tick]++;
difftest_bytes[perf_jtag_tick] += 5;
Expand Down
25 changes: 25 additions & 0 deletions src/test/csrc/common/SimJTAG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/***************************************************************************************
* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
*
* DiffTest is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#ifndef __SIMJTAG_H
#define __SIMJTAG_H

#include "common.h"

extern "C" void jtag_init();
extern "C" int jtag_tick(unsigned char *jtag_TCK, unsigned char *jtag_TMS, unsigned char *jtag_TDI,
unsigned char *jtag_TRSTn, unsigned char jtag_TDO);
#endif // __SIMJTAG_H
4 changes: 2 additions & 2 deletions src/test/csrc/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ int signal_num = 0;
const char *emu_path = NULL;

// Usage in SV/Verilog: xs_assert(`__LINE__);
extern "C" void xs_assert(long long line) {
void xs_assert(long long line) {
if (assert_count >= 0) {
printf("Assertion failed at line %lld.\n", line);
assert_count++;
}
}

// Usage in SV/Verilog: xs_assert_v2(`__FILE__, `__LINE__);
extern "C" void xs_assert_v2(const char *filename, long long line) {
void xs_assert_v2(const char *filename, long long line) {
if (assert_count >= 0) {
printf("Assertion failed at %s:%lld.\n", filename, line);
assert_count++;
Expand Down
2 changes: 2 additions & 0 deletions src/test/csrc/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@ void common_finish();

uint32_t uptime(void);

extern "C" void xs_assert(long long line);
extern "C" void xs_assert_v2(const char *filename, long long line);
#endif // __COMMON_H
11 changes: 4 additions & 7 deletions src/test/csrc/common/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
***************************************************************************************/

#include "device.h"
#include "flash.h"
#include "sdcard.h"
#include "uart.h"
#include "vga.h"
#ifdef SHOW_SCREEN
#include <SDL2/SDL.h>
#endif

void send_key(uint8_t, bool);
void init_sdl(void);

void init_uart(void);
void finish_uart(void);
extern "C" void init_sd(void);
extern "C" void finish_sd(void);
extern "C" void init_flash(void);

void init_device(void) {
#ifdef SHOW_SCREEN
Expand Down
2 changes: 1 addition & 1 deletion src/test/csrc/common/flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ long get_flash_size() {
return flash_bin_size;
}

extern "C" void flash_read(uint32_t addr, uint64_t *data) {
void flash_read(uint32_t addr, uint64_t *data) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_flash_read]++;
difftest_bytes[perf_flash_read] += 12;
Expand Down
1 change: 1 addition & 0 deletions src/test/csrc/common/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ long get_flash_size();
void init_flash(const char *flash_bin);
void flash_finish();

extern "C" void flash_read(uint32_t addr, uint64_t *data);
#endif // __FLASH_H
4 changes: 2 additions & 2 deletions src/test/csrc/common/golden.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "perf.h"
#endif // CONFIG_DIFFTEST_PERFCNT

extern "C" uint8_t pte_helper(uint64_t satp, uint64_t vpn, uint64_t *pte, uint8_t *level) {
uint8_t pte_helper(uint64_t satp, uint64_t vpn, uint64_t *pte, uint8_t *level) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_pte_helper]++;
difftest_bytes[perf_pte_helper] += 25;
Expand Down Expand Up @@ -68,7 +68,7 @@ enum {
#define GET_LOWER32(data) ((data) & ((1UL << 32) - 1))
#define GET_UPPER32(data) ((data) >> 32)

extern "C" uint64_t amo_helper(uint8_t cmd, uint64_t addr, uint64_t wdata, uint8_t mask) {
uint64_t amo_helper(uint8_t cmd, uint64_t addr, uint64_t wdata, uint8_t mask) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_amo_helper]++;
difftest_bytes[perf_amo_helper] += 18;
Expand Down
1 change: 1 addition & 0 deletions src/test/csrc/common/golden.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

// REF Models
extern "C" uint8_t pte_helper(uint64_t satp, uint64_t vpn, uint64_t *pte, uint8_t *level);
extern "C" uint64_t amo_helper(uint8_t cmd, uint64_t addr, uint64_t wdata, uint8_t mask);

typedef union PageTableEntry {
struct {
Expand Down
4 changes: 2 additions & 2 deletions src/test/csrc/common/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ MmapMemory::~MmapMemory() {
#endif
}

extern "C" uint64_t difftest_ram_read(uint64_t rIdx) {
uint64_t difftest_ram_read(uint64_t rIdx) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_difftest_ram_read]++;
difftest_bytes[perf_difftest_ram_read] += 8;
Expand All @@ -321,7 +321,7 @@ extern "C" uint64_t difftest_ram_read(uint64_t rIdx) {
return rdata;
}

extern "C" void difftest_ram_write(uint64_t wIdx, uint64_t wdata, uint64_t wmask) {
void difftest_ram_write(uint64_t wIdx, uint64_t wdata, uint64_t wmask) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_difftest_ram_write]++;
difftest_bytes[perf_difftest_ram_write] += 24;
Expand Down
2 changes: 2 additions & 0 deletions src/test/csrc/common/ram.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

uint64_t pmem_read(uint64_t raddr);
void pmem_write(uint64_t waddr, uint64_t wdata);
extern "C" uint64_t difftest_ram_read(uint64_t rIdx);
extern "C" void difftest_ram_write(uint64_t wIdx, uint64_t wdata, uint64_t wmask);

class InputReader {
public:
Expand Down
3 changes: 0 additions & 3 deletions src/test/csrc/common/sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

FILE *fp = NULL;

extern "C" {

void check_sdcard() {
if (!fp) {
eprintf(ANSI_COLOR_MAGENTA "[warning] sdcard img not found\n");
Expand Down Expand Up @@ -68,4 +66,3 @@ void finish_sd(void) {
fclose(fp);
#endif
}
}
5 changes: 4 additions & 1 deletion src/test/csrc/common/sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
#include "common.h"

extern FILE *fp;

extern "C" void sd_setaddr(uint32_t addr);
extern "C" void sd_read(uint32_t *data);
void init_sd(void);
void finish_sd(void);
#endif // __SDCARD_H
3 changes: 2 additions & 1 deletion src/test/csrc/common/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include "uart.h"
#include "common.h"
#include "stdlib.h"

Expand Down Expand Up @@ -63,7 +64,7 @@ uint8_t uart_getc() {
return ch;
}

extern "C" void uart_getc_legacy(uint8_t *ch) {
void uart_getc_legacy(uint8_t *ch) {
static uint32_t lasttime = 0;
uint32_t now = uptime();

Expand Down
25 changes: 25 additions & 0 deletions src/test/csrc/common/uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/***************************************************************************************
* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
*
* DiffTest is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#ifndef __UART_H
#define __UART_H

#include "common.h"

extern "C" void uart_getc_legacy(uint8_t *ch);
void init_uart(void);
void finish_uart(void);
#endif // __UART_H
10 changes: 5 additions & 5 deletions src/test/csrc/common/vga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include "vga.h"
#include "common.h"

#ifdef CONFIG_DIFFTEST_PERFCNT
#include "perf.h"
#endif // CONFIG_DIFFTEST_PERFCNT
Expand All @@ -33,7 +33,7 @@ static SDL_Window *window;
static SDL_Renderer *renderer;
static SDL_Texture *texture;

extern "C" void put_pixel(uint32_t pixel) {
void put_pixel(uint32_t pixel) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_put_pixel]++;
difftest_bytes[perf_put_pixel] += 4;
Expand All @@ -44,7 +44,7 @@ extern "C" void put_pixel(uint32_t pixel) {
i = 0;
}

extern "C" void vmem_sync(void) {
void vmem_sync(void) {
#ifdef CONFIG_DIFFTEST_PERFCNT
difftest_calls[perf_vmem_sync]++;
#endif // CONFIG_DIFFTEST_PERFCNT
Expand All @@ -65,6 +65,6 @@ void finish_sdl() {
memset(vmem, 0, sizeof(vmem));
}
#else
extern "C" void put_pixel(uint32_t pixel) {}
extern "C" void vmem_sync(void) {}
void put_pixel(uint32_t pixel) {}
void vmem_sync(void) {}
#endif
26 changes: 26 additions & 0 deletions src/test/csrc/common/vga.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/***************************************************************************************
* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
*
* DiffTest is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#ifndef __VGA_H
#define __VGA_H

#include "common.h"

extern "C" void put_pixel(uint32_t pixel);
extern "C" void vmem_sync(void);
void init_sdl();
void finish_sdl();
#endif // __VGA_H

0 comments on commit 48e9a5c

Please sign in to comment.