Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
sim:file [2024/02/26 23:56] – fabian | sim:file [2024/02/27 17:16] (current) – fabian | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== File interaction ====== | ====== File interaction ====== | ||
- | This library exports functions for basic file interaction. Files in actsim can be accessed using the common simulator prefix and file IDs. The common prefix is set using an actsim configuration file. The following is a snippet from the default actsim configuration. | + | This library exports functions for basic file interaction. |
+ | |||
+ | Files in actsim can be accessed using the common simulator prefix and file IDs. The common prefix is set using an actsim configuration file. The following is a snippet from the default actsim configuration. | ||
< | < | ||
Line 218: | Line 220: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Extending the File API ===== | ||
+ | |||
+ | File interactions can be extended using a shared C++ library, however the functions exposed to actsim must be made external C functions due to C++ name mangling. Include the '' | ||
+ | |||
+ | <code c> | ||
+ | extern " | ||
+ | expr_res ret; | ||
+ | ret.width = 1; | ||
+ | ret.v = 1; // on error we return 1 | ||
+ | |||
+ | // make sure we have the appropriate amount of arguments | ||
+ | if (argc != 5) { | ||
+ | std::cerr | ||
+ | << " | ||
+ | << std::endl; | ||
+ | return ret; | ||
+ | } | ||
+ | |||
+ | uint32_t writer_id = args[0].v; | ||
+ | uint8_t verbosity = args[1].v; | ||
+ | uint32_t logger_id = args[2].v; | ||
+ | uint32_t channel = args[3].v; | ||
+ | uint64_t value = args[4].v; | ||
+ | |||
+ | // build the log line | ||
+ | std:: | ||
+ | |||
+ | switch (verbosity) { | ||
+ | case 0: | ||
+ | builder << channel << ":" | ||
+ | break; | ||
+ | |||
+ | case 1: | ||
+ | builder << logger_id << " (" << channel << "): " << std::hex | ||
+ | << value << std::endl; | ||
+ | break; | ||
+ | |||
+ | case 2: | ||
+ | builder << " | ||
+ | << "): Received value " << value << "%x (0x" << std::hex | ||
+ | << value << " | ||
+ | |||
+ | default: | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | ret.v = actsim_file_write_core(writer_id, | ||
+ | return ret; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | When you have compiled your '' | ||
+ | |||
+ | < | ||
+ | begin extern | ||
+ | |||
+ | string_tablex libs " | ||
+ | |||
+ | begin mylib | ||
+ | string path "/ | ||
+ | |||
+ | # logger file output | ||
+ | string my_namespace:: | ||
+ | end | ||
+ | |||
+ | end | ||
+ | </ | ||
+ | |||
+ | And finally, make the function available to CHP: | ||
+ | |||
+ | <code act> | ||
+ | namespace my_namespace { | ||
+ | export function write_log (int< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Mind the name change set in the actsim configuration! Functions in CHP must always return a value. If you do not need a return value, either use a boolean in combination with an assert function for error checking, or just read into a dummy variable. Not reading into anything will currently cause your function to be removed by the optimization pass. | ||