|
| 1 | +/* |
| 2 | +External Buffer |
| 3 | +
|
| 4 | +Interfacing with foreign languages (C) through VHPIDIRECT: |
| 5 | +https://ghdl.readthedocs.io/en/latest/using/Foreign.html |
| 6 | +
|
| 7 | +Two arrays of type uint8_t are allocated and some values are written to the first. |
| 8 | +Then, the VHDL simulation is executed, where the (external) array/buffer |
| 9 | +is used. When the simulation is finished, the results are checked. The content of |
| 10 | +the buffer is printed both before and after the simulation. |
| 11 | +
|
| 12 | +NOTE: This file is expected to be used along with tb_extcp_byte_vector.vhd or tb_extcp_string.vhd |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <stdint.h> |
| 18 | + |
| 19 | +extern int ghdl_main (int argc, char **argv); |
| 20 | + |
| 21 | +uint8_t *D[1]; |
| 22 | +const uint32_t length = 10; |
| 23 | + |
| 24 | +// Check procedure, to be executed when GHDL exits. |
| 25 | +// The simulation is expected to copy the first 1/3 elements to positions [1/3, 2/3), |
| 26 | +// while incrementing each value by one, and then copy elements from [1/3, 2/3) to |
| 27 | +// [2/3, 3/3), while incrementing each value by two. |
| 28 | +static void exit_handler(void) { |
| 29 | + int i, j, z, k; |
| 30 | + uint8_t expected, got; |
| 31 | + k = 0; |
| 32 | + |
| 33 | + for(i=0; i<length; i++) { |
| 34 | + expected = D[0][i]; |
| 35 | + got = D[1][i]; |
| 36 | + if (expected != got) { |
| 37 | + printf("check error %d: %d %d\n", i, expected, got); |
| 38 | + exit(1); |
| 39 | + } |
| 40 | + printf("%d: %d\n", i, got); |
| 41 | + } |
| 42 | + |
| 43 | + free(D[0]); |
| 44 | + free(D[1]); |
| 45 | +} |
| 46 | + |
| 47 | +// Main entrypoint of the application |
| 48 | +int main(int argc, char **argv) { |
| 49 | + // Allocate two buffers |
| 50 | + int i; |
| 51 | + for(i=0; i<2; i++) { |
| 52 | + D[i] = (uint8_t *) malloc(length*sizeof(uint8_t)); |
| 53 | + if ( D[i] == NULL ) { |
| 54 | + perror("execution of malloc() failed!\n"); |
| 55 | + return -1; |
| 56 | + } |
| 57 | + } |
| 58 | + // Initialize the first buffer |
| 59 | + for(i=0; i<length; i++) { |
| 60 | + D[0][i] = (i+1)*11; |
| 61 | + } |
| 62 | + // Print all the buffer |
| 63 | + for(i=0; i<length; i++) { |
| 64 | + printf("%d: %d\n", i, D[0][i]); |
| 65 | + } |
| 66 | + |
| 67 | + // Register a function to be called when GHDL exits |
| 68 | + atexit(exit_handler); |
| 69 | + |
| 70 | + // Start the simulation |
| 71 | + return ghdl_main(argc, argv); |
| 72 | +} |
| 73 | + |
| 74 | +// External through access (mode = extacc) |
| 75 | + |
| 76 | +void set_string_ptr(uint8_t id, uint8_t *p) { |
| 77 | + //printf("C set_string_ptr(%d, %p)\n", id, p); |
| 78 | + D[id] = p; |
| 79 | +} |
| 80 | + |
| 81 | +uintptr_t get_string_ptr(uint8_t id) { |
| 82 | + //printf("C get_string_ptr(%d): %p\n", id, D[id]); |
| 83 | + return (uintptr_t)D[id]; |
| 84 | +} |
| 85 | + |
| 86 | +// External through functions (mode = extfnc) |
| 87 | + |
| 88 | +void write_char(uint8_t id, uint32_t i, uint8_t v ) { |
| 89 | + //printf("C write_char(%d, %d): %d\n", id, i, v); |
| 90 | + D[id][i] = v; |
| 91 | +} |
| 92 | + |
| 93 | +uint8_t read_char(uint8_t id, uint32_t i) { |
| 94 | + //printf("C read_char(%d, %d): %d\n", id, i, D[id][i]); |
| 95 | + return D[id][i]; |
| 96 | +} |
0 commit comments