COMPUTER SCIENCE
Vol. 16. No 1(2008), pp. 49-67
1. Introduction
General purpose of IEC 61131-3 standard introduced in 1998 was to
improve quality of software for programmable controllers. Now the standard is
also a Polish law (PN-EN 61131-3). The IEC (number will be dropped for
brevity) defines five programming languages, namely LD, IL, FBD, ST and
SFC, allowing the user to choose one suitable for particular application,
corresponding to his expertise. Instruction list IL and Structured Text ST are text
languages, whereas Ladder Diagram LD, Function Block Diagram FBD and
Sequential Function Chart SFC are graphical ones (SFC is not an independent
* Developed under MNiSzW grant no. R02 058 03.
50
51
Type
Size (range)
Type
Size
SINT
1B (-128 .. 127)
BOOL
1B (0, 1)
INT
2B (-32768 .. 32767)
BYTE
1B
DINT
4B (-231 .. 231 1)
WORD
2B
DWORD
4B
LWORD
8B
2B (0 .. 65535)
TIME
4B
UDINT 4B (0 .. 232 1)
DATE
4B
TIME_OF_DAY
4B
LINT
63
63
8B (-2 .. 2 1)
USINT 1B (0 .. 255)
UINT
64
ULINT 8B (0 .. 2 1)
REAL
DATE_AND_TIME 8B
STRING
Variable length
The standard defines three levels for accessing variables, i.e. LOCAL,
GLOBAL and ACCESS. LOCALs are available in the program, function block or
function. GLOBALs can be used in the whole project, but programs, blocks or
functions must declare them as EXTERNAL. ACCESS variables exchange data
between different systems.
52
Fig. 1. Examples of IEC 61131-3 standard function blocks with time diagrams:
SR flip-flop, TON on-delay timer, CTU up-counter
(* CTU up-counter *)
(* count up *)
(* reset *)
(* preset value *)
(* signaling output *)
(* current value *)
(* previous CU input *)
(* if reset *)
(* if rising edge at CU input *)
(* Q := TRUE, if CV >= PV *)
(* previous value of CU *)
53
3. CPDev package
3.1. Internal structure
The CPDev package consists of three programs executed by PC and one by
the controller. The PC programs are as follows:
CPDev compiler of ST language,
CPSim simulator,
CPCon configurer of hardware resources.
The programs exchange data through files in appropriate formats. The
CPDev compiler (the same name as the package) generates universal code
executed by virtual machine (VM) run by the controller. The VM operates as an
interpreter. The universal code is a list of primitive instructions of the VM
language called VMASM assembler. VMASM is not related to any particular
processor, however it is close to typical assemblers. The VMASM and virtual
machine are described in Secs. 4, 5 and 6.
Fig. 2 presents relation between CPDev, CPSim and CPCon. Separation of
the package into logic and hardware layers simplifies implementation at
different platforms. The ST source program is compiled into universal
executable code at the logic layer. The compiler employs ST syntax rules, list of
VMASM instructions and POUs from libraries. Besides the universal code the
compiler generates some information for debugging and simulation. The
addresses of directly represented variables [1,2], declared in ST by AT keyword,
are relative addresses (called local here). They are sufficient for the CPSim
simulator but not sufficient for the target platform.
54
Logic
layer
ST language
syntax
Libraries
VMASM assembler
Compiler
CPDev
ST source code
Universal
executable code
Debugger
information
Error list
Simulator
CPSim
Target
platform
Configurer
of hardware resources
CPCon
I/O interface
specification
Hardware
layer
Hardware
allocation map
Communication
Interface specification
55
56
Bistable elements
flip-flop
flip-flop
semaphore
Counters
up
down
up-down
RS
SR
SEMA
CTU
CTD
CTUD
Edge detectors
rising
falling
Timers
pulse
on-delay
off-delay
real time clock
R_TRIG
F_TRIG
TP
TON
TOF
RTC
57
4. Compiler components
4.1. Scanner, parser and code generator
The task of the compiler is to convert XML source file with the project in ST
language into a file with universal executable code in binary format. General
diagram of the compiler operation involving scanner, parser and code generator
is shown in Fig. 4.
The scanner (lexical analyser) analyses character stream from the ST source
file and decomposes it into lexical units, i.e. tokens. The tokens are classified
into categories. Most important categories are listed in Table 4. The tokens with
categories are collected on a list passed to the parser.
Table 4. Token categories recognized by the scanner
Category
Example
Category
Example
identifier
PRG_MOVE_UNIT
integer constant
50
keyword
FUNCTION
operator
typed constant
DINT#1722211
delimiter
comment
(* reset *)
directive
(*$READ*)
real constant
18.32
white space
string constant
'Temperature: '
invalid character
58
Instruction Meaning
ADD
Addition
OR
Logical or
SUB
Subtraction
XOR
Logical xor
MUL
Multiplication
NOT
Binary negation
DIV
Division
MCD
Constant initialization
NEG
Negation
MEMCP
Memory copy
EXPT
Power
**
SHL/SHR
Shift left/right
GT
Greater
>
ROL/ROR
Rotate left/right
LT
Less
<
JMP
Unconditional jump
GE
Greater or equal
>=
JZ
LE
Less or equal
<=
JNZ
EQ
Equal
JR
Relative jump
NE
Not equal
<>
JRN
CONCAT
String concatenation +
JRZ
Conditional relative
jumps
AND
Logical and
RETURN
&
Conditional jumps
59
60
As indicated before, in the first step the scanner decomposes input character
stream into tokens, and classifies them into categories. Here the first token is
FUNCTION_BLOCK from keyword category (Table 4). Hence the next token
CTU is treated as a new identifier of STFunctionBlock class and registered
on the list of global identifiers. The following token VAR_INPUT is a keyword
that begins declaration clause. ST language requires declaration clauses to be
finished by END_VAR. Interpretation of all clauses beginning with VAR, so
VAR_INPUT, VAR_OUTPUT and VAR proceeds according to lexical diagram of
Fig. 7. Taking into account the types (BOOL and INT here), the parser
determines memory sizes for the variables declared. The sizes will be used by
the code generator to resolve addresses (Sec. 5.3).
61
62
As seen, two byte variables are in the Little Endian form. Similarly as
before, RETURN completes the initialization section.
Local addresses of variables are resolved using the sizes determined by the
scanner at the beginning (Sec. 5.1). The first address is 0000. In our example
the variables CU, RESET, PV, Q, CV and CUp are consecutively declared
(Fig. 6). BOOL occupies one byte and INT two. So the variables are at the
addresses 0000, 0001, 0002, 0004, 0005 and 0007, respectively. The
63
addresses of Q, CV and PV are seen at 006F and 0073 in the binary code
(Fig. 8, Little Endian).
As mentioned before, the LCF configuration file is created independently for
each virtual machine. So in one case the mnemonic GE can be replaced by 1102
and in another one by something else. Diversification of the vmcode attributes
is the way to optimize the code for particular processors. Besides, by using
<deny-type> elements of the LCF file (TYPES section) one can restrict the
number of available data types. This provides flexibility of the CPDev package.
By applying <deny-type>, the number of data types for SMC controller
mentioned in the next section has been restricted to ten. So by means of the LCF
files one can create dedicated compilers for particular applications.
64
instruction. Stack emulation and update of the base addresses permit multiple,
concurrent calls of functions and function blocks.
65
The universal part of the virtual machine has been written in industry
standard ANSI C, so it can be directly applied to different processors. As
indicated before, the number of data types and the way in which the machine
instructions are executed are defined by the LCF configuration file. A set of
general specifications has been developed in CPDev for handling processor
components (interrupt system, RTC) and external interfaces (I/O,
communications). The specifications are in the form of prototypes of
corresponding procedures (names, types of inputs and returned outputs). The
prototypes do not depend on processor and hardware solutions.
The file with the prototypes (*.h) is compiled together with the universal
modules of the virtual machine. The contents (bodies) of the specification
procedures can be prepared elsewhere and, as a binary file (e.g. *.obj),
consolidated with the compiled universal modules. This gives the complete code
of the virtual machine for the given platform.
We stress that the contents of the procedures, dependent on the processor and
hardware solutions, may be written by controller designers themselves. This
makes the CPDev package open also in the hardware sense.
66
b)
S MC
RS485
RS485
RS232
SMC
RS485
Of twenty data types available in CPDev (Table 1), the SMC implements
ten, namely BOOL, INT, DINT, UINT, REAL, WORD, DWORD, STRING, TIME
and DATE_AND_TIME. Since SMC does not carry I/Os on board, the CPCon
configurer configures communications only.
The SMC controller is equipped with 8-bit AVR ATmega 128
microcontroller [10]. This is a modern RISC microprocessor able to execute
most of instructions in a single clock cycle, with separate buses for program and
data memories. The universal modules of the virtual machine, i.e. VMASM
assembler interpreter, stack emulator and data type handler (Fig. 11b) have been
compiled by avr-gcc compiler from WinAVR package [11]. Hardware dependent
software, i.e. interrupts, RTC and communication interfaces have been written
by engineers from LUMEL and sent to the authors in binary format.
Consolidation has produced a virtual machine which, written into SMC flash
memory, executes ST programs compiled by CPDev package and downloaded
from PC.
Another machine for ADuC841 microcontroller (MCS-51 core) has been
also developed. The machine for PC is a part of CPSim simulator.
67
8. Conclusions
The CPDev package for programming controllers and other control-andmeasurement devices in the ST language of IEC 61131-3 standard has been
presented. The package is universal, since the compiled code can be executed on
different platforms. However, the execution must be carried out by virtual
machines dedicated for particular processors.
Operation of the CPDev compiler involving scanner, parser and code
generator has been explained by means of a program for CTU counter. The
compiler produces universal executable code which, together with hardware
allocation map, is downloaded into the controller and executed by the virtual
machine. The machine is configured by means of LCF Library Configuration
File which enables selection of data types and some form of code optimization.
The CPDev package is open in terms of both software and hardware. The
user can create libraries with POUs. Hardware designers can program external
interfaces (drivers). Distributed mini-system from LUMEL is the first
application of the package.
References
[1] John K.H., Tiegelkamp M.: IEC 61131-3: Programming Industrial Automation
Systems. Berlin Heidelberg, Springer-Verlag, 2001.
[2] Kasprzyk J.: Programming Industrial Controllers, WNT, Warsaw, 2006 (in Polish).
[3] C# Language Specification
http://msdn2.microsoft.com/enus/vcsharp/aa336809.aspx
[4] Rzo ca D., Sadolewski J., Trybus B.: IEC 61131-3 standard ST compiler into
universal executable code. In: Real-Time Systems. Methods and Applications, WK,
Warsaw, 189-198, 2007 (in Polish).
[5] Stec A., wider Z., Trybus L.: Functional characteristic of the prototype system for
embedded systems programming. In: Real-Time Systems. Methods and
Applications, WK, Warsaw, 179-188, 2007 (in Polish).
[6] Cooper K., Torczon L.: Engineering a Compiler. Morgan Kaufmann, San Francisco,
2003.
[7] Appel A., Palsberg J.: Modern compiler implementation in Java. Second edition.
Cambridge University Press, 2002.
[8] LUMEL Ltd., Zielona Gra http://www.lumel.pl
[9] Modicon MODBUS Protocol Reference Guide. MODICON, Inc., Industrial
Automation Systems, Massachusetts, 1996.
http://www.modbus.org/docs/PI_MBUS_300.pdf
[10] ATmega 128 Datasheet. Atmel, 2007.
http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf
[11] WinAVR Development Tools for the Atmel AVR. http://winavr.sourceforge.net/
[12] ADuc841 Datasheet. Analog Devices, 2003.
http://www.analog.com/UploadedFiles/Data_Sheets/
ADUC841_842_843.pdf