Runner
PLC is the execution engine. It takes a program, holds the current state, and exposes methods to drive execution scan by scan.
Creating a runner
The constructor accepts:
- A
Program(the common case) - A list of rungs (
[rung1, rung2]) Nonefor an empty program (useful in tests)
Optional keyword arguments:
initial_state— aSystemStateto start from instead of the defaulthistory— retention window for the scan log and checkpoints. Duration string ("1h","30m"), scan count (int), orNone(unlimited, default). Prevents unbounded memory growth on long runs.cache— instant-lookup window for fullSystemStatesnapshots. Same formats ashistory.None(default) uses byte-budget-only eviction.history_budget— byte ceiling for the recent-state cache (default 100 MB, minimum 1 MB). Acts as a safety net when duration-based policies aren't enough.
Time modes
runner = PLC(logic, dt=0.010) # fixed-step, 10 ms per scan (default)
runner = PLC(logic, realtime=True) # wall-clock
| Mode | Behavior | Use case |
|---|---|---|
dt=0.010 |
timestamp += dt each scan |
Tests, offline simulation |
realtime=True |
timestamp = actual elapsed time |
Live hardware, integration tests |
dt= is the default. Timer and counter instructions use timestamp, so fixed steps give perfectly reproducible results. realtime=True is intentionally non-deterministic. Scan dt follows host elapsed time.
Real-time clock
Logic that depends on time of day (shift changes, scheduled events) uses the RTC system points (system.rtc.year4, system.rtc.month, system.rtc.hour, etc.). By default, these track wall-clock time. See System points for the full namespace overview.
set_rtc pins the RTC to a specific datetime:
The RTC then advances with simulation time: rtc = base_datetime + (current_sim_time - sim_time_at_set). With a fixed dt, this makes time-of-day logic fully deterministic. With realtime=True, it effectively offsets the wall clock.
Execution methods
step() — one scan
Executes one complete scan cycle (all phases) and returns the committed SystemState.
run(cycles) — N scans
Runs exactly N scans, unless a pause breakpoint fires first. Returns the final state.
run_for(seconds) — advance by time
Keeps stepping until the simulation clock has advanced by the given amount (or a pause breakpoint fires).
run_until(*conditions) — stop on condition
Accepts the same condition expressions used inside rung(). Multiple conditions are AND-ed:
runner.run_until(Motor, ~Fault)
runner.run_until(Temp > 150.0)
runner.run_until(Or(AlarmA, AlarmB, AlarmC))
Stops when the condition is true, a pause breakpoint fires, or max_cycles is reached, whichever comes first.
run_until_fn(predicate) — callable predicate
For conditions that aren't expressible as tag/condition expressions:
The predicate receives the committed SystemState each scan.
jit=
runner.run_for(3600) # an hour of a plain timer at 10 ms takes about half a second
runner.run_for(10, jit=False) # every scan interpreted, recording as it goes
run_for() and run_until() compile your program with a tracing JIT. After the first
1,000 scans of a run, every scan executes in the compiled kernel. Scans that repeat the one
before them run in a guarded loop. No scan is skipped. The run ends on the same state and
scan count as stepping.
A callable predicate, a monitor, a breakpoint or a when() watches every scan. If you use
any of them, the run stays interpreted, as it does for a program with no kernel. A stop
written as a tag or a condition stays in the kernel. Prefer run_until(Tmr.Done) over
run_until(lambda s: s.tags["Tmr_Done"]) for a long wait.
Injecting inputs
patch() — one-shot
The next scan applies the values at its start. Each value stays pending until that scan commits. If that scan raises, or you drop it while it is paused in the debugger, the value stays pending and the next scan applies it. After the commit, the tag holds whatever the program left in it. Multiple patches before a step merge. The last write per tag wins.
.value via context manager
Inside with PLC(...) as plc: (or with runner:), tag .value reads and writes go through the runner's current state:
with PLC(logic) as plc:
Button.value = True # queues a patch
print(Step.value) # reads current value
plc.step() # executes with the queued patch
assert Motor.value is True
A .value read returns the tag's force if it has one. Otherwise it returns a pending patch, and otherwise the committed value. plc.live_value(Step) reads the same value without the with block.
Forces
Forces persist across scans, re-applied at two points each scan:
Phase 3: APPLY FORCES (pre-logic) ← sets force values before any rung runs
Phase 4: EXECUTE LOGIC ← logic may overwrite forced values mid-scan
Phase 5: APPLY FORCES (post-logic) ← re-asserts force values after all logic
This means:
- Forced values are present at scan start and scan end.
- Logic may temporarily change a forced value mid-scan (for example,
latch()on a forced-False tag sets it True temporarily, but the post-logic force pass restores it). - Edge detection (
rise/fall) sees the post-force values that carry across scans.
If a tag is both patched and forced in the same scan, the pre-logic force pass overwrites the patched value. The patch is consumed but has no effect.
For force usage patterns in tests, see Testing — Forces.
Mode control
stop() — enter STOP mode
Sets PLC mode to STOP. Does not clear tags. Idempotent. runner.mode reads "run" or "stop".
Auto-restart from STOP
Any execution method (step, run, run_for, run_until) performs a STOP→RUN transition before executing:
- Non-retentive tags reset to defaults
- Retentive tags preserve values
- Runtime scope resets (
scan_id=0,timestamp=0.0, history/patches/forces cleared)
start() — enter RUN without scanning
runner.start()
runner.force(AutoMode, True) # applies from the first scan
runner.patch({Setpoint: 50})
runner.step()
Performs the STOP→RUN transition now instead of at the next step(), so forces and patches set afterwards reach the first scan. Does nothing in RUN mode.
Keeping forces across STOP→RUN
Off by default. When on, the STOP→RUN transition keeps active forces. Tags and pending patches still reset. reboot() clears forces either way. Forks inherit the setting.
reboot() — power-cycle
Simulates a power cycle. What it keeps depends on battery_present:
True(default): retentive tags keep their values. The rest reset to defaults, as on STOP→RUN.False: every tag resets to its default.
Runtime scope resets the same as STOP→RUN. Runner returns in RUN mode.
battery_present stands for the project's Battery Backup Setup box. sys.battery_present (SC203) reads it. Neither one detects a battery. Test a reboot with battery_present = False too.
Inspecting state
runner.state # SystemState snapshot at latest committed scan
runner.simulation_time # shorthand for state.timestamp
runner.dt # configured scan period in seconds
runner.time_mode # current TimeMode
runner.forces # read-only view of active force overrides
runner.pending_patches # read-only view of patches not yet committed
runner.live_value(Step) # the value Step.value reads now
runner.mode # "run" or "stop"
SystemState fields:
state.scan_id # int — monotonic scan counter (starts at 0)
state.timestamp # float — simulation clock in seconds
state.tags # PMap[str, value] — all tag values
state.memory # PMap[str, value] — internal engine state
Both scan_id and timestamp reset to 0 on STOP→RUN transition or reboot().
History
Enable history retention to keep immutable state snapshots:
runner = PLC(logic) # 100 MB default cache, all scans addressable
runner.history.at(5) # state at scan 5
runner.history.range(3, 7) # [scan 3, 4, 5, 6]
transition = runner.history.previous_transition(
Step,
to=101,
at_or_before=runner.playhead,
)
runner.history.latest(10) # up to 10 most recent (oldest → newest)
Every scan from 0 to the current tip is addressable. Recent scans are served from an in-memory state cache (byte-bounded, default 100 MB). Older scans are reconstructed on demand from the scan log and checkpoints.
A long run_until() or run_for() runs compiled and records no rung firings.
Without history, a stepped run also stops recording once its records hold about 140 MB.
History still serves each of those scans by replaying them on demand.
The first cause(), effect(), cold_rungs() or hot_rungs() call that reaches those
scans steps them again to get their records. The answer is therefore the one that stepping
gives.
A question that would step more than 250,000 scans raises UnrecordedScansError and names
the range. Scans that a question has stepped keep their records. To get an answer, ask
first about a scan nearer the start of that range for cause, or nearer its end for
effect. Then ask again, from Python or the debugger. From Python, cause() also takes
since=.
A run with jit=False and history records every scan in the window.
previous_transition() returns the latest matching recorded transition, or
None.
To bound memory on long runs, set a retention window:
runner = PLC(logic, history="1h") # keep 1 hour of replayable history
runner = PLC(logic, history="1h", cache="5m") # last 5 minutes instant, rest via replay
runner = PLC(logic, history_budget=20 * 1024 * 1024) # 20 MB byte ceiling
history_budget must be at least 1 MB (raises ValueError below that).
history keeps whole checkpoint intervals (200 scans unless you set checkpoint_interval). A runner keeps between history minus one interval and history plus one interval, and at least two intervals.
With history, the runner keeps every value its rungs wrote in the window. A value that changes on every scan takes about 250 bytes a scan, so history="1h" at 10 ms holds about 90 MB for each such tag. history_budget does not count that memory. Shorten history for a program with many such tags.
When the checkpoints in the window pass history_budget, the runner keeps one interval less than history asks for. Each checkpoint counts as one state snapshot.
Time-travel playhead
The playhead is a read-only cursor into history. It doesn't affect execution. step() always appends at the history tip.
runner.playhead # current inspection scan_id
runner.seek(scan_id=5) # jump to a historical scan
runner.rewind(seconds=1.0) # move backward by simulation time
snapshot = runner.history.at(runner.playhead)
rewind(seconds) finds the nearest state where timestamp <= target.
Diff
Compare two retained scans to see what changed:
Returns string-keyed dicts that hold only the tags whose values differ. Missing tags appear as None.
Fork
Create an independent runner from a snapshot:
alt = runner.fork() # from current state (common case)
alt = runner.fork(scan_id=10) # from a retained historical scan
alt = runner.fork_from(scan_id=10) # alias
The fork starts with the snapshot's state and the same time mode. It has clean runtime state, so no forces, patches, breakpoints, or monitors carry over. A fork of a stopped PLC is stopped too, so its first scan restarts it. Only the fork snapshot is in its initial history.
See Testing — Forking for the alternate-outcomes pattern.
Breakpoints and monitors
when() creates condition breakpoints evaluated after each committed scan. monitor() watches a tag for value changes. Both return handles with .remove(), .enable(), .disable().
runner.when(Fault).pause() # halt run()/run_for()/run_until()
runner.when(Fault).snapshot("fault_seen") # label scan in history
runner.when(~Sensor).do(lambda state: runner.patch({Sensor: True})) # run a callback, keep going
runner.monitor(Motor, lambda curr, prev: print(f"{prev} → {curr}"))
.do() runs a callback every scan its condition holds and lets the run continue. It is the hook for reactive inputs. Paired with patch, it re-asserts an input whenever the program drifts it. when(~Sensor).do(...) patches Sensor back True each time it falls. Use patch, not force. A force would pin the tag, so the program could never drift it and the reaction would be moot.
See Testing — Monitoring changes and Testing — Predicate breakpoints for usage patterns.