-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCIE.cpp
executable file
·101 lines (84 loc) · 2.58 KB
/
PCIE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include "PCIE.h"
#if defined(__GNUC__)
#include <dlfcn.h> // dlopen/dlclsoe for linxu
#include <stdbool.h>
#else
#include <windows.h>
#define dlopen(x,y) LoadLibrary(x)
#define dlclose FreeLibrary
#define dlsym GetProcAddress
#define dlerror() "LoadLibrary failed"
#define false 0
#define true 1
#endif //
LPPCIE_Open PCIE_Open;
LPPCIE_Close PCIE_Close;
LPPCIE_Read32 PCIE_Read32;
LPPCIE_Write32 PCIE_Write32;
LPPCIE_Read8 PCIE_Read8;
LPPCIE_Write8 PCIE_Write8;
LPPCIE_DmaWrite PCIE_DmaWrite;
LPPCIE_DmaRead PCIE_DmaRead;
LPPCIE_ConfigRead32 PCIE_ConfigRead32;
void QueryModualName(char szName[])
{
#if defined(__GNUC__)
strcpy(szName, "./terasic_pcie_msgdma.so"); // linux libary, not support yet
#else
// windows
//check OS
bool bIsWow64 = false;
typedef bool (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, bool*);
LPFN_ISWOW64PROCESS fnIsWow64Process;
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
if (NULL != fnIsWow64Process) {
fnIsWow64Process(GetCurrentProcess(), &bIsWow64);
}
if (bIsWow64) {
strcpy(szName, "TERASIC_PCIE_mSDGMA32.DLL"); // 32-bits dll, not support yet
} else {
strcpy(szName, "TERASIC_PCIE_mSGDMA.dll"); // 64-bits dll
}
#endif
}
void *PCIE_Load(void)
{
bool bSuccess = true;
void *lib_handle;
char szName[256];
QueryModualName(szName);
lib_handle = dlopen(szName, RTLD_NOW);
if (!lib_handle) {
printf("Load %s error: %s\r\n", szName, dlerror());
bSuccess = false;
}
if (bSuccess) {
PCIE_Open = (LPPCIE_Open) dlsym((HMODULE)lib_handle, "PCIE_Open");
PCIE_Close = (LPPCIE_Close) dlsym((HMODULE)lib_handle, "PCIE_Close");
PCIE_Read32 = (LPPCIE_Read32) dlsym((HMODULE)lib_handle, "PCIE_Read32");
PCIE_Write32 = (LPPCIE_Write32) dlsym((HMODULE)lib_handle, "PCIE_Write32");
PCIE_Read8 = (LPPCIE_Read8) dlsym((HMODULE)lib_handle, "PCIE_Read8");
PCIE_Write8 = (LPPCIE_Write8) dlsym((HMODULE)lib_handle, "PCIE_Write8");
PCIE_DmaWrite = (LPPCIE_DmaWrite) dlsym((HMODULE)lib_handle, "PCIE_DmaWrite");
PCIE_DmaRead = (LPPCIE_DmaRead) dlsym((HMODULE)lib_handle, "PCIE_DmaRead");
PCIE_ConfigRead32 = (LPPCIE_ConfigRead32) dlsym((HMODULE)lib_handle, "PCIE_ConfigRead32");
if (!PCIE_Open || !PCIE_Close ||
!PCIE_Read32 || !PCIE_Write32 ||
!PCIE_Read8 || !PCIE_Write8 ||
!PCIE_DmaWrite || !PCIE_DmaRead || !PCIE_ConfigRead32
)
bSuccess = false;
if (!bSuccess) {
dlclose((HMODULE)lib_handle);
lib_handle = 0;
}
}
return lib_handle;
}
void PCIE_Unload(void *lib_handle)
{
dlclose((HMODULE)lib_handle);
}