It’s really amazing how much effort sometimes needs to be put in to make the smallest element of functionality work correctly. The case in point is the recent implementation of support for calling functions from within the IEC 61131-3 virtual machine. This implementation required the inclusion of new virtual machine operators (PUSH and POP) to appropriately manage the passing of parameters between the calling code and the function body. There were also considerations that had to be given to the conditional nature of calling functions – namely with respect to the difference defined in the IEC 61131-3 standard between the C and N modifiers that can be applied to the CAL (call) operator; parameter typing and return types; and how to ensure the integrity of the parameter stack between invocations.
All of this work was ultimately manifest in the following, very simple test application:
CAL Func( Count )
FUNCTION Func: INT
The result when all requisite functionality was implemented was the very simple increment of a program variable (Count), albeit by somewhat convoluted means whereby a function is called and the function result is stored in the program variable – rather than direct increment of the program variable. Nevertheless, this provided a very sure means by which function call operations could be tested and I must say that it was particularly rewarding when this test case “just worked” after implementing the final dependent code elements.
Over the last few months, I have made some excellent progress on this project – I have implemented branching and execution jumps in the compiler and run-time and am currently working on the implementation of parameter passing to function calls. This latter task in particular has been interesting in that it has led me to made some additions to the byte code employed within the IEC 61131-3 virtual machine run-time.
For some background, the byte code is the portable instruction set, shared between the compiler and virtual machine elements of this project, that dictate execution of the virtual machine run-time. The intent of using byte codes in this project, rather than compiling directly to C or machine-specific source (such as is performed by the MATIEC, https://bitbucket.org/mjsousa/matiec/) is to provide a very compact and portable instruction set for operations. Moreover, using the interpretation of byte code for directing execution of the virtual machine additionally allows for greater control to be exerted with respect to “unintended” operations that may result from the direct execution of machine assembly. For a further explanation of byte code and it’s use in portable systems, see Wikipedia (http://en.wikipedia.org/wiki/Bytecode or http://en.wikipedia.org/wiki/Java_bytecode).
At this point in time, this open-source IEC 61131-3 virtual machine project is in a very rudimentary, alpha state. Unlike many projects at their outset however, this project has progressed beyond mere aspirations and pie-in-the-sky idealism such that working source code has been developed and committed to source control. Indeed, source code for both the IEC 61131-3 compiler – supporting Instruction List (IL) only – and the corresponding virtual machine has been written, providing a very tangible proof-of-development concept.
The IEC 61131-3 compiler element of this project is a multi-pass compiler that allows for the compilation of programs written in IL to byte code. This compilation includes support for load and store operations; basic mathematic and logic operations; and comparison and branching operations. Also supported by the IEC 61131-3 compiler (and virtual machine) is deferred execution operations, allowing for the easy nesting of mathematic and logic operations. The only basic operations which are unsupported at this point in time are call operations, although this largely a function of limited Program Organisation Unit (POU) support in the IEC 61131-3 virtual machine at this point in time. The output of compiler operations is a byte code output capable of being executed in the virtual machine also under development as part of this project.
This IEC 61131-3 virtual machine under development as part of this project allows for the execution of compiled byte code within a “sand box” environment. All execution within this environment follows a triggered, cyclic pattern which follows the IEC 61131 model of execution – consisting of the reading of inputs, execution of logic code, and assertion of outputs. For development purposes, the number of cycles of execution of the virtual machine is also limited such that cycle-by-cycle operations of the virtual machine can be observed and checked.
All development has been performed in C, with the specific intent that these code bases will be highly portable, and in the case of the IEC 61131-3 virtual machine, amenable to embedded systems. To date however, development and testing has been limited to Linux based platforms – There will however be more detail about this in later posts.
This site has been established to provide running, work-in-progress documentation for an open-source project. This project is intended to implement a compiler for IEC 61131-3 textual languages, the output of which is a byte code intended to be run in a small virtual machine also under development as part of this project.
The IEC 61131-3 international standard describes a set of five programming languages commonly employed in association with programmable logic controllers (PLCs) and remote telemetry units (RTUs). The programming languages described in this standard are:
- Ladder Diagram (LD);
- Function Block Diagram (FBD);
- Structured Text (ST);
- Instruction List (IL); and
- Sequential Function Chart (SFC).
The IEC 61131-3 programming languages intended to be supported by this project are Instruction List (IL) and Structured Text (ST), although support will primarily be implemented for IL only.
Whilst the scope of the IEC 61131-3 standard and in turn, this project is inherently industrial in nature, it should be noted that this project is primarily a personal endeavour to explore concepts of software development relating to real-time virtual machine byte-code execution, industrial automation and telemetry protocols. Indeed, all work being performed on this project is being performed in personal time between the demands of work and family. As such, while effort will be made to ensure continued progress and update of both the code base and this record of development, there may be times where updates are short or sparse inbetween.