diff --git a/examples/vhdl/external_buffer/cp.py b/examples/vhdl/external_buffer/cp.py new file mode 100644 index 000000000..971927c69 --- /dev/null +++ b/examples/vhdl/external_buffer/cp.py @@ -0,0 +1,31 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at http://mozilla.org/MPL/2.0/. +# +# Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com + +from vunit import VUnit +from os import popen +from os.path import join, dirname + +src_path = join(dirname(__file__), 'src') + +c_obj = join(src_path, 'cp.o') +# Compile C application to an object +print(popen(' '.join([ + 'gcc', '-fPIC', + '-c', join(src_path, 'cp.c'), + '-o', c_obj +])).read()) + +# Enable the external feature for strings +vu = VUnit.from_argv(vhdl_standard='2008', compile_builtins=False) +vu.add_builtins({'string': True}) + +lib = vu.add_library('lib') +lib.add_source_files(join(src_path, 'tb_extcp_*.vhd')) + +# Add the C object to the elaboration of GHDL +vu.set_sim_option('ghdl.elab_flags', ['-Wl,' + c_obj]) + +vu.main() diff --git a/examples/vhdl/external_buffer/run.py b/examples/vhdl/external_buffer/run.py index 0f207259b..068b5f883 100644 --- a/examples/vhdl/external_buffer/run.py +++ b/examples/vhdl/external_buffer/run.py @@ -44,7 +44,7 @@ vu.add_builtins({'string': True}) lib = vu.add_library('lib') -lib.add_source_files(join(src_path, '*.vhd')) +lib.add_source_files(join(src_path, 'tb_ext_*.vhd')) # Add the C object to the elaboration of GHDL vu.set_sim_option('ghdl.elab_flags', ['-Wl,' + c_obj]) diff --git a/examples/vhdl/external_buffer/src/cp.c b/examples/vhdl/external_buffer/src/cp.c new file mode 100644 index 000000000..dde9c2236 --- /dev/null +++ b/examples/vhdl/external_buffer/src/cp.c @@ -0,0 +1,96 @@ +/* +External Buffer + +Interfacing with foreign languages (C) through VHPIDIRECT: +https://ghdl.readthedocs.io/en/latest/using/Foreign.html + +Two arrays of type uint8_t are allocated and some values are written to the first. +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. + +NOTE: This file is expected to be used along with tb_extcp_byte_vector.vhd or tb_extcp_string.vhd +*/ + +#include +#include +#include + +extern int ghdl_main (int argc, char **argv); + +uint8_t *D[1]; +const uint32_t length = 10; + +// 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) { + int i, j, z, k; + uint8_t expected, got; + k = 0; + + for(i=0; i