Skip to content

Commit e31e7e0

Browse files
committed
feat(examples/vhdl/array_axis_vcs): use helper functions instead of declaring arrays and access types
1 parent 65983fd commit e31e7e0

3 files changed

Lines changed: 55 additions & 29 deletions

File tree

examples/vhdl/array_axis_vcs/src/test/main.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
extern int ghdl_main (int argc, char **argv);
66

7-
int32_t *V[2];
7+
uint8_t *V[2];
88
uint32_t length = 100;
99

1010
// get_param is used by GHDL to retrieve parameter values (integers).
@@ -25,10 +25,12 @@ uint32_t get_param(uint32_t w) {
2525
return o;
2626
}
2727

28-
// get_addr is used by GHDL to retrieve pointers to the base addresses of the buffers.
29-
uintptr_t get_addr(uint32_t w) {
30-
//printf("get_addr(%d): %p\n", w, (void *)V[w]o);
31-
return (uintptr_t)V[w];
28+
void write_byte ( uint8_t id, uint32_t i, uint8_t v ) {
29+
V[id][i] = v;
30+
}
31+
32+
uint8_t read_byte ( uint8_t id, uint32_t i ) {
33+
return V[id][i];
3234
}
3335

3436
// check evaluates if the result produced by the UUT is equivalent to some other softwre procedure.
@@ -54,7 +56,7 @@ int main(int argc, char **argv) {
5456
// Allocate the memory buffers that are to be shared between the software and the simulation.
5557
int i;
5658
for (i=0 ; i<2 ; i++) {
57-
V[i] = (int32_t *) malloc(length*sizeof(uint32_t));
59+
V[i] = (uint8_t *) malloc(length*sizeof(uint32_t));
5860
if ( V[i] == NULL ) {
5961
perror("execution of malloc() failed!\n");
6062
return -1;
@@ -63,15 +65,16 @@ int main(int argc, char **argv) {
6365

6466
// Initialize one of the buffers with random data.
6567
for (i=0 ; i<length ; i++) {
66-
V[0][i]=i*100+rand()/(RAND_MAX/100);
68+
((int32_t*)V[0])[i] = i*100+rand()/(RAND_MAX/100);
69+
printf("V[%d]: %d\n", i, ((int32_t*)V[0])[i]);
6770
}
6871

6972
// Execute the simulation to let the UUT copy the data from one buffer to another.
7073
ghdl_main(gargc, gargv);
7174

7275
// Check that the UUT did what it was expected to do.
7376
printf("> Call 'check'\n");
74-
if ( check(V[0], V[1], length) != 0 ) {
77+
if ( check((int32_t*)V[0], (int32_t*)V[1], length) != 0 ) {
7578
printf("check failed!\n");
7679
return -1;
7780
}

examples/vhdl/array_axis_vcs/src/test/pkg_c.vhd

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,78 @@
44
--
55
-- Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com
66

7+
library ieee;
8+
context ieee.ieee_std_context;
9+
710
package pkg_c is
811

912
function get_param(f: integer) return integer;
10-
attribute foreign of get_param :
11-
function is "VHPIDIRECT get_param";
13+
attribute foreign of get_param : function is "VHPIDIRECT get_param";
1214

13-
--integer'high is 2147483647, but the limit here seems to be 128 times less. Otherwise, it fails.
14-
constant max_length: integer:= 16777215;
15-
type buffer_t is array(integer range 0 to max_length) of integer;
16-
type buffer_p is access buffer_t;
15+
procedure write_byte ( id, i, v: integer ) ;
16+
attribute foreign of write_byte : procedure is "VHPIDIRECT write_byte";
1717

18-
impure function get_addr(f: integer) return buffer_p;
19-
attribute foreign of get_addr :
20-
function is "VHPIDIRECT get_addr";
18+
impure function read_byte ( id, i: integer ) return integer;
19+
attribute foreign of read_byte : function is "VHPIDIRECT read_byte";
2120

2221
type memory_t is record
2322
-- Private
2423
p_id: integer;
2524
end record;
2625

2726
impure function new_memory(id: integer := -1) return memory_t;
28-
procedure write_word ( memory: memory_t; i, v: integer );
29-
impure function read_word ( memory: memory_t; i: integer ) return integer;
27+
procedure write_word ( memory: memory_t; i: natural; w: std_logic_vector );
28+
impure function read_word ( memory: memory_t; i, bytes_per_word: integer ) return std_logic_vector;
29+
30+
procedure write_byte ( memory: memory_t; i: integer; v: std_logic_vector(7 downto 0) );
31+
impure function read_byte ( memory: memory_t; i: integer ) return std_logic_vector;
3032

3133
end pkg_c;
3234

3335
package body pkg_c is
36+
37+
-- VHPI
38+
3439
function get_param(f: integer) return integer is begin
3540
assert false report "VHPI" severity failure;
3641
end function;
3742

38-
impure function get_addr(f: integer) return buffer_p is begin
43+
procedure write_byte ( id, i, v: integer ) is begin
44+
assert false report "VHPI" severity failure;
45+
end procedure;
46+
47+
impure function read_byte ( id, i: integer ) return integer is begin
3948
assert false report "VHPI" severity failure;
4049
end function;
4150

51+
-- VHDL
52+
53+
procedure write_byte ( memory: memory_t; i: integer; v: std_logic_vector(7 downto 0) ) is
54+
begin
55+
write_byte(memory.p_id, i, to_integer(unsigned(v)));
56+
end procedure;
57+
58+
impure function read_byte ( memory: memory_t; i: integer ) return std_logic_vector is begin
59+
return std_logic_vector(to_unsigned(read_byte(memory.p_id, i), 8));
60+
end function;
61+
4262
impure function new_memory(id: integer := -1) return memory_t is begin
4363
return (p_id => id);
4464
end;
4565

46-
procedure write_word ( memory: memory_t; i, v: integer ) is
47-
variable buf: buffer_p := get_addr(memory.p_id);
48-
begin
49-
buf(i) := v;
66+
procedure write_word ( memory: memory_t; i: natural; w: std_logic_vector ) is begin
67+
for idx in 0 to w'length/8-1 loop
68+
write_byte(memory, i + idx, w(8*idx+7 downto 8*idx));
69+
end loop;
5070
end procedure;
5171

52-
impure function read_word ( memory: memory_t; i: integer ) return integer is
53-
variable buf: buffer_p := get_addr(memory.p_id);
72+
impure function read_word ( memory: memory_t; i, bytes_per_word: integer ) return std_logic_vector is
73+
variable tmp: std_logic_vector(31 downto 0);
5474
begin
55-
return buf(i);
75+
for idx in 0 to bytes_per_word-1 loop
76+
tmp(8*idx+7 downto 8*idx) := read_byte(memory, i + idx);
77+
end loop;
78+
return tmp;
5679
end function;
5780

5881
end pkg_c;

examples/vhdl/array_axis_vcs/src/test/tb_c_axis_loop.vhd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ begin
8686

8787
for y in 0 to stream_length-1 loop
8888
wait until rising_edge(clk);
89-
push_axi_stream(net, m_axis, std_logic_vector(to_signed(pkg_c.read_word(ibuffer, y), data_width)) , tlast => '0');
89+
push_axi_stream(net, m_axis, pkg_c.read_word(ibuffer, 4*y, 4) , tlast => '0');
9090
end loop;
9191

9292
info("m_I sent!");
@@ -106,7 +106,7 @@ begin
106106

107107
for y in 0 to stream_length-1 loop
108108
pop_axi_stream(net, s_axis, tdata => o, tlast => last);
109-
pkg_c.write_word(obuffer, y, to_integer(signed(o)));
109+
pkg_c.write_word(obuffer, 4*y, o);
110110
end loop;
111111

112112
info("m_O read!");

0 commit comments

Comments
 (0)