# FPGA Implementation of IEC-61131-3-Based Hardware Aided Counters for PLC
## 3 FPGA Counters Implementations
### Software-Like Counters
A comparison of the time required for the CPU to process all the operations associated with the counter has also been made. There are 16 such instructions. For solutions with the CAL operator, the execution of this instruction must be added in addition, while for solutions with short operators, the CAL instruction must be added to each instruction that affects the counter, that is: CU, CD, R, and PV/LD R. Depending on how the state of the QU and QD outputs are working out, these executions may also be required for operations of reading these states. For the presented CPU, the execution of each instruction requires one clock cycle, so determining the time to operate the entire counter is to multiply this number by the execution time of a single instruction. The situation is different for the Siemens solutions presented for comparison. Such a comparison for a few selected Siemens solutions is presented in Table 4. The table shows the measured processing time for the program using all capabilities of the counter, as well as using its individual fragments. There is huge disproportion for the older controller S7-315 between the implementation of the counter operations as a whole for the classical solution and the standard compliant (Table 4). This disproportion has been removed in newer solutions. It can also be seen that the structure proposed by the authors significantly exceeds the time performance of all units taken for comparison. However, due to the different possible solutions and the fact that the vendor PLCs perform even relatively more tasks than the presented solution, it was decided to make another, seemingly more interesting comparison. For example, in the S7-315 controller, the edge detection operations take three times more than basic instruction and these operations for the S7-319 take almost four times more time to execute. The operation of loading the counter status into CR for this controller takes more than thirty times longer than the basic instruction! In the authors' proposed solution, these operations take the same amount of time as the basic instructions. The execution times of individual counter tasks were compared with respect to the duration of the instruction that seems to be basic for every CPU controller--the instruction for reading the status of the contents of the PLC's memory cell M (Marker, Memory) was taken into account. Now it is much clearer that the implementation of counter operations proposed by the authors in terms of time is much more efficient than the one occurring in Siemens PLCs, regardless of whether counters are 16-bit or 32-bit, and whether in the classic version (not compliant with the provisions of the Standard), or in the version compliant with the Standard (most closely to short operators' ones). In the best case, for the S7-315 controller and the counter not conforming to the Standard the difference reaches more than four times, while for the counter conforming to the Standard and the S7-1516 controller, the difference is more than five times. These differences are due to the fact that the counter operations, for the proposed units, thanks to the hardware support, are implemented at the same time, or rather should be said, the same number of clock periods, as the basic instructions (such as reading or writing a memory cell). \begin{table}
\begin{tabular}{c c c c c c} \hline \hline \multirow{2}{*}{**Counters**} & **16** & \multicolumn{2}{c}{**256**} & \multicolumn{2}{c}{**1024**} \\  & **ult/ff/ultram** & **fmax** & **ult/ff/ultram** & **fmax** & **ult/ff/ultram** & **fmax** \\  & & **[MHz]** & & **[MHz]** & & **[MHz]** \\ \hline Hard CNTs & & & & & & \\ (operators) & 1401/1056/0 & 164 & 22,097/16,896/0 & 145 & 89,364/67,584/0 & 150 \\ Hard CNTs & & & & & & \\ (CAL) & 1321/1120/0 & 177 & 21,600/17,920/0 & 177 & 86,687/71,680/0 & 166 \\ Soft-like CNTs & & & & & & \\ (operators) & 198/0/66 & 130 & 396/0/264 & 119 & 1271/0/1056 & 108 \\ Soft-like CNTs & & & & & \\ (CAL) & 184/0/70 & 123 & 394/0/280 & 114 & 1330/0/1120 & 103 \\ \hline \hline \end{tabular}
\end{table}