|
4 | 4 | # |
5 | 5 | # Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com |
6 | 6 |
|
7 | | -from os import popen, mkdir |
| 7 | +from os import popen |
8 | 8 | from os.path import join, dirname |
9 | 9 | from vunit import VUnit |
| 10 | +import ctypes |
| 11 | + |
| 12 | + |
| 13 | +def preconf(output_path): |
| 14 | + global opath |
| 15 | + opath = output_path |
| 16 | + return True |
| 17 | + |
| 18 | + |
| 19 | +def get_args(output_path): |
| 20 | + args = [line.rstrip('\n') for line in open(join(output_path, 'ghdl', 'args.txt'))] |
| 21 | + xargs = (ctypes.POINTER(ctypes.c_char) * (len(args) + 1))() |
| 22 | + for i, arg in enumerate(args): |
| 23 | + xargs[i] = ctypes.create_string_buffer(arg.encode('utf-8')) |
| 24 | + return args[0], xargs |
| 25 | + |
| 26 | + |
| 27 | +def unload(lib): |
| 28 | + import _ctypes |
| 29 | + # On GNU/Linux |
| 30 | + _ctypes.dlclose(lib._handle) |
| 31 | + # On Windows |
| 32 | + #_ctypes.FreeLibrary(gbin._handle) |
| 33 | + |
10 | 34 |
|
11 | 35 | std = "2008" |
12 | 36 | #std = "93" |
|
20 | 44 | lib = ui.add_library("lib") |
21 | 45 | lib.add_source_files(join(src_path, "test", "*.vhd")) |
22 | 46 |
|
23 | | -try: |
24 | | - mkdir(join(src_path, '..', 'vunit_out')) |
25 | | -except OSError as exc: |
26 | | - pass |
27 | | - |
28 | | -c_obj = join(src_path, '..', 'vunit_out', 'main.o') |
| 47 | +c_obj = join(src_path, 'test', 'main.o') |
29 | 48 | print(popen('gcc -fPIC -rdynamic -c '+join(src_path, '**', 'main.c')+' -o '+c_obj).read()) |
30 | 49 |
|
31 | 50 | ui.set_objects([c_obj]) |
32 | 51 |
|
33 | | -ui.main() |
| 52 | +for tb in lib.get_test_benches(pattern='*', allow_empty=False): |
| 53 | + tb.set_pre_config(preconf) |
| 54 | + |
| 55 | +ui.set_sim_option("ghdl.elab_e", True) |
| 56 | +ui._args.elaborate = True |
| 57 | +try: |
| 58 | + ui.main() |
| 59 | +except SystemExit as exc: |
| 60 | + if exc.code is not 0: |
| 61 | + exit(exc.code) |
| 62 | + |
| 63 | +bin_path, xargv = get_args(opath) |
| 64 | + |
| 65 | +print("\nREGULAR EXECUTION") |
| 66 | +gbin = ctypes.CDLL(bin_path) |
| 67 | +gbin.main(len(xargv)-1, xargv) |
| 68 | +unload(gbin) |
| 69 | + |
| 70 | +print("\nPYTHON ALLOCATION") |
| 71 | +gbin = ctypes.CDLL(bin_path) |
| 72 | + |
| 73 | +buf = ctypes.create_string_buffer(bytes([111, 122, 133, 144, 155]), 15) |
| 74 | + |
| 75 | +for x in range(0, 15): |
| 76 | + print("py " + str(x) + ": " + str(int.from_bytes(buf[x], "little"))) |
| 77 | + |
| 78 | +gbin.set_bytevec_addr(0, buf) |
| 79 | +gbin.main(len(xargv)-1, xargv) |
| 80 | + |
| 81 | +for x in range(0, 15): |
| 82 | + print("py " + str(x) + ": " + str(int.from_bytes(buf[x], "little"))) |
| 83 | + |
| 84 | +unload(gbin) |
0 commit comments