|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# AWL simulator - SFCs |
| 4 | +# |
| 5 | +# Copyright 2026 QuackS7 contributors |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 2 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License along |
| 18 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +# |
| 21 | + |
| 22 | +from __future__ import division, absolute_import, print_function, unicode_literals |
| 23 | +#from awlsim.common.cython_support cimport * #@cy |
| 24 | +from awlsim.common.compat import * |
| 25 | + |
| 26 | +from awlsim.common.exceptions import * |
| 27 | +from awlsim.common.util import * |
| 28 | + |
| 29 | +from awlsim.core.systemblocks.systemblocks import * #+cimport |
| 30 | +from awlsim.core.systemblocks.error_codes import * |
| 31 | +from awlsim.core.blockinterface import * |
| 32 | +from awlsim.core.memory import * #+cimport |
| 33 | + |
| 34 | + |
| 35 | +class SFC4(SFC): #+cdef |
| 36 | + name = (4, "READ_RTM", "read runtime meter") |
| 37 | + |
| 38 | + interfaceFields = { |
| 39 | + BlockInterfaceField.FTYPE_IN : ( |
| 40 | + BlockInterfaceField(name="NR", dataType="BYTE"), |
| 41 | + ), |
| 42 | + BlockInterfaceField.FTYPE_OUT : ( |
| 43 | + BlockInterfaceField(name="RET_VAL", dataType="INT"), |
| 44 | + BlockInterfaceField(name="CQ", dataType="BOOL"), |
| 45 | + BlockInterfaceField(name="CV", dataType="INT"), |
| 46 | + ), |
| 47 | + } |
| 48 | + |
| 49 | + def __init__(self, cpu): |
| 50 | + SFC.__init__(self, cpu) |
| 51 | + # 8 runtime meters, each (running: bool, hours: int). |
| 52 | + # Until SFC 2 SET_RTM / SFC 3 CTRL_RTM are added, every meter |
| 53 | + # reads back (False, 0) — the correct state for a CPU that has |
| 54 | + # never started any runtime meter. |
| 55 | + self.__meters = [[False, 0] for _ in range(8)] |
| 56 | + |
| 57 | + def run(self): #+cpdef |
| 58 | +#@cy cdef S7StatusWord s |
| 59 | +#@cy cdef uint32_t nr |
| 60 | + |
| 61 | + s = self.cpu.statusWord |
| 62 | + |
| 63 | + nr = AwlMemoryObject_asScalar( |
| 64 | + self.fetchInterfaceFieldByName("NR")) |
| 65 | + |
| 66 | + if nr > 7: |
| 67 | + # Siemens SFC_e §6.5 mandates flat specific code 0x8080 for |
| 68 | + # NR out of range, not the general-code E_RPARM encoding. |
| 69 | + # See docs/siemens/SFC_e (1).pdf §6.5 and rowlf's FLAG-B |
| 70 | + # ruling (2026-04-17, user-ratified). |
| 71 | + self.storeInterfaceFieldByName("RET_VAL", |
| 72 | + make_AwlMemoryObject_fromScalar(0x8080, 16)) |
| 73 | + self.storeInterfaceFieldByName("CQ", |
| 74 | + make_AwlMemoryObject_fromScalar(0, 1)) |
| 75 | + self.storeInterfaceFieldByName("CV", |
| 76 | + make_AwlMemoryObject_fromScalar(0, 16)) |
| 77 | + s.BIE = 0 |
| 78 | + return |
| 79 | + |
| 80 | + running, hours = self.__meters[nr] |
| 81 | + |
| 82 | + self.storeInterfaceFieldByName("RET_VAL", |
| 83 | + make_AwlMemoryObject_fromScalar(0, 16)) |
| 84 | + self.storeInterfaceFieldByName("CQ", |
| 85 | + make_AwlMemoryObject_fromScalar(1 if running else 0, 1)) |
| 86 | + self.storeInterfaceFieldByName("CV", |
| 87 | + make_AwlMemoryObject_fromScalar(hours & 0xFFFF, 16)) |
| 88 | + s.BIE = 1 |
0 commit comments