From 994b2b6b8f467b16adac19cd0f05f2a6fcb10ab3 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 6 Jan 2026 05:28:32 +0000 Subject: [PATCH] Add %MX (bool memory) support for OpenPLC v4 - Add bool_memory_ptr declaration under #ifdef OPENPLC_V4 in glueVars.c.j2 - Add setBufferPointers_v4 function with bool_memory parameter under #ifdef OPENPLC_V4 - Add %MX case to GlueGenerator.__glue_logic wrapped in #ifdef OPENPLC_V4 When compiled with -DOPENPLC_V4 flag (set in openplc-runtime compile.sh), the generated glueVars.c will include: - bool_memory_ptr pointer for %MX variables - setBufferPointers_v4 function that the runtime calls to pass bool_memory This enables %MX located variables to be glued to the runtime's bool_memory buffer, completing the %MX support added in openplc-runtime PR #63. For v3 compatibility, the %MX glue code is excluded when OPENPLC_V4 is not defined, so the same template works for both v3 and v4 runtimes. Co-Authored-By: Thiago Alves --- GlueGenerator.py | 9 ++++++++- templates/glueVars.c.j2 | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/GlueGenerator.py b/GlueGenerator.py index fedcf6f9e..d5182fc8c 100644 --- a/GlueGenerator.py +++ b/GlueGenerator.py @@ -58,7 +58,14 @@ def __glue_logic(self, varName): return f"lint_output_ptr[{pos1}] = (IEC_ULINT *){varName};" elif kind == "M": - if sub == "W": + if sub == "X": + # %MX (bool memory) is only supported in OpenPLC v4 + return ( + f"#ifdef OPENPLC_V4\n" + f" bool_memory_ptr[{pos1}][{pos2}] = (IEC_BOOL *){varName};\n" + f"#endif" + ) + elif sub == "W": return f"int_memory_ptr[{pos1}] = (IEC_UINT *){varName};" elif sub == "D": return f"dint_memory_ptr[{pos1}] = (IEC_UDINT *){varName};" diff --git a/templates/glueVars.c.j2 b/templates/glueVars.c.j2 index b6b4fafd3..0390ab932 100644 --- a/templates/glueVars.c.j2 +++ b/templates/glueVars.c.j2 @@ -106,6 +106,9 @@ extern unsigned long long common_ticktime__; static IEC_UINT *(*int_memory_ptr) = NULL; static IEC_UDINT *(*dint_memory_ptr) = NULL; static IEC_ULINT *(*lint_memory_ptr) = NULL; +#ifdef OPENPLC_V4 + static IEC_BOOL *(*bool_memory_ptr)[8] = NULL; +#endif void setBufferPointers(IEC_BOOL *input_bool[BUFFER_SIZE][8], IEC_BOOL *output_bool[BUFFER_SIZE][8], IEC_BYTE *input_byte[BUFFER_SIZE], IEC_BYTE *output_byte[BUFFER_SIZE], @@ -129,6 +132,24 @@ extern unsigned long long common_ticktime__; dint_memory_ptr = dint_memory; lint_memory_ptr = lint_memory; } + +#ifdef OPENPLC_V4 + void setBufferPointers_v4(IEC_BOOL *input_bool[BUFFER_SIZE][8], IEC_BOOL *output_bool[BUFFER_SIZE][8], + IEC_BYTE *input_byte[BUFFER_SIZE], IEC_BYTE *output_byte[BUFFER_SIZE], + IEC_UINT *input_int[BUFFER_SIZE], IEC_UINT *output_int[BUFFER_SIZE], + IEC_UDINT *input_dint[BUFFER_SIZE], IEC_UDINT *output_dint[BUFFER_SIZE], + IEC_ULINT *input_lint[BUFFER_SIZE], IEC_ULINT *output_lint[BUFFER_SIZE], + IEC_UINT *int_memory[BUFFER_SIZE], IEC_UDINT *dint_memory[BUFFER_SIZE], + IEC_ULINT *lint_memory[BUFFER_SIZE], IEC_BOOL *memory_bool[BUFFER_SIZE][8]) + { + // Call the base function to set all standard pointers + setBufferPointers(input_bool, output_bool, input_byte, output_byte, + input_int, output_int, input_dint, output_dint, + input_lint, output_lint, int_memory, dint_memory, lint_memory); + // Set the v4-specific bool_memory pointer + bool_memory_ptr = memory_bool; + } +#endif #endif void glueVars()