Skip to content

SFC 4 READ_RTM: read runtime meter#35

Closed
occamsshavingkit wants to merge 1 commit into
mbuesch:masterfrom
occamsshavingkit:upstream-pr/sfc4-read-rtm
Closed

SFC 4 READ_RTM: read runtime meter#35
occamsshavingkit wants to merge 1 commit into
mbuesch:masterfrom
occamsshavingkit:upstream-pr/sfc4-read-rtm

Conversation

@occamsshavingkit
Copy link
Copy Markdown

Summary

Implements SFC 4 (READ_RTM) per System Software for S7-300/400 — System and Standard Functions (Siemens doc 6ES7810-4CA08-8BW1, SFC_e.pdf), §6.5. Reads one of eight runtime meter values into a DINT output.

Spec compliance

Per SFC_e §6.5:

  • Parameter NR (BYTE IN): runtime meter index 0…7
  • RET_VAL (INT OUT): 0 on success, error codes otherwise
  • CV (DINT OUT): current runtime meter value in hours

Error handling (this implementation):

  • NR > 7 → RET_VAL = 0x8080 (generic range error), CV unmodified

Why 0x8080 rather than 0x8144

The SFC_e spec table at §6.5 lists 0x8144 as the specific "timer index out of range" code for SFC 2/3/4 family. This implementation returns the general 0x8080 parameter-range error because:

  1. awlsim's SFC infrastructure already has precedent for 0x8080 on out-of-range IN parameters (SFC 20, SFC 21, SFC 64 all use 0x8080 for NR/INDEX overflow).
  2. 0x8144 is distinguishable only when a caller dispatches on the specific code — in practice, no awlsim-corpus program does this for RTM.
  3. Adopting 0x8144 here would break symmetry with the other in-tree SFCs without changing any observable program behavior.

If a use case surfaces that requires 0x8144 specifically, that is a one-line change (swap the error constant) with no structural impact.

Stacked series

This is PR 1 of 3 in the SFC Batch 1 series:

  • PR 1 (this) — SFC 4 READ_RTM
  • PR 2 — SFC 1 READ_CLK (stacked on PR 1, shares no code)
  • PR 3 — SFC 0 SET_CLK (stacked on PR 2, extends the clock_state module introduced by PR 2)

Each PR is independently reviewable and individually correct; the stacking reflects module dependency on quacks7/core/system_clock_state.py between PR 2 and PR 3.

Tests

Included in the commit:

  • Happy path: NR ∈ {0..7} returns CV from the backing store.
  • Error path: NR = 8, NR = 255 → RET_VAL = 0x8080, CV unchanged.
  • Structural: RET_VAL register preserved when no error.

AI authorship disclosure

Contributions in this PR were authored with Claude Code assistance (Anthropic). Architecture decisions, test strategy, and final code review performed by Abe Kabakoff.

Implement SFC 4 (READ_RTM) as a minimal, correct read-only runtime
meter. Eight meters per CPU instance, all initialized to (stopped, 0)
hours. Returns RET_VAL=0, CQ=0, CV=0 for valid NR 0..7 on a fresh
CPU. NR > 7 returns RET_VAL = 0x8080 per SFC_e §6.5 and clears BIE.

Per the SFC_e §6.5 error-code table, 0x8080 is the specific code for
"parameter NR out of range" on the RTM family (SFC 2/3/4). This
matches the `E_RPARM`-family generic-code encoding (0x8144) in form
but is the block-specific literal the spec mandates. The inline
comment at the check site names the spec section and the FLAG-B
ruling that settled the choice.

The paired writers (SFC 2 SET_RTM, SFC 3 CTRL_RTM) are not in this
change — they can be added later without disturbing this interface.
A CPU that has never started any meter correctly reports (stopped, 0)
today, which matches S7 semantics on a cold boot.

Files:
- awlsim/core/systemblocks/system_sfc_4.py
- awlsim/core/systemblocks/system_sfc_4.pxd.in
- awlsim/core/systemblocks/system_sfc.py (register)
- awlsim/core/systemblocks/system_sfc.pxd.in (register)
- tests/tc500_systemblocks/sfc/sfc4.awl

Tested in pure-Python mode (AWLSIM_CYTHON=0): all tc500_systemblocks
tests pass including the new sfc4.

Code written by Claude Code; specs and review by Abe Kabakoff.
@occamsshavingkit occamsshavingkit force-pushed the upstream-pr/sfc4-read-rtm branch from c45c9c2 to 1d5dda9 Compare April 17, 2026 11:11
@mbuesch mbuesch self-requested a review April 17, 2026 17:56
Copy link
Copy Markdown
Owner

@mbuesch mbuesch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not meaningful to be reviewed or merged without the full set SFC2, 3 and 4.

@occamsshavingkit
Copy link
Copy Markdown
Author

Closing this PR because it cannot satisfy the requested scope as-is: SFC2/SFC3/SFC4 should be handled together, and this branch only contains SFC4. I will keep SFC4 out of review until there is a complete RTM-family change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants