-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathmain.c
More file actions
96 lines (83 loc) · 2.66 KB
/
Copy pathmain.c
File metadata and controls
96 lines (83 loc) · 2.66 KB
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
/*
External Buffer
Interfacing with foreign languages (C) through VHPIDIRECT:
https://ghdl.readthedocs.io/en/latest/using/Foreign.html
An array of type uint8_t is allocated and some values are written to the first 1/3
positions. Then, the VHDL simulation is executed, where the (external) array/buffer
is used. When the simulation is finished, the results are checked. The content of
the buffer is printed both before and after the simulation.
This source file is used for both string/byte_vector and integer_vector.
Accordingly, TYPE must be defined as uint8_t or int32_t during compilation.
Keep in mind that the buffer (D) is of type uint8_t*, independently of TYPE, i.e.
accesses are casted.
NOTE: This file is expected to be used along with tb_ext_string.vhd, tb_ext_byte_vector.vhd
or tb_ext_integer_vector.vhd
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include "vhpidirect_user.h"
const uint32_t length = 5;
/*
Check procedure, to be executed when GHDL exits.
The simulation is expected to copy the first 1/3 elements to positions [1/3, 2/3),
while incrementing each value by one, and then copy elements from [1/3, 2/3) to
[2/3, 3/3), while incrementing each value by two.
*/
static void exit_handler(void) {
unsigned i, j, z, k;
TYPE expected, got;
k = 0;
for (j=0; j<3; j++) {
k += j;
for(i=0; i<length; i++) {
z = (length*j)+i;
expected = (i+1)*11 + k;
got = ((TYPE*)D[1])[z];
if (expected != got) {
printf("check error %d: %d %d\n", z, expected, got);
exit(1);
}
printf("%d: %d\n", z, got);
}
}
free(D[0]);
free(D[1]);
}
// Main entrypoint of the application
int main(int argc, char **argv) {
// Allocate a buffer which is three times the number of values
// that we want to copy/modify
D[0] = (uint8_t *) malloc(5*sizeof(int32_t));
if ( D[0] == NULL ) {
perror("execution of malloc() failed!\n");
return -1;
}
// Initialize 'params' array
int32_t *P = (int32_t*)D[0];
P[0] = INT_MIN+10;
P[1] = INT_MIN;
P[2] = 3; // clk_step
P[3] = 0; // update
P[4] = length; // block_length
// Initialize the first 1/3 of the buffer
D[1] = (uint8_t *) malloc(3*length*sizeof(TYPE));
if ( D[1] == NULL ) {
perror("execution of malloc() failed!\n");
return -1;
}
int i;
for(i=0; i<length; i++) {
((TYPE*)D[1])[i] = (i+1)*11;
}
// Print all the buffer
printf("sizeof: %lu\n", sizeof(TYPE));
for(i=0; i<3*length; i++) {
printf("%d: %d\n", i, ((TYPE*)D[1])[i]);
}
// Register a function to be called when GHDL exits
atexit(exit_handler);
// Start the simulation
return ghdl_main(argc, argv);
}