# FPGA Implementation of IEC-61131-3-Based Hardware Aided Counters for PLC
## 3 FPGA Counters Implementations
### Software-Like Counters
The execution unit of the counter requires about 184 LUTs and 70 LUTRAMs, but each counter instance increases the number of LUTs by approximately 1.1, and the number of LUTRAMs increases by 1 LUTRAM per counter. Their functionality is based on memory blocks, so LUTRAMs are widely utilized. Moreover, LUTs and LUTRAMs used in FPGA are 6-input tables, so the utilized resources are constant for the number of counters not exceeding 64. The occupancy of elements of the structure is in this case smaller and increases much slower than for the hard structure. To fully compare hardware counters with software-like ones, the timing analysis is necessary. The maximum clock frequency (in MHz) of those solutions is presented in Figure 15. \begin{table}
\begin{tabular}{c c c c c c c} \hline \hline \multirow{2}{*}{**No. of Counters**} & \multicolumn{3}{c}{**Hardware Counters**} & \multicolumn{3}{c}{**Software-Like Counters**} \\  & **LUT** & **LUTRAM** & **FF** & **LUT** & **LUTRAM** & **FF** \\ \hline
2 & 169 & 0 & 140 & 184 & 70 & 0 \\
16 & 1321 & 0 & 1120 & 184 & 70 & 0 \\
64 & 5407 & 0 & 4480 & 184 & 70 & 0 \\
128 & 10,919 & 0 & 8960 & 254 & 140 & 0 \\
256 & 21,600 & 0 & 17,920 & 394 & 280 & 0 \\
512 & 43,298 & 0 & 35,840 & 760 & 560 & 0 \\
1024 & 86,687 & 0 & 71,680 & 1330 & 1120 & 0 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: The comparison of the logic utilization for hardware and software-like counters. Figure 14: The comparison of the logic utilization for hardware and software-like counters. As can be seen for software-like counters, the frequency is always lower and decreases faster with increasing the number of instances of the counters. Hardware counters are much faster, even for the big structures. Summarizing the results for the utilization and time analysis, it can be concluded that for a small number of counters required in a given application, they can be implemented in a hardware way, while for a large number of counters, a software-like solution should be used. In order to prove full functionality of the idea presented in the paper, simple CPU units with basic instructions were implemented together with counters as presented in Figure 6. Synthesis was carried out for xcvu5p-flvc2104-1-i (Virtex UtraScale+). Results are in Table 2--it considers the conclusion that hardware counters are suggested for small numbers of instances while software-like ones are dedicated for a large number of counters instances. The xcvu5p FPGA is much more powerful than xc7a100t, but in this application, the difference manifests itself mainly in maximum frequency. Units with simple operators (Figures 7 and 12) were also implemented in xc7a100t FPGA. The direct comparison for 16, 256 and 1024 counter instances is presented in Table 3. In terms of both speed and utilized logic, there are no drastic differences between units for simple operators and with CAL instruction. For hardware units, it is even more advantageous to implement a unit with CAL in terms of speed. This is probably due to the 'cutting' of combinational circuits with registers, which is good for critical paths. In a soft-like unit, more memory is implemented for the CAL unit, which in turn adversely affects the operating frequency. \begin{table}
\begin{tabular}{c c c c} \hline \hline
**No of Counters** & & **CPU + Hard CNTs** & **CPU + Soft CNTs** \\ \hline
2 & LUT/FF/LUTRAM/BRAM & 300/179/33/0.5 & - \\  & max [MHz] & 302 & - \\
16 & LUT/FF/LUTRAM/BRAM & 1481/1159/33/0.5 & - \\  & fmax [MHz] & 300 & - \\
64 & LUT/FF/LUTRAM/BRAM & 5384/4523/33/0.5 & 338/39/103/0.5 \\  & fmax [MHz] & 298 & 230 \\
128 & LUT/FF/LUTRAM/BRAM & - & 408/39/173/0.5 \\  & fmax [MHz] & - & 224 \\
256 & LUT/FF/LUTRAM/BRAM & - & 548/39/313/0.5 \\  & fmax [MHz] & - & 218 \\
512 & LUT/FF/LUTRAM/BRAM & - & 828/39/593/0.5 \\  & fmax [MHz] & - & 216 \\
1024 & LUT/FF/LUTRAM/BRAM & - & 1467/39/1153/0.5 \\  & fmax [MHz] & - & 216 \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Result for CPU with counters. Figure 15: The time analysis for hardware and software-like counters ([MHz]).