Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Header: std_u/logic, N Dim unconstrained arrays. #3

Closed
wants to merge 0 commits into from

Conversation

radonnachie
Copy link

@radonnachie radonnachie commented Apr 13, 2020

ghdl/issues/3

This pull request contains only the updates to ghdl.h (and supporting main.c, tb.vhd and run.sh) from umarcor/ghdl/pull/1.

Updates

  • std_ulogic and std_logic have test cases, See HDL_LOGIC_STATE and HDL_LOGIC_CHAR in ghdl.h.
  • int[] now is correctly passed to a fat-pointer, to be received by an unconstrained 1D array in vhdl
    • When declared with [] (for any N dimensions), memcpy() is used to fill a new pointer for the fat-pointer going to ghdl. See the updated ghdlFromArray(). This process effectively decouples the fat-pointer's data from the original [], so any updates to the C-side data will not be reflected in vhdl.
    • When Ndim arrays are declared with *, the pointer itself is passed to the fat-pointer going to ghdl. See the new ghdlFromPointer(). This means updates to the data are reflected on both ends.
  • rename range_t to bounds_t
  • bounds/2D/3D_t collapsed to bounds_t
  • ghdl_NaturalDimArr_t->bounds is now bounds_t* (as if it were range_t*)
    • This enables N dimensional arrays to be easily passed to vhdl's N dimensional unconstrained
      arrays
  • Fat-pointers often malloc the data to the internal data pointer, to pass it to vhdl. main.c now keeps track of the malloc'd pointers to free them when freePointers() is called from tb.vhd.

Pasted below is the output of the updated test (./run.sh)


Each index of an array is given the value of 11*(indexId+1), where indexId is the sequential enumeration of the possible indices: eg. 2D array (2, 3): (0,0)=0, (0,1)=1, (0,2)=2, (1,0)=3, (2,0)=4...etc.
For the 2D and 3D verification printouts in C, the (indexId) is printed after the [index list].

1D Array values [6]:
[0]: 11
[1]: 22
[2]: 33
[3]: 44
[4]: 55
[5]: 66

2D Array values [2,3]:
mat[0][0] = 11  mat[0][1] = 22  mat[0][2] = 33
mat[1][0] = 44  mat[1][1] = 55  mat[1][2] = 66

3D Array values [2,4,3]:
d3[0][0][0] = 11        d3[0][0][1] = 22        d3[0][0][2] = 33
d3[0][1][0] = 44        d3[0][1][1] = 55        d3[0][1][2] = 66
d3[0][2][0] = 77        d3[0][2][1] = 88        d3[0][2][2] = 99
d3[0][3][0] = 110       d3[0][3][1] = 121       d3[0][3][2] = 132

d3[1][0][0] = 143       d3[1][0][1] = 154       d3[1][0][2] = 165
d3[1][1][0] = 176       d3[1][1][1] = 187       d3[1][1][2] = 198
d3[1][2][0] = 209       d3[1][2][1] = 220       d3[1][2][2] = 231
d3[1][3][0] = 242       d3[1][3][1] = 253       d3[1][3][2] = 264

###NOTE###
The 3D array's values have been set to -1 after ghdlFromArray().
This change is decoupled from the VHDL unconstrained array
because ghdlFromArray() makes a deep copy.
So the assertions of 3D values in tb.vhd will still succeed.
d3[2][4][3] = 31

v_logic  : H
v_ulogic : Z
v_char : k
v_int  : -6
v_nat  : 9
v_pos  : 3
v_real : 3.340000
v_bool : 1
v_bit  : 1
v_time : 20000000
v_rec  : 0x1ffefffd78 y 5
v_enum : 2 2
v_str  : 0x1c3728 'hellostr' [8]
v_vec_int  : 0x1c3750 [5]
v_vec_real : 0x1c3790 [6]
v_vec_bool : 0x1ffefffd74 [4]
v_vec_bit  : 0x1c37f8 [4]
v_vec_phy  : 0x1ffefffd30 [3]
v_vec_rec  : 0x1ffefffcf8 [2]
v_vec_enum : 0x1ffefffcbd [3]
v_2vec_real : 0x1ffefffcbd [2, 3]

Verify GHDL Matrix in C
array: 0x1ffefffbf0
bounds: 0x1c3898
bounds[1].left: 0
bounds[1].right: 1
bounds[1].dir: 0
bounds[1].len: 2
bounds[2].left: 0
bounds[2].right: 2
bounds[2].dir: 0
bounds[2].len: 3
C assert: 11 == (val: 11) @ [0,0](0)
C assert: 22 == (val: 22) @ [0,1](1)
C assert: 33 == (val: 33) @ [1,0](2)
C assert: 44 == (val: 44) @ [1,1](3)
C assert: 55 == (val: 55) @ [2,0](4)
C assert: 66 == (val: 66) @ [2,1](5)
v_mat_int  : 0x1ffefffbf0 [3,2]


Verify the 3D GHDL array in C
array: 0x1ffefffb98
bounds: 0x1c38c8
bounds[1].left: 0
bounds[1].right: 1
bounds[1].dir: 0
bounds[1].len: 2
bounds[2].left: 0
bounds[2].right: 1
bounds[2].dir: 0
bounds[2].len: 2
bounds[3].left: 0
bounds[3].right: 2
bounds[3].dir: 0
bounds[3].len: 3
C assert: 11 == (val: 11) @ [0,0,0](0)
C assert: 22 == (val: 22) @ [0,0,1](1)
C assert: 33 == (val: 33) @ [0,1,0](2)
C assert: 44 == (val: 44) @ [0,1,1](3)
C assert: 55 == (val: 55) @ [1,0,0](4)
C assert: 66 == (val: 66) @ [1,0,1](5)
C assert: 77 == (val: 77) @ [1,1,0](6)
C assert: 88 == (val: 88) @ [1,1,1](7)
C assert: 99 == (val: 99) @ [2,0,0](8)
C assert: 110 == (val: 110) @ [2,0,1](9)
C assert: 121 == (val: 121) @ [2,1,0](10)
C assert: 132 == (val: 132) @ [2,1,1](11)
v_3d_int  : 0x1ffefffb98 [3,2,2]

end testCinterface

tb.vhd:132:5:@0ms:(report note): g_str'length: 11
tb.vhd:134:7:@0ms:(report note): g_str: HELLO WORLD
tb.vhd:136:5:@0ms:(report note): string: HELLO WORLD
tb.vhd:138:5:@0ms:(report note): g_int_vec'length: 6
tb.vhd:140:7:@0ms:(report note): 0: 11
tb.vhd:140:7:@0ms:(report note): 1: 22
tb.vhd:140:7:@0ms:(report note): 2: 33
tb.vhd:140:7:@0ms:(report note): 3: 44
tb.vhd:140:7:@0ms:(report note): 4: 55
tb.vhd:140:7:@0ms:(report note): 5: 66
tb.vhd:144:5:@0ms:(report note): g_line: HELLO WORLD
tb.vhd:145:5:@0ms:(report note): getLine: HELLO WORLD
tb.vhd:172:5:@0ms:(report note): g_int_mat'length: 2
tb.vhd:176:9:@0ms:(report note): Asserting Mat [0,0]: 11
tb.vhd:176:9:@0ms:(report note): Asserting Mat [0,1]: 22
tb.vhd:176:9:@0ms:(report note): Asserting Mat [0,2]: 33
tb.vhd:176:9:@0ms:(report note): Asserting Mat [1,0]: 44
tb.vhd:176:9:@0ms:(report note): Asserting Mat [1,1]: 55
tb.vhd:176:9:@0ms:(report note): Asserting Mat [1,2]: 66
tb.vhd:182:5:@0ms:(report note): g_int_3d'length: 2
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,0,0]: 11
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,0,1]: 22
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,0,2]: 33
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,1,0]: 44
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,1,1]: 55
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,1,2]: 66
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,2,0]: 77
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,2,1]: 88
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,2,2]: 99
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,3,0]: 110
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,3,1]: 121
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,3,2]: 132
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,0,0]: 143
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,0,1]: 154
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,0,2]: 165
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,1,0]: 176
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,1,1]: 187
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,1,2]: 198
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,2,0]: 209
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,2,1]: 220
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,2,2]: 231
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,3,0]: 242
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,3,1]: 253
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,3,2]: 264
tb.vhd:194:5:@0ms:(report note): No errors/failures. Concluding testbench.
==981== LEAK SUMMARY:
==981==    definitely lost: 0 bytes in 0 blocks
==981==    indirectly lost: 0 bytes in 0 blocks
==981==      possibly lost: 0 bytes in 0 blocks
==981==    still reachable: 21,995 bytes in 40 blocks

@radonnachie
Copy link
Author

radonnachie commented Apr 13, 2020

When declared with [] (for any N dimensions), memcpy() is used to fill a new pointer for the fat-pointer going to ghdl. See the updated ghdlFromArray(). This process effectively decouples the fat-pointer's data from the original [], so any updates to the C-side data will not be reflected in vhdl.

I would like to recouple them, because [x][y][...etc is much easier. I have included the helper function getFlatIndex(). but still... I'm feeling like even if it is possible, the result will be that the original [] variable will become a *, so one will have to use flat indices anyway.

double (*vec2_real)[len[0]] = (double(*)[len[0]])vec2_real_base;

I see this will come in handy for the above


How do I fix "exec: \"/src/vhpidirect/demo/run.sh\": permission denied": unknown.

@radonnachie
Copy link
Author

Is printing out:

[0,0](0): 11    [0,1](1): 22
[1,0](2): 33    [1,1](3): 44
[2,0](4): 55    [2,1](5): 66

instead of

[0,0](0): 11    [0,1](1): 22    [0,2](2): 33
[1,0](3): 44    [1,1](4): 55    [1,2](5): 66

which is how I see ((11, 22, 33), (44, 55, 66)).

The original test of the 2D real was:
double (*vec2_real)[len[0]] = (double(*)[len[0]])vec2_real_base;

I have changed it to, which I strongly believe makes more sense.
double (*vec2_real)[len[1]] = (double(*)[len[1]])vec2_real_base;

Thisalso means that there is no longer a discrepancy in the orders in which the ranges are set between ghdlFromArray and ghdlToArray

@radonnachie
Copy link
Author

Now test output

sudo bash ./run.sh ./

1D Array values [6]:
[0]: 11
[1]: 22
[2]: 33
[3]: 44
[4]: 55
[5]: 66

2D Array values [2,3]:
mat[0][0] = 11  mat[0][1] = 22  mat[0][2] = 33
mat[1][0] = 44  mat[1][1] = 55  mat[1][2] = 66

3D Array values [2,4,3]:
d3[0][0][0] = 11        d3[0][0][1] = 22        d3[0][0][2] = 33
d3[0][1][0] = 44        d3[0][1][1] = 55        d3[0][1][2] = 66
d3[0][2][0] = 77        d3[0][2][1] = 88        d3[0][2][2] = 99
d3[0][3][0] = 110       d3[0][3][1] = 121       d3[0][3][2] = 132

d3[1][0][0] = 143       d3[1][0][1] = 154       d3[1][0][2] = 165
d3[1][1][0] = 176       d3[1][1][1] = 187       d3[1][1][2] = 198
d3[1][2][0] = 209       d3[1][2][1] = 220       d3[1][2][2] = 231
d3[1][3][0] = 242       d3[1][3][1] = 253       d3[1][3][2] = 264

###NOTE###
The 3D array's values have been set to -1 after ghdlFromArray().
This change is decoupled from the VHDL unconstrained array
because ghdlFromArray() makes a deep copy.
So the assertions of 3D values in tb.vhd will still succeed.
d3[2][4][3] = 32767

v_logic  : H
v_ulogic : Z
v_char : k
v_int  : -6
v_nat  : 9
v_pos  : 3
v_real : 3.340000
v_bool : 1
v_bit  : 1
v_time : 20000000
v_rec  : 0x7ffff64cb0b8 y 5
v_enum : 2 2
v_str  : 0x7f84f36bb768 'hellostr'      length [8]
v_vec_int  : 0x7f84f36bb790     length [5]
v_vec_real : 0x7f84f36bb7d0     length [6]
v_vec_bool : 0x7ffff64cb0b4     length [4]
v_vec_bit  : 0x7f84f36bb838     length [4]
v_vec_phy  : 0x7ffff64cb070     length [3]
v_vec_rec  : 0x7ffff64cb038     length [2]
v_vec_enum : 0x7ffff64caffd     length [3]
v_2vec_real : 0x7ffff64caffd    lengths [3,2]

Verify GHDL Matrix in C
array: 0x7ffff64caf30
bounds: 0x7f84f36bb8d8
bounds[1].left: 0
bounds[1].right: 1
bounds[1].dir: 0
bounds[1].len: 2
bounds[2].left: 0
bounds[2].right: 2
bounds[2].dir: 0
bounds[2].len: 3
C assert mat_int[x][y] == A
[0,0](0): 11 == 11              [0,1](1): 22 == 22              [0,2](2): 33 == 33 
[1,0](3): 44 == 44              [1,1](4): 55 == 55              [1,2](5): 66 == 66 
v_mat_int  : 0x7ffff64caf30     lengths [2,3]


Verify the 3D GHDL array in C
array: 0x7ffff64caed8
bounds: 0x7f84f36bb908
bounds[1].left: 0
bounds[1].right: 1
bounds[1].dir: 0
bounds[1].len: 2
bounds[2].left: 0
bounds[2].right: 1
bounds[2].dir: 0
bounds[2].len: 2
bounds[3].left: 0
bounds[3].right: 2
bounds[3].dir: 0
bounds[3].len: 3
C assert d3_int[x][y][z] == A
[0,0,0](0): 11 == 11    [0,0,1](1): 22 == 22    [0,0,2](2): 33 == 33
[0,1,0](3): 44 == 44    [0,1,1](4): 55 == 55    [0,1,2](5): 66 == 66

[1,0,0](6): 77 == 77    [1,0,1](7): 88 == 88    [1,0,2](8): 99 == 99
[1,1,0](9): 110 == 110  [1,1,1](10): 121 == 121 [1,1,2](11): 132 == 132

v_3d_int  : 0x7ffff64caed8 [2,2,3]

end testCinterface

tb.vhd:132:5:@0ms:(report note): g_str'length: 11
tb.vhd:134:7:@0ms:(report note): g_str: HELLO WORLD
tb.vhd:136:5:@0ms:(report note): string: HELLO WORLD
tb.vhd:138:5:@0ms:(report note): g_int_vec'length: 6
tb.vhd:140:7:@0ms:(report note): 0: 11
tb.vhd:140:7:@0ms:(report note): 1: 22
tb.vhd:140:7:@0ms:(report note): 2: 33
tb.vhd:140:7:@0ms:(report note): 3: 44
tb.vhd:140:7:@0ms:(report note): 4: 55
tb.vhd:140:7:@0ms:(report note): 5: 66
tb.vhd:144:5:@0ms:(report note): g_line: HELLO WORLD
tb.vhd:145:5:@0ms:(report note): getLine: HELLO WORLD
tb.vhd:172:5:@0ms:(report note): g_int_mat'length: 2
tb.vhd:176:9:@0ms:(report note): Asserting Mat [0,0]: 11
tb.vhd:176:9:@0ms:(report note): Asserting Mat [0,1]: 22
tb.vhd:176:9:@0ms:(report note): Asserting Mat [0,2]: 33
tb.vhd:176:9:@0ms:(report note): Asserting Mat [1,0]: 44
tb.vhd:176:9:@0ms:(report note): Asserting Mat [1,1]: 55
tb.vhd:176:9:@0ms:(report note): Asserting Mat [1,2]: 66
tb.vhd:182:5:@0ms:(report note): g_int_3d'length: 2
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,0,0]: 11
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,0,1]: 22
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,0,2]: 33
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,1,0]: 44
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,1,1]: 55
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,1,2]: 66
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,2,0]: 77
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,2,1]: 88
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,2,2]: 99
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,3,0]: 110
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,3,1]: 121
tb.vhd:187:11:@0ms:(report note): Asserting 3D [0,3,2]: 132
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,0,0]: 143
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,0,1]: 154
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,0,2]: 165
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,1,0]: 176
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,1,1]: 187
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,1,2]: 198
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,2,0]: 209
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,2,1]: 220
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,2,2]: 231
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,3,0]: 242
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,3,1]: 253
tb.vhd:187:11:@0ms:(report note): Asserting 3D [1,3,2]: 264
tb.vhd:194:5:@0ms:(report note): No errors/failures. Concluding testbench.

@radonnachie
Copy link
Author

radonnachie commented Apr 13, 2020

@umarcor

I think there are 2 more things I want to do with ghdl.h:

  • Double check what ghdl_NaturalAccDimArr_t are, then expand them to be unbounded in dimensions like ghdl_NaturalDimArr_t
  • Split ghdl.h > ghdl.h + ghdl.c
    • I think this point may be a bone of contention, but I also think that software people get quite concerned when they see function definitions in header files.
  • Test the following:
    • ghdlAccToString

@umarcor
Copy link
Owner

umarcor commented Apr 13, 2020

I moved the example to subdir cinterface, and I updated the name of the doc file (demo.rst to cinterface.rst). Please, rebase so that differences are shown.

@radonnachie
Copy link
Author

I believe I did the rebase imperfectly... I just had to apply the total change in commit "Rebase.." Hopefully next time is better.

@umarcor
Copy link
Owner

umarcor commented Apr 15, 2020

I believe I did the rebase imperfectly... I just had to apply the total change in commit "Rebase.." Hopefully next time is better.

Unless you really want to preserve the (or some) of commits, I suggest to first squash all the commits in your branch to have a single one on top of mine. Then, git rebase -i umarcor/header and remove all but the last one (yours). Following this procedure, you should need to resolve conflicts once only, and there should not be location mismatches.

@umarcor
Copy link
Owner

umarcor commented Sep 1, 2020

@RocketRoss, I accidentally force-pushed to branch RocketRoss:header and this PR was unwantedly closed. I pushed the content to branch rocket-header in this repo, so you can pick it and force-push here.

@radonnachie radonnachie deleted the header branch October 10, 2020 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants