from awlsim.common.cython_support cimport *
from awlsim.common.cpuspecs cimport *
from awlsim.common.movingavg cimport *
from awlsim.core.statusword cimport *
from awlsim.core.callstack cimport *
from awlsim.core.lstack cimport *
from awlsim.core.offset cimport *
from awlsim.core.operators cimport *
from awlsim.core.memory cimport *
from awlsim.core.blocks cimport *
from awlsim.core.userdefinedtypes cimport *
from awlsim.core.datablocks cimport *
from awlsim.core.timers cimport *
from awlsim.core.counters cimport *
from awlsim.core.insnmeas cimport *
from awlsim.core.systemblocks.systemblocks cimport *


cdef class S7CPU(object):
	cdef public object cbCycleExit
	cdef public object cbCycleExitData
	cdef public object cbBlockExit
	cdef public object cbBlockExitData
	cdef public object cbPostInsn
	cdef public object cbPostInsnData
	cdef public object cbPeripheralRead
	cdef public object cbPeripheralReadData
	cdef public object cbPeripheralWrite
	cdef public object cbPeripheralWriteData
	cdef public object cbScreenUpdate
	cdef public object cbScreenUpdateData

	cdef public double now
	cdef public double __nowOffset

	cdef public S7CPUSpecs specs
	cdef public object conf
	cdef public object prog
	cdef public double cycleTimeLimit
	cdef public double __runtimeLimit
	cdef public _Bool __obTempPresetsEnabled
	cdef public _Bool __extendedInsnsEnabled
	cdef public dict __dateAndTimeWeekdayMap

	cdef public list __udts
	cdef public list __dbs
	cdef public list __obs
	cdef public OB __ob1
	cdef public dict obTempPresetHandlers
	cdef public list __fcs
	cdef public list __fbs
	cdef public list __sfcs
	cdef public dict __sfcsExtended
	cdef public list __sfbs
	cdef public dict __sfbsExtended
	cdef public uint32_t __udtsAlloc
	cdef public uint32_t __dbsAlloc
	cdef public uint32_t __obsAlloc
	cdef public uint32_t __fcsAlloc
	cdef public uint32_t __fbsAlloc
	cdef public uint32_t __sfcsAlloc
	cdef public uint32_t __sfbsAlloc

	cdef public S7StatusWord statusWord
	cdef public _Bool is4accu
	cdef public Accu accu1
	cdef public Accu accu2
	cdef public Accu accu3
	cdef public Accu accu4
	cdef public list timers
	cdef public list counters
	cdef public AwlMemory flags
	cdef public AwlMemory inputs
	cdef public AwlMemory outputs
	cdef public Addressregister ar1
	cdef public Addressregister ar2
	cdef public DB dbRegister
	cdef public DB diRegister
	cdef public DB db0

	cdef public _Bool mcrActive
	cdef public list mcrStack

	cdef public AwlOffset __clockMemByteOffset
	cdef public double __nextClockMemTime
	cdef public uint32_t __clockMemCount
	cdef public uint32_t __clockMemCountLCM

	cdef public int32_t relativeJump
	cdef public CallStackElem callStackTop
	cdef public uint32_t callStackDepth
	cdef public LStackAllocator activeLStack

	cdef public uint32_t __insnCount
	cdef public uint32_t __cycleCount
	cdef public double insnPerSecond
	cdef public double avgInsnPerCycle
	cdef public double cycleStartTime
	cdef public double minCycleTime
	cdef public double maxCycleTime
	cdef public double avgCycleTime
	cdef public MovingAvg __cycleTimeMovAvg
	cdef public double startupTime
	cdef public double __speedMeasureStartTime
	cdef public uint32_t __speedMeasureStartInsnCount
	cdef public uint32_t __speedMeasureStartCycleCount
	cdef public MovingAvg __timestampUpdMovAvg
	cdef public double __timestampUpdInter
	cdef public uint32_t __timestampUpdInterMask
	cdef public InsnMeas __insnMeas

	cdef UDT getUDT(self, uint16_t index)
	cdef DB getDB(self, uint16_t index)
	cdef CodeBlock getOB(self, uint16_t index)
	cdef CodeBlock getFC(self, uint16_t index)
	cdef CodeBlock getFB(self, uint16_t index)
	cdef CodeBlock getSFC(self, int32_t index)
	cdef CodeBlock getSFB(self, int32_t index)

	cdef void setMcrActive(self, _Bool active)
	cdef _Bool mcrIsOn(self)
	cdef mcrStackAppend(self, S7StatusWord statusWord)
	cdef mcrStackPop(self)

	cdef AwlMemoryObject fetch(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchIMM(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchIMM_DT(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchIMM_PTR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchIMM_STR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchDBLG(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchDBNO(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchDILG(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchDINO(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchAR2(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_Z(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_NZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_POS(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_NEG(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_POSZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_NEGZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchSTW_UO(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchE(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchA(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchM(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchL(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchVL(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchDB(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchDI(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchPE(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchT(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchNAMED_LOCAL(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchNAMED_LOCAL_PTR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchNAMED_DBVAR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchINDIRECT(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchVirtACCU(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchVirtAR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
	cdef AwlMemoryObject __fetchVirtDBR(self, AwlOperator operator, uint32_t allowedWidths) except NULL

	cdef store(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeE(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeA(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeM(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeL(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeVL(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeDB(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeDI(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storePA(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeAR2(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeSTW(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeNAMED_LOCAL(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeNAMED_DBVAR(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
	cdef __storeINDIRECT(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)

	cdef bytearray fetchOutputRange(self, uint32_t byteOffset, uint32_t byteCount)
	cdef uint8_t fetchOutputByte(self, uint32_t byteOffset)
	cdef bytearray fetchInputRange(self, uint32_t byteOffset, uint32_t byteCount)
	cdef uint8_t fetchInputByte(self, uint32_t byteOffset)
	cdef storeInputRange(self, uint32_t byteOffset, bytearray data)
	cdef storeInputByte(self, uint32_t byteOffset, uint8_t data)

	cdef updateTimestamp(self)
	cdef __cycleTimeExceed(self)
	cdef __checkRunTimeLimit(self)

	cdef runCycle(self)
	cdef __runOB(self, OB block)
	cdef void run_BE(self)
	cdef openDB(self, int32_t dbNumber, _Bool openDI)
	cdef run_AUF(self, AwlOperator dbOper)
	cdef void run_TDB(self)

	cdef Accu getAccu(self, uint32_t index)
	cdef Addressregister getAR(self, uint32_t index)
	cdef Timer getTimer(self, uint32_t index)
	cdef Counter getCounter(self, uint32_t index)

	cdef int32_t labelIdxToRelJump(self, uint32_t labelIndex)
	cdef void jumpToLabel(self, uint32_t labelIndex)
	cdef void jumpRelative(self, int32_t insnOffset)

	cdef run_CALL(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters, _Bool raw)
	cdef CallStackElem __call_FC(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_RAW_FC(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_FB(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_RAW_FB(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_SFC(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_RAW_SFC(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_SFB(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_RAW_SFB(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_INDIRECT(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_MULTI_FB(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)
	cdef CallStackElem __call_MULTI_SFB(self, AwlOperator blockOper, AwlOperator dbOper, tuple parameters)

	cdef AwlOperator __translateFCNamedLocalOper(self, AwlOperator operator, _Bool store)

	cdef makeCurrentDateAndTime(self, uint8_t *byteArray, uint32_t offset)

	cdef __dumpLStackFrame(self, prefix, LStackFrame *frame)
