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.datablocks cimport *
from awlsim.common.cpuspecs 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 dict udts
	cdef public dict dbs
	cdef public dict obs
	cdef public dict obTempPresetHandlers
	cdef public dict fcs
	cdef public dict fbs
	cdef public dict sfcs
	cdef public dict sfbs

	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 uint32_t maxCallStackDepth
	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 object __avgCycleTimes
	cdef public uint32_t __avgCycleTimesCount
	cdef public double __avgCycleTimesSum
	cdef public double startupTime
	cdef public double __speedMeasureStartTime
	cdef public uint32_t __speedMeasureStartInsnCount
	cdef public uint32_t __speedMeasureStartCycleCount

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

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

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

	cpdef bytearray fetchOutputRange(self, uint32_t byteOffset, uint32_t byteCount)
	cpdef bytearray fetchInputRange(self, uint32_t byteOffset, uint32_t byteCount)
	cpdef storeInputRange(self, uint32_t byteOffset, bytearray data)

	cdef updateTimestamp(self)
	cdef __cycleTimeExceed(self)
	cdef __runTimeExceed(self)

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

	cdef Accu getAccu(self, uint32_t index)
	cdef Addressregister getAR(self, uint32_t index)

	cdef int32_t labelIdxToRelJump(self, uint32_t labelIndex) except? 0x7FFFFFFF
	cdef jumpToLabel(self, uint32_t labelIndex)
	cdef 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, bytearray byteArray, uint32_t offset)

	cdef __dumpLStackFrame(self, prefix, LStackFrame *frame)

cdef class ParenStackElem(object):
	cdef public S7CPU cpu
	cdef public uint32_t insnType
	cdef public _Bool NER
	cdef public _Bool VKE
	cdef public _Bool OR
