Skip to content

AGSweeney/MPLC

Repository files navigation

MPLC — Portable IEC 61131 Runtime

MPLC is a host-compiled IEC 61131-3 toolchain and fixed-memory bytecode runtime written in C for embedded controllers and Linux targets. The runtime and HAL are portable across platforms; MPLC Studio currently provides an integrated edit, compile, deploy, and serial-debug workflow for NetBurner MOD54415 hardware.

Features

  • Structured Text (IEC 61131-3): full statement set (IF/CASE with ranges/FOR/WHILE/REPEAT/EXIT/CONTINUE/RETURN), IEC operator precedence incl. **, all elementary types (8/16/32/64-bit signed/unsigned integers, bit strings, REAL/LREAL, TIME/DATE/TOD/DT, STRING, CHAR), derived types (TYPE: enumerations, subranges, aliases, arrays, structures), all literal forms (based 2#/8#/16#, typed INT#, duration T#1h30m, date/time, $-escaped strings), user FUNCTION and FUNCTION_BLOCK POUs with formal/non-formal invocation and EN/ENO, the standard function library (conversions, math, MIN/MAX/LIMIT/SEL/MUX, shifts/rotates, string functions, time arithmetic), standard FBs (TON/TOF/TP/CTU/CTD/CTUD/R_TRIG/F_TRIG/SR/RS) callable from ST, VAR_INPUT/VAR_OUTPUT/VAR_IN_OUT/VAR_TEMP/VAR_GLOBAL/VAR_EXTERNAL, CONSTANT, RETAIN (persisted via HAL), initial values, and %I/%Q/%M direct addressing
  • Ladder Diagram: NO/NC and P/N transition-sensing contacts, normal/negated/set/reset and P/N transition-sensing coils, jumps to labelled rungs, conditional RETURN, EN/ENO block gating, nested input branches, output branches, and the standard FB set
  • Languages (experimental): IL, FBD, SFC; PLCopen XML body import
  • Portable VM: fixed-memory stack machine (bool/i32/i64/f64 classes) with call frames, native standard-function dispatch, and native standard FB dispatch
  • HAL boundary: GPIO/analog/time/retain abstracted per platform
  • Motion: PLCopen-inspired motion runtime with native MC_* function blocks and portable hardware backends — see docs/motion-hal.md
  • Package format: versioned .mplc binary (no dynamic linking on target)
  • MPLC Studio: ST editor; Ladder Diagram editor (early development); in-process compile, device discovery, HTTP upload/reboot, serial monitor — see tools/mplc-studio/README.md

Not yet implemented from IEC 61131-3: WSTRING/WCHAR, CONFIGURATION/RESOURCE/TASK declarations in source (a single cyclic task is emitted), REF_TO references, object-oriented extensions (CLASS/METHOD/INTERFACE), namespaces, and full PLCopen TC6 LD interchange geometry. The Studio ladder editor does not yet expose the new LD elements (P/N, negated coils, jumps, EN/ENO); they compile from PLCopen XML.

Example (ST)

VAR_GLOBAL
    StartBtn AT %IX0.0 : BOOL;
    Motor    AT %QX0.0 : BOOL;
END_VAR

FUNCTION_BLOCK DebouncedStart
VAR_INPUT
    raw : BOOL;
END_VAR
VAR
    t : TON;
END_VAR
VAR_OUTPUT
    stable : BOOL;
END_VAR
    t(IN := raw, PT := T#50ms);
    stable := t.Q;
END_FUNCTION_BLOCK

PROGRAM Main
VAR
    db : DebouncedStart;
END_VAR
    db(raw := StartBtn);
    Motor := db.stable;
END_PROGRAM

Compile with mplc compile program.st -o program.mplc (see Quick start below).

Quick start (toolchain)

cmake -B build
cmake --build build
./build/mplc compile tests/compiler/sample.st -o build/sample.mplc
./build/mplc inspect build/sample.mplc
ctest --test-dir build

On Windows (Visual Studio generator):

cmake -B build_local
cmake --build build_local --config Release --target mplc
build_local\compiler\Release\mplc.exe compile tests\compiler\sample.st -o build_local\sample.mplc

ST sources may use //, (* *), and # line comments (same as the embedded Studio compiler).

MPLC Studio (NetBurner workflow)

MPLC Studio — Ladder Diagram

The ladder editor is in early development — the UI, branch model, and LD compiler path are functional but still evolving; expect rough edges compared to Structured Text.

The ladder editor (tools/mplc-studio/app/src/ladder/) is a grid-based Qt Graphics View canvas: rungs hold contacts, coils, and function blocks; Surround with Branch / + Leg build nested input OR branches; output branches fork parallel coils (with optional instructions before each coil). Programs persist as PLCopen TC6 XML (.xml) and compile through the LD frontend (compiler/frontend/ld/) into the same .mplc bytecode as ST. View Generated ST exports ladder logic for inspection.

# Build the IDE
cd tools\mplc-studio
.\scripts\build-mplc-studio.ps1 -QtDir "C:\Qt\6.8.3\msvc2022_64"

# Optional: Windows installer (Inno Setup 6)
.\scripts\build-mplc-studio-installer.ps1 -QtDir "C:\Qt\6.8.3\msvc2022_64"

Output: tools/mplc-studio/build/app/MPLCStudio.exe or tools/mplc-studio/dist/MPLCStudio-0.1.1-Setup.exe

Typical loop: edit .stCompile (writes <source>.mplc beside the file) → UploadReboot → watch boot logs on the Serial Monitor tab (115200, DTR/RTS on).

Documentation

Document Description
docs/architecture.md Compiler, VM, package format, ports
docs/motion-hal.md Motion HAL, units, backend integration examples
docs/README.md Documentation index
docs/netburner-mod54415.md MOD54415 bring-up and deployment
platforms/netburner/README.md NetBurner platform overview
platforms/netburner/mod54415/nb_project/README.md NBEclipse / make firmware project
tools/mplc-studio/README.md Studio features, build, installer

Layout

compiler/          Host compiler (ST/IL/LD/FBD/SFC → .mplc)
runtime/           Portable VM, scheduler, I/O, stdlib
schema/            Package ABI (mplc_abi.h)
hal/               HAL contract
platforms/         generic, linux, baremetal, netburner
tools/mplc-studio/ Qt IDE + Inno Setup installer
tests/             Compiler and runtime unit tests
docs/              Architecture and platform guides

License

BSD 3-Clause License. See LICENSE.

About

Portable IEC 61131 runtime for Embedded Devices

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors