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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
| #include <stdio.h>
#include "xil_io.h"
#include "xparameters.h"
#include "xaxidma.h"
#include "xmatrixmul.h"
#undef str
#include "ap_fixed.h"
#define DIM 3
#define SIZE ((DIM)*(DIM))
#include "xil_printf.h"
typedef ap_fixed<22,10> mat_a_t;
typedef ap_fixed<22,10> mat_b_t;
typedef ap_fixed<22,10> result_t;
XAxiDma AxiDma;
XMatrixmul XMatrixmul_dev;
XMatrixmul_Config XMatrixmul_config = { 0, XPAR_MATRIXMUL_0_S_AXI_CONTROL_BUS_BASEADDR};
int init_dma(){
XAxiDma_Config *CfgPtr;
int status;
CfgPtr = XAxiDma_LookupConfig(XPAR_AXI_DMA_0_DEVICE_ID);
if(!CfgPtr){
print("Error looking for AXI DMA config\n\r");
return XST_FAILURE;
}
status = XAxiDma_CfgInitialize(&AxiDma,CfgPtr);
if(status != XST_SUCCESS){
print("Error initializing DMA\n\r");
return XST_FAILURE;
}
//check for scatter gather mode
if(XAxiDma_HasSg(&AxiDma)){
print("Error DMA configured in SG mode\n\r");
return XST_FAILURE;
}
/* Disable interrupts, we use polling mode */
XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK,XAXIDMA_DEVICE_TO_DMA);
XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK,XAXIDMA_DMA_TO_DEVICE);
return XST_SUCCESS;
}
//---------------------------------------------------------------------------
void print_accel_status(void)
{
int isDone, isIdle, isReady;
isDone = XMatrixmul_IsDone(&XMatrixmul_dev);
isIdle = XMatrixmul_IsIdle(&XMatrixmul_dev);
isReady = XMatrixmul_IsReady(&XMatrixmul_dev);
xil_printf("MatMultAccel Status: isDone %d, isIdle %d, isReady%d\r\n", isDone, isIdle, isReady);
}
//---------------------------------------------------------------------------
int main()
{
int i, j;
int status, err=0;
mat_a_t res_hw[DIM][DIM], res_sw[DIM][DIM];
mat_a_t dma_size = SIZE * sizeof(mat_a_t);
xil_printf("\r********************************\n\r");
xil_printf("\r MATRIX MULT in Vivado HLS + DMA\n\n\r");
// Init DMA
status = init_dma();
if(status != XST_SUCCESS){
print("\rError: DMA init failed\n");
return XST_FAILURE;
}
print("\rDMA Init done\n\r");
// input data; Matrix Initiation
mat_a_t A[3][3] = { {1.1, 2.2, 3.3},{4.4, 5.5, 6.6},{7.7, 8.8, 9.9} };
mat_b_t B[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9} };
// Run the HW Accelerator;
status = XMatrixmul_CfgInitialize(&XMatrixmul_dev, &XMatrixmul_config);
if(status != XST_SUCCESS){
xil_printf("Error: example setup failed\r\n");
return XST_FAILURE;
}
// the interruption are not connected in fact.
XMatrixmul_InterruptGlobalDisable(&XMatrixmul_dev);
XMatrixmul_InterruptDisable(&XMatrixmul_dev,1);
XMatrixmul_Start(&XMatrixmul_dev);
//flush the cache
Xil_DCacheFlushRange((u32)A,dma_size);
Xil_DCacheFlushRange((u32)B,dma_size);
Xil_DCacheFlushRange((u32)res_hw,dma_size);
print("\rCache cleared\n\r");
//transfer A to the Vivado HLS block
status = XAxiDma_SimpleTransfer(&AxiDma, (u32) A, dma_size, XAXIDMA_DMA_TO_DEVICE);
if (status != XST_SUCCESS) {
xil_printf("Error: DMA transfer matrix A to Vivado HLS block failed\n");
return XST_FAILURE;
}
while (XAxiDma_Busy(&AxiDma, XAXIDMA_DMA_TO_DEVICE)) ;
//transfer B to the Vivado HLS block
status = XAxiDma_SimpleTransfer(&AxiDma, (u32) B, dma_size, XAXIDMA_DMA_TO_DEVICE);
if (status != XST_SUCCESS) {
xil_printf("Error: DMA transfer matrix B to Vivado HLS block failed\n");
return XST_FAILURE;
}
while (XAxiDma_Busy(&AxiDma, XAXIDMA_DMA_TO_DEVICE)) ;
//get results from the Vivado HLS block
status = XAxiDma_SimpleTransfer(&AxiDma, (mat_a_t) res_hw, dma_size, XAXIDMA_DEVICE_TO_DMA);
if (status != XST_SUCCESS) {
xil_printf("Error: DMA transfer from Vivado HLS block failed\n");
return XST_FAILURE;
}
while (XAxiDma_Busy(&AxiDma, XAXIDMA_DEVICE_TO_DMA)) ;
Xil_DCacheFlushRange((u32)res_hw,dma_size);
for (i = 0; i < DIM; i++){
for (j = 0; j < DIM; j++){
printf("%f\n",(float)res_hw[i][j]);
}
}
printf("\r Done\n\r");
return 0;
} |